Passed
Push — master ( b1d82b...4261f4 )
by Aimeos
03:11
created

Standard::import()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 8
nop 0
dl 0
loc 21
rs 9.5555
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-2024
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product;
12
13
sprintf( 'goods' ); // for translation
14
sprintf( 'product' ); // for translation
15
16
17
/**
18
 * Default implementation of product JQAdm client.
19
 *
20
 * @package Admin
21
 * @subpackage JQAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
25
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
26
{
27
	/** admin/jqadm/product/name
28
	 * Class name of the used account favorite client implementation
29
	 *
30
	 * Each default admin client can be replace by an alternative imlementation.
31
	 * To use this implementation, you have to set the last part of the class
32
	 * name as configuration value so the client factory knows which class it
33
	 * has to instantiate.
34
	 *
35
	 * For example, if the name of the default class is
36
	 *
37
	 *  \Aimeos\Admin\JQAdm\Product\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Product\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/product/name = Myfavorite
46
	 *
47
	 * The value is the last part of your own class name and it's case sensitive,
48
	 * so take care that the configuration value is exactly named like the last
49
	 * part of the class name.
50
	 *
51
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
52
	 * characters are possible! You should always start the last part of the class
53
	 * name with an upper case character and continue only with lower case characters
54
	 * or numbers. Avoid chamel case names like "MyFavorite"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2016.01
58
	 */
59
60
61
	/**
62
	 * Adds the required data used in the template
63
	 *
64
	 * @param \Aimeos\Base\View\Iface $view View object
65
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
66
	 */
67
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
68
	{
69
		$view->itemSubparts = $this->getSubClientNames();
70
		$view->itemTypes = $this->getTypeItems();
71
		return $view;
72
	}
73
74
75
	/**
76
	 * Batch update of a resource
77
	 *
78
	 * @return string|null Output to display
79
	 */
80
	public function batch() : ?string
81
	{
82
		$view = $this->view();
83
84
		if( !empty( $ids = $view->param( 'id' ) ) )
85
		{
86
			$manager = \Aimeos\MShop::create( $this->context(), 'index' );
87
			$filter = $manager->filter()->add( ['product.id' => $ids] )->slice( 0, count( $ids ) );
88
			$items = $manager->search( $filter, $this->getDomains() );
89
90
			$data = $view->param( 'item', [] );
91
92
			foreach( $items as $item ) {
93
				$temp = $data; $item->fromArray( $temp, true );
94
			}
95
96
			$view->items = $items;
97
98
			foreach( $this->getSubClients() as $client ) {
99
				$client->batch();
100
			}
101
102
			$manager->save( $items );
103
		}
104
105
		return $this->redirect( 'product', 'search', null, 'save' );
106
	}
107
108
109
	/**
110
	 * Copies a resource
111
	 *
112
	 * @return string|null HTML output
113
	 */
114
	public function copy() : ?string
115
	{
116
		$view = $this->object()->data( $this->view() );
117
118
		try
119
		{
120
			if( ( $id = $view->param( 'id' ) ) === null )
121
			{
122
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
123
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
124
			}
125
126
			$manager = \Aimeos\MShop::create( $this->context(), 'product' );
127
			$view->item = $manager->get( $id, $this->getDomains() );
128
129
			$view->itemData = $this->toArray( $view->item, true );
130
			$view->itemBody = parent::copy();
131
		}
132
		catch( \Exception $e )
133
		{
134
			$this->report( $e, 'copy' );
135
		}
136
137
		return $this->render( $view );
138
	}
139
140
141
	/**
142
	 * Creates a new resource
143
	 *
144
	 * @return string|null HTML output
145
	 */
146
	public function create() : ?string
147
	{
148
		$view = $this->object()->data( $this->view() );
149
150
		try
151
		{
152
			$data = $view->param( 'item', [] );
153
154
			if( !isset( $view->item ) ) {
155
				$view->item = \Aimeos\MShop::create( $this->context(), 'product' )->create();
156
			}
157
158
			$data['product.siteid'] = $view->item->getSiteId();
159
160
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
161
			$view->itemBody = parent::create();
162
		}
163
		catch( \Exception $e )
164
		{
165
			$this->report( $e, 'create' );
166
		}
167
168
		return $this->render( $view );
169
	}
