Passed
Push — master ( 85cd01...c58492 )
by Aimeos
03:18
created

Standard::search()   A

Complexity

Conditions 2
Paths 11

Size

Total Lines 49
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
c 0
b 0
f 0
nc 11
nop 0
dl 0
loc 49
rs 9.6333
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Service;
12
13
sprintf( 'configuration' ); // 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
	/**
28
	 * Adds the required data used in the template
29
	 *
30
	 * @param \Aimeos\MW\View\Iface $view View object
31
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
32
	 */
33
	public function data( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
34
	{
35
		$ds = DIRECTORY_SEPARATOR;
36
37
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Decorator' );
38
		$view->itemProviders = [
39
			'delivery' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Delivery' ),
40
			'payment' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Payment' ),
41
		];
42
43
		$view->itemSubparts = $this->getSubClientNames();
44
		$view->itemTypes = $this->getTypeItems();
45
46
		return $view;
47
	}
48
49
50
	/**
51
	 * Copies a resource
52
	 *
53
	 * @return string|null HTML output
54
	 */
55
	public function copy() : ?string
56
	{
57
		$view = $this->getObject()->data( $this->view() );
58
59
		try
60
		{
61
			if( ( $id = $view->param( 'id' ) ) === null )
62
			{
63
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
64
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
65
			}
66
67
			$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
68
69
			$view->item = $manager->get( $id, $this->getDomains() );
70
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
71
			$view->itemData = $this->toArray( $view->item, true );
72
			$view->itemBody = parent::copy();
73
		}
74
		catch( \Exception $e )
75
		{
76
			$this->report( $e, 'copy' );
77
		}
78
79
		return $this->render( $view );
80
	}
81
82
83
	/**
84
	 * Creates a new resource
85
	 *
86
	 * @return string|null HTML output
87
	 */
88
	public function create() : ?string
89
	{
90
		$view = $this->getObject()->data( $this->view() );
91
92
		try
93
		{
94
			$data = $view->param( 'item', [] );
95
96
			if( !isset( $view->item ) ) {
97
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'service' )->create();
98
			}
99
100
			$data['service.siteid'] = $view->item->getSiteId();
101
102
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
103
			$view->itemBody = parent::create();
104
		}
105
		catch( \Exception $e )
106
		{
107
			$this->report( $e, 'create' );
108
		}
109
110
		return $this->render( $view );
111
	}
112
113
114
	/**
115
	 * Deletes a resource
116
	 *
117
	 * @return string|null HTML output
118
	 */
119
	public function delete() : ?string
120
	{
121
		$view = $this->view();
122
123
		$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
124
		$manager->begin();
125
126
		try
127
		{
128
			if( ( $ids = $view->param( 'id' ) ) === null )
129
			{
130
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
131
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
132
			}
133
134
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
135
			$search->setConditions( $search->compare( '==', 'service.id', $ids ) );
136
			$items = $manager->search( $search, $this->getDomains() );
137
138
			foreach( $items as $item )
139
			{
140
				$view->item = $item;
141
				parent::delete();
142
			}
143
144
			$manager->delete( $items->toArray() );
145
			$manager->commit();
146
147
			return $this->redirect( 'service', 'search', null, 'delete' );
148
		}
149
		catch( \Exception $e )
150
		{
151
			$manager->rollback();
152
			$this->report( $e, 'delete' );
153
		}
154
155
		return $this->search();
156
	}
157
158
159
	/**
160
	 * Returns a single resource
161
	 *
162
	 * @return string|null HTML output
163
	 */
164
	public function get() : ?string
165
	{
166
		$view = $this->getObject()->data( $this->view() );
167
168
		try
169
		{
170
			if( ( $id = $view->param( 'id' ) ) === null )
171
			{
172
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
173
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
174
			}
175
176
			$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
177
178
			$view->item = $manager->get( $id, $this->getDomains() );
179
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
180
			$view->itemData = $this->toArray( $view->item );
181
			$view->itemBody = parent::get();
182
		}
183
		catch( \Exception $e )
184
		{
185
			$this->report( $e, 'get' );
186
		}
187
188
		return $this->render( $view );
189
	}
190
191
192
	/**
193
	 * Saves the data
194
	 *
195
	 * @return string|null HTML output
196
	 */
197
	public function save() : ?string
198
	{
199
		$view = $this->view();
200
201
		$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
202
		$manager->begin();
203
204
		try
205
		{
206
			$item = $this->fromArray( $view->param( 'item', [] ) );
207
			$view->item = $item->getId() ? $item : $manager->save( $item );
208
			$view->itemBody = parent::save();
209
210
			$manager->save( clone $view->item );
211
			$manager->commit();
212
213
			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

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