Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 13
rs 10
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-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Plugin;
12
13
sprintf( 'plugin' ); // for translation
14
15
16
/**
17
 * Default implementation of plugin 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
	 * Adds the required data used in the template
28
	 *
29
	 * @param \Aimeos\MW\View\Iface $view View object
30
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
31
	 */
32
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
33
	{
34
		$ds = DIRECTORY_SEPARATOR;
35
36
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Decorator' );
37
		$view->itemProviders = [
38
			'order' => $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Order' )
39
		];
40
41
		$view->itemSubparts = $this->getSubClientNames();
42
		$view->itemTypes = $this->getTypeItems();
43
44
		return $view;
45
	}
46
47
48
	/**
49
	 * Copies a resource
50
	 *
51
	 * @return string|null HTML output
52
	 */
53
	public function copy() : ?string
54
	{
55
		$view = $this->getObject()->addData( $this->getView() );
56
57
		try
58
		{
59
			if( ( $id = $view->param( 'id' ) ) === null ) {
60
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
61
			}
62
63
			$manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' );
64
65
			$view->item = $manager->getItem( $id );
66
			$view->itemData = $this->toArray( $view->item, true );
67
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
68
			$view->itemBody = '';
69
70
			foreach( $this->getSubClients() as $idx => $client )
71
			{
72
				$view->tabindex = ++$idx + 1;
73
				$view->itemBody .= $client->copy();
74
			}
75
		}
76
		catch( \Exception $e )
77
		{
78
			$this->report( $e, 'copy' );
79
		}
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Creates a new resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function create() : ?string
91
	{
92
		$view = $this->getObject()->addData( $this->getView() );
93
94
		try
95
		{
96
			$data = $view->param( 'item', [] );
97
98
			if( !isset( $view->item ) ) {
99
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'plugin' )->createItem();
100
			}
101
102
			$data['plugin.siteid'] = $view->item->getSiteId();
103
104
			$view->itemData = $data;
105
			$view->itemBody = '';
106
107
			foreach( $this->getSubClients() as $idx => $client )
108
			{
109
				$view->tabindex = ++$idx + 1;
110
				$view->itemBody .= $client->create();
111
			}
112
		}
113
		catch( \Exception $e )
114
		{
115
			$this->report( $e, 'create' );
116
		}
117
118
		return $this->render( $view );
119
	}
120
121
122
	/**
123
	 * Deletes a resource
124
	 *
125
	 * @return string|null HTML output
126
	 */
127
	public function delete() : ?string
128
	{
129
		$view = $this->getView();
130
131
		$manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' );
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( '==', 'plugin.id', $ids ) );
142
			$items = $manager->searchItems( $search );
143
144
			foreach( $items as $item )
145
			{
146
				$view->item = $item;
147
148
				foreach( $this->getSubClients() as $client ) {
149
					$client->delete();
150
				}
151
			}
152
153
			$manager->deleteItems( $items->toArray() );
154
			$manager->commit();
155
156
			$this->nextAction( $view, 'search', 'plugin', null, 'delete' );
157
			return null;
158
		}
159
		catch( \Exception $e )
160
		{
161
			$manager->rollback();
162
			$this->report( $e, 'delete' );
163
		}
164
165
		return $this->search();
166
	}
167
168
169
	/**
170
	 * Returns a single resource
171
	 *
172
	 * @return string|null HTML output
173
	 */
174
	public function get() : ?string
175
	{
176
		$view = $this->getObject()->addData( $this->getView() );
177
178
		try
179
		{
180
			if( ( $id = $view->param( 'id' ) ) === null ) {
181
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
182
			}
183
184
			$manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' );
185
186
			$view->item = $manager->getItem( $id );
187
			$view->itemData = $this->toArray( $view->item );
188
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
189
			$view->itemBody = '';
190
191
			foreach( $this->getSubClients() as $idx => $client )
192
			{
193
				$view->tabindex = ++$idx + 1;
194
				$view->itemBody .= $client->get();
195
			}
196
		}
197
		catch( \Exception $e )
198
		{
199
			$this->report( $e, 'get' );
200
		}
201
202
		return $this->render( $view );
203
	}
204
205
206
	/**
207
	 * Saves the data
208
	 *
209
	 * @return string|null HTML output
210
	 */
211
	public function save() : ?string
212
	{
213
		$view = $this->getView();
214
215
		$manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' );
216
		$manager->begin();
217
218
		try
219
		{
220
			$item = $this->fromArray( $view->param( 'item', [] ) );
221
			$view->item = $item->getId() ? $item : $manager->saveItem( $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

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