Passed
Push — master ( 396060...842212 )
by Aimeos
08:52
created

Base::batchBase()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 26
rs 9.5222
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
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\Macro\Iface
24
{
25
	use \Aimeos\Macro\Macroable;
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\ContextIface $context Context object
39
	 */
40
	public function __construct( \Aimeos\MShop\ContextIface $context )
41
	{
42
		$this->context = $context;
43
	}
44
45
46
	/**
47
	 * Adds the required data used in the attribute template
48
	 *
49
	 * @param \Aimeos\Base\View\Iface $view View object
50
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
51
	 */
52
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\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\Base\View\Iface The view object which generates the admin output
103
	 */
104
	protected function view() : \Aimeos\Base\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\Base\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\Base\View\Iface $view ) : \Aimeos\Admin\JQAdm\Iface
121
	{
122
		$this->view = $view;
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Batch update of a resource
129
	 *
130
	 * @return string|null Output to display
131
	 */
132
	public function batch() : ?string
133
	{
134
		foreach( $this->getSubClients() as $client ) {
135
			$client->batch();
136
		}
137
138
		return null;
139
	}
140
141
142
	/**
143
	 * Copies a resource
144
	 *
145
	 * @return string|null Output to display
146
	 */
147
	public function copy() : ?string
148
	{
149
		$body = null;
150
		$view = $this->view();
151
152
		foreach( $this->getSubClients() as $idx => $client )
153
		{
154
			$view->tabindex = ++$idx + 1;
155
			$body .= $client->copy();
156
		}
157
158
		return $body;
159
	}
160
161
162
	/**
163
	 * Creates a new resource
164
	 *
165
	 * @return string|null Output to display
166
	 */
167
	public function create() : ?string
168
	{
169
		$body = null;
170
		$view = $this->view();
171
172
		foreach( $this->getSubClients() as $idx => $client )
173
		{
174
			$view->tabindex = ++$idx + 1;
175
			$body .= $client->create();
176
		}
177
178
		return $body;
179
	}
180
181
182
	/**
183
	 * Deletes a resource
184
	 *
185
	 * @return string|null Output to display
186
	 */
187
	public function delete() : ?string
188
	{
189
		$body = null;
190
191
		foreach( $this->getSubClients() as $client ) {
192
			$body .= $client->delete();
193
		}
194
195
		return $body;
196
	}
197
198
199
	/**
200
	 * Exports a resource
201
	 *
202
	 * @return string|null Output to display
203
	 */
204
	public function export() : ?string
205
	{
206
		$body = null;
207
208
		foreach( $this->getSubClients() as $client ) {
209
			$body .= $client->export();
210
		}
211
212
		return $body;
213
	}
214
215
216
	/**
217
	 * Returns a resource
218
	 *
219
	 * @return string|null Output to display
220
	 */
221
	public function get() : ?string
222
	{
223
		$body = null;
224
		$view = $this->view();
225
226
		foreach( $this->getSubClients() as $idx => $client )
227
		{
228
			$view->tabindex = ++$idx + 1;
229
			$body .= $client->get();
230
		}
231
232
		return $body;
233
	}
234
235
236
	/**
237
	 * Imports a resource
238
	 *
239
	 * @return string|null Output to display
240
	 * @deprecated 2021.01
241
	 */
242
	public function import() : ?string
243
	{
244
		$body = null;
245
246
		foreach( $this->getSubClients() as $client ) {
247
			$body .= $client->import();
248
		}
249
250
		return null;
251
	}
252
253
254
	/**
255
	 * Saves the data
256
	 *
257
	 * @return string|null Output to display
258
	 */
259
	public function save() : ?string
260
	{
261
		$body = null;
262
263
		foreach( $this->getSubClients() as $client ) {
264
			$body .= $client->save();
265
		}
266
267
		return $body;
268
	}
269
270
271
	/**
272
	 * Returns a list of resource according to the conditions
273
	 *
274
	 * @return string|null Output to display
275
	 */
276
	public function search() : ?string
277
	{
278
		$body = null;
279
280
		foreach( $this->getSubClients() as $client ) {
281
			$body .= $client->search();
282
		}
283
284
		return $body;
285
	}
286
287
288
	/**
289
	 * Returns the PSR-7 response object for the request
290
	 *
291
	 * @return \Psr\Http\Message\ResponseInterface Response object
292
	 */
293
	public function response() : \Psr\Http\Message\ResponseInterface
294
	{
295
		return $this->view()->response();
296
	}
297
298
299
	/**
300
	 * Adds the decorators to the client object
301
	 *
302
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
303
	 * @param array $decorators List of decorator name that should be wrapped around the client
304
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\"
305
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
306
	 */
307
	protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Admin\JQAdm\Iface
308
	{
309
		foreach( $decorators as $name )
310
		{
311
			$classname = $classprefix . $name;
312
313
			if( ctype_alnum( $name ) === false )
314
			{
315
				$msg = $this->context->translate( 'admin', 'Invalid class name "%1$s"' );
316
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
317
			}
318
319
			if( class_exists( $classname ) === false )
320
			{
321
				$msg = $this->context->translate( 'admin', 'Class "%1$s" not found' );
322
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
323
			}
324
325
			$client = new $classname( $client, $this->context );
326
327
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\Iface', $client );
328
		}
329
330
		return $client;
331
	}
332
333
334
	/**
335
	 * Adds the decorators to the client object
336
	 *
337
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
338
	 * @param string $path Admin string in lower case, e.g. "catalog/detail/basic"
339
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
340
	 */
341
	protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, string $path ) : \Aimeos\Admin\JQAdm\Iface
