Passed
Branch master (d032f7)
by Aimeos
05:30
created

Base::nextAction()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 46
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 37
nc 8
nop 5
dl 0
loc 46
rs 9.0168
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), 2015-2018
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm;
12
13
sprintf( 'type' ); // for translation
14
15
16
/**
17
 * Common abstract class for all admin client classes.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
abstract class Base
23
	implements \Aimeos\Admin\JQAdm\Iface
24
{
25
	private $view;
26
	private $aimeos;
27
	private $context;
28
	private $subclients;
29
30
31
	/**
32
	 * Initializes the class instance.
33
	 *
34
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
37
	{
38
		$this->context = $context;
39
	}
40
41
42
	/**
43
	 * Catch unknown methods
44
	 *
45
	 * @param string $name Name of the method
46
	 * @param array $param List of method parameter
47
	 * @throws \Aimeos\Admin\JQAdm\Exception If method call failed
48
	 */
49
	public function __call( $name, array $param )
50
	{
51
		throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Unable to call method "%1$s"', $name ) );
52
	}
53
54
55
	/**
56
	 * Returns the Aimeos bootstrap object
57
	 *
58
	 * @return \Aimeos\Bootstrap The Aimeos bootstrap object
59
	 */
60
	public function getAimeos()
61
	{
62
		if( !isset( $this->aimeos ) ) {
63
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Aimeos object not available' ) );
64
		}
65
66
		return $this->aimeos;
67
	}
68
69
70
	/**
71
	 * Sets the Aimeos bootstrap object
72
	 *
73
	 * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object
74
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
75
	 */
76
	public function setAimeos( \Aimeos\Bootstrap $aimeos )
77
	{
78
		$this->aimeos = $aimeos;
79
		return $this;
80
	}
81
82
83
	/**
84
	 * Returns the view object that will generate the admin output.
85
	 *
86
	 * @return \Aimeos\MW\View\Iface The view object which generates the admin output
87
	 */
88
	public function getView()
89
	{
90
		if( !isset( $this->view ) ) {
91
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'No view available' ) );
92
		}
93
94
		return $this->view;
95
	}
96
97
98
	/**
99
	 * Sets the view object that will generate the admin output.
100
	 *
101
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output
102
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
103
	 */
104
	public function setView( \Aimeos\MW\View\Iface $view )
105
	{
106
		$this->view = $view;
107
		return $this;
108
	}
109
110
111
	/**
112
	 * Copies a resource
113
	 *
114
	 * @return string|null admin output to display or null for redirecting to the list
115
	 */
116
	public function copy()
117
	{
118
		foreach( $this->getSubClients() as $client ) {
119
			$client->copy();
120
		}
121
	}
122
123
124
	/**
125
	 * Creates a new resource
126
	 *
127
	 * @return string|null admin output to display or null for redirecting to the list
128
	 */
129
	public function create()
130
	{
131
		foreach( $this->getSubClients() as $client ) {
132
			$client->create();
133
		}
134
	}
135
136
137
	/**
138
	 * Deletes a resource
139
	 *
140
	 * @return string|null admin output to display or null for redirecting to the list
141
	 */
142
	public function delete()
143
	{
144
		foreach( $this->getSubClients() as $client ) {
145
			$client->delete();
146
		}
147
	}
148
149
150
	/**
151
	 * Exports a resource
152
	 *
153
	 * @return string|null admin output to display or null for redirecting to the list
154
	 */
155
	public function export()
156
	{
157
		foreach( $this->getSubClients() as $client ) {
158
			$client->export();
159
		}
160
	}
161
162
163
	/**
164
	 * Returns a resource
165
	 *
166
	 * @return string|null admin output to display or null for redirecting to the list
167
	 */
168
	public function get()
169
	{
170
		foreach( $this->getSubClients() as $client ) {
171
			$client->get();
172
		}
173
	}
174
175
176
	/**
177
	 * Imports a resource
178
	 *
179
	 * @return string|null admin output to display or null for redirecting to the list
180
	 */