170
171
172
	/**
173
	 * Deletes a resource
174
	 *
175
	 * @return string|null HTML output
176
	 */
177
	public function delete() : ?string
178
	{
179
		$tags = ['product'];
180
		$view = $this->view();
181
		$context = $this->context();
182
183
		$manager = \Aimeos\MShop::create( $context, 'product' );
184
		$manager->begin();
185
186
		try
187
		{
188
			if( ( $ids = $view->param( 'id' ) ) === null )
189
			{
190
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
191
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
192
			}
193
194
			$search = $manager->filter()->add( 'product.id', '==', $ids )->slice( 0, count( (array) $ids ) );
195
			$items = $manager->search( $search, $this->getDomains() );
196
197
			foreach( $items as $id => $item )
198
			{
199
				$tags[] = 'product-' . $id;
200
				$view->item = $item;
201
				parent::delete();
202
			}
203
204
			$manager->delete( $items->toArray() );
205
			$manager->commit();
206
207
			\Aimeos\MShop::create( $context, 'index' )->delete( $items->toArray() );
208
			$context->cache()->deleteByTags( $tags );
209
210
			return $this->redirect( 'product', 'search', null, 'delete' );
211
		}
212
		catch( \Exception $e )
213
		{
214
			$manager->rollback();
215
			$this->report( $e, 'delete' );
216
		}
217
218
		return $this->search();
219
	}
220
221
222
	/**
223
	 * Returns a single resource
224
	 *
225
	 * @return string|null HTML output
226
	 */
227
	public function get() : ?string
228
	{
229
		$view = $this->object()->data( $this->view() );
230
231
		try
232
		{
233
			if( ( $id = $view->param( 'id' ) ) === null )
234
			{
235
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
236
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
237
			}
238
239
			$manager = \Aimeos\MShop::create( $this->context(), 'product' );
240
241
			$view->item = $manager->get( $id, $this->getDomains() );
242
			$view->itemData = $this->toArray( $view->item );
243
			$view->itemBody = parent::get();
244
		}
245
		catch( \Exception $e )
246
		{
247
			$this->report( $e, 'get' );
248
		}
249
250
		return $this->render( $view );
251
	}
252
253
254
	/**
255
	 * Imports a file
256
	 *
257
	 * @return string|null Output to display or redirect
258
	 */
259
	public function import() : ?string
260
	{
261
		$context = $this->context();
262
		$fs = $context->fs( 'fs-import' );
263
		$site = $context->locale()->getSiteCode();
0 ignored issues
show
Bug introduced by
The method getSiteCode() does not exist on Aimeos\MShop\Locale\Item\Iface. Did you maybe mean getSiteId()? ( Ignorable by Annotation )

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

263
		$site = $context->locale()->/** @scrutinizer ignore-call */ getSiteCode();

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...
264
		$dir = $context->config()->get( 'controller/jobs/product/import/csv/location', 'product' );
265
266
		if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $dir . '/' . $site ) === false ) {
267
			$fs->mkdir( $dir . '/' . $site );
268
		}
269
270
		$uploads = (array) $this->view()->request()->getUploadedFiles();
271
		$files = $this->val( $uploads, 'import' );
272
273
		foreach( is_array( $files ) ? $files : [$files] as $idx => $file )
274
		{
275
			$filename = date( 'YmdHis' ) . '_' . str_pad( $idx, 3, '0', STR_PAD_LEFT ) . '_' . substr( md5( microtime( true ) ), 0, 4 ) . '.csv';
276
			$fs->writes( $dir . '/' . $site . '/' . $filename, $file->getStream()->detach() );
277
		}
278
279
		return $this->redirect( 'product', 'search', null, 'upload' );
280
	}
281
282
283
	/**
284
	 * Saves the data
285
	 *
286
	 * @return string|null HTML output
287
	 */
288
	public function save() : ?string
