Passed
Push — master ( 6b0eee...f86db2 )
by Aimeos
08:30 queued 05:17
created

Base::redirect()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 55
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 43
nc 12
nop 4
dl 0
loc 55
rs 8.6097
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
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, \Aimeos\MW\Macro\Iface
24
{
25
	use \Aimeos\MW\Macro\Traits;
26
27
28
	private $view;
29
	private $aimeos;
30
	private $context;
31
	private $subclients;
32
	private $object;
33
34
35
	/**
36
	 * Initializes the class instance.
37
	 *
38
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
39
	 */
40
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
41
	{
42
		$this->context = $context;
43
	}
44
45
46
	/**
47
	 * Adds the required data used in the attribute template
48
	 *
49
	 * @param \Aimeos\MW\View\Iface $view View object
50
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
51
	 */
52
	public function data( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
53
	{
54
		return $view;
55
	}
56
57
58
	/**
59
	 * Returns the Aimeos bootstrap object
60
	 *
61
	 * @return \Aimeos\Bootstrap The Aimeos bootstrap object
62
	 */
63
	public function getAimeos() : \Aimeos\Bootstrap
64
	{
65
		if( !isset( $this->aimeos ) ) {
66
			throw new \Aimeos\Admin\JQAdm\Exception( $this->context->translate( 'admin', 'Aimeos object not available' ) );
67
		}
68
69
		return $this->aimeos;
70
	}
71
72
73
	/**
74
	 * Sets the Aimeos bootstrap object
75
	 *
76
	 * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object
77
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
78
	 */
79
	public function setAimeos( \Aimeos\Bootstrap $aimeos ) : \Aimeos\Admin\JQAdm\Iface
80
	{
81
		$this->aimeos = $aimeos;
82
		return $this;
83
	}
84
85
86
	/**
87
	 * Makes the outer decorator object available to inner objects
88
	 *
89
	 * @param \Aimeos\Admin\JQAdm\Iface $object Outmost object
90
	 * @return \Aimeos\Admin\JQAdm\Iface Same object for fluent interface
91
	 */
92
	public function setObject( \Aimeos\Admin\JQAdm\Iface $object ) : \Aimeos\Admin\JQAdm\Iface
93
	{
94
		$this->object = $object;
95
		return $this;
96
	}
97
98
99
	/**
100
	 * Returns the view object that will generate the admin output.
101
	 *
102
	 * @return \Aimeos\MW\View\Iface The view object which generates the admin output
103
	 */
104
	protected function view() : \Aimeos\MW\View\Iface
105
	{
106
		if( !isset( $this->view ) ) {
107
			throw new \Aimeos\Admin\JQAdm\Exception( $this->context->translate( 'admin', 'No view available' ) );
108
		}
109
110
		return $this->view;
111
	}
112
113
114
	/**
115
	 * Sets the view object that will generate the admin output.
116
	 *
117
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output
118
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
119
	 */
120
	public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Admin\JQAdm\Iface
121
	{
122
		$this->view = $view;
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Copies a resource
129
	 *
130
	 * @return string|null Output to display
131
	 */
132
	public function copy() : ?string
133
	{
134
		$body = null;
135
		$view = $this->view();
136
137
		foreach( $this->getSubClients() as $idx => $client )
138
		{
139
			$view->tabindex = ++$idx + 1;
140
			$body .= $client->copy();
141
		}
142
143
		return $body;
144
	}
145
146
147
	/**
148
	 * Creates a new resource
149
	 *
150
	 * @return string|null Output to display
151
	 */
152
	public function create() : ?string
153
	{
154
		$body = null;
155
		$view = $this->view();
156
157
		foreach( $this->getSubClients() as $idx => $client )
158
		{
159
			$view->tabindex = ++$idx + 1;
160
			$body .= $client->create();
161
		}
162
163
		return $body;
164
	}
165
166
167
	/**
168
	 * Deletes a resource
169
	 *
170
	 * @return string|null Output to display
171
	 */
172
	public function delete() : ?string
173
	{
174
		$body = null;
175
176
		foreach( $this->getSubClients() as $client ) {
177
			$body .= $client->delete();
178
		}
179
180
		return $body;
181
	}
182
183
184
	/**
185
	 * Exports a resource
186
	 *
187
	 * @return string|null Output to display
188
	 */
189
	public function export() : ?string
190
	{
191
		$body = null;
192
193
		foreach( $this->getSubClients() as $client ) {
194
			$body .= $client->export();
195
		}
196
197
		return $body;
198
	}
199
200
201
	/**
202
	 * Returns a resource
203
	 *
204
	 * @return string|null Output to display
205
	 */
206
	public function get() : ?string
207
	{
208
		$body = null;
209
		$view = $this->view();
210
211
		foreach( $this->getSubClients() as $idx => $client )
212
		{
213
			$view->tabindex = ++$idx + 1;
214
			$body .= $client->get();
215
		}
216
217
		return $body;
218
	}
219
220
221
	/**
222
	 * Imports a resource
223
	 *
224
	 * @return string|null Output to display
225
	 * @deprecated 2021.01
226
	 */
227
	public function import() : ?string
228
	{
229
		$body = null;
230
231
		foreach( $this->getSubClients() as $client ) {
232
			$body .= $client->import();
233
		}
234
235
		return null;
236
	}
237
238
239
	/**
240
	 * Saves the data
241
	 *
242
	 * @return string|null Output to display
243
	 */
244
	public function save() : ?string
245
	{
246
		$body = null;
247
248
		foreach( $this->getSubClients() as $client ) {
249
			$body .= $client->save();
250
		}
251
252
		return $body;
253
	}
254
255
256
	/**
257
	 * Returns a list of resource according to the conditions
258
	 *
259
	 * @return string|null Output to display
260
	 */
261
	public function search() : ?string
262
	{
263
		$body = null;
264
265
		foreach( $this->getSubClients() as $client ) {
266
			$body .= $client->search();
267
		}
268
269
		return $body;
270
	}
271
272
273
	/**
274
	 * Returns the PSR-7 response object for the request
275
	 *
276
	 * @return \Psr\Http\Message\ResponseInterface Response object
277
	 */
278
	public function response() : \Psr\Http\Message\ResponseInterface
279
	{
280
		return $this->view()->response();
281
	}
282
283
284
	/**
285
	 * Adds the decorators to the client object
286
	 *
287
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
288
	 * @param array $decorators List of decorator name that should be wrapped around the client
289
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\"
290
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
291
	 */
292
	protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Admin\JQAdm\Iface
293
	{
294
		foreach( $decorators as $name )
295
		{
296
			$classname = $classprefix . $name;
297
298
			if( ctype_alnum( $name ) === false )
299
			{
300
				$msg = $this->context->translate( 'admin', 'Invalid class name "%1$s"' );
301
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
302
			}
303
304
			if( class_exists( $classname ) === false )
305
			{
306
				$msg = $this->context->translate( 'admin', 'Class "%1$s" not found' );
307
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
308
			}
309
310
			$client = new $classname( $client, $this->context );
311
312
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\Iface', $client );
313
		}
314
315
		return $client;
316
	}
317
318
319
	/**
320
	 * Adds the decorators to the client object
321
	 *
322
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
323
	 * @param string $path Admin string in lower case, e.g. "catalog/detail/basic"
324
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
325
	 */
326
	protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, string $path ) : \Aimeos\Admin\JQAdm\Iface
327
	{
328
		if( !is_string( $path ) || $path === '' )
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
329
		{
330
			$msg = $this->context->translate( 'admin', 'Invalid domain "%1$s"' );
331
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $path ) );
332
		}
333
334
		$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) );