342
	{
343
		if( !is_string( $path ) || $path === '' )
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
344
		{
345
			$msg = $this->context->translate( 'admin', 'Invalid domain "%1$s"' );
346
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $path ) );
347
		}
348
349
		$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) );
350
		$config = $this->context->config();
351
352
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
353
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] );
354
		$client = $this->addDecorators( $client, $decorators, $classprefix );
355
356
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\';
357
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] );
358
		$client = $this->addDecorators( $client, $decorators, $classprefix );
359
360
		return $client;
361
	}
362
363
364
	/**
365
	 * Modifiy several items at once
366
	 *
367
	 * @param string $domain Data domain of the items
368
	 * @param string|null $resource Resource name or null for domain name
369
	 * @return string|null Output to display
370
	 */
371
	protected function batchBase( string $domain, string $resource = null ) : ?string
372
	{
373
		$view = $this->view();
374
375
		if( !empty( $ids = $view->param( 'id' ) ) )
376
		{
377
			$manager = \Aimeos\MShop::create( $this->context(), $domain );
378
			$filter = $manager->filter()->add( [str_replace( '/', '.', $domain ) . '.id' => $ids] )->slice( 0, count( $ids ) );
379
			$items = $manager->search( $filter, $this->getDomains() );
380
381
			$data = $view->param( 'item', [] );
382
383
			foreach( $items as $item ) {
384
				$temp = $data; $item->fromArray( $temp, true );
385
			}
386
387
			$view->items = $items;
388
389
			foreach( $this->getSubClients() as $client ) {
390
				$client->batch();
391
			}
392
393
			$manager->save( $items );
394
		}
395
396
		return $this->redirect( $resource ?: $domain, 'search', null, 'save' );
397
	}
398
399
400
	/**
401
	 * Returns the sub-client given by its name.
402
	 *
403
	 * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree)
404
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
405
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-part object
406
	 */
407
	protected function createSubClient( string $path, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
408
	{
409
		$path = strtolower( $path );
410
411
		if( $name === null ) {
412
			$name = $this->context->config()->get( 'admin/jqadm/' . $path . '/name', 'Standard' );
413
		}
414
415
		if( empty( $name ) || ctype_alnum( $name ) === false )
416
		{
417
			$msg = $this->context->translate( 'admin', 'Invalid characters in client name "%1$s"' );
418
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $name ) );
419
		}
420
421
		$subnames = str_replace( '/', '\\', ucwords( $path, '/' ) );
422
		$classname = '\\Aimeos\\Admin\\JQAdm\\' . $subnames . '\\' . $name;
423
424
		if( class_exists( $classname ) === false )
425
		{
426
			$msg = $this->context->translate( 'admin', 'Class "%1$s" not available' );
427
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
428
		}
429
430
		$object = new $classname( $this->context );
431
		$object = \Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Iface', $object );
432
		$object = $this->addClientDecorators( $object, $path );
433
434
		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

434
		return $object->/** @scrutinizer ignore-call */ setObject( $object )->setAimeos( $this->aimeos )->setView( $this->view );