181
	public function import()
182
	{
183
		foreach( $this->getSubClients() as $client ) {
184
			$client->import();
185
		}
186
	}
187
188
189
	/**
190
	 * Saves the data
191
	 *
192
	 * @return string|null admin output to display or null for redirecting to the list
193
	 */
194
	public function save()
195
	{
196
		foreach( $this->getSubClients() as $client ) {
197
			$client->save();
198
		}
199
	}
200
201
202
	/**
203
	 * Returns a list of resource according to the conditions
204
	 *
205
	 * @return string admin output to display
206
	 */
207
	public function search()
208
	{
209
		foreach( $this->getSubClients() as $client ) {
210
			$client->search();
211
		}
212
	}
213
214
215
	/**
216
	 * Adds the decorators to the client object
217
	 *
218
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
219
	 * @param array $decorators List of decorator name that should be wrapped around the client
220
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\"
221
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
222
	 */
223
	protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, $classprefix )
224
	{
225
		foreach( $decorators as $name )
226
		{
227
			if( ctype_alnum( $name ) === false )
228
			{
229
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
230
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) );
231
			}
232
233
			$classname = $classprefix . $name;
234
235
			if( class_exists( $classname ) === false ) {
236
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
237
			}
238
239
			$client = new $classname( $client, $this->context );
240
241
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\Iface', $client );
242
		}
243
244
		return $client;
245
	}
246
247
248
	/**
249
	 * Adds the decorators to the client object
250
	 *
251
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
252
	 * @param string $path Admin string in lower case, e.g. "catalog/detail/basic"
253
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
254
	 */
255
	protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, $path )
256
	{
257
		if( !is_string( $path ) || $path === '' ) {
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
258
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid domain "%1$s"', $path ) );
259
		}
260
261
		$localClass = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) );
262
		$config = $this->context->getConfig();
263
264
		$decorators = $config->get( 'admin/jqadm/common/decorators/default', [] );
265
		$excludes = $config->get( 'admin/jqadm/' . $path . '/decorators/excludes', [] );
266
267
		foreach( $decorators as $key => $name )
268
		{
269
			if( in_array( $name, $excludes ) ) {
270
				unset( $decorators[$key] );
271
			}
272
		}
273
274
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
275
		$client = $this->addDecorators( $client, $decorators, $classprefix );
276
277
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
278
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] );
279
		$client = $this->addDecorators( $client, $decorators, $classprefix );
280
281
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\';
282
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] );
283
		$client = $this->addDecorators( $client, $decorators, $classprefix );
284
285
		return $client;
286
	}
287
288
289
	/**
290
	 * Returns the sub-client given by its name.
291
	 *
292
	 * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree)
293
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
294
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-part object
295
	 */
296
	protected function createSubClient( $path, $name )
297
	{
298
		$path = strtolower( $path );
299
300
		if( $name === null ) {
301
			$name = $this->context->getConfig()->get( 'admin/jqadm/' . $path . '/name', 'Standard' );
302
		}
303
304
		if( empty( $name ) || ctype_alnum( $name ) === false ) {
305
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid characters in client name "%1$s"', $name ) );
306
		}
307
308
		$subnames = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) );
309
310
		$classname = '\\Aimeos\\Admin\\JQAdm\\' . $subnames . '\\' . $name;
311
312
		if( class_exists( $classname ) === false ) {
313
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
314
		}
315
316
		$object = new $classname( $this->context );
317
318
		\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Iface', $object );
319
320
		$object = $this->addClientDecorators( $object, $path );
321
		$object->setAimeos( $this->aimeos );
0 ignored issues
show
Bug introduced by
The method setAimeos() does not exist on Aimeos\Admin\JQAdm\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface or Aimeos\Admin\JQAdm\Common\Decorator\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

321
		$object->/** @scrutinizer ignore-call */ 
322
           setAimeos( $this->aimeos );
