Passed
Push — master ( b3723d...cfff87 )
by Aimeos
03:56
created

Standard::fromArray()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 8
nop 1
dl 0
loc 28
rs 9.2222
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-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Plugin;
12
13
sprintf( 'setup' ); // for translation
14
sprintf( 'plugin' ); // for translation
15
16
17
/**
18
 * Default implementation of plugin 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/plugin/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\Plugin\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Plugin\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/plugin/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 2017.07
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
		$ds = DIRECTORY_SEPARATOR;
70
71
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Decorator' );
72
		$view->itemProviders = [
73
			'order' => $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Order' )
74
		];
75
76
		$view->itemSubparts = $this->getSubClientNames();
77
		$view->itemTypes = $this->getTypeItems();
78
79
		return $view;
80
	}
81
82
83
	/**
84
	 * Batch update of a resource
85
	 *
86
	 * @return string|null Output to display
87
	 */
88
	public function batch() : ?string
89
	{
90
		return $this->batchBase( 'plugin' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('plugin') targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
	}
92
93
94
	/**
95
	 * Copies a resource
96
	 *
97
	 * @return string|null HTML output
98
	 */
99
	public function copy() : ?string
100
	{
101
		$view = $this->object()->data( $this->view() );
102
103
		try
104
		{
105
			if( ( $id = $view->param( 'id' ) ) === null )
106
			{
107
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
108
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
109
			}
110
111
			$manager = \Aimeos\MShop::create( $this->context(), 'plugin' );
112
113
			$view->item = $manager->get( $id );
114
			$view->itemData = $this->toArray( $view->item, true );
115
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
116
			$view->itemBody = parent::copy();
117
		}
118
		catch( \Exception $e )
119
		{
120
			$this->report( $e, 'copy' );
121
		}
122
123
		return $this->render( $view );
124
	}
125
126
127
	/**
128
	 * Creates a new resource
129
	 *
130
	 * @return string|null HTML output
131
	 */
132
	public function create() : ?string
133
	{
134
		$view = $this->object()->data( $this->view() );
135
136
		try
137
		{
138
			$data = $view->param( 'item', [] );
139
140
			if( !isset( $view->item ) ) {
141
				$view->item = \Aimeos\MShop::create( $this->context(), 'plugin' )->create();
142
			}
143
144
			$data['plugin.siteid'] = $view->item->getSiteId();
145
146
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
147
			$view->itemBody = parent::create();
148
		}
149
		catch( \Exception $e )
150
		{
151
			$this->report( $e, 'create' );
152
		}
153
154
		return $this->render( $view );
155
	}
156
157
158
	/**
159
	 * Deletes a resource
160
	 *
161
	 * @return string|null HTML output
162
	 */
163
	public function delete() : ?string
164
	{
165
		$view = $this->view();
166
167
		$manager = \Aimeos\MShop::create( $this->context(), 'plugin' );
168
		$manager->begin();
169
170
		try
171
		{
172
			if( ( $ids = $view->param( 'id' ) ) === null )
173
			{
174
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
175
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
176
			}
177
178
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
179
			$search->setConditions( $search->compare( '==', 'plugin.id', $ids ) );
180
			$items = $manager->search( $search );
181
182
			foreach( $items as $item )
183
			{
184
				$view->item = $item;
185
				parent::delete();
186
			}
187
188
			$manager->delete( $items->toArray() );
189
			$manager->commit();
190
191
			return $this->redirect( 'plugin', 'search', null, 'delete' );
192
		}
193
		catch( \Exception $e )
194
		{
195
			$manager->rollback();
196
			$this->report( $e, 'delete' );
197
		}
198
199
		return $this->search();
200
	}
201
202
203
	/**
204
	 * Returns a single resource
205
	 *
206
	 * @return string|null HTML output
207
	 */
208
	public function get() : ?string
209
	{
210
		$view = $this->object()->data( $this->view() );
211
212
		try
213
		{
214
			if( ( $id = $view->param( 'id' ) ) === null )
215
			{
216
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
217
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
218
			}
219
220
			$manager = \Aimeos\MShop::create( $this->context(), 'plugin' );
221
222
			$view->item = $manager->get( $id );
223
			$view->itemData = $this->toArray( $view->item );
224
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
225
			$view->itemBody = parent::get();
226
		}
227
		catch( \Exception $e )
228
		{
229
			$this->report( $e, 'get' );
230
		}
231
232
		return $this->render( $view );
233
	}
234
235
236
	/**
237
	 * Saves the data
238
	 *
239
	 * @return string|null HTML output
240
	 */
241
	public function save() : ?string
242
	{
243
		$view = $this->view();
244
245
		$manager = \Aimeos\MShop::create( $this->context(), 'plugin' );
246
		$manager->begin();
247
248
		try
249
		{
250
			$item = $this->fromArray( $view->param( 'item', [] ) );
251
			$view->item = $item->getId() ? $item : $manager->save( $item );
252
			$view->itemBody = parent::save();
253
254
			$manager->save( clone $view->item );
255
			$manager->commit();
256
257
			return $this->redirect( 'plugin', $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

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