Completed
Push — master ( 7818a8...a3fccc )
by Aimeos
04:08
created

Standard   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 541
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 141
c 0
b 0
f 0
dl 0
loc 541
rs 9.44
wmc 37

15 Methods

Rating   Name   Duplication   Size   Complexity  
A addData() 0 14 1
A getTypeItems() 0 8 1
A search() 0 49 2
A delete() 0 35 4
A getSubClientNames() 0 36 1
A getDomains() 0 14 1
A getConfigAttributes() 0 8 2
A save() 0 25 3
A copy() 0 23 3
A get() 0 23 3
A getSubClient() 0 76 1
A create() 0 23 3
B fromArray() 0 31 8
A toArray() 0 22 3
A render() 0 25 1
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\Service;
12
13
sprintf( 'service' ); // for translation
14
15
16
/**
17
 * Default implementation of service 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 . 'Service' . $ds . 'Provider' . $ds . 'Decorator' );
37
		$view->itemProviders = [
38
			'delivery' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Delivery' ),
39
			'payment' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Payment' ),
40
		];
41
42
		$view->itemSubparts = $this->getSubClientNames();
43
		$view->itemTypes = $this->getTypeItems();
44
45
		return $view;
46
	}
47
48
49
	/**
50
	 * Copies a resource
51
	 *
52
	 * @return string|null HTML output
53
	 */
54
	public function copy() : ?string
55
	{
56
		$view = $this->getObject()->addData( $this->getView() );
57
58
		try
59
		{
60
			if( ( $id = $view->param( 'id' ) ) === null ) {
61
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
62
			}
63
64
			$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
65
66
			$view->item = $manager->getItem( $id, $this->getDomains() );
67
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
68
			$view->itemData = $this->toArray( $view->item, true );
69
			$view->itemBody = parent::copy();
70
		}
71
		catch( \Exception $e )
72
		{
73
			$this->report( $e, 'copy' );
74
		}
75
76
		return $this->render( $view );
77
	}
78
79
80
	/**
81
	 * Creates a new resource
82
	 *
83
	 * @return string|null HTML output
84
	 */
85
	public function create() : ?string
86
	{
87
		$view = $this->getObject()->addData( $this->getView() );
88
89
		try
90
		{
91
			$data = $view->param( 'item', [] );
92
93
			if( !isset( $view->item ) ) {
94
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'service' )->createItem();
95
			}
96
97
			$data['service.siteid'] = $view->item->getSiteId();
98
99
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
100
			$view->itemBody = parent::create();
101
		}
102
		catch( \Exception $e )
103
		{
104
			$this->report( $e, 'create' );
105
		}
106
107
		return $this->render( $view );
108
	}
109
110
111
	/**
112
	 * Deletes a resource
113
	 *
114
	 * @return string|null HTML output
115
	 */
116
	public function delete() : ?string
117
	{
118
		$view = $this->getView();
119
120
		$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
121
		$manager->begin();
122
123
		try
124
		{
125
			if( ( $ids = $view->param( 'id' ) ) === null ) {
126
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
127
			}
128
129
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
130
			$search->setConditions( $search->compare( '==', 'service.id', $ids ) );
131
			$items = $manager->searchItems( $search, $this->getDomains() );
132
133
			foreach( $items as $item )
134
			{
135
				$view->item = $item;
136
				parent::delete();
137
			}
138
139
			$manager->deleteItems( $items->toArray() );
140
			$manager->commit();
141
142
			return $this->redirect( 'service', 'search', null, 'delete' );
143
		}
144
		catch( \Exception $e )
145
		{
146
			$manager->rollback();
147
			$this->report( $e, 'delete' );
148
		}
149
150
		return $this->search();
151
	}
152
153
154
	/**
155
	 * Returns a single resource
156
	 *
157
	 * @return string|null HTML output
158
	 */
159
	public function get() : ?string
160
	{
161
		$view = $this->getObject()->addData( $this->getView() );
162
163
		try
164
		{
165
			if( ( $id = $view->param( 'id' ) ) === null ) {
166
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
167
			}
168
169
			$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
170
171
			$view->item = $manager->getItem( $id, $this->getDomains() );
172
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
173
			$view->itemData = $this->toArray( $view->item );
174
			$view->itemBody = parent::get();
175
		}
176
		catch( \Exception $e )
177
		{
178
			$this->report( $e, 'get' );
179
		}
180
181
		return $this->render( $view );
182
	}
183
184
185
	/**
186
	 * Saves the data
187
	 *
188
	 * @return string|null HTML output
189
	 */
190
	public function save() : ?string
191
	{
192
		$view = $this->getView();
193
194
		$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
195
		$manager->begin();
196
197
		try
198
		{
199
			$item = $this->fromArray( $view->param( 'item', [] ) );
200
			$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

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