|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2020 |
|
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
|
|
|
private $object; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Initializes the class instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->context = $context; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Catch unknown methods |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name Name of the method |
|
47
|
|
|
* @param array $param List of method parameter |
|
48
|
|
|
* @throws \Aimeos\Admin\JQAdm\Exception If method call failed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __call( string $name, array $param ) |
|
51
|
|
|
{ |
|
52
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Unable to call method "%1$s"', $name ) ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Adds the required data used in the attribute template |
|
58
|
|
|
* |
|
59
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
|
60
|
|
|
* @return \Aimeos\MW\View\Iface View object with assigned parameters |
|
61
|
|
|
*/ |
|
62
|
|
|
public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
|
63
|
|
|
{ |
|
64
|
|
|
return $view; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the Aimeos bootstrap object |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Aimeos\Bootstrap The Aimeos bootstrap object |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getAimeos() : \Aimeos\Bootstrap |
|
74
|
|
|
{ |
|
75
|
|
|
if( !isset( $this->aimeos ) ) { |
|
76
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Aimeos object not available' ) ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->aimeos; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets the Aimeos bootstrap object |
|
85
|
|
|
* |
|
86
|
|
|
* @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object |
|
87
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setAimeos( \Aimeos\Bootstrap $aimeos ) : \Aimeos\Admin\JQAdm\Iface |
|
90
|
|
|
{ |
|
91
|
|
|
$this->aimeos = $aimeos; |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Makes the outer decorator object available to inner objects |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Aimeos\Admin\JQAdm\Iface $object Outmost object |
|
100
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Same object for fluent interface |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setObject( \Aimeos\Admin\JQAdm\Iface $object ) : \Aimeos\Admin\JQAdm\Iface |
|
103
|
|
|
{ |
|
104
|
|
|
$this->object = $object; |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the view object that will generate the admin output. |
|
111
|
|
|
* |
|
112
|
|
|
* @return \Aimeos\MW\View\Iface The view object which generates the admin output |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getView() : \Aimeos\MW\View\Iface |
|
115
|
|
|
{ |
|
116
|
|
|
if( !isset( $this->view ) ) { |
|
117
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'No view available' ) ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->view; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Sets the view object that will generate the admin output. |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Aimeos\MW\View\Iface $view The view object which generates the admin output |
|
128
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Admin\JQAdm\Iface |
|
131
|
|
|
{ |
|
132
|
|
|
$this->view = $view; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Copies a resource |
|
139
|
|
|
* |
|
140
|
|
|
* @return string|null Output to display |
|
141
|
|
|
*/ |
|
142
|
|
|
public function copy() : ?string |
|
143
|
|
|
{ |
|
144
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
145
|
|
|
$client->copy(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Creates a new resource |
|
154
|
|
|
* |
|
155
|
|
|
* @return string|null Output to display |
|
156
|
|
|
*/ |
|
157
|
|
|
public function create() : ?string |
|
158
|
|
|
{ |
|
159
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
160
|
|
|
$client->create(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return null; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Deletes a resource |
|
169
|
|
|
* |
|
170
|
|
|
* @return string|null Output to display |
|
171
|
|
|
*/ |
|
172
|
|
|
public function delete() : ?string |
|
173
|
|
|
{ |
|
174
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
175
|
|
|
$client->delete(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Exports a resource |
|
184
|
|
|
* |
|
185
|
|
|
* @return string|null Output to display |
|
186
|
|
|
*/ |
|
187
|
|
|
public function export() : ?string |
|
188
|
|
|
{ |
|
189
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
190
|
|
|
$client->export(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return null; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns a resource |
|
199
|
|
|
* |
|
200
|
|
|
* @return string|null Output to display |
|
201
|
|
|
*/ |
|
202
|
|
|
public function get() : ?string |
|
203
|
|
|
{ |
|
204
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
205
|
|
|
$client->get(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return null; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Imports a resource |
|
214
|
|
|
* |
|
215
|
|
|
* @return string|null Output to display |
|
216
|
|
|
*/ |
|
217
|
|
|
public function import() : ?string |
|
218
|
|
|
{ |
|
219
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
220
|
|
|
$client->import(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return null; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Saves the data |
|
229
|
|
|
* |
|
230
|
|
|
* @return string|null Output to display |
|
231
|
|
|
*/ |
|
232
|
|
|
public function save() : ?string |
|
233
|
|
|
{ |
|
234
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
235
|
|
|
$client->save(); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return null; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Returns a list of resource according to the conditions |
|
244
|
|
|
* |
|
245
|
|
|
* @return string|null Output to display |
|
246
|
|
|
*/ |
|
247
|
|
|
public function search() : ?string |
|
248
|
|
|
{ |
|
249
|
|
|
foreach( $this->getSubClients() as $client ) { |
|
250
|
|
|
$client->search(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return null; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Adds the decorators to the client object |
|
259
|
|
|
* |
|
260
|
|
|
* @param \Aimeos\Admin\JQAdm\Iface $client Admin object |
|
261
|
|
|
* @param array $decorators List of decorator name that should be wrapped around the client |
|
262
|
|
|
* @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\" |
|
263
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Admin object |
|
264
|
|
|
*/ |
|
265
|
|
|
protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Admin\JQAdm\Iface |
|
266
|
|
|
{ |
|
267
|
|
|
foreach( $decorators as $name ) |
|
268
|
|
|
{ |
|
269
|
|
|
if( ctype_alnum( $name ) === false ) |
|
270
|
|
|
{ |
|
271
|
|
|
$classname = is_string( $name ) ? $classprefix . $name : '<not a string>'; |
|
272
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) ); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$classname = $classprefix . $name; |
|
276
|
|
|
|
|
277
|
|
|
if( class_exists( $classname ) === false ) { |
|
278
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ) ); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$client = new $classname( $client, $this->context ); |
|
282
|
|
|
|
|
283
|
|
|
\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\Iface', $client ); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $client; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Adds the decorators to the client object |
|
292
|
|
|
* |
|
293
|
|
|
* @param \Aimeos\Admin\JQAdm\Iface $client Admin object |
|
294
|
|
|
* @param string $path Admin string in lower case, e.g. "catalog/detail/basic" |
|
295
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Admin object |
|
296
|
|
|
*/ |
|
297
|
|
|
protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, string $path ) : \Aimeos\Admin\JQAdm\Iface |
|
298
|
|
|
{ |
|
299
|
|
|
if( !is_string( $path ) || $path === '' ) { |
|
|
|
|
|
|
300
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid domain "%1$s"', $path ) ); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
$localClass = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) ); |
|
304
|
|
|
$config = $this->context->getConfig(); |
|
305
|
|
|
|
|
306
|
|
|
$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\'; |
|
307
|
|
|
$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] ); |
|
308
|
|
|
$client = $this->addDecorators( $client, $decorators, $classprefix ); |
|
309
|
|
|
|
|
310
|
|
|
$classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\'; |
|
311
|
|
|
$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] ); |
|
312
|
|
|
$client = $this->addDecorators( $client, $decorators, $classprefix ); |
|
313
|
|
|
|
|
314
|
|
|
return $client; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Returns the sub-client given by its name. |
|
320
|
|
|
* |
|
321
|
|
|
* @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree) |
|
322
|
|
|
* @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
|
323
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Sub-part object |
|
324
|
|
|
*/ |
|
325
|
|
|
protected function createSubClient( string $path, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
|
326
|
|
|
{ |
|
327
|
|
|
$path = strtolower( $path ); |
|
328
|
|
|
|
|
329
|
|
|
if( $name === null ) { |
|
330
|
|
|
$name = $this->context->getConfig()->get( 'admin/jqadm/' . $path . '/name', 'Standard' ); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
if( empty( $name ) || ctype_alnum( $name ) === false ) { |
|
334
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid characters in client name "%1$s"', $name ) ); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$subnames = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) ); |
|
338
|
|
|
|
|
339
|
|
|
$classname = '\\Aimeos\\Admin\\JQAdm\\' . $subnames . '\\' . $name; |
|
340
|
|
|
|
|
341
|
|
|
if( class_exists( $classname ) === false ) { |
|
342
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
$object = new $classname( $this->context ); |
|
346
|
|
|
$object = \Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Iface', $object ); |
|
347
|
|
|
$object = $this->addClientDecorators( $object, $path ); |
|
348
|
|
|
|
|
349
|
|
|
return $object->setObject( $object )->setAimeos( $this->aimeos )->setView( $this->view ); |
|
|
|
|
|
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Returns the value for the given key in the array |
|
355
|
|
|
* |
|
356
|
|
|
* @param array $values Multi-dimensional associative list of key/value pairs |
|
357
|
|
|
* @param string $key Parameter key like "name" or "list/test" for associative arrays |
|
358
|
|
|
* @param mixed $default Returned value if no one for key is available |
|
359
|
|
|
* @return mixed Value from the array or default value if not present in array |
|
360
|
|
|
*/ |
|
361
|
|
|
protected function getValue( array $values, $key, $default = null ) |
|
362
|
|
|
{ |
|
363
|
|
|
foreach( explode( '/', trim( $key, '/' ) ) as $part ) |
|
364
|
|
|
{ |
|
365
|
|
|
if( isset( $values[$part] ) ) { |
|
366
|
|
|
$values = $values[$part]; |
|
367
|
|
|
} else { |
|
368
|
|
|
return $default; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
return $values; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* Returns the known client parameters and their values |
|
378
|
|
|
* |
|
379
|
|
|
* @param array $names List of parameter names |
|
380
|
|
|
* @return array Associative list of parameters names as key and their values |
|
381
|
|
|
*/ |
|
382
|
|
|
protected function getClientParams( $names = ['id', 'resource', 'site', 'lang'] ) : array |
|
383
|
|
|
{ |
|
384
|
|
|
$list = []; |
|
385
|
|
|
|
|
386
|
|
|
foreach( $names as $name ) |
|
387
|
|
|
{ |
|
388
|
|
|
if( ( $val = $this->view->param( $name ) ) !== null ) { |
|
389
|
|
|
$list[$name] = $val; |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
return $list; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Returns the context object. |
|
399
|
|
|
* |
|
400
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Context object |
|
401
|
|
|
*/ |
|
402
|
|
|
protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
|
403
|
|
|
{ |
|
404
|
|
|
return $this->context; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Returns the list of sub-client names configured for the client. |
|
410
|
|
|
* |
|
411
|
|
|
* @return array List of admin client names |
|
412
|
|
|
*/ |
|
413
|
|
|
abstract protected function getSubClientNames() : array; |
|
414
|
|
|
|
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* Returns the available class names without namespace that are stored in the given path |
|
418
|
|
|
* |
|
419
|
|
|
* @param string $relpath Path relative to the include paths |
|
420
|
|
|
* @param string[] $excludes List of file names to execlude |
|
421
|
|
|
* @return string[] List of available class names |
|
422
|
|
|
*/ |
|
423
|
|
|
protected function getClassNames( string $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] ) : array |
|
424
|
|
|
{ |
|
425
|
|
|
$list = []; |
|
426
|
|
|
|
|
427
|
|
|
foreach( $this->getAimeos()->getIncludePaths() as $path ) |
|
428
|
|
|
{ |
|
429
|
|
|
$path .= DIRECTORY_SEPARATOR . $relpath; |
|
430
|
|
|
|
|
431
|
|
|
if( is_dir( $path ) ) |
|
432
|
|
|
{ |
|
433
|
|
|
foreach( new \DirectoryIterator( $path ) as $entry ) |
|
434
|
|
|
{ |
|
435
|
|
|
if( $entry->isFile() && !in_array( $entry->getFileName(), $excludes ) ) { |
|
436
|
|
|
$list[] = pathinfo( $entry->getFileName(), PATHINFO_FILENAME ); |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
sort( $list ); |
|
443
|
|
|
return $list; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* Returns the array of criteria conditions based on the given parameters |
|
449
|
|
|
* |
|
450
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
|
451
|
|
|
* @return array Multi-dimensional associative list of criteria conditions |
|
452
|
|
|
*/ |
|
453
|
|
|
protected function getCriteriaConditions( array $params ) : array |
|
454
|
|
|
{ |
|
455
|
|
|
$expr = []; |
|
456
|
|
|
|
|
457
|
|
|
if( isset( $params['key'] ) ) |
|
458
|
|
|
{ |
|
459
|
|
|
foreach( (array) $params['key'] as $idx => $key ) |
|
460
|
|
|
{ |
|
461
|
|
|
if( $key != '' && isset( $params['op'][$idx] ) && $params['op'][$idx] != '' |
|
462
|
|
|
&& isset( $params['val'][$idx] ) && $params['val'][$idx] != '' |
|
463
|
|
|
) { |
|
464
|
|
|
$expr[] = [$params['op'][$idx] => [$key => $params['val'][$idx]]]; |
|
465
|
|
|
} |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
if( !empty( $expr ) ) { |
|
469
|
|
|
$expr = ['&&' => $expr]; |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
return $expr; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* Returns the array of criteria sortations based on the given parameters |
|
479
|
|
|
* |
|
480
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
|
481
|
|
|
* @return array Associative list of criteria sortations |
|
482
|
|
|
*/ |
|
483
|
|
|
protected function getCriteriaSortations( array $params ) : array |
|
484
|
|
|
{ |
|
485
|
|
|
$sortation = []; |
|
486
|
|
|
|
|
487
|
|
|
foreach( $params as $sort ) |
|
488
|
|
|
{ |
|
489
|
|
|
if( $sort[0] === '-' ) { |
|
490
|
|
|
$sortation[substr( $sort, 1 )] = '-'; |
|
491
|
|
|
} else { |
|
492
|
|
|
$sortation[$sort] = '+'; |
|
493
|
|
|
} |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
return $sortation; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Returns the outer decoratorator of the object |
|
502
|
|
|
* |
|
503
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Outmost object |
|
504
|
|
|
*/ |
|
505
|
|
|
protected function getObject() : Iface |
|
506
|
|
|
{ |
|
507
|
|
|
if( isset( $this->object ) ) { |
|
508
|
|
|
return $this->object; |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
return $this; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Returns the configured sub-clients or the ones named in the default parameter if none are configured. |
|
517
|
|
|
* |
|
518
|
|
|
* @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names |
|
519
|
|
|
*/ |
|
520
|
|
|
protected function getSubClients() : array |
|
521
|
|
|
{ |
|
522
|
|
|
if( !isset( $this->subclients ) ) |
|
523
|
|
|
{ |
|
524
|
|
|
$this->subclients = []; |
|
525
|
|
|
|
|
526
|
|
|
foreach( $this->getSubClientNames() as $name ) { |
|
527
|
|
|
$this->subclients[] = $this->getSubClient( $name ); |
|
528
|
|
|
} |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
return $this->subclients; |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* Initializes the criteria object based on the given parameter |
|
537
|
|
|
* |
|
538
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
|
539
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
|
540
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
|
541
|
|
|
*/ |
|
542
|
|
|
protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
|
543
|
|
|
{ |
|
544
|
|
|
if( isset( $params['filter'] ) ) { |
|
545
|
|
|
$criteria = $this->initCriteriaConditions( $criteria, (array) $params['filter'] ); |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
if( isset( $params['sort'] ) ) { |
|
549
|
|
|
$criteria = $this->initCriteriaSortations( $criteria, (array) $params['sort'] ); |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
$page = []; |
|
553
|
|
|
if( isset( $params['page'] ) ) { |
|
554
|
|
|
$page = (array) $params['page']; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
return $this->initCriteriaSlice( $criteria, $page ); |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* Writes the exception details to the log |
|
563
|
|
|
* |
|
564
|
|
|
* @param \Exception $e Exception object |
|
565
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
|
566
|
|
|
*/ |
|
567
|
|
|
protected function log( \Exception $e ) : Iface |
|
568
|
|
|
{ |
|
569
|
|
|
$logger = $this->context->getLogger(); |
|
570
|
|
|
$logger->log( $e->getMessage(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' ); |
|
571
|
|
|
$logger->log( $e->getTraceAsString(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' ); |
|
572
|
|
|
|
|
573
|
|
|
return $this; |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
|
|
577
|
|
|
/** |
|
578
|
|
|
* Returns a map of code/item pairs |
|
579
|
|
|
* |
|
580
|
|
|
* @param \Aimeos\MShop\Common\Item\Type\Iface[] $items Associative list of type items |
|
581
|
|
|
* @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of codes as keys and items as values |
|
582
|
|
|
*/ |
|
583
|
|
|
protected function map( \Aimeos\Map $items ) : array |
|
584
|
|
|
{ |
|
585
|
|
|
$list = []; |
|
586
|
|
|
|
|
587
|
|
|
foreach( $items as $item ) { |
|
588
|
|
|
$list[$item->getCode()] = $item; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
return $list; |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
|
|
595
|
|
|
/** |
|
596
|
|
|
* Adds a redirect to the response for the next action |
|
597
|
|
|
* |
|
598
|
|
|
* @param string $resource Resource name |
|
599
|
|
|
* @param string|null $action Next action |
|
600
|
|
|
* @param string|null $id ID of the next resource item |
|
601
|
|
|
* @param string|null $act Current action name |
|
602
|
|
|
* @return string|null Returns value for the actions |
|
603
|
|
|
*/ |
|
604
|
|
|
protected function redirect( string $resource, ?string $action, string $id = null, |
|
605
|
|
|
string $method = null ) : ?string |
|
606
|
|
|
{ |
|
607
|
|
|
$params = $this->getClientParams(); |
|
608
|
|
|
$context = $this->getContext(); |
|
609
|
|
|
$view = $this->getView(); |
|
610
|
|
|
|
|
611
|
|
|
$params['resource'] = $resource; |
|
612
|
|
|
unset( $params['id'] ); |
|
613
|
|
|
|
|
614
|
|
|
switch( $action ) |
|
615
|
|
|
{ |
|
616
|
|
|
case 'search': |
|
617
|
|
|
$target = $view->config( 'admin/jqadm/url/search/target' ); |
|
618
|
|
|
$cntl = $view->config( 'admin/jqadm/url/search/controller', 'Jqadm' ); |
|
619
|
|
|
$action = $view->config( 'admin/jqadm/url/search/action', 'search' ); |
|
620
|
|
|
$conf = $view->config( 'admin/jqadm/url/search/config', [] ); |
|
621
|
|
|
$url = $view->url( $target, $cntl, $action, $params, [], $conf ); |
|
622
|
|
|
break; |
|
623
|
|
|
case 'create': |
|
624
|
|
|
$params['parentid'] = $id; |
|
625
|
|
|
$target = $view->config( 'admin/jqadm/url/create/target' ); |
|
626
|
|
|
$cntl = $view->config( 'admin/jqadm/url/create/controller', 'Jqadm' ); |
|
627
|
|
|
$action = $view->config( 'admin/jqadm/url/create/action', 'create' ); |
|
628
|
|
|
$conf = $view->config( 'admin/jqadm/url/create/config', [] ); |
|
629
|
|
|
$url = $view->url( $target, $cntl, $action, $params, [], $conf ); |
|
630
|
|
|
break; |
|
631
|
|
|
case 'copy': |
|
632
|
|
|
$target = $view->config( 'admin/jqadm/url/copy/target' ); |
|
633
|
|
|
$cntl = $view->config( 'admin/jqadm/url/copy/controller', 'Jqadm' ); |
|
634
|
|
|
$action = $view->config( 'admin/jqadm/url/copy/action', 'copy' ); |
|
635
|
|
|
$conf = $view->config( 'admin/jqadm/url/copy/config', [] ); |
|
636
|
|
|
$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf ); |
|
637
|
|
|
break; |
|
638
|
|
|
default: |
|
639
|
|
|
$target = $view->config( 'admin/jqadm/url/get/target' ); |
|
640
|
|
|
$cntl = $view->config( 'admin/jqadm/url/get/controller', 'Jqadm' ); |
|
641
|
|
|
$action = $view->config( 'admin/jqadm/url/get/action', 'get' ); |
|
642
|
|
|
$conf = $view->config( 'admin/jqadm/url/get/config', [] ); |
|
643
|
|
|
$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf ); |
|
644
|
|
|
} |
|
645
|
|
|
|
|
646
|
|
|
switch( $method ) |
|
647
|
|
|
{ |
|
648
|
|
|
case 'save': |
|
649
|
|
|
$context->getSession()->set( 'info', [$context->getI18n()->dt( 'admin', 'Item saved successfully' )] ); break; |
|
650
|
|
|
case 'delete': |
|
651
|
|
|
$context->getSession()->set( 'info', [$context->getI18n()->dt( 'admin', 'Item deleted successfully' )] ); break; |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
|
|
$view->response()->withStatus( 302 ); |
|
655
|
|
|
$view->response()->withHeader( 'Location', $url ); |
|
656
|
|
|
$view->response()->withHeader( 'Cache-Control', 'no-store' ); |
|
657
|
|
|
|
|
658
|
|
|
return null; |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
|
|
662
|
|
|
/** |
|
663
|
|
|
* Writes the exception details to the log |
|
664
|
|
|
* |
|
665
|
|
|
* @param \Exception $e Exception object |
|
666
|
|
|
* @param string $method Method it's called from |
|
667
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
|
668
|
|
|
*/ |
|
669
|
|
|
protected function report( \Exception $e, string $method ) : Iface |
|
670
|
|
|
{ |
|
671
|
|
|
$view = $this->view; |
|
672
|
|
|
$i18n = $this->context->getI18n(); |
|
673
|
|
|
|
|
674
|
|
|
if( $e instanceof \Aimeos\Admin\JQAdm\Exception ) |
|
675
|
|
|
{ |
|
676
|
|
|
$view->errors = array_merge( $view->get( 'errors', [] ), [$e->getMessage()] ); |
|
677
|
|
|
return $this; |
|
678
|
|
|
} |
|
679
|
|
|
elseif( $e instanceof \Aimeos\MShop\Exception ) |
|
680
|
|
|
{ |
|
681
|
|
|
$view->errors = array_merge( $view->get( 'errors', [] ), [$i18n->dt( 'mshop', $e->getMessage() )] ); |
|
682
|
|
|
return $this; |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
switch( $method ) |
|
686
|
|
|
{ |
|
687
|
|
|
case 'save': $msg = $i18n->dt( 'admin', 'Error saving data' ); break; |
|
688
|
|
|
case 'delete': $msg = $i18n->dt( 'admin', 'Error deleting data' ); break; |
|
689
|
|
|
default: $msg = $i18n->dt( 'admin', 'Error retrieving data' ); break; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] ); |
|
693
|
|
|
|
|
694
|
|
|
return $this->log( $e ); |
|
695
|
|
|
} |
|
696
|
|
|
|
|
697
|
|
|
|
|
698
|
|
|
/** |
|
699
|
|
|
* Stores and returns the parameters used for searching items |
|
700
|
|
|
* |
|
701
|
|
|
* @param array $params GET/POST parameter set |
|
702
|
|
|
* @param string $name Name of the panel/subpanel |
|
703
|
|
|
* @return array Associative list of parameters for searching items |
|
704
|
|
|
*/ |
|
705
|
|
|
protected function storeSearchParams( array $params, string $name ) : array |
|
706
|
|
|
{ |
|
707
|
|
|
$key = 'aimeos/admin/jqadm/' . $name; |
|
708
|
|
|
$session = $this->getContext()->getSession(); |
|
709
|
|
|
|
|
710
|
|
|
if( isset( $params['filter'] ) ) { |
|
711
|
|
|
$session->set( $key . '/filter', $params['filter'] ); |
|
712
|
|
|
} |
|
713
|
|
|
|
|
714
|
|
|
if( isset( $params['sort'] ) ) { |
|
715
|
|
|
$session->set( $key . '/sort', $params['sort'] ); |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
if( isset( $params['page'] ) ) { |
|
719
|
|
|
$session->set( $key . '/page', $params['page'] ); |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
|
|
if( isset( $params['fields'] ) ) { |
|
723
|
|
|
$session->set( $key . '/fields', $params['fields'] ); |
|
724
|
|
|
} |
|
725
|
|
|
|
|
726
|
|
|
return [ |
|
727
|
|
|
'fields' => $session->get( $key . '/fields' ), |
|
728
|
|
|
'filter' => $session->get( $key . '/filter' ), |
|
729
|
|
|
'page' => $session->get( $key . '/page' ), |
|
730
|
|
|
'sort' => $session->get( $key . '/sort' ), |
|
731
|
|
|
]; |
|
732
|
|
|
} |
|
733
|
|
|
|
|
734
|
|
|
|
|
735
|
|
|
/** |
|
736
|
|
|
* Initializes the criteria object with conditions based on the given parameter |
|
737
|
|
|
* |
|
738
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
|
739
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
|
740
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
|
741
|
|
|
*/ |
|
742
|
|
|
private function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
|
743
|
|
|
{ |
|
744
|
|
|
if( ( $cond = $criteria->toConditions( $this->getCriteriaConditions( $params ) ) ) !== null ) { |
|
745
|
|
|
return $criteria->setConditions( $criteria->combine( '&&', [$cond, $criteria->getConditions()] ) ); |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
|
|
return $criteria; |
|
749
|
|
|
} |
|
750
|
|
|
|
|
751
|
|
|
|
|
752
|
|
|
/** |
|
753
|
|
|
* Initializes the criteria object with the slice based on the given parameter. |
|
754
|
|
|
* |
|
755
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
|
756
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
|
757
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
|
758
|
|
|
*/ |
|
759
|
|
|
private function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
|
760
|
|
|
{ |
|
761
|
|
|
$start = ( isset( $params['offset'] ) ? $params['offset'] : 0 ); |
|
762
|
|
|
$size = ( isset( $params['limit'] ) ? $params['limit'] : 25 ); |
|
763
|
|
|
|
|
764
|
|
|
return $criteria->setSlice( $start, $size ); |
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
|
|
|
|
768
|
|
|
/** |
|
769
|
|
|
* Initializes the criteria object with sortations based on the given parameter |
|
770
|
|
|
* |
|
771
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
|
772
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
|
773
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
|
774
|
|
|
*/ |
|
775
|
|
|
private function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
|
776
|
|
|
{ |
|
777
|
|
|
return $criteria->setSortations( $criteria->toSortations( $this->getCriteriaSortations( $params ) ) ); |
|
778
|
|
|
} |
|
779
|
|
|
} |
|
780
|
|
|
|