Loading history...
322
		$object->setView( $this->view );
323
324
		return $object;
325
	}
326
327
328
	/**
329
	 * Returns the value for the given key in the array
330
	 *
331
	 * @param array $values Multi-dimensional associative list of key/value pairs
332
	 * @param string $key Parameter key like "name" or "list/test" for associative arrays
333
	 * @param mixed $default Returned value if no one for key is available
334
	 * @return mixed Value from the array or default value if not present in array
335
	 */
336
	protected function getValue( array $values, $key, $default = null )
337
	{
338
		foreach( explode( '/', trim( $key, '/' ) ) as $part )
339
		{
340
			if( isset( $values[$part] ) ) {
341
				$values = $values[$part];
342
			} else {
343
				return $default;
344
			}
345
		}
346
347
		return $values;
348
	}
349
350
351
	/**
352
	 * Returns the known client parameters and their values
353
	 *
354
	 * @param array $names List of parameter names
355
	 * @return array Associative list of parameters names as key and their values
356
	 */
357
	protected function getClientParams( $names = array( 'id', 'resource', 'site', 'lang' ) )
358
	{
359
		$list = [];
360
361
		foreach( $names as $name )
362
		{
363
			if( ( $val = $this->view->param( $name ) ) !== null ) {
364
				$list[$name] = $val;
365
			}
366
		}
367
368
		return $list;
369
	}
370
371
372
	/**
373
	 * Returns the context object.
374
	 *
375
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
376
	 */
377
	protected function getContext()
378
	{
379
		return $this->context;
380
	}
381
382
383
	/**
384
	 * Returns the list of sub-client names configured for the client.
385
	 *
386
	 * @return array List of admin client names
387
	 */
388
	abstract protected function getSubClientNames();
389
390
391
	/**
392
	 * Returns the available class names without namespace that are stored in the given path
393
	 *
394
	 * @param string $relpath Path relative to the include paths
395
	 * @param string[] $excludes List of file names to execlude
396
	 * @return string[] List of available class names
397
	 */
398
	protected function getClassNames( $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] )
399
	{
400
		$list = [];
401
402
		foreach( $this->getAimeos()->getIncludePaths() as $path )
403
		{
404
			$path .= DIRECTORY_SEPARATOR . $relpath;
405
406
			if( is_dir( $path ) )
407
			{
408
				foreach( new \DirectoryIterator( $path ) as $entry )
409
				{
410
					if( $entry->isFile() && !in_array( $entry->getFileName(), $excludes ) ) {
411
						$list[] = pathinfo( $entry->getFileName(), PATHINFO_FILENAME );
412
					}
413
				}
414
			}
415
		}
416
417
		sort( $list );
418
		return $list;
419
	}
420
421
422
	/**
423
	 * Returns the array of criteria conditions based on the given parameters
424
	 *
425
	 * @param array $params List of criteria data with condition, sorting and paging
426
	 * @return array Multi-dimensional associative list of criteria conditions
427
	 */
428
	protected function getCriteriaConditions( array $params )
429
	{
430
		$expr = [];
431
432
		if( isset( $params['key'] ) )
433
		{
434
			foreach( (array) $params['key'] as $idx => $key )
435
			{
436
				if( $key != '' && isset( $params['op'][$idx] ) && $params['op'][$idx] != ''
437
					&& isset( $params['val'][$idx] ) && $params['val'][$idx] != ''
438
				) {
439
					$expr[] = [$params['op'][$idx] => [$key => $params['val'][$idx]]];
440
				}
441
			}
442
443
			if( !empty( $expr ) ) {
444
				$expr = ['&&' => $expr];
445
			}
446
		}
447
448
		return $expr;
449
	}
450
451
452
	/**
453
	 * Returns the array of criteria sortations based on the given parameters
454
	 *
455
	 * @param array $params List of criteria data with condition, sorting and paging
456
	 * @return array Associative list of criteria sortations
457
	 */
458
	protected function getCriteriaSortations( array $params )
