Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::search()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 8
nop 0
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Catalog;
12
13
sprintf( 'catalog' ); // for translation
14
15
16
/**
17
 * Default implementation of catalog JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/**
27
	 * Adds the required data used in the template
28
	 *
29
	 * @param \Aimeos\MW\View\Iface $view View object
30
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
31
	 */
32
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
33
	{
34
		$view->itemSubparts = $this->getSubClientNames();
35
		return $view;
36
	}
37
38
39
	/**
40
	 * Copies a resource
41
	 *
42
	 * @return string|null HTML output
43
	 */
44
	public function copy() : ?string
45
	{
46
		$view = $this->getObject()->addData( $this->getView() );
47
48
		try
49
		{
50
			if( ( $id = $view->param( 'id' ) ) === null ) {
51
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
52
			}
53
54
			$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
55
			$view->item = $manager->getItem( $id, $this->getDomains() );
56
57
			$view->itemData = $this->toArray( $view->item, true );
58
			$view->itemRootId = $this->getRootId();
59
			$view->itemBody = '';
60
61
			foreach( $this->getSubClients() as $idx => $client )
62
			{
63
				$view->tabindex = ++$idx + 1;
64
				$view->itemBody .= $client->copy();
65
			}
66
		}
67
		catch( \Exception $e )
68
		{
69
			$this->report( $e, 'copy' );
70
		}
71
72
		return $this->render( $view );
73
	}
74
75
76
	/**
77
	 * Creates a new resource
78
	 *
79
	 * @return string|null HTML output
80
	 */
81
	public function create() : ?string
82
	{
83
		$view = $this->getObject()->addData( $this->getView() );
84
85
		try
86
		{
87
			$data = $view->param( 'item', [] );
88
89
			if( !isset( $view->item ) ) {
90
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'catalog' )->createItem();
91
			}
92
93
			$data['catalog.siteid'] = $view->item->getSiteId();
94
			$data['catalog.parentid'] = $view->item->getParentId() ?: $view->param( 'parentid', $view->param( 'item/catalog.parentid' ) );
95
96
			$view->itemRootId = $this->getRootId();
97
			$view->itemData = $data;
98
			$view->itemBody = '';
99
100
			foreach( $this->getSubClients() as $idx => $client )
101
			{
102
				$view->tabindex = ++$idx + 1;
103
				$view->itemBody .= $client->create();
104
			}
105
		}
106
		catch( \Exception $e )
107
		{
108
			$this->report( $e, 'create' );
109
		}
110
111
		return $this->render( $view );
112
	}
113
114
115
	/**
116
	 * Deletes a resource
117
	 *
118
	 * @return string|null HTML output
119
	 */
120
	public function delete() : ?string
121
	{
122
		$view = $this->getView();
123
124
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
125
		$manager->begin();
126
127
		try
128
		{
129
			if( ( $ids = $view->param( 'id' ) ) === null ) {
130
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
131
			}
132
133
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
134
			$search->setConditions( $search->compare( '==', 'catalog.id', $ids ) );
135
			$items = $manager->searchItems( $search, $this->getDomains() );
136
137
			foreach( $items as $item )
138
			{
139
				$view->item = $item;
140
141
				foreach( $this->getSubClients() as $client ) {
142
					$client->delete();
143
				}
144
			}
145
146
			$manager->deleteItems( $items->toArray() );
147
			$manager->commit();
148
149
			$this->nextAction( $view, 'search', 'catalog', null, 'delete' );
150
			return null;
151
		}
152
		catch( \Exception $e )
153
		{
154
			$manager->rollback();
155
			$this->report( $e, 'delete' );
156
		}
157
158
		return $this->search();
159
	}
160
161
162
	/**
163
	 * Returns a single resource
164
	 *
165
	 * @return string|null HTML output
166
	 */
167
	public function get() : ?string