Loading history...
435
	}
436
437
438
	/**
439
	 * Returns the value for the given key in the array
440
	 *
441
	 * @param array $values Multi-dimensional associative list of key/value pairs
442
	 * @param string $key Parameter key like "name" or "list/test" for associative arrays
443
	 * @param mixed $default Returned value if no one for key is available
444
	 * @return mixed Value from the array or default value if not present in array
445
	 */
446
	protected function val( array $values, $key, $default = null )
447
	{
448
		foreach( explode( '/', trim( $key, '/' ) ) as $part )
449
		{
450
			if( is_array( $values ) && isset( $values[$part] ) ) {
451
				$values = $values[$part];
452
			} else {
453
				return $default;
454
			}
455
		}
456
457
		return $values;
458
	}
459
460
461
	/**
462
	 * Returns the known client parameters and their values
463
	 *
464
	 * @param array $names List of parameter names
465
	 * @return array Associative list of parameters names as key and their values
466
	 */
467
	protected function getClientParams( $names = ['id', 'resource', 'site', 'locale'] ) : array
468
	{
469
		$list = [];
470
471
		foreach( $names as $name )
472
		{
473
			if( ( $val = $this->view->param( $name ) ) !== null && !is_array( $val ) ) {
474
				$list[$name] = $val;
475
			}
476
		}
477
478
		return $list;
479
	}
480
481
482
	/**
483
	 * Returns the domain names whose items should be fetched too
484
	 *
485
	 * @return string[] List of domain names
486
	 */
487
	protected function getDomains() : array
488
	{
489
		return [];
490
	}
491
492
493
	/**
494
	 * Returns the context object.
495
	 *
496
	 * @return \Aimeos\MShop\ContextIface Context object
497
	 */
498
	protected function context() : \Aimeos\MShop\ContextIface
499
	{
500
		return $this->context;
501
	}
502
503
504
	/**
505
	 * Returns the available class names without namespace that are stored in the given path
506
	 *
507
	 * @param string $relpath Path relative to the include paths
508
	 * @param string[] $excludes List of file names to execlude
509
	 * @return string[] List of available class names
510
	 */
511
	protected function getClassNames( string $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] ) : array
512
	{
513
		$list = [];
514
515
		foreach( $this->getAimeos()->getIncludePaths() as $path )
516
		{
517
			$path .= DIRECTORY_SEPARATOR . $relpath;
518
519
			if( is_dir( $path ) )
520
			{
521
				foreach( new \DirectoryIterator( $path ) as $entry )
522
				{
523
					if( $entry->isFile() && !in_array( $entry->getFileName(), $excludes ) ) {
524
						$list[] = pathinfo( $entry->getFileName(), PATHINFO_FILENAME );
525
					}
526
				}
527
			}
528
		}
529
530
		sort( $list );
531
		return $list;
532
	}
533
534
535
	/**
536
	 * Returns the array of criteria conditions based on the given parameters
537
	 *
538
	 * @param array $params List of criteria data with condition, sorting and paging
539
	 * @return array Multi-dimensional associative list of criteria conditions
540
	 */
541
	protected function getCriteriaConditions( array $params ) : array
542
	{
543
		$expr = [];
544
545
		if( isset( $params['key'] ) )
546
		{
547
			foreach( (array) $params['key'] as $idx => $key )
548
			{
549
				if( $key != '' && isset( $params['op'][$idx] ) && $params['op'][$idx] != ''
550
					&& isset( $params['val'][$idx] ) && $params['val'][$idx] != ''
551
				) {
552
					$expr[] = [$params['op'][$idx] => [$key => $params['val'][$idx]]];
553
				}
554
			}
555
556
			if( !empty( $expr ) ) {
557
				$expr = ['&&' => $expr];
558
			}
559
		}
560
561
		return $expr;
562
	}
563
564
565
	/**
566
	 * Returns the outer decoratorator of the object
567
	 *
568
	 * @return \Aimeos\Admin\JQAdm\Iface Outmost object
569
	 */
570
	protected function object() : Iface
571
	{
572
		if( isset( $this->object ) ) {
573
			return $this->object;
574
		}
575
576
		return $this;
577
	}
578
579
580
	/**
581
	 * Returns the sub-client given by its name.
582
	 *
583
	 * @param string $type Name of the client type
584
	 * @param string|null $name Name of the sub-client (Default if null)
585
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
586
	 */