289
	{
290
		$view = $this->view();
291
		$context = $this->context();
292
293
		$manager = \Aimeos\MShop::create( $context, 'index' );
294
		$manager->begin();
295
296
		try
297
		{
298
			$item = $this->fromArray( $view->param( 'item', [] ) );
299
			$view->item = $item->getId() ? $item : $manager->save( $item );
300
			$view->itemBody = parent::save();
301
302
			$item = $manager->save( clone $view->item );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
303
			$manager->commit();
304
305
			return $this->redirect( 'product', $view->param( 'next' ), $view->item->getId(), 'save' );
0 ignored issues
show
Bug introduced by
It seems like $view->item->getId() can also be of type Aimeos\Map; however, parameter $id of Aimeos\Admin\JQAdm\Base::redirect() does only seem to accept null|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

305
			return $this->redirect( 'product', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
306
		}
307
		catch( \Exception $e )
308
		{
309
			$manager->rollback();
310
			$this->report( $e, 'save' );
311
		}
312
313
		return $this->create();
314
	}
315
316
317
	/**
318
	 * Returns a list of resource according to the conditions
319
	 *
320
	 * @return string|null HTML output
321
	 */
322
	public function search() : ?string
323
	{
324
		$view = $this->view();
325
326
		try
327
		{
328
			$total = 0;
329
			$domains = map( $this->getDomains() )->remove( 'product' );
0 ignored issues
show
Bug introduced by
'product' of type string is incompatible with the type iterable expected by parameter $keys of Aimeos\Map::remove(). ( Ignorable by Annotation )

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

329
			$domains = map( $this->getDomains() )->remove( /** @scrutinizer ignore-type */ 'product' );
Loading history...
330
			$params = $this->storeFilter( $view->param(), 'product' );
331
			$manager = \Aimeos\MShop::create( $this->context(), 'product' );
332
333
			$search = $manager->filter()->order( 'product.id' );
334
			$search = $this->initCriteria( $search, $params );
335
336
			$view->items = $manager->search( $search, $domains->toArray(), $total );
337
			$view->filterAttributes = $manager->getSearchAttributes( true );
338
			$view->filterOperators = $search->getOperators();
339
			$view->itemTypes = $this->getTypeItems();
340
			$view->itemBody = parent::search();
341
			$view->total = $total;
342
		}
343
		catch( \Exception $e )
344
		{
345
			$this->report( $e, 'search' );
346
		}
347
348
		/** admin/jqadm/product/template-list
349
		 * Relative path to the HTML body template for the product list.
350
		 *
351
		 * The template file contains the HTML code and processing instructions
352
		 * to generate the result shown in the body of the frontend. The
353
		 * configuration string is the path to the template file relative
354
		 * to the templates directory (usually in templates/admin/jqadm).
355
		 *
356
		 * You can overwrite the template file configuration in extensions and
357
		 * provide alternative templates. These alternative templates should be
358
		 * named like the default one but with the string "default" replaced by
359
		 * an unique name. You may use the name of your project for this. If
360
		 * you've implemented an alternative client class as well, "default"
361
		 * should be replaced by the name of the new class.
362
		 *
363
		 * @param string Relative path to the template creating the HTML code
364
		 * @since 2016.04
365
		 */
366
		$tplconf = 'admin/jqadm/product/template-list';
367
		$default = 'product/list';
368
369
		return $view->render( $view->config( $tplconf, $default ) );
370
	}
371
372
373
	/**
374
	 * Returns the sub-client given by its name.
375
	 *
376
	 * @param string $type Name of the client type
377
	 * @param string|null $name Name of the sub-client (Default if null)
378
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
379
	 */
380
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
381
	{
382
		/** admin/jqadm/product/decorators/excludes
383
		 * Excludes decorators added by the "common" option from the product JQAdm client
384
		 *
385
		 * Decorators extend the functionality of a class by adding new aspects
386
		 * (e.g. log what is currently done), executing the methods of the underlying
387
		 * class only in certain conditions (e.g. only for logged in users) or
388
		 * modify what is returned to the caller.
389
		 *
390
		 * This option allows you to remove a decorator added via
391
		 * "client/jqadm/common/decorators/default" before they are wrapped
392
		 * around the JQAdm client.
393
		 *
394
		 *  admin/jqadm/product/decorators/excludes = array( 'decorator1' )
395
		 *
396
		 * This would remove the decorator named "decorator1" from the list of
397
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
398
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
399
		 *
400
		 * @param array List of decorator names
401
		 * @since 2016.01
402
		 * @see admin/jqadm/common/decorators/default
403
		 * @see admin/jqadm/product/decorators/global
404
		 * @see admin/jqadm/product/decorators/local
405
		 */
406
407
		/** admin/jqadm/product/decorators/global
408
		 * Adds a list of globally available decorators only to the product JQAdm client
409
		 *
410
		 * Decorators extend the functionality of a class by adding new aspects
411
		 * (e.g. log what is currently done), executing the methods of the underlying
412
		 * class only in certain conditions (e.g. only for logged in users) or
413
		 * modify what is returned to the caller.
414
		 *
415
		 * This option allows you to wrap global decorators
416
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
417
		 *
418
		 *  admin/jqadm/product/decorators/global = array( 'decorator1' )
419
		 *
420
		 * This would add the decorator named "decorator1" defined by
421
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
422
		 *
423
		 * @param array List of decorator names
424
		 * @since 2016.01
425
		 * @see admin/jqadm/common/decorators/default
426
		 * @see admin/jqadm/product/decorators/excludes
427
		 * @see admin/jqadm/product/decorators/local
428
		 */
429
430
		/** admin/jqadm/product/decorators/local
431
		 * Adds a list of local decorators only to the product JQAdm client
432
		 *
433
		 * Decorators extend the functionality of a class by adding new aspects
434
		 * (e.g. log what is currently done), executing the methods of the underlying
435
		 * class only in certain conditions (e.g. only for logged in users) or
436
		 * modify what is returned to the caller.
437
		 *
438
		 * This option allows you to wrap local decorators
439
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
440
		 *
441
		 *  admin/jqadm/product/decorators/local = array( 'decorator2' )
442
		 *
443
		 * This would add the decorator named "decorator2" defined by
444
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
445
		 *
446
		 * @param array List of decorator names
447
		 * @since 2016.01
448
		 * @see admin/jqadm/common/decorators/default
449
		 * @see admin/jqadm/product/decorators/excludes
450
		 * @see admin/jqadm/product/decorators/global
451
		 */
452
		return $this->createSubClient( 'product/' . $type, $name );
453
	}
454
455
456
	/**
457
	 * Returns the domain names whose items should be fetched too
458
	 *
459
	 * @return string[] List of domain names
460
	 */
461
	protected function getDomains() : array
462
	{
463
		/** admin/jqadm/product/domains
464
		 * List of domain items that should be fetched along with the product
465
		 *
466
		 * If you need to display additional content, you can configure your own
467
		 * list of domains (attribute, media, price, product, text, etc. are
468
		 * domains) whose items are fetched from the storage.
469
		 *
470
		 * @param array List of domain names
471
		 * @since 2016.01
472
		 */
473
		return $this->context()->config()->get( 'admin/jqadm/product/domains', [] );
474
	}
475
476
477
	/**
478
	 * Returns the list of sub-client names configured for the client.
479
	 *
480
	 * @return array List of JQAdm client names
481
	 */
482
	protected function getSubClientNames() : array
483
	{
484
		/** admin/jqadm/product/subparts
485
		 * List of JQAdm sub-clients rendered within the product section
486
		 *
487
		 * The output of the frontend is composed of the code generated by the JQAdm
488
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
489
		 * that are responsible for rendering certain sub-parts of the output. The
490
		 * sub-clients can contain JQAdm clients themselves and therefore a
491
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
492
		 * the output that is placed inside the container of its parent.
493
		 *
494
		 * At first, always the JQAdm code generated by the parent is printed, then
495
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
496
		 * determines the order of the output of these sub-clients inside the parent
497
		 * container. If the configured list of clients is
498
		 *
499
		 *  array( "subclient1", "subclient2" )
500
		 *
501
		 * you can easily change the order of the output by reordering the subparts:
502
		 *
503
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
504
		 *
505
		 * You can also remove one or more parts if they shouldn't be rendered:
506
		 *
507
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
508
		 *
509
		 * As the clients only generates structural JQAdm, the layout defined via CSS
510
		 * should support adding, removing or reordering content by a fluid like
511
		 * design.
512
		 *
513
		 * @param array List of sub-client names
514
		 * @since 2016.01
515
		 */
516
		return $this->context()->config()->get( 'admin/jqadm/product/subparts', [] );
517
	}
518
519
520
	/**
521
	 * Returns the available product type items
522
	 *
523
	 * @return \Aimeos\Map List of item implementing \Aimeos\MShop\Common\Type\Iface
524
	 */
525
	protected function getTypeItems() : \Aimeos\Map
526
	{
527
		$typeManager = \Aimeos\MShop::create( $this->context(), 'product/type' );
528
		$search = $typeManager->filter( true )->order( 'product.type.code' )->slice( 0, 10000 );
529
530
		return $typeManager->search( $search );
531
	}
532
533
534
	/**
535
	 * Creates new and updates existing items using the data array
536
	 *
537
	 * @param array $data Data array
538
	 * @return \Aimeos\MShop\Product\Item\Iface New product item object
539
	 */
540
	protected function fromArray( array $data ) : \Aimeos\MShop\Product\Item\Iface
541
	{
542
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
543
544
		if( isset( $data['product.id'] ) && $data['product.id'] != '' ) {
545
			$item = $manager->get( $data['product.id'], $this->getDomains() );
546
		} else {
547
			$item = $manager->create();
548
		}
549
550
		$item->fromArray( $data, true )->setConfig( [] );
551
552
		foreach( (array) $this->val( $data, 'config', [] ) as $cfg )
553
		{
554
			if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
555
				$item->setConfigValue( $key, json_decode( $val, true ) ?? $val );
556
			}
557
		}
558
559
		return $item;
560
	}
