Standard   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 551
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 136
dl 0
loc 551
rs 9.6
c 0
b 0
f 0
wmc 35

15 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 23 3
A batch() 0 3 1
A data() 0 13 1
A copy() 0 25 3
A get() 0 25 3
A getSubClientNames() 0 35 1
A toArray() 0 22 3
A delete() 0 36 4
A save() 0 25 3
A fromArray() 0 23 6
A getTypeItems() 0 6 1
A search() 0 47 2
A getSubClient() 0 73 1
A render() 0 24 1
A getConfigAttributes() 0 8 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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 plugin panel 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()->add( 'plugin.id', '==', $ids )->slice( 0, count( (array) $ids ) );
179
			$items = $manager->search( $search );
180
181
			foreach( $items as $item )
182
			{
183
				$view->item = $item;
184
				parent::delete();
185
			}
186
187
			$manager->delete( $items );
188
			$manager->commit();
189
190
			return $this->redirect( 'plugin', 'search', null, 'delete' );
191
		}
192
		catch( \Exception $e )
193
		{
194
			$manager->rollback();
195
			$this->report( $e, 'delete' );
196
		}
197
198
		return $this->search();
199
	}
200
201
202
	/**
203
	 * Returns a single resource
204
	 *
205
	 * @return string|null HTML output
206
	 */
207
	public function get() : ?string
208
	{
209
		$view = $this->object()->data( $this->view() );
210
211
		try
212
		{
213
			if( ( $id = $view->param( 'id' ) ) === null )
214
			{
215
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
216
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
217
			}
218
219
			$manager = \Aimeos\MShop::create( $this->context(), 'plugin' );
220
221
			$view->item = $manager->get( $id );
222
			$view->itemData = $this->toArray( $view->item );
223
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
224
			$view->itemBody = parent::get();
225
		}
226
		catch( \Exception $e )
227
		{
228
			$this->report( $e, 'get' );
229
		}
230
231
		return $this->render( $view );
232
	}
233
234
235
	/**
236
	 * Saves the data
237
	 *
238
	 * @return string|null HTML output
239
	 */
240
	public function save() : ?string
241
	{
242
		$view = $this->view();
243
244
		$manager = \Aimeos\MShop::create( $this->context(), 'plugin' );
245
		$manager->begin();
246
247
		try
248
		{
249
			$item = $this->fromArray( $view->param( 'item', [] ) );
250
			$view->item = $item->getId() ? $item : $manager->save( $item );
251
			$view->itemBody = parent::save();
252
253
			$manager->save( clone $view->item );
254
			$manager->commit();
255
256
			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

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