168
	{
169
		$view = $this->getObject()->addData( $this->getView() );
170
171
		try
172
		{
173
			if( ( $id = $view->param( 'id' ) ) === null ) {
174
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
175
			}
176
177
			$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
178
179
			$view->item = $manager->getItem( $id, $this->getDomains() );
180
			$view->itemData = $this->toArray( $view->item );
181
			$view->itemRootId = $this->getRootId();
182
			$view->itemBody = '';
183
184
			foreach( $this->getSubClients() as $idx => $client )
185
			{
186
				$view->tabindex = ++$idx + 1;
187
				$view->itemBody .= $client->get();
188
			}
189
		}
190
		catch( \Exception $e )
191
		{
192
			$this->report( $e, 'get' );
193
		}
194
195
		return $this->render( $view );
196
	}
197
198
199
	/**
200
	 * Saves the data
201
	 *
202
	 * @return string|null HTML output
203
	 */
204
	public function save() : ?string
205
	{
206
		$view = $this->getView();
207
208
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
209
		$manager->begin();
210
211
		try
212
		{
213
			$item = $this->fromArray( $view->param( 'item', [] ) );
214
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

214
			$view->item = $item->getId() ? $item : $manager->/** @scrutinizer ignore-call */ saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
			$view->itemBody = '';
216
217
			foreach( $this->getSubClients() as $client ) {
218
				$view->itemBody .= $client->save();
219
			}
220
221
			$manager->saveItem( clone $view->item );
222
			$manager->commit();
223
224
			$action = $view->param( 'next' );
225
			$id = ( $action === 'create' ? $view->item->getParentId() : $view->item->getId() );
226
227
			$this->nextAction( $view, $action, 'catalog', $id, 'save' );
228
			return null;
229
		}
230
		catch( \Exception $e )
231
		{
232
			$manager->rollback();
233
			$this->report( $e, 'save' );
234
		}
235
236
		return $this->create();
237
	}
238
239
240
	/**
241
	 * Returns the catalog root node
242
	 *
243
	 * @return string|null HTML output
244
	 */
245
	public function search() : ?string
246
	{
247
		$view = $this->getView();
248
249
		try
250
		{
251
			$view->item = \Aimeos\MShop::create( $this->getContext(), 'catalog' )->createItem();
252
			$view->itemRootId = $this->getRootId();
253
			$view->itemBody = '';
254
255
			foreach( $this->getSubClients() as $client ) {
256
				$view->itemBody .= $client->search();
257
			}
258
		}
259
		catch( \Exception $e )
260
		{
261
			$this->report( $e, 'search' );
262
		}
263
264
		return $this->render( $view );
265
	}
266
267
268
	/**
269
	 * Returns the sub-client given by its name.
270
	 *
271
	 * @param string $type Name of the client type
272
	 * @param string|null $name Name of the sub-client (Default if null)
273
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
274
	 */