335
		$config = $this->context->getConfig();
336
337
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
338
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] );
339
		$client = $this->addDecorators( $client, $decorators, $classprefix );
340
341
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\';
342
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] );
343
		$client = $this->addDecorators( $client, $decorators, $classprefix );
344
345
		return $client;
346
	}
347
348
349
	/**
350
	 * Returns the sub-client given by its name.
351
	 *
352
	 * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree)
353
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
354
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-part object
355
	 */
356
	protected function createSubClient( string $path, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
357
	{
358
		$path = strtolower( $path );
359
360
		if( $name === null ) {
361
			$name = $this->context->getConfig()->get( 'admin/jqadm/' . $path . '/name', 'Standard' );
362
		}
363
364
		if( empty( $name ) || ctype_alnum( $name ) === false )
365
		{
366
			$msg = $this->context->translate( 'admin', 'Invalid characters in client name "%1$s"' );
367
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $name ) );
368
		}
369
370
		$subnames = str_replace( '/', '\\', ucwords( $path, '/' ) );
371
		$classname = '\\Aimeos\\Admin\\JQAdm\\' . $subnames . '\\' . $name;
372
373
		if( class_exists( $classname ) === false )
374
		{
375
			$msg = $this->context->translate( 'admin', 'Class "%1$s" not available' );
376
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
377
		}
