Standard   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 572
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 138
c 1
b 0
f 0
dl 0
loc 572
rs 9.52
wmc 36

16 Methods

Rating   Name   Duplication   Size   Complexity  
A batch() 0 3 1
A data() 0 12 1
A copy() 0 25 3
A create() 0 23 3
A delete() 0 36 4
A search() 0 47 2
A getSubClientNames() 0 35 1
A getDomains() 0 13 1
A getConfigAttributes() 0 8 2
A save() 0 25 3
A get() 0 25 3
A getSubClient() 0 73 1
A fromArray() 0 23 6
A toArray() 0 23 3
A render() 0 24 1
A getTypeItems() 0 6 1
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\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 service 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\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
	 */
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->itemDecorators = $this->getClassNames( 'MShop/Service/Provider/Decorator' );
70
		$view->itemProviders = [
71
			'delivery' => $this->getClassNames( 'MShop/Service/Provider/Delivery' ),
72
			'payment' => $this->getClassNames( 'MShop/Service/Provider/Payment' ),
73
		];
74
75
		$view->itemSubparts = $this->getSubClientNames();
76
		$view->itemTypes = $this->getTypeItems();
77
78
		return $view;
79
	}
80
81
82
	/**
83
	 * Batch update of a resource
84
	 *
85
	 * @return string|null Output to display
86
	 */
87
	public function batch() : ?string
88
	{
89
		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...
90
	}
91
92
93
	/**
94
	 * Copies a resource
95
	 *
96
	 * @return string|null HTML output
97
	 */
98
	public function copy() : ?string
99
	{
100
		$view = $this->object()->data( $this->view() );
101
102
		try
103
		{
104
			if( ( $id = $view->param( 'id' ) ) === null )
105
			{
106
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
107
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
108
			}
109
110
			$manager = \Aimeos\MShop::create( $this->context(), 'service' );
111
112
			$view->item = $manager->get( $id, $this->getDomains() );
113
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
114
			$view->itemData = $this->toArray( $view->item, true );
115
			$view->itemBody = parent::copy();
116
		}
117
		catch( \Exception $e )
118
		{
119
			$this->report( $e, 'copy' );
120
		}
121
122
		return $this->render( $view );
123
	}
124
125
126
	/**
127
	 * Creates a new resource
128
	 *
129
	 * @return string|null HTML output
130
	 */
131
	public function create() : ?string
132
	{
133
		$view = $this->object()->data( $this->view() );
134
135
		try
136
		{
137
			$data = $view->param( 'item', [] );
138
139
			if( !isset( $view->item ) ) {
140
				$view->item = \Aimeos\MShop::create( $this->context(), 'service' )->create();
141
			}
142
143
			$data['service.siteid'] = $view->item->getSiteId();
144
145
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
146
			$view->itemBody = parent::create();
147
		}
148
		catch( \Exception $e )
149
		{
150
			$this->report( $e, 'create' );
151
		}
152
153
		return $this->render( $view );
154
	}
155
156
157
	/**
158
	 * Deletes a resource
159
	 *
160
	 * @return string|null HTML output
161
	 */
162
	public function delete() : ?string
163
	{
164
		$view = $this->view();
165
166
		$manager = \Aimeos\MShop::create( $this->context(), 'service' );
167
		$manager->begin();
168
169
		try
170
		{
171
			if( ( $ids = $view->param( 'id' ) ) === null )
172
			{
173
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
174
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
175
			}
176
177
			$search = $manager->filter()->add( 'service.id', '==', $ids )->slice( 0, count( (array) $ids ) );
178
			$items = $manager->search( $search, $this->getDomains() );
179
180
			foreach( $items as $item )
181
			{
182
				$view->item = $item;
183
				parent::delete();
184
			}
185
186
			$manager->delete( $items );
187
			$manager->commit();
188
189
			return $this->redirect( 'service', 'search', null, 'delete' );
190
		}
191
		catch( \Exception $e )
192
		{
193
			$manager->rollback();
194
			$this->report( $e, 'delete' );
195
		}
196
197
		return $this->search();
198
	}
199
200
201
	/**
202
	 * Returns a single resource
203
	 *
204
	 * @return string|null HTML output
205
	 */
206
	public function get() : ?string
207
	{
208
		$view = $this->object()->data( $this->view() );
209
210
		try
211
		{
212
			if( ( $id = $view->param( 'id' ) ) === null )
213
			{
214
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
215
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
216
			}
217
218
			$manager = \Aimeos\MShop::create( $this->context(), 'service' );
219
220
			$view->item = $manager->get( $id, $this->getDomains() );
221
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
222
			$view->itemData = $this->toArray( $view->item );
223
			$view->itemBody = parent::get();
224
		}
225
		catch( \Exception $e )
226
		{
227
			$this->report( $e, 'get' );
228
		}
229
230
		return $this->render( $view );
231
	}
232
233
234
	/**
235
	 * Saves the data
236
	 *
237
	 * @return string|null HTML output
238
	 */
239
	public function save() : ?string
240
	{
241
		$view = $this->view();
242
243
		$manager = \Aimeos\MShop::create( $this->context(), 'service' );
244
		$manager->begin();
245
246
		try
247
		{
248
			$item = $this->fromArray( $view->param( 'item', [] ) );
249
			$view->item = $item->getId() ? $item : $manager->save( $item );
250
			$view->itemBody = parent::save();
251
252
			$manager->save( clone $view->item );
253
			$manager->commit();
254
255
			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

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