459
	{
460
		$sortation = [];
461
462
		foreach( $params as $sort )
463
		{
464
			if( $sort[0] === '-' ) {
465
				$sortation[substr( $sort, 1 )] = '-';
466
			} else {
467
				$sortation[$sort] = '+';
468
			}
469
		}
470
471
		return $sortation;
472
	}
473
474
475
	/**
476
	 * Returns the configured sub-clients or the ones named in the default parameter if none are configured.
477
	 *
478
	 * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names
479
	 */
480
	protected function getSubClients()
481
	{
482
		if( !isset( $this->subclients ) )
483
		{
484
			$this->subclients = [];
485
486
			foreach( $this->getSubClientNames() as $name ) {
487
				$this->subclients[] = $this->getSubClient( $name );
488
			}
489
		}
490
491
		return $this->subclients;
492
	}
493
494
495
	/**
496
	 * Initializes the criteria object based on the given parameter
497
	 *
498
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
499
	 * @param array $params List of criteria data with condition, sorting and paging
500
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
501
	 */
502
	protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params )
503
	{
504
		if( isset( $params['filter'] ) ) {
505
			$criteria = $this->initCriteriaConditions( $criteria, (array) $params['filter'] );
506
		}
507
508
		if( isset( $params['sort'] ) ) {
509
			$criteria = $this->initCriteriaSortations( $criteria, (array) $params['sort'] );
510
		}
511
512
		$page = [];
513
		if( isset( $params['page'] ) ) {
514
			$page = (array) $params['page'];
515
		}
516
517
		return $this->initCriteriaSlice( $criteria, $page );
518
	}
519
520
521
	/**
522
	 * Writes the exception details to the log
523
	 *
524
	 * @param \Exception $e Exception object
525
	 */
526
	protected function logException( \Exception $e )
527
	{
528
		$logger = $this->context->getLogger();
529
530
		$logger->log( $e->getMessage(), \Aimeos\MW\Logger\Base::WARN, 'admin/jqadm' );
531
		$logger->log( $e->getTraceAsString(), \Aimeos\MW\Logger\Base::WARN, 'admin/jqadm' );
532
	}
533
534
535
	/**
536
	 * Returns a map of code/item pairs
537
	 *
538
	 * @param \Aimeos\MShop\Common\Item\Type\Iface[] $items Associative list of type items
539
	 * @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of codes as keys and items as values
540
	 */
541
	protected function map( array $items )
542
	{
543
		$list = [];
544
545
		foreach( $items as $item ) {
546
			$list[$item->getCode()] = $item;
547
		}
548
549
		return $list;
550
	}
551
552
553
	/**
554
	 * Adds a redirect to the response for the next action
555
	 *
556
	 * @param \Aimeos\MW\View\Iface $view View object
557
	 * @param string $action Next action
558
	 * @param string $resource Resource name
559
	 * @param string $id ID of the next resource item
560
	 * @return \Aimeos\MW\View\Iface Modified view object
561
	 */
562
	protected function nextAction( \Aimeos\MW\View\Iface $view, $action, $resource, $id = null, $act = null )