378
379
		$object = new $classname( $this->context );
380
		$object = \Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Iface', $object );
381
		$object = $this->addClientDecorators( $object, $path );
382
383
		return $object->setObject( $object )->setAimeos( $this->aimeos )->setView( $this->view );
0 ignored issues
show
Bug introduced by
The method setObject() 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

383
		return $object->/** @scrutinizer ignore-call */ setObject( $object )->setAimeos( $this->aimeos )->setView( $this->view );
Loading history...
384
	}
385
386
387
	/**
388
	 * Returns the value for the given key in the array
389
	 *
390
	 * @param array $values Multi-dimensional associative list of key/value pairs
391
	 * @param string $key Parameter key like "name" or "list/test" for associative arrays
392
	 * @param mixed $default Returned value if no one for key is available
393
	 * @return mixed Value from the array or default value if not present in array
394
	 */
395
	protected function val( array $values, $key, $default = null )
396
	{
397
		foreach( explode( '/', trim( $key, '/' ) ) as $part )
398
		{
399
			if( is_array( $values ) && isset( $values[$part] ) ) {
400
				$values = $values[$part];
401
			} else {
402
				return $default;
403
			}
404
		}
405
406
		return $values;
407
	}
408
409
410
	/**
411
	 * Returns the known client parameters and their values
412
	 *
413
	 * @param array $names List of parameter names
414
	 * @return array Associative list of parameters names as key and their values
415
	 */
416
	protected function getClientParams( $names = ['id', 'resource', 'site', 'locale'] ) : array
417
	{
418
		$list = [];
419
420
		foreach( $names as $name )
421
		{
422
			if( ( $val = $this->view->param( $name ) ) !== null && !is_array( $val ) ) {
423
				$list[$name] = $val;
424
			}
425
		}
426
427
		return $list;
428
	}
429
430
431
	/**
432
	 * Returns the context object.
433
	 *
434
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
435
	 */
436
	protected function getContext() : \Aimeos\MShop\Context\Item\Iface
437
	{
438
		return $this->context;
439
	}
440
441
442
	/**
443
	 * Returns the list of sub-client names configured for the client.
444
	 *
445
	 * @return array List of admin client names
446
	 */
447
	abstract protected function getSubClientNames() : array;
448
449
450
	/**
451
	 * Returns the available class names without namespace that are stored in the given path
452
	 *
453
	 * @param string $relpath Path relative to the include paths
454
	 * @param string[] $excludes List of file names to execlude
455
	 * @return string[] List of available class names
456
	 */
457
	protected function getClassNames( string $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] ) : array