587
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
588
	{
589
		$msg = $this->context()->translate( 'admin', 'Not implemented' );
590
		throw new \Aimeos\Admin\JQAdm\Exception( $msg );
591
	}
592
593
594
	/**
595
	 * Returns the configured sub-clients or the ones named in the default parameter if none are configured.
596
	 *
597
	 * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names
598
	 */
599
	protected function getSubClients() : array
600
	{
601
		if( !isset( $this->subclients ) )
602
		{
603
			$this->subclients = [];
604
605
			foreach( $this->getSubClientNames() as $name ) {
606
				$this->subclients[] = $this->getSubClient( $name );
607
			}
608
		}
609
610
		return $this->subclients;
611
	}
612
613
614
	/**
615
	 * Returns the list of sub-client names configured for the client.
616
	 *
617
	 * @return array List of admin client names
618
	 */
619
	protected function getSubClientNames() : array
620
	{
621
		return [];
622
	}
623
624
625
	/**
626
	 * Initializes the criteria object based on the given parameter
627
	 *
628
	 * @param \Aimeos\Base\Criteria\Iface $criteria Criteria object
629
	 * @param array $params List of criteria data with condition, sorting and paging
630
	 * @return \Aimeos\Base\Criteria\Iface Initialized criteria object
631
	 */
632
	protected function initCriteria( \Aimeos\Base\Criteria\Iface $criteria, array $params ) : \Aimeos\Base\Criteria\Iface
633
	{
634
		if( isset( $params['sort'] ) && !empty( $params['sort'] ) ) {
635
			$criteria->order( $params['sort'] );
636
		}
637
638
		return $criteria->slice( $params['page']['offset'] ?? 0, $params['page']['limit'] ?? 25 )
639
			->add( $criteria->parse( $this->getCriteriaConditions( $params['filter'] ?? [] ) ) );
640
	}
641
642
643
	/**
644
	 * Flattens the nested configuration array
645
	 *
646
	 * @param array $config Multi-dimensional list of key/value pairs
647
	 * @param string $path Path of keys separated by slashes (/) to add new values for
648
	 * @return array List of arrays with "key" and "val" keys
649
	 */
650
	protected function flatten( array $config, string $path = '' ) : array
651
	{
652
		$list = [];
653
654
		foreach( $config as $key => $val )
655
		{
656
			if( is_array( $val ) ) {
657
				$list = array_merge( $list, $this->flatten( $val, $path . '/' . $key ) );
658
			} else {
659
				$list[] = ['key' => trim( $path . '/' . $key, '/' ), 'val' => $val];
660
			}
661
		}
662
663
		return $list;
664
	}
665
666
667
	/**
668
	 * Writes the exception details to the log
669
	 *
670
	 * @param \Exception $e Exception object
671
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
672
	 */
673
	protected function log( \Exception $e ) : Iface
674
	{
675
		$msg = $e->getMessage() . PHP_EOL;
676
677
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception ) {
678
			$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

678
			$msg .= /** @scrutinizer ignore-type */ print_r( $e->getDetails(), true ) . PHP_EOL;
Loading history...
679
		}
680
681
		$this->context->logger()->error( $msg . $e->getTraceAsString(), 'admin/jqadm' );
682
683
		return $this;
684
	}
685
686
687
	/**
688
	 * Adds a redirect to the response for the next action
689
	 *
690
	 * @param string $resource Resource name
691
	 * @param string|null $action Next action
692
	 * @param string|null $id ID of the next resource item
693
	 * @param string|null $method Current method name
694
	 * @param array $params URL parameters to use
695
	 * @return string|null Returns value for the actions
696
	 */
697
	protected function redirect( string $resource, ?string $action, string $id = null,
698
		string $method = null, array $params = [] ) : ?string