563
	{
564
		$params = $this->getClientParams();
565
		$params['resource'] = $resource;
566
		unset( $params['id'] );
567
568
		if( $act ) {
569
			$params['act'] = $act;
570
		}
571
572
		switch( $action )
573
		{
574
			case 'search':
575
				$target = $view->config( 'admin/jqadm/url/search/target' );
576
				$cntl = $view->config( 'admin/jqadm/url/search/controller', 'Jqadm' );
577
				$action = $view->config( 'admin/jqadm/url/search/action', 'search' );
578
				$conf = $view->config( 'admin/jqadm/url/search/config', [] );
579
				$url = $view->url( $target, $cntl, $action, $params, [], $conf );
580
				break;
581
			case 'create':
582
				$target = $view->config( 'admin/jqadm/url/create/target' );
583
				$cntl = $view->config( 'admin/jqadm/url/create/controller', 'Jqadm' );
584
				$action = $view->config( 'admin/jqadm/url/create/action', 'create' );
585
				$conf = $view->config( 'admin/jqadm/url/create/config', [] );
586
				$url = $view->url( $target, $cntl, $action, $params, [], $conf );
587
				break;
588
			case 'copy':
589
				$target = $view->config( 'admin/jqadm/url/copy/target' );
590
				$cntl = $view->config( 'admin/jqadm/url/copy/controller', 'Jqadm' );
591
				$action = $view->config( 'admin/jqadm/url/copy/action', 'copy' );
592
				$conf = $view->config( 'admin/jqadm/url/copy/config', [] );
593
				$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf );
594
				break;
595
			default:
596
				$target = $view->config( 'admin/jqadm/url/get/target' );
597
				$cntl = $view->config( 'admin/jqadm/url/get/controller', 'Jqadm' );
598
				$action = $view->config( 'admin/jqadm/url/get/action', 'get' );
599
				$conf = $view->config( 'admin/jqadm/url/get/config', [] );
600
				$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf );
601
		}
602
603
		$view->response()->withStatus( 302 );
604
		$view->response()->withHeader( 'Location', $url );
605
		$view->response()->withHeader( 'Cache-Control', 'no-store' );
606
607
		return $view;
608
	}
609
610
611
	/**
612
	 * Stores and returns the parameters used for searching items
613
	 *
614
	 * @param array $params GET/POST parameter set
615
	 * @param string $name Name of the panel/subpanel
616
	 * @return array Associative list of parameters for searching items
617
	 */
618
	protected function storeSearchParams( array $params, $name )
619
	{
620
		$key = 'aimeos/admin/jqadm/' . $name;
621
		$session = $this->getContext()->getSession();
622
623
		if( isset( $params['filter'] ) ) {
624
			$session->set( $key . '/filter', $params['filter'] );
625
		}
626
627
		if( isset( $params['sort'] ) ) {
628
			$session->set( $key . '/sort', $params['sort'] );
629
		}
630
631
		if( isset( $params['page'] ) ) {
632
			$session->set( $key . '/page', $params['page'] );
633
		}
634
635
		if( isset( $params['fields'] ) ) {
636
			$session->set( $key . '/fields', $params['fields'] );
637
		}
638
639
		return [
640
			'fields' => $session->get( $key . '/fields' ),
641
			'filter' => $session->get( $key . '/filter' ),
642
			'page' => $session->get( $key . '/page' ),
643
			'sort' => $session->get( $key . '/sort' ),
644
		];
645
	}
646
647
648
	/**
649
	 * Initializes the criteria object with conditions based on the given parameter
650
	 *
651
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
652
	 * @param array $params List of criteria data with condition, sorting and paging
653
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
654
	 */
655
	private function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params )
656
	{
657
		$expr = [
658
			$criteria->toConditions( $this->getCriteriaConditions( $params ) ),
659
			$criteria->getConditions(),
660
		];
661
662
		return $criteria->setConditions( $criteria->combine( '&&', $expr ) );
663
	}
664
665
666
	/**
667
	 * Initializes the criteria object with the slice based on the given parameter.
668
	 *
669
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
670
	 * @param array $params List of criteria data with condition, sorting and paging
671
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
672
	 */
673
	private function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params )
674
	{
675
		$start = ( isset( $params['offset'] ) ? $params['offset'] : 0 );
676
		$size = ( isset( $params['limit'] ) ? $params['limit'] : 25 );
677
678
		return $criteria->setSlice( $start, $size );
679
	}
680
681
682
	/**
683
	 * Initializes the criteria object with sortations based on the given parameter
684
	 *
685
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
686
	 * @param array $params List of criteria data with condition, sorting and paging
687
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
688
	 */
689
	private function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params )
690
	{
691
		return $criteria->setSortations( $criteria->toSortations( $this->getCriteriaSortations( $params ) ) );
692
	}
693
}
694