Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard::delete()   A

Complexity

Conditions 5
Paths 25

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 25
nop 0
dl 0
loc 40
rs 9.2568
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
	 * Copies a resource
28
	 *
29
	 * @return string|null HTML output
30
	 */
31
	public function copy() : ?string
32
	{
33
		$view = $this->getView();
34
		$context = $this->getContext();
35
36
		try
37
		{
38
			if( ( $id = $view->param( 'id' ) ) === null ) {
39
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
40
			}
41
42
			$manager = \Aimeos\MShop::create( $context, 'catalog' );
43
			$view->item = $manager->getItem( $id, $this->getDomains() );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemRootId = $this->getRootId();
48
			$view->itemBody = '';
49
50
			foreach( $this->getSubClients() as $idx => $client )
51
			{
52
				$view->tabindex = ++$idx + 1;
53
				$view->itemBody .= $client->copy();
54
			}
55
		}
56
		catch( \Exception $e )
57
		{
58
			$this->report( $e, 'copy' );
59
		}
60
61
		return $this->render( $view );
62
	}
63
64
65
	/**
66
	 * Creates a new resource
67
	 *
68
	 * @return string|null HTML output
69
	 */
70
	public function create() : ?string
71
	{
72
		$view = $this->getView();
73
		$context = $this->getContext();
74
75
		try
76
		{
77
			$data = $view->param( 'item', [] );
78
79
			if( !isset( $view->item ) ) {
80
				$view->item = \Aimeos\MShop::create( $context, 'catalog' )->createItem();
81
			}
82
83
			$data['catalog.siteid'] = $view->item->getSiteId();
84
			$data['catalog.parentid'] = $view->item->getParentId() ?: $view->param( 'parentid', $view->param( 'item/catalog.parentid' ) );
85
86
			$view->itemSubparts = $this->getSubClientNames();
87
			$view->itemRootId = $this->getRootId();
88
			$view->itemData = $data;
89
			$view->itemBody = '';
90
91
			foreach( $this->getSubClients() as $idx => $client )
92
			{
93
				$view->tabindex = ++$idx + 1;
94
				$view->itemBody .= $client->create();
95
			}
96
		}
97
		catch( \Exception $e )
98
		{
99
			$this->report( $e, 'create' );
100
		}
101
102
		return $this->render( $view );
103
	}
104
105
106
	/**
107
	 * Deletes a resource
108
	 *
109
	 * @return string|null HTML output
110
	 */
111
	public function delete() : ?string
112
	{
113
		$view = $this->getView();
114
		$context = $this->getContext();
115
116
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
117
		$manager->begin();
118
119
		try
120
		{
121
			if( ( $ids = $view->param( 'id' ) ) === null ) {
122
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
123
			}
124
125
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
126
			$search->setConditions( $search->compare( '==', 'catalog.id', $ids ) );
127
			$items = $manager->searchItems( $search, $this->getDomains() );
128
129
			foreach( $items as $item )
130
			{
131
				$view->item = $item;
132
133
				foreach( $this->getSubClients() as $client ) {
134
					$client->delete();
135
				}
136
			}
137
138
			$manager->deleteItems( $items->toArray() );
139
			$manager->commit();
140
141
			$this->nextAction( $view, 'search', 'catalog', null, 'delete' );
142
			return null;
143
		}
144
		catch( \Exception $e )
145
		{
146
			$manager->rollback();
147
			$this->report( $e, 'delete' );
148
		}
149
150
		return $this->search();
151
	}
152
153
154
	/**
155
	 * Returns a single resource
156
	 *
157
	 * @return string|null HTML output
158
	 */
159
	public function get() : ?string
160
	{
161
		$view = $this->getView();
162
		$context = $this->getContext();
163
164
		try
165
		{
166
			if( ( $id = $view->param( 'id' ) ) === null ) {
167
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
168
			}
169
170
			$manager = \Aimeos\MShop::create( $context, 'catalog' );
171
172
			$view->item = $manager->getItem( $id, $this->getDomains() );
173
			$view->itemSubparts = $this->getSubClientNames();
174
			$view->itemData = $this->toArray( $view->item );
175
			$view->itemRootId = $this->getRootId();
176
			$view->itemBody = '';
177
178
			foreach( $this->getSubClients() as $idx => $client )
179
			{
180
				$view->tabindex = ++$idx + 1;
181
				$view->itemBody .= $client->get();
182
			}
183
		}
184
		catch( \Exception $e )
185
		{
186
			$this->report( $e, 'get' );
187
		}
188
189
		return $this->render( $view );
190
	}
191
192
193
	/**
194
	 * Saves the data
195
	 *
196
	 * @return string|null HTML output
197
	 */
198
	public function save() : ?string
199
	{
200
		$view = $this->getView();
201
		$context = $this->getContext();
202
203
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
204
		$manager->begin();
205
206
		try
207
		{
208
			$item = $this->fromArray( $view->param( 'item', [] ) );
209
			$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

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