Passed
Push — master ( b95f84...8de0c9 )
by Aimeos
04:01
created

Standard::fromArray()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
nc 6
nop 1
dl 0
loc 23
rs 9.5555
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
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 HTML output
30
	 */
31
	public function copy()
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, 'index' );
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( \Aimeos\MShop\Exception $e )
57
		{
58
			$error = array( 'product-item' => $context->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( 'product-item' => $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
		$context = $this->getContext();
82
83
		try
84
		{
85
			$data = $view->param( 'item', [] );
86
87
			if( !isset( $view->item ) ) {
88
				$view->item = \Aimeos\MShop::create( $context, 'index' )->createItem();
89
			}
90
91
			$data['product.siteid'] = $view->item->getSiteId();
92
93
			$view->itemSubparts = $this->getSubClientNames();
94
			$view->itemTypes = $this->getTypeItems();
95
			$view->itemData = $data;
96
			$view->itemBody = '';
97
98
			foreach( $this->getSubClients() as $idx => $client )
99
			{
100
				$view->tabindex = ++$idx + 1;
101
				$view->itemBody .= $client->create();
102
			}
103
		}
104
		catch( \Aimeos\MShop\Exception $e )
105
		{
106
			$error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
107
			$view->errors = $view->get( 'errors', [] ) + $error;
108
			$this->logException( $e );
109
		}
110
		catch( \Exception $e )
111
		{
112
			$error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
113
			$view->errors = $view->get( 'errors', [] ) + $error;
114
			$this->logException( $e );
115
		}
116
117
		return $this->render( $view );
118
	}
119
120
121
	/**
122
	 * Deletes a resource
123
	 *
124
	 * @return string|null HTML output
125
	 */
126
	public function delete()
127
	{
128
		$view = $this->getView();
129
		$context = $this->getContext();
130
131
		$manager = \Aimeos\MShop::create( $context, 'index' );
132
		$manager->begin();
133
134
		try
135
		{
136
			if( ( $ids = $view->param( 'id' ) ) === null ) {
137
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
138
			}
139
140
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
141
			$search->setConditions( $search->compare( '==', 'product.id', $ids ) );
142
			$items = $manager->searchItems( $search, $this->getDomains() );
143
144
			foreach( $items as $item )
145
			{
146
				$view->item = $item;
147
148
				foreach( $this->getSubClients() as $client ) {
149
					$client->delete();
150
				}
151
152
				$manager->saveItem( $view->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

152
				$manager->/** @scrutinizer ignore-call */ 
153
              saveItem( $view->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...
153
			}
154
155
			$manager->deleteItems( array_keys( $items ) );
156
			$manager->commit();
157
158
			$this->nextAction( $view, 'search', 'product', null, 'delete' );
159
			return;
160
		}
161
		catch( \Aimeos\MShop\Exception $e )
162
		{
163
			$error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
164
			$view->errors = $view->get( 'errors', [] ) + $error;
165
			$this->logException( $e );
166
		}
167
		catch( \Exception $e )
168
		{
169
			$error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
170
			$view->errors = $view->get( 'errors', [] ) + $error;
171
			$this->logException( $e );
172
		}
173
174
		$manager->rollback();
175
176
		return $this->search();
177
	}
178
179
180
	/**
181
	 * Returns a single resource
182
	 *
183
	 * @return string HTML output
184
	 */
185
	public function get()
186
	{
187
		$view = $this->getView();
188
		$context = $this->getContext();
189
190
		try
191
		{
192
			if( ( $id = $view->param( 'id' ) ) === null ) {
193
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
194
			}
195
196
			$manager = \Aimeos\MShop::create( $context, 'index' );
197
198
			$view->item = $manager->getItem( $id, $this->getDomains() );
199
			$view->itemSubparts = $this->getSubClientNames();
200
			$view->itemData = $this->toArray( $view->item );
201
			$view->itemTypes = $this->getTypeItems();
202
			$view->itemBody = '';
203
204
			foreach( $this->getSubClients() as $idx => $client )
205
			{
206
				$view->tabindex = ++$idx + 1;
207
				$view->itemBody .= $client->get();
208
			}
209
		}
210
		catch( \Aimeos\MShop\Exception $e )
211
		{
212
			$error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
213
			$view->errors = $view->get( 'errors', [] ) + $error;
214
			$this->logException( $e );
215
		}
216
		catch( \Exception $e )
217
		{
218
			$error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
219
			$view->errors = $view->get( 'errors', [] ) + $error;
220
			$this->logException( $e );
221
		}
222
223
		return $this->render( $view );
224
	}
225
226
227
	/**
228
	 * Saves the data
229
	 *
230
	 * @return string HTML output
231
	 */
232
	public function save()
233
	{
234
		$view = $this->getView();
235
		$context = $this->getContext();
236
237
		$manager = \Aimeos\MShop::create( $context, 'index' );
238
		$manager->begin();
239
240
		try
241
		{
242
			$item = $this->fromArray( $view->param( 'item', [] ) );
243
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
244
			$view->itemBody = '';
245
246
			foreach( $this->getSubClients() as $client ) {
247
				$view->itemBody .= $client->save();
248
			}
249
250
			$manager->saveItem( clone $view->item );
251
			$manager->commit();
252
253
			$this->nextAction( $view, $view->param( 'next' ), 'product', $view->item->getId(), 'save' );
254
			return;
255
		}
256
		catch( \Aimeos\Admin\JQAdm\Exception $e )
257
		{
258
			// fall through to create
259
		}
260
		catch( \Aimeos\MShop\Exception $e )
261
		{
262
			$error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
263
			$view->errors = $view->get( 'errors', [] ) + $error;
264
			$this->logException( $e );
265
		}
266
		catch( \Exception $e )
267
		{
268
			$error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
269
			$view->errors = $view->get( 'errors', [] ) + $error;
270
			$this->logException( $e );
271
		}
272
273
		$manager->rollback();
274
275
		return $this->create();
276
	}
277
278
279
	/**
280
	 * Returns a list of resource according to the conditions
281
	 *
282
	 * @return string HTML output
283
	 */
284
	public function search()
285
	{
286
		$view = $this->getView();
287
		$context = $this->getContext();
288
289
		try
290
		{
291
			$total = 0;
292
			$params = $this->storeSearchParams( $view->param(), 'product' );
293
			$manager = \Aimeos\MShop::create( $context, 'index' );
294
295
			$search = $manager->createSearch();
296
			$search->setSortations( [$search->sort( '+', 'product.id')] );
297
			$search = $this->initCriteria( $search, $params );
298
299
			$view->items = $manager->searchItems( $search, array_diff( $this->getDomains(), ['product'] ), $total );
300
			$view->filterAttributes = $manager->getSearchAttributes( true );
301
			$view->filterOperators = $search->getOperators();
302
			$view->itemTypes = $this->getTypeItems();
303
			$view->total = $total;
304
			$view->itemBody = '';
305
306
			foreach( $this->getSubClients() as $client ) {
307
				$view->itemBody .= $client->search();
308
			}
309
		}
310
		catch( \Aimeos\MShop\Exception $e )
311
		{
312
			$error = array( 'product-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
313
			$view->errors = $view->get( 'errors', [] ) + $error;
314
			$this->logException( $e );
315
		}
316
		catch( \Exception $e )
317
		{
318
			$error = array( 'product-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
319
			$view->errors = $view->get( 'errors', [] ) + $error;
320
			$this->logException( $e );
321
		}
322
323
		/** admin/jqadm/product/template-list
324
		 * Relative path to the HTML body template for the product list.
325
		 *
326
		 * The template file contains the HTML code and processing instructions
327
		 * to generate the result shown in the body of the frontend. The
328
		 * configuration string is the path to the template file relative
329
		 * to the templates directory (usually in admin/jqadm/templates).
330
		 *
331
		 * You can overwrite the template file configuration in extensions and
332
		 * provide alternative templates. These alternative templates should be
333
		 * named like the default one but with the string "default" replaced by
334
		 * an unique name. You may use the name of your project for this. If
335
		 * you've implemented an alternative client class as well, "default"
336
		 * should be replaced by the name of the new class.
337
		 *
338
		 * @param string Relative path to the template creating the HTML code
339
		 * @since 2016.04
340
		 * @category Developer
341
		 */
342
		$tplconf = 'admin/jqadm/product/template-list';
343
		$default = 'product/list-standard';
344
345
		return $view->render( $view->config( $tplconf, $default ) );
346
	}
347
348
349
	/**
350
	 * Returns the sub-client given by its name.
351
	 *
352
	 * @param string $type Name of the client type
353
	 * @param string|null $name Name of the sub-client (Default if null)
354
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
355
	 */
356
	public function getSubClient( $type, $name = null )
357
	{
358
		/** admin/jqadm/product/decorators/excludes
359
		 * Excludes decorators added by the "common" option from the product JQAdm client
360
		 *
361
		 * Decorators extend the functionality of a class by adding new aspects
362
		 * (e.g. log what is currently done), executing the methods of the underlying
363
		 * class only in certain conditions (e.g. only for logged in users) or
364
		 * modify what is returned to the caller.
365
		 *
366
		 * This option allows you to remove a decorator added via
367
		 * "client/jqadm/common/decorators/default" before they are wrapped
368
		 * around the JQAdm client.
369
		 *
370
		 *  admin/jqadm/product/decorators/excludes = array( 'decorator1' )
371
		 *
372
		 * This would remove the decorator named "decorator1" from the list of
373
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
374
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
375
		 *
376
		 * @param array List of decorator names
377
		 * @since 2016.01
378
		 * @category Developer
379
		 * @see admin/jqadm/common/decorators/default
380
		 * @see admin/jqadm/product/decorators/global
381
		 * @see admin/jqadm/product/decorators/local
382
		 */
383
384
		/** admin/jqadm/product/decorators/global
385
		 * Adds a list of globally available decorators only to the product JQAdm client
386
		 *
387
		 * Decorators extend the functionality of a class by adding new aspects
388
		 * (e.g. log what is currently done), executing the methods of the underlying
389
		 * class only in certain conditions (e.g. only for logged in users) or
390
		 * modify what is returned to the caller.
391
		 *
392
		 * This option allows you to wrap global decorators
393
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
394
		 *
395
		 *  admin/jqadm/product/decorators/global = array( 'decorator1' )
396
		 *
397
		 * This would add the decorator named "decorator1" defined by
398
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
399
		 *
400
		 * @param array List of decorator names
401
		 * @since 2016.01
402
		 * @category Developer
403
		 * @see admin/jqadm/common/decorators/default
404
		 * @see admin/jqadm/product/decorators/excludes
405
		 * @see admin/jqadm/product/decorators/local
406
		 */
407
408
		/** admin/jqadm/product/decorators/local
409
		 * Adds a list of local decorators only to the product JQAdm client
410
		 *
411
		 * Decorators extend the functionality of a class by adding new aspects
412
		 * (e.g. log what is currently done), executing the methods of the underlying
413
		 * class only in certain conditions (e.g. only for logged in users) or
414
		 * modify what is returned to the caller.
415
		 *
416
		 * This option allows you to wrap local decorators
417
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
418
		 *
419
		 *  admin/jqadm/product/decorators/local = array( 'decorator2' )
420
		 *
421
		 * This would add the decorator named "decorator2" defined by
422
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
423
		 *
424
		 * @param array List of decorator names
425
		 * @since 2016.01
426
		 * @category Developer
427
		 * @see admin/jqadm/common/decorators/default
428
		 * @see admin/jqadm/product/decorators/excludes
429
		 * @see admin/jqadm/product/decorators/global
430
		 */
431
		return $this->createSubClient( 'product/' . $type, $name );
432
	}
433
434
435
	/**
436
	 * Returns the domain names whose items should be fetched too
437
	 *
438
	 * @return string[] List of domain names
439
	 */
440
	protected function getDomains()
441
	{
442
		/** admin/jqadm/product/domains
443
		 * List of domain items that should be fetched along with the product
444
		 *
445
		 * If you need to display additional content, you can configure your own
446
		 * list of domains (attribute, media, price, product, text, etc. are
447
		 * domains) whose items are fetched from the storage.
448
		 *
449
		 * @param array List of domain names
450
		 * @since 2016.01
451
		 * @category Developer
452
		 */
453
		$domains = array( 'attribute', 'media', 'price', 'product', 'product/property', 'text' );
454
455
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/domains', $domains );
456
	}
457
458
459
	/**
460
	 * Returns the list of sub-client names configured for the client.
461
	 *
462
	 * @return array List of JQAdm client names
463
	 */
464
	protected function getSubClientNames()
465
	{
466
		/** admin/jqadm/product/standard/subparts
467
		 * List of JQAdm sub-clients rendered within the product section
468
		 *
469
		 * The output of the frontend is composed of the code generated by the JQAdm
470
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
471
		 * that are responsible for rendering certain sub-parts of the output. The
472
		 * sub-clients can contain JQAdm clients themselves and therefore a
473
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
474
		 * the output that is placed inside the container of its parent.
475
		 *
476
		 * At first, always the JQAdm code generated by the parent is printed, then
477
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
478
		 * determines the order of the output of these sub-clients inside the parent
479
		 * container. If the configured list of clients is
480
		 *
481
		 *  array( "subclient1", "subclient2" )
482
		 *
483
		 * you can easily change the order of the output by reordering the subparts:
484
		 *
485
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
486
		 *
487
		 * You can also remove one or more parts if they shouldn't be rendered:
488
		 *
489
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
490
		 *
491
		 * As the clients only generates structural JQAdm, the layout defined via CSS
492
		 * should support adding, removing or reordering content by a fluid like
493
		 * design.
494
		 *
495
		 * @param array List of sub-client names
496
		 * @since 2016.01
497
		 * @category Developer
498
		 */
499
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/standard/subparts', [] );
500
	}
501
502
503
	/**
504
	 * Returns the available product type items
505
	 *
506
	 * @return array List of item implementing \Aimeos\MShop\Common\Type\Iface
507
	 */
508
	protected function getTypeItems()
509
	{
510
		$typeManager = \Aimeos\MShop::create( $this->getContext(), 'product/type' );
511
512
		$search = $typeManager->createSearch( true )->setSlice( 0, 10000 );
513
		$search->setSortations( [$search->sort( '+', 'product.type.position' )] );
514
515
		return $this->map( $typeManager->searchItems( $search ) );
516
	}
517
518
519
	/**
520
	 * Creates new and updates existing items using the data array
521
	 *
522
	 * @param array $data Data array
523
	 * @return \Aimeos\MShop\Product\Item\Iface New product item object
524
	 */
525
	protected function fromArray( array $data )
526
	{
527
		$conf = [];
528
529
		foreach( (array) $this->getValue( $data, 'config', [] ) as $idx => $entry )
530
		{
531
			if( ( $key = trim( $entry['key'] ?? '' ) ) !== '' ) {
532
				$conf[$key] = trim( $entry['val'] ?? '' );
533
			}
534
		}
535
536
		$manager = \Aimeos\MShop::create( $this->getContext(), 'index' );
537
538
		if( isset( $data['product.id'] ) && $data['product.id'] != '' ) {
539
			$item = $manager->getItem( $data['product.id'], $this->getDomains() );
540
		} else {
541
			$item = $manager->createItem();
542
		}
543
544
		$item->fromArray( $data, true );
545
		$item->setConfig( $conf );
0 ignored issues
show
Bug introduced by
The method setConfig() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Item\Iface such as Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Common\Item\Lists\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Catalog\Item\Standard or Aimeos\MShop\Plugin\Item\Standard or Aimeos\MShop\Locale\Item\Site\Standard or Aimeos\MShop\Common\Item\Lists\Standard or Aimeos\MShop\Service\Item\Standard or Aimeos\MShop\Coupon\Item\Standard or Aimeos\MShop\Product\Item\Standard or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Common\Item\Lists\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Product\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

545
		$item->/** @scrutinizer ignore-call */ 
546
         setConfig( $conf );
Loading history...
546
547
		return $item;
548
	}
549
550
551
	/**
552
	 * Constructs the data array for the view from the given item
553
	 *
554
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object
555
	 * @return string[] Multi-dimensional associative list of item data
556
	 */
557
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, $copy = false )
558
	{
559
		$data = $item->toArray( true );
560
		$data['config'] = [];
561
562
		if( $copy === true )
563
		{
564
			$data['product.siteid'] = $this->getContext()->getLocale()->getSiteId();
565
			$data['product.code'] = $data['product.code'] . '_copy';
566
			$data['product.id'] = '';
567
		}
568
569
		foreach( $item->getConfig() as $key => $value ) {
570
			$data['config'][] = ['key' => $key, 'val' => $value];
571
		}
572
573
		return $data;
574
	}
575
576
577
	/**
578
	 * Returns the rendered template including the view data
579
	 *
580
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
581
	 * @return string HTML output
582
	 */
583
	protected function render( \Aimeos\MW\View\Iface $view )
584
	{
585
		/** admin/jqadm/product/template-item
586
		 * Relative path to the HTML body template for the product item.
587
		 *
588
		 * The template file contains the HTML code and processing instructions
589
		 * to generate the result shown in the body of the frontend. The
590
		 * configuration string is the path to the template file relative
591
		 * to the templates directory (usually in admin/jqadm/templates).
592
		 *
593
		 * You can overwrite the template file configuration in extensions and
594
		 * provide alternative templates. These alternative templates should be
595
		 * named like the default one but with the string "default" replaced by
596
		 * an unique name. You may use the name of your project for this. If
597
		 * you've implemented an alternative client class as well, "default"
598
		 * should be replaced by the name of the new class.
599
		 *
600
		 * @param string Relative path to the template creating the HTML code
601
		 * @since 2016.04
602
		 * @category Developer
603
		 */
604
		$tplconf = 'admin/jqadm/product/template-item';
605
		$default = 'product/item-standard';
606
607
		return $view->render( $view->config( $tplconf, $default ) );
608
	}
609
}
610