458
	{
459
		$list = [];
460
461
		foreach( $this->getAimeos()->getIncludePaths() as $path )
462
		{
463
			$path .= DIRECTORY_SEPARATOR . $relpath;
464
465
			if( is_dir( $path ) )
466
			{
467
				foreach( new \DirectoryIterator( $path ) as $entry )
468
				{
469
					if( $entry->isFile() && !in_array( $entry->getFileName(), $excludes ) ) {
470
						$list[] = pathinfo( $entry->getFileName(), PATHINFO_FILENAME );
471
					}
472
				}
473
			}
474
		}
475
476
		sort( $list );
477
		return $list;
478
	}
479
480
481
	/**
482
	 * Returns the array of criteria conditions based on the given parameters
483
	 *
484
	 * @param array $params List of criteria data with condition, sorting and paging
485
	 * @return array Multi-dimensional associative list of criteria conditions
486
	 */
487
	protected function getCriteriaConditions( array $params ) : array
488
	{
489
		$expr = [];
490
491
		if( isset( $params['key'] ) )
492
		{
493
			foreach( (array) $params['key'] as $idx => $key )
494
			{
495
				if( $key != '' && isset( $params['op'][$idx] ) && $params['op'][$idx] != ''
496
					&& isset( $params['val'][$idx] ) && $params['val'][$idx] != ''
497
				) {
498
					$expr[] = [$params['op'][$idx] => [$key => $params['val'][$idx]]];
499
				}
500
			}
501
502
			if( !empty( $expr ) ) {
503
				$expr = ['&&' => $expr];
504
			}
505
		}
506
507
		return $expr;
508
	}
509
510
511
	/**
512
	 * Returns the outer decoratorator of the object
513
	 *
514
	 * @return \Aimeos\Admin\JQAdm\Iface Outmost object
515
	 */
516
	protected function object() : Iface
517
	{
518
		if( isset( $this->object ) ) {
519
			return $this->object;
520
		}
521
522
		return $this;
523
	}
524
525
526
	/**
527
	 * Returns the configured sub-clients or the ones named in the default parameter if none are configured.
528
	 *
529
	 * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names
530
	 */
531
	protected function getSubClients() : array
532
	{
533
		if( !isset( $this->subclients ) )
534
		{
535
			$this->subclients = [];
536
537
			foreach( $this->getSubClientNames() as $name ) {
538
				$this->subclients[] = $this->getSubClient( $name );
539
			}
540
		}
541
542
		return $this->subclients;
543
	}
544
545
546
	/**
547
	 * Initializes the criteria object based on the given parameter
548
	 *
549
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
550
	 * @param array $params List of criteria data with condition, sorting and paging
551
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
552
	 */
553
	protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface
554
	{
555
		if( isset( $params['sort'] ) && !empty( $params['sort'] ) ) {
556
			$criteria->order( $params['sort'] );
557
		}
558
559
		return $criteria->slice( $params['page']['offset'] ?? 0, $params['page']['limit'] ?? 25 )
560
			->add( $criteria->parse( $this->getCriteriaConditions( $params['filter'] ?? [] ) ) );
561
	}
562
563
564
	/**
565
	 * Flattens the nested configuration array
566
	 *
567
	 * @param array $config Multi-dimensional list of key/value pairs
568
	 * @param string $path Path of keys separated by slashes (/) to add new values for
569
	 * @return array List of arrays with "key" and "val" keys
570
	 */
571
	protected function flatten( array $config, string $path = '' ) : array
572
	{
573
		$list = [];
574
575
		foreach( $config as $key => $val )
576
		{
577
			if( is_array( $val ) ) {
578
				$list = array_merge( $list, $this->flatten( $val, $path . '/' . $key ) );
579
			} else {
580
				$list[] = ['key' => trim( $path . '/' . $key, '/' ), 'val' => $val];
581
			}
582
		}
583
584
		return $list;
585
	}
586
587
588
	/**
589
	 * Writes the exception details to the log
590
	 *
591
	 * @param \Exception $e Exception object
592
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
593
	 */
