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\Product\Selection; |
12
|
|
|
|
13
|
|
|
sprintf( 'selection' ); // for translation |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default implementation of product selection JQAdm client. |
18
|
|
|
* |
19
|
|
|
* @package Admin |
20
|
|
|
* @subpackage JQAdm |
21
|
|
|
*/ |
22
|
|
|
class Standard |
23
|
|
|
extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
24
|
|
|
implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
25
|
|
|
{ |
26
|
|
|
/** admin/jqadm/product/selection/name |
27
|
|
|
* Name of the selection subpart used by the JQAdm product implementation |
28
|
|
|
* |
29
|
|
|
* Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Selection\Myname". |
30
|
|
|
* The name is case-sensitive and you should avoid camel case names like "MyName". |
31
|
|
|
* |
32
|
|
|
* @param string Last part of the JQAdm class name |
33
|
|
|
* @since 2016.04 |
34
|
|
|
* @category Developer |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Copies a resource |
40
|
|
|
* |
41
|
|
|
* @return string|null HTML output |
42
|
|
|
*/ |
43
|
|
|
public function copy() : ?string |
44
|
|
|
{ |
45
|
|
|
$view = $this->getObject()->addData( $this->getView() ); |
46
|
|
|
$view->selectionData = $this->toArray( $view->item, true ); |
47
|
|
|
$view->selectionBody = parent::copy(); |
48
|
|
|
|
49
|
|
|
return $this->render( $view ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates a new resource |
55
|
|
|
* |
56
|
|
|
* @return string|null HTML output |
57
|
|
|
*/ |
58
|
|
|
public function create() : ?string |
59
|
|
|
{ |
60
|
|
|
$view = $this->getObject()->addData( $this->getView() ); |
61
|
|
|
$siteid = $this->getContext()->getLocale()->getSiteId(); |
62
|
|
|
$data = $view->param( 'selection', [] ); |
63
|
|
|
|
64
|
|
|
foreach( $data as $idx => $entry ) |
65
|
|
|
{ |
66
|
|
|
$data[$idx]['product.lists.siteid'] = $siteid; |
67
|
|
|
$data[$idx]['product.siteid'] = $siteid; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$view->selectionData = $data; |
71
|
|
|
$view->selectionBody = parent::create(); |
72
|
|
|
|
73
|
|
|
return $this->render( $view ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns a single resource |
79
|
|
|
* |
80
|
|
|
* @return string|null HTML output |
81
|
|
|
*/ |
82
|
|
|
public function get() : ?string |
83
|
|
|
{ |
84
|
|
|
$view = $this->getObject()->addData( $this->getView() ); |
85
|
|
|
$view->selectionData = $this->toArray( $view->item ); |
86
|
|
|
$view->selectionBody = parent::get(); |
87
|
|
|
|
88
|
|
|
return $this->render( $view ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Saves the data |
94
|
|
|
* |
95
|
|
|
* @return string|null HTML output |
96
|
|
|
*/ |
97
|
|
|
public function save() : ?string |
98
|
|
|
{ |
99
|
|
|
$view = $this->getView(); |
100
|
|
|
|
101
|
|
|
if( $view->item->getType() === 'select' ) |
102
|
|
|
{ |
103
|
|
|
$this->fromArray( $view->item, $view->param( 'selection', [] ) ); |
104
|
|
|
$view->selectionBody = parent::save(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the sub-client given by its name. |
113
|
|
|
* |
114
|
|
|
* @param string $type Name of the client type |
115
|
|
|
* @param string|null $name Name of the sub-client (Default if null) |
116
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
117
|
|
|
*/ |
118
|
|
|
public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
119
|
|
|
{ |
120
|
|
|
/** admin/jqadm/product/selection/decorators/excludes |
121
|
|
|
* Excludes decorators added by the "common" option from the product JQAdm client |
122
|
|
|
* |
123
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
124
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
125
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
126
|
|
|
* modify what is returned to the caller. |
127
|
|
|
* |
128
|
|
|
* This option allows you to remove a decorator added via |
129
|
|
|
* "admin/jqadm/common/decorators/default" before they are wrapped |
130
|
|
|
* around the JQAdm client. |
131
|
|
|
* |
132
|
|
|
* admin/jqadm/product/selection/decorators/excludes = array( 'decorator1' ) |
133
|
|
|
* |
134
|
|
|
* This would remove the decorator named "decorator1" from the list of |
135
|
|
|
* common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
136
|
|
|
* "admin/jqadm/common/decorators/default" to the JQAdm client. |
137
|
|
|
* |
138
|
|
|
* @param array List of decorator names |
139
|
|
|
* @since 2016.01 |
140
|
|
|
* @category Developer |
141
|
|
|
* @see admin/jqadm/common/decorators/default |
142
|
|
|
* @see admin/jqadm/product/selection/decorators/global |
143
|
|
|
* @see admin/jqadm/product/selection/decorators/local |
144
|
|
|
*/ |
145
|
|
|
|
146
|
|
|
/** admin/jqadm/product/selection/decorators/global |
147
|
|
|
* Adds a list of globally available decorators only to the product JQAdm client |
148
|
|
|
* |
149
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
150
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
151
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
152
|
|
|
* modify what is returned to the caller. |
153
|
|
|
* |
154
|
|
|
* This option allows you to wrap global decorators |
155
|
|
|
* ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
156
|
|
|
* |
157
|
|
|
* admin/jqadm/product/selection/decorators/global = array( 'decorator1' ) |
158
|
|
|
* |
159
|
|
|
* This would add the decorator named "decorator1" defined by |
160
|
|
|
* "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
161
|
|
|
* |
162
|
|
|
* @param array List of decorator names |
163
|
|
|
* @since 2016.01 |
164
|
|
|
* @category Developer |
165
|
|
|
* @see admin/jqadm/common/decorators/default |
166
|
|
|
* @see admin/jqadm/product/selection/decorators/excludes |
167
|
|
|
* @see admin/jqadm/product/selection/decorators/local |
168
|
|
|
*/ |
169
|
|
|
|
170
|
|
|
/** admin/jqadm/product/selection/decorators/local |
171
|
|
|
* Adds a list of local decorators only to the product JQAdm client |
172
|
|
|
* |
173
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
174
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
175
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
176
|
|
|
* modify what is returned to the caller. |
177
|
|
|
* |
178
|
|
|
* This option allows you to wrap local decorators |
179
|
|
|
* ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client. |
180
|
|
|
* |
181
|
|
|
* admin/jqadm/product/selection/decorators/local = array( 'decorator2' ) |
182
|
|
|
* |
183
|
|
|
* This would add the decorator named "decorator2" defined by |
184
|
|
|
* "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client. |
185
|
|
|
* |
186
|
|
|
* @param array List of decorator names |
187
|
|
|
* @since 2016.01 |
188
|
|
|
* @category Developer |
189
|
|
|
* @see admin/jqadm/common/decorators/default |
190
|
|
|
* @see admin/jqadm/product/selection/decorators/excludes |
191
|
|
|
* @see admin/jqadm/product/selection/decorators/global |
192
|
|
|
*/ |
193
|
|
|
return $this->createSubClient( 'product/selection/' . $type, $name ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the list of sub-client names configured for the client. |
199
|
|
|
* |
200
|
|
|
* @return array List of JQAdm client names |
201
|
|
|
*/ |
202
|
|
|
protected function getSubClientNames() : array |
203
|
|
|
{ |
204
|
|
|
/** admin/jqadm/product/selection/subparts |
205
|
|
|
* List of JQAdm sub-clients rendered within the product selection section |
206
|
|
|
* |
207
|
|
|
* The output of the frontend is composed of the code generated by the JQAdm |
208
|
|
|
* clients. Each JQAdm client can consist of serveral (or none) sub-clients |
209
|
|
|
* that are responsible for rendering certain sub-parts of the output. The |
210
|
|
|
* sub-clients can contain JQAdm clients themselves and therefore a |
211
|
|
|
* hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
212
|
|
|
* the output that is placed inside the container of its parent. |
213
|
|
|
* |
214
|
|
|
* At first, always the JQAdm code generated by the parent is printed, then |
215
|
|
|
* the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
216
|
|
|
* determines the order of the output of these sub-clients inside the parent |
217
|
|
|
* container. If the configured list of clients is |
218
|
|
|
* |
219
|
|
|
* array( "subclient1", "subclient2" ) |
220
|
|
|
* |
221
|
|
|
* you can easily change the order of the output by reordering the subparts: |
222
|
|
|
* |
223
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
224
|
|
|
* |
225
|
|
|
* You can also remove one or more parts if they shouldn't be rendered: |
226
|
|
|
* |
227
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1" ) |
228
|
|
|
* |
229
|
|
|
* As the clients only generates structural JQAdm, the layout defined via CSS |
230
|
|
|
* should support adding, removing or reordering content by a fluid like |
231
|
|
|
* design. |
232
|
|
|
* |
233
|
|
|
* @param array List of sub-client names |
234
|
|
|
* @since 2016.01 |
235
|
|
|
* @category Developer |
236
|
|
|
*/ |
237
|
|
|
return $this->getContext()->getConfig()->get( 'admin/jqadm/product/selection/subparts', [] ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Creates new and updates existing items using the data array |
243
|
|
|
* |
244
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items |
245
|
|
|
* @param array $data Data array |
246
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface Modified product item |
247
|
|
|
*/ |
248
|
|
|
protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface |
249
|
|
|
{ |
250
|
|
|
$context = $this->getContext(); |
251
|
|
|
$manager = \Aimeos\MShop::create( $context, 'product' ); |
252
|
|
|
$listManager = \Aimeos\MShop::create( $context, 'product/lists' ); |
253
|
|
|
|
254
|
|
|
$prodIds = map( $data )->col( 'product.id' )->toArray(); |
255
|
|
|
$filter = $manager->filter()->add( ['product.id' => $prodIds] ); |
256
|
|
|
$prodItems = $manager->search( $filter, ['attribute' => ['variant']] ); |
257
|
|
|
|
258
|
|
|
$listItems = $item->getListItems( 'product', 'default', null, false ); |
259
|
|
|
$prodIds = []; |
260
|
|
|
|
261
|
|
|
foreach( $data as $idx => $entry ) |
262
|
|
|
{ |
263
|
|
|
if( ( $litem = $item->getListItem( 'product', 'default', $entry['product.id'], false ) ) === null ) { |
264
|
|
|
$litem = $listManager->create()->setType( 'default' ); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if( ( $refItem = $prodItems->get( $entry['product.id'] ) ) === null |
268
|
|
|
&& ( $refItem = $litem->getRefItem() ) === null |
269
|
|
|
) { |
270
|
|
|
$refItem = $manager->create()->setType( 'default' ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$litem->fromArray( $entry, true )->setPosition( $idx ); |
274
|
|
|
$refItem->fromArray( $entry, true ); |
275
|
|
|
|
276
|
|
|
if( isset( $entry['attr'] ) ) { |
277
|
|
|
$refItem = $this->fromArrayAttributes( $refItem, $entry['attr'] ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$item->addListItem( 'product', $litem, $manager->save( $refItem ) ); |
|
|
|
|
281
|
|
|
|
282
|
|
|
$prodIds[] = $data[$idx]['stock.productid'] = $refItem->getId(); |
283
|
|
|
unset( $listItems[$litem->getId()] ); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$this->fromArrayStocks( $prodIds, $data ); |
287
|
|
|
|
288
|
|
|
return $item->deleteListItems( $listItems->toArray() ); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Updates the variant attributes of the given product item |
294
|
|
|
* |
295
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $refItem Article item object |
296
|
|
|
* @param array $entry Associative list of key/values for product attribute references |
297
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface Updated artice item object |
298
|
|
|
*/ |
299
|
|
|
protected function fromArrayAttributes( \Aimeos\MShop\Product\Item\Iface $refItem, array $entry ) |
300
|
|
|
{ |
301
|
|
|
$listManager = \Aimeos\MShop::create( $this->getContext(), 'product/lists' ); |
302
|
|
|
$litems = $refItem->getListItems( 'attribute', 'variant', null, false ); |
303
|
|
|
$pos = 0; |
304
|
|
|
|
305
|
|
|
foreach( $entry as $attr ) |
306
|
|
|
{ |
307
|
|
|
if( !isset( $attr['product.lists.refid'] ) || $attr['product.lists.refid'] == '' ) { |
308
|
|
|
continue; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if( ( $litem = $refItem->getListItem( 'attribute', 'variant', $attr['product.lists.refid'], false ) ) === null ) { |
312
|
|
|
$litem = $listManager->create()->setType( 'variant' ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$litem = $litem->fromArray( $attr, true )->setPosition( $pos++ ); |
316
|
|
|
|
317
|
|
|
$refItem->addListItem( 'attribute', $litem, $litem->getRefItem() ); |
318
|
|
|
unset( $litems[$litem->getId()] ); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $refItem->deleteListItems( $litems->toArray() ); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Updates the stocklevels for the products |
327
|
|
|
* |
328
|
|
|
* @param array $data List of product codes |
329
|
|
|
* @param array $data Data array |
330
|
|
|
*/ |
331
|
|
|
protected function fromArrayStocks( array $prodIds, array $data ) |
332
|
|
|
{ |
333
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' ); |
334
|
|
|
|
335
|
|
|
$search = $manager->filter()->slice( 0, count( $prodIds ) )->add( [ |
336
|
|
|
'stock.productid' => $prodIds, |
337
|
|
|
'stock.type' => 'default', |
338
|
|
|
] ); |
339
|
|
|
|
340
|
|
|
$stockItems = $manager->search( $search ); |
341
|
|
|
$map = $stockItems->col( 'stock.id', 'stock.productid' ); |
342
|
|
|
$list = []; |
343
|
|
|
|
344
|
|
|
foreach( $data as $entry ) |
345
|
|
|
{ |
346
|
|
|
if( !isset( $entry['stock.stocklevel'] ) ) { |
347
|
|
|
continue; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if( ( $stockItem = $stockItems->get( $map[$entry['stock.productid']] ?? null ) ) === null ) { |
351
|
|
|
$stockItem = $manager->create(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$stockItem->fromArray( $entry, true )->setType( 'default' ); |
355
|
|
|
unset( $stockItems[$stockItem->getId()] ); |
356
|
|
|
|
357
|
|
|
$list[] = $stockItem; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
try |
361
|
|
|
{ |
362
|
|
|
$manager->begin(); |
363
|
|
|
$manager->delete( $stockItems->toArray() )->save( $list, false ); |
364
|
|
|
$manager->commit(); |
365
|
|
|
} |
366
|
|
|
catch( \Exception $e ) |
367
|
|
|
{ |
368
|
|
|
$manager->rollback(); |
369
|
|
|
throw $e; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Constructs the data array for the view from the given item |
376
|
|
|
* |
377
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items |
378
|
|
|
* @param bool $copy True if items should be copied |
379
|
|
|
* @return string[] Multi-dimensional associative list of item data |
380
|
|
|
*/ |
381
|
|
|
protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array |
382
|
|
|
{ |
383
|
|
|
if( $item->getType() !== 'select' ) { |
384
|
|
|
return []; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$data = []; |
388
|
|
|
$siteId = $this->getContext()->getLocale()->getSiteId(); |
389
|
|
|
|
390
|
|
|
|
391
|
|
|
foreach( $item->getListItems( 'product', 'default', null, false ) as $id => $listItem ) |
392
|
|
|
{ |
393
|
|
|
if( ( $refItem = $listItem->getRefItem() ) === null ) { |
394
|
|
|
continue; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$list = $listItem->toArray( true ) + $refItem->toArray( true ); |
398
|
|
|
|
399
|
|
|
if( $copy === true ) |
400
|
|
|
{ |
401
|
|
|
$list['product.lists.siteid'] = $siteId; |
402
|
|
|
$list['product.lists.id'] = ''; |
403
|
|
|
$list['product.siteid'] = $siteId; |
404
|
|
|
$list['product.id'] = ''; |
405
|
|
|
$list['product.code'] = $list['product.code'] . '_' . substr( md5( microtime( true ) ), -5 ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
$idx = 0; |
409
|
|
|
|
410
|
|
|
foreach( $refItem->getListItems( 'attribute', 'variant', null, false ) as $litem ) |
411
|
|
|
{ |
412
|
|
|
if( ( $attrItem = $litem->getRefItem() ) !== null ) { |
413
|
|
|
$list['attr'][$idx++] = $litem->toArray( true ) + $attrItem->toArray( true ); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$list = array_merge( $list, $refItem->getStockItems( 'default' )->first( map() )->toArray() ); |
418
|
|
|
|
419
|
|
|
$data[] = $list; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return $data; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Returns the rendered template including the view data |
428
|
|
|
* |
429
|
|
|
* @param \Aimeos\MW\View\Iface $view View object with data assigned |
430
|
|
|
* @return string HTML output |
431
|
|
|
*/ |
432
|
|
|
protected function render( \Aimeos\MW\View\Iface $view ) : string |
433
|
|
|
{ |
434
|
|
|
/** admin/jqadm/product/selection/template-item |
435
|
|
|
* Relative path to the HTML body template of the selection subpart for products. |
436
|
|
|
* |
437
|
|
|
* The template file contains the HTML code and processing instructions |
438
|
|
|
* to generate the result shown in the body of the frontend. The |
439
|
|
|
* configuration string is the path to the template file relative |
440
|
|
|
* to the templates directory (usually in admin/jqadm/templates). |
441
|
|
|
* |
442
|
|
|
* You can overwrite the template file configuration in extensions and |
443
|
|
|
* provide alternative templates. These alternative templates should be |
444
|
|
|
* named like the default one but with the string "default" replaced by |
445
|
|
|
* an unique name. You may use the name of your project for this. If |
446
|
|
|
* you've implemented an alternative client class as well, "default" |
447
|
|
|
* should be replaced by the name of the new class. |
448
|
|
|
* |
449
|
|
|
* @param string Relative path to the template creating the HTML code |
450
|
|
|
* @since 2016.04 |
451
|
|
|
* @category Developer |
452
|
|
|
*/ |
453
|
|
|
$tplconf = 'admin/jqadm/product/selection/template-item'; |
454
|
|
|
$default = 'product/item-selection-standard'; |
455
|
|
|
|
456
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|