699
	{
700
		$params += $this->getClientParams();
701
		$context = $this->context();
702
		$view = $this->view();
703
704
		$params['resource'] = $resource;
705
		unset( $params['id'] );
706
707
		switch( $action )
708
		{
709
			case 'search':
710
				$url = $view->link( 'admin/jqadm/url/search', $params ); break;
711
			case 'create':
712
				$url = $view->link( 'admin/jqadm/url/create', ['parentid' => $id] + $params ); break;
713
			case 'copy':
714
				$url = $view->link( 'admin/jqadm/url/copy', ['id' => $id] + $params ); break;
715
			default:
716
				$url = $view->link( 'admin/jqadm/url/get', ['id' => $id] + $params );
717
		}
718
719
		switch( $method )
720
		{
721
			case 'save':
722
				$context->session()->set( 'info', [$context->translate( 'admin', 'Item saved successfully' )] ); break;
723
			case 'delete':
724
				$context->session()->set( 'info', [$context->translate( 'admin', 'Item deleted successfully' )] ); break;
725
		}
726
727
		$view->response()->withStatus( 302 );
728
		$view->response()->withHeader( 'Location', $url );
729
		$view->response()->withHeader( 'Cache-Control', 'no-store' );
730
731
		return null;
732
	}
733
734
735
	/**
736
	 * Writes the exception details to the log
737
	 *
738
	 * @param \Exception $e Exception object
739
	 * @param string $method Method it's called from
740
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
741
	 */
742
	protected function report( \Exception $e, string $method ) : Iface
743
	{
744
		$view = $this->view;
745
		$i18n = $this->context->i18n();
746
747
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception )
748
		{
749
			$view->errors = array_merge( $view->get( 'errors', [] ), [$e->getMessage()] );
750
			return $this->log( $e );
751
		}
752
		elseif( $e instanceof \Aimeos\MShop\Exception )
753
		{
754
			$view->errors = array_merge( $view->get( 'errors', [] ), [$i18n->dt( 'mshop', $e->getMessage() )] );
755
			return $this->log( $e );
756
		}
757
758
		switch( $method )
759
		{
760
			case 'save': $msg = $i18n->dt( 'admin', 'Error saving data' ); break;
761
			case 'delete': $msg = $i18n->dt( 'admin', 'Error deleting data' ); break;
762
			default: $msg = $i18n->dt( 'admin', 'Error retrieving data' ); break;
763
		}
764
765
		$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
766
767
		return $this->log( $e );
768
	}
769
770
771
	/**
772
	 * Checks and returns the request parameter for the given name
773
	 *
774
	 * @param string $name Name of the request parameter, can be a path like 'page/limit'
775
	 * @return mixed Parameter value
776
	 * @throws \Aimeos\Admin\JQAdm\Exception If the parameter is missing
777
	 */
778
	protected function require( string $name )
779
	{
780
		if( ( $value = $this->view()->param( $name ) ) !== null ) {
781
			return $value;
782
		}
783
784
		$msg = $this->context->translate( 'admin', 'Required parameter "%1$s" is missing' );
785
		throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $name ) );
786
	}
787
788
789
	/**
790
	 * Stores and returns the parameters used for searching items
791
	 *
792
	 * @param array $params GET/POST parameter set
793
	 * @param string $name Name of the panel/subpanel
794
	 * @return array Associative list of parameters for searching items
795
	 */
796
	protected function storeFilter( array $params, string $name ) : array
797
	{
798
		$key = 'aimeos/admin/jqadm/' . $name;
799
		$session = $this->context()->session();
800
801
		foreach( ['fields', 'filter', 'page', 'sort'] as $part )
802
		{
803
			if( isset( $params[$part] ) ) {
804
				$session->set( $key . '/' . $part, $params[$part] );
805
			}
806
		}
807
808
		return [
809
			'fields' => $session->get( $key . '/fields' ),
810
			'filter' => $session->get( $key . '/filter' ),
811
			'page' => $session->get( $key . '/page' ),
812
			'sort' => $session->get( $key . '/sort' ),
813
		];
814
	}
815
816
817
	/**
818
	 * Throws an exception with given details
819
	 *
820
	 * @param array $errors List of key/message pairs of errors
821
	 * @throws \Aimeos\Admin\JQAdm\Exception Exception with error details
822
	 */
823
	protected function notify( array $errors ) : Iface
824
	{
825
		$list = [];
826
		$i18n = $this->context->i18n();
827
828
		foreach( $errors as $key => $error )
829
		{
830
			if( $error ) {
831
				$list[] = $key . ': ' . $i18n->dt( 'mshop', $error );
832
			}
833
		}
834
835
		if( !empty( $list ) ) {
836
			throw new \Aimeos\Admin\JQAdm\Exception( join( "\n", $list ) );
837
		}
838
839
		return $this;
840
	}
841
}
842