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

Standard::create()   A

Complexity

Conditions 4
Paths 20

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 20
nop 0
dl 0
loc 32
rs 9.7
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), 2015-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product;
12
13
sprintf( 'product' ); // for translation
14
15
16
/**
17
 * Default implementation of product 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, 'product' );
43
			$view->item = $manager->getItem( $id, $this->getDomains() );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemTypes = $this->getTypeItems();
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, 'product' )->createItem();
81
			}
82
83
			$data['product.siteid'] = $view->item->getSiteId();
84
85
			$view->itemSubparts = $this->getSubClientNames();
86
			$view->itemTypes = $this->getTypeItems();
87
			$view->itemData = $data;
88
			$view->itemBody = '';
89
90
			foreach( $this->getSubClients() as $idx => $client )
91
			{
92
				$view->tabindex = ++$idx + 1;
93
				$view->itemBody .= $client->create();
94
			}
95
		}
96
		catch( \Exception $e )
97
		{
98
			$this->report( $e, 'create' );
99
		}
100
101
		return $this->render( $view );
102
	}
103
104
105
	/**
106
	 * Deletes a resource
107
	 *
108
	 * @return string|null HTML output
109
	 */
110
	public function delete() : ?string
111
	{
112
		$tags = ['product'];
113
		$view = $this->getView();
114
		$context = $this->getContext();
115
116
		$manager = \Aimeos\MShop::create( $context, 'product' );
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( '==', 'product.id', $ids ) );
127
			$items = $manager->searchItems( $search, $this->getDomains() );
128
129
			foreach( $items as $id => $item )
130
			{
131
				$view->item = $item;
132
				$tags[] = 'product-' . $id;
133
134
				foreach( $this->getSubClients() as $client ) {
135
					$client->delete();
136
				}
137
			}
138
139
			$manager->deleteItems( $items->toArray() );
140
			$manager->commit();
141
142
			\Aimeos\MShop::create( $context, 'index' )->deleteItems( $items->toArray() );
143
			$context->getCache()->deleteByTags( $tags );
144
145
			$this->nextAction( $view, 'search', 'product', null, 'delete' );
146
			return null;
147
		}
148
		catch( \Exception $e )
149
		{
150
			$manager->rollback();
151
			$this->report( $e, 'delete' );
152
		}
153
154
		return $this->search();
155
	}
156
157
158
	/**
159
	 * Returns a single resource
160
	 *
161
	 * @return string|null HTML output
162
	 */
163
	public function get() : ?string
164
	{
165
		$view = $this->getView();
166
		$context = $this->getContext();
167
168
		try
169
		{
170
			if( ( $id = $view->param( 'id' ) ) === null ) {
171
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
172
			}
173
174
			$manager = \Aimeos\MShop::create( $context, 'product' );
175
176
			$view->item = $manager->getItem( $id, $this->getDomains() );
177
			$view->itemSubparts = $this->getSubClientNames();
178
			$view->itemData = $this->toArray( $view->item );
179
			$view->itemTypes = $this->getTypeItems();
180
			$view->itemBody = '';
181
182
			foreach( $this->getSubClients() as $idx => $client )
183
			{
184
				$view->tabindex = ++$idx + 1;
185
				$view->itemBody .= $client->get();
186
			}
187
		}
188
		catch( \Exception $e )
189
		{
190
			$this->report( $e, 'get' );
191
		}
192
193
		return $this->render( $view );
194
	}
195
196
197
	/**
198
	 * Saves the data
199
	 *
200
	 * @return string|null HTML output
201
	 */
202
	public function save() : ?string
203
	{
204
		$view = $this->getView();
205
		$context = $this->getContext();
206
207
		$manager = \Aimeos\MShop::create( $context, 'product' );
208
		$manager->begin();
209
210
		try
211
		{
212
			$item = $this->fromArray( $view->param( 'item', [] ) );
213
			$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

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