275
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
276
	{
277
		/** admin/jqadm/catalog/decorators/excludes
278
		 * Excludes decorators added by the "common" option from the catalog JQAdm client
279
		 *
280
		 * Decorators extend the functionality of a class by adding new aspects
281
		 * (e.g. log what is currently done), executing the methods of the underlying
282
		 * class only in certain conditions (e.g. only for logged in users) or
283
		 * modify what is returned to the caller.
284
		 *
285
		 * This option allows you to remove a decorator added via
286
		 * "client/jqadm/common/decorators/default" before they are wrapped
287
		 * around the JQAdm client.
288
		 *
289
		 *  admin/jqadm/catalog/decorators/excludes = array( 'decorator1' )
290
		 *
291
		 * This would remove the decorator named "decorator1" from the list of
292
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
293
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
294
		 *
295
		 * @param array List of decorator names
296
		 * @since 2016.01
297
		 * @category Developer
298
		 * @see admin/jqadm/common/decorators/default
299
		 * @see admin/jqadm/catalog/decorators/global
300
		 * @see admin/jqadm/catalog/decorators/local
301
		 */
302
303
		/** admin/jqadm/catalog/decorators/global
304
		 * Adds a list of globally available decorators only to the catalog JQAdm client
305
		 *
306
		 * Decorators extend the functionality of a class by adding new aspects
307
		 * (e.g. log what is currently done), executing the methods of the underlying
308
		 * class only in certain conditions (e.g. only for logged in users) or
309
		 * modify what is returned to the caller.
310
		 *
311
		 * This option allows you to wrap global decorators
312
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
313
		 *
314
		 *  admin/jqadm/catalog/decorators/global = array( 'decorator1' )
315
		 *
316
		 * This would add the decorator named "decorator1" defined by
317
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
318
		 *
319
		 * @param array List of decorator names
320
		 * @since 2016.01
321
		 * @category Developer
322
		 * @see admin/jqadm/common/decorators/default
323
		 * @see admin/jqadm/catalog/decorators/excludes
324
		 * @see admin/jqadm/catalog/decorators/local
325
		 */
326
327
		/** admin/jqadm/catalog/decorators/local
328
		 * Adds a list of local decorators only to the catalog JQAdm client
329
		 *
330
		 * Decorators extend the functionality of a class by adding new aspects
331
		 * (e.g. log what is currently done), executing the methods of the underlying
332
		 * class only in certain conditions (e.g. only for logged in users) or
333
		 * modify what is returned to the caller.
334
		 *
335
		 * This option allows you to wrap local decorators
336
		 * ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client.
337
		 *
338
		 *  admin/jqadm/catalog/decorators/local = array( 'decorator2' )
339
		 *
340
		 * This would add the decorator named "decorator2" defined by
341
		 * "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client.
342
		 *
343
		 * @param array List of decorator names
344
		 * @since 2016.01
345
		 * @category Developer
346
		 * @see admin/jqadm/common/decorators/default
347
		 * @see admin/jqadm/catalog/decorators/excludes
348
		 * @see admin/jqadm/catalog/decorators/global
349
		 */
350
		return $this->createSubClient( 'catalog/' . $type, $name );
351
	}
352
353
354
	/**
355
	 * Returns the domain names whose items should be fetched too
356
	 *
357
	 * @return string[] List of domain names
358
	 */
359
	protected function getDomains() : array
360
	{
361
		/** admin/jqadm/catalog/domains
362
		 * List of domain items that should be fetched along with the catalog
363
		 *
364
		 * If you need to display additional content, you can configure your own
365
		 * list of domains (attribute, media, price, catalog, text, etc. are
366
		 * domains) whose items are fetched from the storage.
367
		 *
368
		 * @param array List of domain names
369
		 * @since 2016.01
370
		 * @category Developer
371
		 */
372
		return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/domains', [] );
373
	}
374
375
376
	/**
377
	 * Returns the IDs of the root category
378
	 *
379
	 * @return string|null ID of the root category
380
	 */
381
	protected function getRootId() : ?string
382
	{
383
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
384
385
		try {
386
			return $manager->getTree( null, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE )->getId();
387
		} catch( \Exception $e ) {
388
			return null;
389
		}
390
	}
391
392
393
	/**
394
	 * Returns the list of sub-client names configured for the client.
395
	 *
396
	 * @return array List of JQAdm client names
397
	 */
398
	protected function getSubClientNames() : array