594
	protected function log( \Exception $e ) : Iface
595
	{
596
		$msg = $e->getMessage() . PHP_EOL;
597
598
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception ) {
599
			$msg .= print_r( $e->getDetails(), true ) . PHP_EOL;
0 ignored issues
show
Bug introduced by
Are you sure print_r($e->getDetails(), true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

599
			$msg .= /** @scrutinizer ignore-type */ print_r( $e->getDetails(), true ) . PHP_EOL;
Loading history...
600
		}
601
602
		$this->context->getLogger()->log( $msg . $e->getTraceAsString(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' );
603
604
		return $this;
605
	}
606
607
608
	/**
609
	 * Returns a map of code/item pairs
610
	 *
611
	 * @param \Aimeos\MShop\Common\Item\Type\Iface[] $items Associative list of type items
612
	 * @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of codes as keys and items as values
613
	 * @deprecated 2021.01
614
	 */
615
	protected function map( \Aimeos\Map $items ) : array
616
	{
617
		$list = [];
618
619
		foreach( $items as $item ) {
620
			$list[$item->getCode()] = $item;
621
		}
622
623
		return $list;
624
	}
625
626
627
	/**
628
	 * Adds a redirect to the response for the next action
629
	 *
630
	 * @param string $resource Resource name
631
	 * @param string|null $action Next action
632
	 * @param string|null $id ID of the next resource item
633
	 * @param string|null $act Current action name
634
	 * @return string|null Returns value for the actions
635
	 */
636
	protected function redirect( string $resource, ?string $action, string $id = null,
637
		string $method = null ) : ?string
638
	{
639
		$params = $this->getClientParams();
640
		$context = $this->getContext();
641
		$view = $this->view();
642
643
		$params['resource'] = $resource;
644
		unset( $params['id'] );
645
646
		switch( $action )
647
		{
648
			case 'search':
649
				$target = $view->config( 'admin/jqadm/url/search/target' );
650
				$cntl = $view->config( 'admin/jqadm/url/search/controller', 'Jqadm' );
651
				$action = $view->config( 'admin/jqadm/url/search/action', 'search' );
652
				$conf = $view->config( 'admin/jqadm/url/search/config', [] );
653
				$url = $view->url( $target, $cntl, $action, $params, [], $conf );
654
				break;
655
			case 'create':
656
				$params['parentid'] = $id;
657
				$target = $view->config( 'admin/jqadm/url/create/target' );
658
				$cntl = $view->config( 'admin/jqadm/url/create/controller', 'Jqadm' );
659
				$action = $view->config( 'admin/jqadm/url/create/action', 'create' );
660
				$conf = $view->config( 'admin/jqadm/url/create/config', [] );
661
				$url = $view->url( $target, $cntl, $action, $params, [], $conf );
662
				break;
663
			case 'copy':
664
				$target = $view->config( 'admin/jqadm/url/copy/target' );
665
				$cntl = $view->config( 'admin/jqadm/url/copy/controller', 'Jqadm' );
666
				$action = $view->config( 'admin/jqadm/url/copy/action', 'copy' );
667
				$conf = $view->config( 'admin/jqadm/url/copy/config', [] );
668
				$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf );
669
				break;
670
			default:
671
				$target = $view->config( 'admin/jqadm/url/get/target' );
672
				$cntl = $view->config( 'admin/jqadm/url/get/controller', 'Jqadm' );
673
				$action = $view->config( 'admin/jqadm/url/get/action', 'get' );
674
				$conf = $view->config( 'admin/jqadm/url/get/config', [] );
675
				$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf );
676
		}
677
678
		switch( $method )
679
		{
680
			case 'save':
681
				$context->getSession()->set( 'info', [$context->translate( 'admin', 'Item saved successfully' )] ); break;
682
			case 'delete':
683
				$context->getSession()->set( 'info', [$context->translate( 'admin', 'Item deleted successfully' )] ); break;
684
		}
685
686
		$view->response()->withStatus( 302 );
687
		$view->response()->withHeader( 'Location', $url );
688
		$view->response()->withHeader( 'Cache-Control', 'no-store' );
689
690
		return null;
691
	}
