Passed
Push — master ( 2804bd...934c66 )
by Aimeos
03:16
created

Standard::getConfigAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
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-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Service;
12
13
sprintf( 'setup' ); // for translation
14
sprintf( 'service' ); // for translation
15
16
17
/**
18
 * Default implementation of service 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/service/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\Service\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Service\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/service/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
	 * @category Developer
59
	 */
60
61
62
	/**
63
	 * Adds the required data used in the template
64
	 *
65
	 * @param \Aimeos\Base\View\Iface $view View object
66
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
67
	 */
68
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
69
	{
70
		$ds = DIRECTORY_SEPARATOR;
71
72
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Decorator' );
73
		$view->itemProviders = [
74
			'delivery' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Delivery' ),
75
			'payment' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Payment' ),
76
		];
77
78
		$view->itemSubparts = $this->getSubClientNames();
79
		$view->itemTypes = $this->getTypeItems();
80
81
		return $view;
82
	}
83
84
85
	/**
86
	 * Batch update of a resource
87
	 *
88
	 * @return string|null Output to display
89
	 */
90
	public function batch() : ?string
91
	{
92
		return $this->batchBase( 'service' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('service') 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...
93
	}
94
95
96
	/**
97
	 * Copies a resource
98
	 *
99
	 * @return string|null HTML output
100
	 */
101
	public function copy() : ?string
102
	{
103
		$view = $this->object()->data( $this->view() );
104
105
		try
106
		{
107
			if( ( $id = $view->param( 'id' ) ) === null )
108
			{
109
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
110
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
111
			}
112
113
			$manager = \Aimeos\MShop::create( $this->context(), 'service' );
114
115
			$view->item = $manager->get( $id, $this->getDomains() );
116
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
117
			$view->itemData = $this->toArray( $view->item, true );
118
			$view->itemBody = parent::copy();
119
		}
120
		catch( \Exception $e )
121
		{
122
			$this->report( $e, 'copy' );
123
		}
124
125
		return $this->render( $view );
126
	}
127
128
129
	/**
130
	 * Creates a new resource
131
	 *
132
	 * @return string|null HTML output
133
	 */
134
	public function create() : ?string
135
	{
136
		$view = $this->object()->data( $this->view() );
137
138
		try
139
		{
140
			$data = $view->param( 'item', [] );
141
142
			if( !isset( $view->item ) ) {
143
				$view->item = \Aimeos\MShop::create( $this->context(), 'service' )->create();
144
			}
145
146
			$data['service.siteid'] = $view->item->getSiteId();
147
148
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
149
			$view->itemBody = parent::create();
150
		}
151
		catch( \Exception $e )
152
		{
153
			$this->report( $e, 'create' );
154
		}
155
156
		return $this->render( $view );
157
	}
158
159
160
	/**
161
	 * Deletes a resource
162
	 *
163
	 * @return string|null HTML output
164
	 */
165
	public function delete() : ?string
166
	{
167
		$view = $this->view();
168
169
		$manager = \Aimeos\MShop::create( $this->context(), 'service' );
170
		$manager->begin();
171
172
		try
173
		{
174
			if( ( $ids = $view->param( 'id' ) ) === null )
175
			{
176
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
177
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
178
			}
179
180
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
181
			$search->setConditions( $search->compare( '==', 'service.id', $ids ) );
182
			$items = $manager->search( $search, $this->getDomains() );
183
184
			foreach( $items as $item )
185
			{
186
				$view->item = $item;
187
				parent::delete();
188
			}
189
190
			$manager->delete( $items->toArray() );
191
			$manager->commit();
192
193
			return $this->redirect( 'service', 'search', null, 'delete' );
194
		}
195
		catch( \Exception $e )
196
		{
197
			$manager->rollback();
198
			$this->report( $e, 'delete' );
199
		}
200
201
		return $this->search();
202
	}
203
204
205
	/**
206
	 * Returns a single resource
207
	 *
208
	 * @return string|null HTML output
209
	 */
210
	public function get() : ?string
211
	{
212
		$view = $this->object()->data( $this->view() );
213
214
		try
215
		{
216
			if( ( $id = $view->param( 'id' ) ) === null )
217
			{
218
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
219
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
220
			}
221
222
			$manager = \Aimeos\MShop::create( $this->context(), 'service' );
223
224
			$view->item = $manager->get( $id, $this->getDomains() );
225
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
226
			$view->itemData = $this->toArray( $view->item );
227
			$view->itemBody = parent::get();
228
		}
229
		catch( \Exception $e )
230
		{
231
			$this->report( $e, 'get' );
232
		}
233
234
		return $this->render( $view );
235
	}
236
237
238
	/**
239
	 * Saves the data
240
	 *
241
	 * @return string|null HTML output
242
	 */
243
	public function save() : ?string
244
	{
245
		$view = $this->view();
246
247
		$manager = \Aimeos\MShop::create( $this->context(), 'service' );
248
		$manager->begin();
249
250
		try
251
		{
252
			$item = $this->fromArray( $view->param( 'item', [] ) );
253
			$view->item = $item->getId() ? $item : $manager->save( $item );
254
			$view->itemBody = parent::save();
255
256
			$manager->save( clone $view->item );
257
			$manager->commit();
258
259
			return $this->redirect( 'service', $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

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