Passed
Branch master (d032f7)
by Aimeos
05:30
created

Standard::create()   A

Complexity

Conditions 4
Paths 11

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 11
nop 0
dl 0
loc 27
rs 9.7666
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-2018
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Catalog\Product;
12
13
sprintf( 'product' ); // for translation
14
15
16
/**
17
 * Default implementation of catalog 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
	/** admin/jqadm/catalog/product/name
27
	 * Name of the product subpart used by the JQAdm catalog implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Catalog\Product\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.07
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string HTML output
42
	 */
43
	public function copy()
44
	{
45
		$view = $this->getView();
46
47
		try
48
		{
49
			$view->productListTypes = $this->getListTypes();
50
			$view->productBody = '';
51
52
			foreach( $this->getSubClients() as $client ) {
53
				$view->productBody .= $client->copy();
54
			}
55
		}
56
		catch( \Aimeos\MShop\Exception $e )
57
		{
58
			$error = array( 'catalog-product' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
59
			$view->errors = $view->get( 'errors', [] ) + $error;
60
			$this->logException( $e );
61
		}
62
		catch( \Exception $e )
63
		{
64
			$error = array( 'catalog-product' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
65
			$view->errors = $view->get( 'errors', [] ) + $error;
66
			$this->logException( $e );
67
		}
68
69
		return $this->render( $view );
70
	}
71
72
73
	/**
74
	 * Creates a new resource
75
	 *
76
	 * @return string HTML output
77
	 */
78
	public function create()
79
	{
80
		$view = $this->getView();
81
82
		try
83
		{
84
			$view->productListTypes = $this->getListTypes();
85
			$view->productBody = '';
86
87
			foreach( $this->getSubClients() as $client ) {
88
				$view->productBody .= $client->create();
89
			}
90
		}
91
		catch( \Aimeos\MShop\Exception $e )
92
		{
93
			$error = array( 'catalog-product' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
94
			$view->errors = $view->get( 'errors', [] ) + $error;
95
			$this->logException( $e );
96
		}
97
		catch( \Exception $e )
98
		{
99
			$error = array( 'catalog-product' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
100
			$view->errors = $view->get( 'errors', [] ) + $error;
101
			$this->logException( $e );
102
		}
103
104
		return $this->render( $view );
105
	}
106
107
108
	/**
109
	 * Returns a single resource
110
	 *
111
	 * @return string HTML output
112
	 */
113
	public function get()
114
	{
115
		$view = $this->getView();
116
117
		try
118
		{
119
			$total = 0;
120
			$params = $this->storeSearchParams( $view->param( 'cp', [] ), 'catalogproduct' );
121
			$listItems = $this->getListItems( $view->item, $params, $total );
122
123
			$view->productItems = $this->getProductItems( $listItems );
124
			$view->productData = $this->toArray( $listItems );
125
			$view->productListTypes = $this->getListTypes();
126
			$view->productTotal = $total;
127
			$view->productBody = '';
128
129
			foreach( $this->getSubClients() as $client ) {
130
				$view->productBody .= $client->get();
131
			}
132
		}
133
		catch( \Aimeos\MShop\Exception $e )
134
		{
135
			$error = array( 'catalog-product' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
136
			$view->errors = $view->get( 'errors', [] ) + $error;
137
			$this->logException( $e );
138
		}
139
		catch( \Exception $e )
140
		{
141
			$error = array( 'catalog-product' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
142
			$view->errors = $view->get( 'errors', [] ) + $error;
143
			$this->logException( $e );
144
		}
145
146
		return $this->render( $view );
147
	}
148
149
150
	/**
151
	 * Saves the data
152
	 */
153
	public function save()
154
	{
155
		$view = $this->getView();
156
		$context = $this->getContext();
157
158
		$manager = \Aimeos\MShop::create( $context, 'catalog/lists' );
159
160
		$manager->begin();
161
162
		try
163
		{
164
			$this->storeSearchParams( $view->param( 'cp', [] ), 'catalogproduct' );
165
			$this->fromArray( $view->item, $view->param( 'product', [] ) );
166
			$view->productBody = '';
167
168
			foreach( $this->getSubClients() as $client ) {
169
				$view->productBody .= $client->save();
170
			}
171
172
			$manager->commit();
173
			return;
174
		}
175
		catch( \Aimeos\MShop\Exception $e )
176
		{
177
			$error = array( 'catalog-item-media' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
178
			$view->errors = $view->get( 'errors', [] ) + $error;
179
			$this->logException( $e );
180
		}
181
		catch( \Exception $e )
182
		{
183
			$error = array( 'catalog-item-media' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
184
			$view->errors = $view->get( 'errors', [] ) + $error;
185
			$this->logException( $e );
186
		}
187
188
		$manager->rollback();
189
190
		throw new \Aimeos\Admin\JQAdm\Exception();
191
	}
192
193
194
	/**
195
	 * Returns the sub-client given by its name.
196
	 *
197
	 * @param string $type Name of the client type
198
	 * @param string|null $name Name of the sub-client (Default if null)
199
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
200
	 */
201
	public function getSubClient( $type, $name = null )
202
	{
203
		/** admin/jqadm/catalog/product/decorators/excludes
204
		 * Excludes decorators added by the "common" option from the catalog JQAdm client
205
		 *
206
		 * Decorators extend the functionality of a class by adding new aspects
207
		 * (e.g. log what is currently done), executing the methods of the underlying
208
		 * class only in certain conditions (e.g. only for logged in users) or
209
		 * modify what is returned to the caller.
210
		 *
211
		 * This option allows you to remove a decorator added via
212
		 * "admin/jqadm/common/decorators/default" before they are wrapped
213
		 * around the JQAdm client.
214
		 *
215
		 *  admin/jqadm/catalog/product/decorators/excludes = array( 'decorator1' )
216
		 *
217
		 * This would remove the decorator named "decorator1" from the list of
218
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
219
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
220
		 *
221
		 * @param array List of decorator names
222
		 * @since 2017.07
223
		 * @category Developer
224
		 * @see admin/jqadm/common/decorators/default
225
		 * @see admin/jqadm/catalog/product/decorators/global
226
		 * @see admin/jqadm/catalog/product/decorators/local
227
		 */
228
229
		/** admin/jqadm/catalog/product/decorators/global
230
		 * Adds a list of globally available decorators only to the catalog JQAdm client
231
		 *
232
		 * Decorators extend the functionality of a class by adding new aspects
233
		 * (e.g. log what is currently done), executing the methods of the underlying
234
		 * class only in certain conditions (e.g. only for logged in users) or
235
		 * modify what is returned to the caller.
236
		 *
237
		 * This option allows you to wrap global decorators
238
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
239
		 *
240
		 *  admin/jqadm/catalog/product/decorators/global = array( 'decorator1' )
241
		 *
242
		 * This would add the decorator named "decorator1" defined by
243
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
244
		 *
245
		 * @param array List of decorator names
246
		 * @since 2017.07
247
		 * @category Developer
248
		 * @see admin/jqadm/common/decorators/default
249
		 * @see admin/jqadm/catalog/product/decorators/excludes
250
		 * @see admin/jqadm/catalog/product/decorators/local
251
		 */
252
253
		/** admin/jqadm/catalog/product/decorators/local
254
		 * Adds a list of local decorators only to the catalog JQAdm client
255
		 *
256
		 * Decorators extend the functionality of a class by adding new aspects
257
		 * (e.g. log what is currently done), executing the methods of the underlying
258
		 * class only in certain conditions (e.g. only for logged in users) or
259
		 * modify what is returned to the caller.
260
		 *
261
		 * This option allows you to wrap local decorators
262
		 * ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client.
263
		 *
264
		 *  admin/jqadm/catalog/product/decorators/local = array( 'decorator2' )
265
		 *
266
		 * This would add the decorator named "decorator2" defined by
267
		 * "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client.
268
		 *
269
		 * @param array List of decorator names
270
		 * @since 2017.07
271
		 * @category Developer
272
		 * @see admin/jqadm/common/decorators/default
273
		 * @see admin/jqadm/catalog/product/decorators/excludes
274
		 * @see admin/jqadm/catalog/product/decorators/global
275
		 */
276
		return $this->createSubClient( 'catalog/product/' . $type, $name );
277
	}
278
279
280
	/**
281
	 * Returns the catalog list items referencing the products
282
	 *
283
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object
284
	 * @param array $params Associative list of GET/POST parameters
285
	 * @param integer $total Value/result parameter that will contain the item total afterwards
286
	 * @return \Aimeos\MShop\Common\Item\List\Iface[] Catalog list items referencing the products
287
	 */
288
	protected function getListItems( \Aimeos\MShop\Catalog\Item\Iface $item, array $params = [], &$total )
289
	{
290
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
291
292
		$search = $manager->createSearch();
293
		$search->setSortations( [$search->sort( '+', 'catalog.lists.position' )] );
294
295
		$search = $this->initCriteria( $search, $params, 'catalogproduct' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Admin\JQAdm\Base::initCriteria() has too many arguments starting with 'catalogproduct'. ( Ignorable by Annotation )

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

295
		/** @scrutinizer ignore-call */ 
296
  $search = $this->initCriteria( $search, $params, 'catalogproduct' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
296
		$expr = [
297
			$search->getConditions(),
298
			$search->compare( '==', 'catalog.lists.parentid', $item->getId() ),
299
			$search->compare( '==', 'catalog.lists.domain', 'product' ),
300
		];
301
		$search->setConditions( $search->combine( '&&', $expr ) );
302
303
		return $manager->searchItems( $search, [], $total );
304
	}
305
306
307
	/**
308
	 * Returns the available product list types
309
	 *
310
	 * @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of type IDs as keys and type codes as values
311
	 */
312
	protected function getListTypes()
313
	{
314
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists/type' );
315
316
		$search = $manager->createSearch( true )->setSlice( 0, 10000 );
317
		$search->setConditions( $search->compare( '==', 'catalog.lists.type.domain', 'product' ) );
318
		$search->setSortations( [$search->sort( '+', 'catalog.lists.type.position' )] );
319
320
		return $this->map( $manager->searchItems( $search ) );
321
	}
322
323
324
	/**
325
	 * Returns the product items referenced by the given list items
326
	 *
327
	 * @param \Aimeos\MShop\Common\Item\List\Iface[] $listItems Catalog list items referencing the products
328
	 * @return \Aimeos\MShop\Product\Item\Iface[] Associative list of product IDs as keys and items as values
329
	 */
330
	protected function getProductItems( array $listItems )
331
	{
332
		$list = [];
333
334
		foreach( $listItems as $listItem ) {
335
			$list[] = $listItem->getRefId();
336
		}
337
338
		$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
339
340
		$search = $manager->createSearch()->setSlice( 0, count( $list ) );
341
		$search->setConditions( $search->compare( '==', 'product.id', $list ) );
342
343
		return $manager->searchItems( $search );
344
	}
345
346
347
	/**
348
	 * Returns the list of sub-client names configured for the client.
349
	 *
350
	 * @return array List of JQAdm client names
351
	 */
352
	protected function getSubClientNames()
353
	{
354
		/** admin/jqadm/catalog/product/standard/subparts
355
		 * List of JQAdm sub-clients rendered within the catalog product section
356
		 *
357
		 * The output of the frontend is composed of the code generated by the JQAdm
358
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
359
		 * that are responsible for rendering certain sub-parts of the output. The
360
		 * sub-clients can contain JQAdm clients themselves and therefore a
361
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
362
		 * the output that is placed inside the container of its parent.
363
		 *
364
		 * At first, always the JQAdm code generated by the parent is printed, then
365
		 * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients
366
		 * determines the product of the output of these sub-clients inside the parent
367
		 * container. If the configured list of clients is
368
		 *
369
		 *  array( "subclient1", "subclient2" )
370
		 *
371
		 * you can easily change the product of the output by reproducting the subparts:
372
		 *
373
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
374
		 *
375
		 * You can also remove one or more parts if they shouldn't be rendered:
376
		 *
377
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
378
		 *
379
		 * As the clients only generates structural JQAdm, the layout defined via CSS
380
		 * should support adding, removing or reproducting content by a fluid like
381
		 * design.
382
		 *
383
		 * @param array List of sub-client names
384
		 * @since 2017.07
385
		 * @category Developer
386
		 */
387
		return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/product/standard/subparts', [] );
388
	}
389
390
391
	/**
392
	 * Creates new and updates existing items using the data array
393
	 *
394
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object without referenced domain items
395
	 * @param string[] $data Data array
396
	 */
397
	protected function fromArray( \Aimeos\MShop\Catalog\Item\Iface $item, array $data )
398
	{
399
		$context = $this->getContext();
400
		$listIds = $this->getValue( $data, 'catalog.lists.id', [] );
401
402
		$listManager = \Aimeos\MShop::create( $context, 'catalog/lists' );
403
404
		$search = $listManager->createSearch()->setSlice( 0, count( $listIds ) );
0 ignored issues
show
Bug introduced by
It seems like $listIds can also be of type string; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

404
		$search = $listManager->createSearch()->setSlice( 0, count( /** @scrutinizer ignore-type */ $listIds ) );
Loading history...
405
		$search->setConditions( $search->compare( '==', 'catalog.lists.id', $listIds ) );
406
407
		$listItem = $listManager->createItem();
408
		$listItem->setParentId( $item->getId() );
0 ignored issues
show
Bug introduced by
The method setParentId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

408
		$listItem->/** @scrutinizer ignore-call */ 
409
             setParentId( $item->getId() );

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...
409
		$listItem->setDomain( 'product' );
410
411
412
		foreach( (array) $listIds as $idx => $listid )
413
		{
414
			if( isset( $listItems[$listid] ) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $listItems does not exist. Did you maybe mean $listItem?
Loading history...
415
				$litem = $listItems[$listid];
416
			} else {
417
				$litem = clone $listItem;
418
			}
419
420
			$litem->setId( $listid ?: null );
421
422
			if( isset( $data['catalog.lists.refid'][$idx] ) ) {
423
				$litem->setRefId( $this->getValue( $data, 'catalog.lists.refid/' . $idx ) );
424
			}
425
426
			if( isset( $data['catalog.lists.status'][$idx] ) ) {
427
				$litem->setStatus( $this->getValue( $data, 'catalog.lists.status/' . $idx ) );
428
			}
429
430
			if( isset( $data['catalog.lists.type'][$idx] ) ) {
431
				$litem->setType( $this->getValue( $data, 'catalog.lists.type/' . $idx ) );
432
			}
433
434
			if( isset( $data['catalog.lists.position'][$idx] ) ) {
435
				$litem->setPosition( $this->getValue( $data, 'catalog.lists.position/' . $idx ) );
436
			}
437
438
			if( isset( $data['catalog.lists.datestart'][$idx] ) ) {
439
				$litem->setDateStart( $this->getValue( $data, 'catalog.lists.datestart/' . $idx ) );
440
			}
441
442
			if( isset( $data['catalog.lists.dateend'][$idx] ) ) {
443
				$litem->setDateEnd( $this->getValue( $data, 'catalog.lists.dateend/' . $idx ) );
444
			}
445
446
			if( isset( $data['catalog.lists.config'][$idx] )
447
				&& ( $conf = json_decode( $this->getValue( $data, 'catalog.lists.config/' . $idx ), true ) ) !== null
0 ignored issues
show
Bug introduced by
It seems like $this->getValue($data, '....lists.config/' . $idx) can also be of type string[]; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

447
				&& ( $conf = json_decode( /** @scrutinizer ignore-type */ $this->getValue( $data, 'catalog.lists.config/' . $idx ), true ) ) !== null
Loading history...
448
			) {
449
				$litem->setConfig( $conf );
450
			}
451
452
			if( isset( $data['config'][$idx]['key'] ) )
453
			{
454
				$conf = [];
455
456
				foreach( (array) $data['config'][$idx]['key'] as $pos => $key )
457
				{
458
					if( trim( $key ) !== '' && isset( $data['config'][$idx]['val'][$pos] ) ) {
459
						$conf[$key] = $data['config'][$idx]['val'][$pos];
460
					}
461
				}
462
463
				$litem->setConfig( $conf );
464
			}
465
466
			$listManager->saveItem( $litem, false );
467
		}
468
	}
469
470
471
	/**
472
	 * Constructs the data array for the view from the given item
473
	 *
474
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems Catalog list items referencing the products
475
	 * @return string[] Multi-dimensional associative list of item data
476
	 */
477
	protected function toArray( array $listItems )
478
	{
479
		$data = [];
480
481
		foreach( $listItems as $listItem )
482
		{
483
			foreach( $listItem->toArray( true ) as $key => $value ) {
484
				$data[$key][] = $value;
485
			}
486
		}
487
488
		return $data;
489
	}
490
491
492
	/**
493
	 * Returns the rendered template including the view data
494
	 *
495
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
496
	 * @return string HTML output
497
	 */
498
	protected function render( \Aimeos\MW\View\Iface $view )
499
	{
500
		/** admin/jqadm/catalog/product/template-item
501
		 * Relative path to the HTML body template of the product subpart for catalogs.
502
		 *
503
		 * The template file contains the HTML code and processing instructions
504
		 * to generate the result shown in the body of the frontend. The
505
		 * configuration string is the path to the template file relative
506
		 * to the templates directory (usually in admin/jqadm/templates).
507
		 *
508
		 * You can overwrite the template file configuration in extensions and
509
		 * provide alternative templates. These alternative templates should be
510
		 * named like the default one but with the string "default" replaced by
511
		 * an unique name. You may use the name of your project for this. If
512
		 * you've implemented an alternative client class as well, "default"
513
		 * should be replaced by the name of the new class.
514
		 *
515
		 * @param string Relative path to the template creating the HTML code
516
		 * @since 2016.04
517
		 * @category Developer
518
		 */
519
		$tplconf = 'admin/jqadm/catalog/product/template-item';
520
		$default = 'catalog/item-product-standard';
521
522
		return $view->render( $view->config( $tplconf, $default ) );
523
	}
524
}
525