Passed
Push — master ( 27cbe5...770b51 )
by Aimeos
03:36
created

Standard::search()   A

Complexity

Conditions 2
Paths 11

Size

Total Lines 48
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

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