Completed
Push — master ( 7818a8...a3fccc )
by Aimeos
04:08
created

Standard::create()   A

Complexity

Conditions 4
Paths 14

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 14
nop 0
dl 0
loc 25
rs 9.8333
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 = parent::copy();
60
		}
61
		catch( \Exception $e )
62
		{
63
			$this->report( $e, 'copy' );
64
		}
65
66
		return $this->render( $view );
67
	}
68
69
70
	/**
71
	 * Creates a new resource
72
	 *
73
	 * @return string|null HTML output
74
	 */
75
	public function create() : ?string
76
	{
77
		$view = $this->getObject()->addData( $this->getView() );
78
79
		try
80
		{
81
			$data = $view->param( 'item', [] );
82
83
			if( !isset( $view->item ) ) {
84
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'catalog' )->createItem();
85
			}
86
87
			$data['catalog.siteid'] = $view->item->getSiteId();
88
			$data['catalog.parentid'] = $view->item->getParentId() ?: $view->param( 'parentid', $view->param( 'item/catalog.parentid' ) );
89
90
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
91
			$view->itemRootId = $this->getRootId();
92
			$view->itemBody = parent::create();
93
		}
94
		catch( \Exception $e )
95
		{
96
			$this->report( $e, 'create' );
97
		}
98
99
		return $this->render( $view );
100
	}
101
102
103
	/**
104
	 * Deletes a resource
105
	 *
106
	 * @return string|null HTML output
107
	 */
108
	public function delete() : ?string
109
	{
110
		$view = $this->getView();
111
112
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
113
		$manager->begin();
114
115
		try
116
		{
117
			if( ( $ids = $view->param( 'id' ) ) === null ) {
118
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
119
			}
120
121
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
122
			$search->setConditions( $search->compare( '==', 'catalog.id', $ids ) );
123
			$items = $manager->searchItems( $search, $this->getDomains() );
124
125
			foreach( $items as $item )
126
			{
127
				$view->item = $item;
128
				parent::delete();
129
			}
130
131
			$manager->deleteItems( $items->toArray() );
132
			$manager->commit();
133
134
			return $this->redirect( 'catalog', 'search', null, 'delete' );
135
		}
136
		catch( \Exception $e )
137
		{
138
			$manager->rollback();
139
			$this->report( $e, 'delete' );
140
		}
141
142
		return $this->search();
143
	}
144
145
146
	/**
147
	 * Returns a single resource
148
	 *
149
	 * @return string|null HTML output
150
	 */
151
	public function get() : ?string
152
	{
153
		$view = $this->getObject()->addData( $this->getView() );
154
155
		try
156
		{
157
			if( ( $id = $view->param( 'id' ) ) === null ) {
158
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
159
			}
160
161
			$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
162
163
			$view->item = $manager->getItem( $id, $this->getDomains() );
164
			$view->itemData = $this->toArray( $view->item );
165
			$view->itemRootId = $this->getRootId();
166
			$view->itemBody = parent::get();
167
		}
168
		catch( \Exception $e )
169
		{
170
			$this->report( $e, 'get' );
171
		}
172
173
		return $this->render( $view );
174
	}
175
176
177
	/**
178
	 * Saves the data
179
	 *
180
	 * @return string|null HTML output
181
	 */
182
	public function save() : ?string
183
	{
184
		$view = $this->getView();
185
186
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
187
		$manager->begin();
188
189
		try
190
		{
191
			$item = $this->fromArray( $view->param( 'item', [] ) );
192
			$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

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