561
562
563
	/**
564
	 * Constructs the data array for the view from the given item
565
	 *
566
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object
567
	 * @return string[] Multi-dimensional associative list of item data
568
	 */
569
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
570
	{
571
		$data = $item->toArray( true );
572
		$data['config'] = $this->flatten( $item->getConfig() );
573
574
		if( $copy === true )
575
		{
576
			$hash = substr( md5( microtime( true ) ), -5 );
577
			$data['product.code'] = $data['product.code'] . '_' . $hash;
578
			$data['product.label'] = $data['product.label'] . '_' . $hash;
579
			$data['product.siteid'] = $this->context()->locale()->getSiteId();
580
			$data['product.ctime'] = '';
581
			$data['product.url'] = '';
582
			$data['product.id'] = '';
583
		}
584
585
		return $data;
586
	}
587
588
589
	/**
590
	 * Returns the rendered template including the view data
591
	 *
592
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
593
	 * @return string HTML output
594
	 */
595
	protected function render( \Aimeos\Base\View\Iface $view ) : string
596
	{
597
		/** admin/jqadm/product/template-item
598
		 * Relative path to the HTML body template for the product item.
599
		 *
600
		 * The template file contains the HTML code and processing instructions
601
		 * to generate the result shown in the body of the frontend. The
602
		 * configuration string is the path to the template file relative
603
		 * to the templates directory (usually in templates/admin/jqadm).
604
		 *
605
		 * You can overwrite the template file configuration in extensions and
606
		 * provide alternative templates. These alternative templates should be
607
		 * named like the default one but with the string "default" replaced by
608
		 * an unique name. You may use the name of your project for this. If
609
		 * you've implemented an alternative client class as well, "default"
610
		 * should be replaced by the name of the new class.
611
		 *
612
		 * @param string Relative path to the template creating the HTML code
613
		 * @since 2016.04
614
		 */
615
		$tplconf = 'admin/jqadm/product/template-item';
616
		$default = 'product/item';
617
618
		return $view->render( $view->config( $tplconf, $default ) );
619
	}
620
}
621