692
693
694
	/**
695
	 * Writes the exception details to the log
696
	 *
697
	 * @param \Exception $e Exception object
698
	 * @param string $method Method it's called from
699
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
700
	 */
701
	protected function report( \Exception $e, string $method ) : Iface
702
	{
703
		$view = $this->view;
704
		$i18n = $this->context->getI18n();
705
706
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception )
707
		{
708
			$view->errors = array_merge( $view->get( 'errors', [] ), [$e->getMessage()] );
709
			return $this->log( $e );
710
		}
711
		elseif( $e instanceof \Aimeos\MShop\Exception )
712
		{
713
			$view->errors = array_merge( $view->get( 'errors', [] ), [$i18n->dt( 'mshop', $e->getMessage() )] );
714
			return $this->log( $e );
715
		}
716
717
		switch( $method )
718
		{
719
			case 'save': $msg = $i18n->dt( 'admin', 'Error saving data' ); break;
720
			case 'delete': $msg = $i18n->dt( 'admin', 'Error deleting data' ); break;
721
			default: $msg = $i18n->dt( 'admin', 'Error retrieving data' ); break;
722
		}
723
724
		$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
725
726
		return $this->log( $e );
727
	}
728
729
730
	/**
731
	 * Checks and returns the request parameter for the given name
732
	 *
733
	 * @param string $name Name of the request parameter, can be a path like 'page/limit'
734
	 * @return mixed Parameter value
735
	 * @throws \Aimeos\Admin\JQAdm\Exception If the parameter is missing
736
	 */
737
	protected function require( string $name )
738
	{
739
		if( ( $value = $this->view()->param( $name ) ) !== null ) {
740
			return $value;
741
		}
742
743
		$msg = $this->context->translate( 'admin', 'Required parameter "%1$s" is missing' );
744
		throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $name ) );
745
	}
746
747
748
	/**
749
	 * Stores and returns the parameters used for searching items
750
	 *
751
	 * @param array $params GET/POST parameter set
752
	 * @param string $name Name of the panel/subpanel
753
	 * @return array Associative list of parameters for searching items
754
	 */
755
	protected function storeFilter( array $params, string $name ) : array
756
	{
757
		$key = 'aimeos/admin/jqadm/' . $name;
758
		$session = $this->getContext()->getSession();
759
760
		foreach( ['fields', 'filter', 'page', 'sort'] as $part )
761
		{
762
			if( isset( $params[$part] ) ) {
763
				$session->set( $key . '/' . $part, $params[$part] );
764
			}
765
		}
766
767
		return [
768
			'fields' => $session->get( $key . '/fields' ),
769
			'filter' => $session->get( $key . '/filter' ),
770
			'page' => $session->get( $key . '/page' ),
771
			'sort' => $session->get( $key . '/sort' ),
772
		];
773
	}
774
775
776
	/**
777
	 * Throws an exception with given details
778
	 *
779
	 * @param array $errors List of key/message pairs of errors
780
	 * @throws \Aimeos\Admin\JQAdm\Exception Exception with error details
781
	 */
782
	protected function notify( array $errors ) : Iface
783
	{
784
		$list = [];
785
		$i18n = $this->context->getI18n();
786
787
		foreach( $errors as $key => $error )
788
		{
789
			if( $error ) {
790
				$list[] = $key . ': ' . $i18n->dt( 'mshop', $error );
791
			}
792
		}
793
794
		if( !empty( $list ) ) {
795
			throw new \Aimeos\Admin\JQAdm\Exception( join( "\n", $list ) );
796
		}
797
798
		return $this;
799
	}
800
}
801