399
	{
400
		/** admin/jqadm/catalog/standard/subparts
401
		 * List of JQAdm sub-clients rendered within the catalog section
402
		 *
403
		 * The output of the frontend is composed of the code generated by the JQAdm
404
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
405
		 * that are responsible for rendering certain sub-parts of the output. The
406
		 * sub-clients can contain JQAdm clients themselves and therefore a
407
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
408
		 * the output that is placed inside the container of its parent.
409
		 *
410
		 * At first, always the JQAdm code generated by the parent is printed, then
411
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
412
		 * determines the order of the output of these sub-clients inside the parent
413
		 * container. If the configured list of clients is
414
		 *
415
		 *  array( "subclient1", "subclient2" )
416
		 *
417
		 * you can easily change the order of the output by reordering the subparts:
418
		 *
419
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
420
		 *
421
		 * You can also remove one or more parts if they shouldn't be rendered:
422
		 *
423
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
424
		 *
425
		 * As the clients only generates structural JQAdm, the layout defined via CSS
426
		 * should support adding, removing or reordering content by a fluid like
427
		 * design.
428
		 *
429
		 * @param array List of sub-client names
430
		 * @since 2016.01
431
		 * @category Developer
432
		 */
433
		return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/standard/subparts', [] );
434
	}
435
436
437
	/**
438
	 * Creates new and updates existing items using the data array
439
	 *
440
	 * @param array $data Data array
441
	 * @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object
442
	 */
443
	protected function fromArray( array $data ) : \Aimeos\MShop\Catalog\Item\Iface
444
	{
445
		$conf = [];
446
447
		foreach( (array) $this->getValue( $data, 'config', [] ) as $idx => $entry )
448
		{
449
			if( ( $key = trim( $entry['key'] ?? '' ) ) !== '' ) {
450
				$conf[$key] = trim( $entry['val'] ?? '' );
451
			}
452
		}
453
454
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
455
456
		if( isset( $data['catalog.id'] ) && $data['catalog.id'] != '' ) {
457
			$item = $manager->getItem( $data['catalog.id'], $this->getDomains() );
458
		} else {
459
			$item = $manager->createItem();
460
		}
461
462
		$item->fromArray( $data, true );
463
		$item->setConfig( $conf );
464
465
		if( $item->getId() == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $item->getId() of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
466
			return $manager->insertItem( $item, $data['catalog.parentid'] ?: null );
467
		}
468
469
		return $item;
470
	}
471
472
473
	/**
474
	 * Constructs the data array for the view from the given item
475
	 *
476
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object
477
	 * @return string[] Multi-dimensional associative list of item data
478
	 */
479
	protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, bool $copy = false ) : array
480
	{
481
		$data = $item->toArray( true );
482
		$data['config'] = [];
483
484
		if( $copy === true )
485
		{
486
			$data['catalog.id'] = '';
487
			$data['catalog.siteid'] = $item->getSiteId();
488
			$data['catalog.code'] = $data['catalog.code'] . '_copy';
489
		}
490
491
		foreach( $item->getConfig() as $key => $value ) {
492
			$data['config'][] = ['key' => $key, 'val' => $value];
493
		}
494
495
		return $data;
496
	}
497
498
499
	/**
500
	 * Returns the rendered template including the view data
501
	 *
502
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
503
	 * @return string|null HTML output
504
	 */
505
	protected function render( \Aimeos\MW\View\Iface $view ) : string
506
	{
507
		/** admin/jqadm/catalog/template-item
508
		 * Relative path to the HTML body template for the catalog item.
509
		 *
510
		 * The template file contains the HTML code and processing instructions
511
		 * to generate the result shown in the body of the frontend. The
512
		 * configuration string is the path to the template file relative
513
		 * to the templates directory (usually in admin/jqadm/templates).
514
		 *
515
		 * You can overwrite the template file configuration in extensions and
516
		 * provide alternative templates. These alternative templates should be
517
		 * named like the default one but with the string "default" replaced by
518
		 * an unique name. You may use the name of your project for this. If
519
		 * you've implemented an alternative client class as well, "default"
520
		 * should be replaced by the name of the new class.
521
		 *
522
		 * @param string Relative path to the template creating the HTML code
523
		 * @since 2016.04
524
		 * @category Developer
525
		 */
526
		$tplconf = 'admin/jqadm/catalog/template-item';
527
		$default = 'catalog/item-standard';
528
529
		return $view->render( $view->config( $tplconf, $default ) );
530
	}
531
}
532