|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2021 |
|
6
|
|
|
* @package Admin |
|
7
|
|
|
* @subpackage JQAdm |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Admin\JQAdm\Catalog\Product; |
|
12
|
|
|
|
|
13
|
|
|
sprintf( 'product' ); // for translation |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Default implementation of catalog product 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/catalog/product/name |
|
27
|
|
|
* Name of the product subpart used by the JQAdm catalog implementation |
|
28
|
|
|
* |
|
29
|
|
|
* Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Catalog\Product\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 2017.07 |
|
34
|
|
|
* @category Developer |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Adds the required data used in the template |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
|
42
|
|
|
* @return \Aimeos\MW\View\Iface View object with assigned parameters |
|
43
|
|
|
*/ |
|
44
|
|
|
public function data( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
|
45
|
|
|
{ |
|
46
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'product/lists/type' ); |
|
47
|
|
|
|
|
48
|
|
|
$search = $manager->filter( true )->slice( 0, 10000 ) |
|
49
|
|
|
->add( ['product.lists.type.domain' => 'catalog'] ) |
|
50
|
|
|
->order( 'product.lists.type.position' ); |
|
51
|
|
|
|
|
52
|
|
|
$view->productListTypes = $manager->search( $search ); |
|
53
|
|
|
|
|
54
|
|
|
return $view; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Copies a resource |
|
60
|
|
|
* |
|
61
|
|
|
* @return string|null HTML output |
|
62
|
|
|
*/ |
|
63
|
|
|
public function copy() : ?string |
|
64
|
|
|
{ |
|
65
|
|
|
$view = $this->object()->data( $this->view() ); |
|
66
|
|
|
$view->productBody = parent::copy(); |
|
67
|
|
|
|
|
68
|
|
|
return $this->render( $view ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Creates a new resource |
|
74
|
|
|
* |
|
75
|
|
|
* @return string|null HTML output |
|
76
|
|
|
*/ |
|
77
|
|
|
public function create() : ?string |
|
78
|
|
|
{ |
|
79
|
|
|
$view = $this->object()->data( $this->view() ); |
|
80
|
|
|
$view->productBody = parent::create(); |
|
81
|
|
|
|
|
82
|
|
|
return $this->render( $view ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns a single resource |
|
88
|
|
|
* |
|
89
|
|
|
* @return string|null HTML output |
|
90
|
|
|
*/ |
|
91
|
|
|
public function get() : ?string |
|
92
|
|
|
{ |
|
93
|
|
|
$total = 0; |
|
94
|
|
|
$view = $this->object()->data( $this->view() ); |
|
95
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
|
96
|
|
|
|
|
97
|
|
|
$filter = $manager->filter(); |
|
98
|
|
|
$func = $filter->make( 'product:has', ['catalog', ['default', 'promotion'], $view->item->getId()] ); |
|
99
|
|
|
|
|
100
|
|
|
$params = $this->storeFilter( $view->param( 'cp', [] ), 'catalogproduct' ); |
|
101
|
|
|
$filter = $this->initCriteria( $filter, $params, 'catalogproduct' )->add( $func, '!=', null ); |
|
|
|
|
|
|
102
|
|
|
$products = $manager->search( $filter, [], $total ); |
|
103
|
|
|
|
|
104
|
|
|
$view->productItems = $products; |
|
105
|
|
|
$view->productData = $this->toArray( $view->item, $products ); |
|
106
|
|
|
$view->productBody = parent::get(); |
|
107
|
|
|
$view->productTotal = $total; |
|
108
|
|
|
|
|
109
|
|
|
return $this->render( $view ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Saves the data |
|
115
|
|
|
* |
|
116
|
|
|
* @return string|null HTML output |
|
117
|
|
|
*/ |
|
118
|
|
|
public function save() : ?string |
|
119
|
|
|
{ |
|
120
|
|
|
$view = $this->view(); |
|
121
|
|
|
|
|
122
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
|
123
|
|
|
$manager->begin(); |
|
124
|
|
|
|
|
125
|
|
|
try |
|
126
|
|
|
{ |
|
127
|
|
|
$this->storeFilter( $view->param( 'cp', [] ), 'catalogproduct' ); |
|
128
|
|
|
$this->fromArray( $view->item, $view->param( 'product', [] ) ); |
|
129
|
|
|
$view->productBody = parent::save(); |
|
130
|
|
|
|
|
131
|
|
|
$manager->commit(); |
|
132
|
|
|
} |
|
133
|
|
|
catch( \Exception $e ) |
|
134
|
|
|
{ |
|
135
|
|
|
$manager->rollback(); |
|
136
|
|
|
throw $e; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns the sub-client given by its name. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $type Name of the client type |
|
147
|
|
|
* @param string|null $name Name of the sub-client (Default if null) |
|
148
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
|
151
|
|
|
{ |
|
152
|
|
|
/** admin/jqadm/catalog/product/decorators/excludes |
|
153
|
|
|
* Excludes decorators added by the "common" option from the catalog JQAdm client |
|
154
|
|
|
* |
|
155
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
156
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
157
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
158
|
|
|
* modify what is returned to the caller. |
|
159
|
|
|
* |
|
160
|
|
|
* This option allows you to remove a decorator added via |
|
161
|
|
|
* "admin/jqadm/common/decorators/default" before they are wrapped |
|
162
|
|
|
* around the JQAdm client. |
|
163
|
|
|
* |
|
164
|
|
|
* admin/jqadm/catalog/product/decorators/excludes = array( 'decorator1' ) |
|
165
|
|
|
* |
|
166
|
|
|
* This would remove the decorator named "decorator1" from the list of |
|
167
|
|
|
* common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
|
168
|
|
|
* "admin/jqadm/common/decorators/default" to the JQAdm client. |
|
169
|
|
|
* |
|
170
|
|
|
* @param array List of decorator names |
|
171
|
|
|
* @since 2017.07 |
|
172
|
|
|
* @category Developer |
|
173
|
|
|
* @see admin/jqadm/common/decorators/default |
|
174
|
|
|
* @see admin/jqadm/catalog/product/decorators/global |
|
175
|
|
|
* @see admin/jqadm/catalog/product/decorators/local |
|
176
|
|
|
*/ |
|
177
|
|
|
|
|
178
|
|
|
/** admin/jqadm/catalog/product/decorators/global |
|
179
|
|
|
* Adds a list of globally available decorators only to the catalog JQAdm client |
|
180
|
|
|
* |
|
181
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
182
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
183
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
184
|
|
|
* modify what is returned to the caller. |
|
185
|
|
|
* |
|
186
|
|
|
* This option allows you to wrap global decorators |
|
187
|
|
|
* ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
|
188
|
|
|
* |
|
189
|
|
|
* admin/jqadm/catalog/product/decorators/global = array( 'decorator1' ) |
|
190
|
|
|
* |
|
191
|
|
|
* This would add the decorator named "decorator1" defined by |
|
192
|
|
|
* "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
|
193
|
|
|
* |
|
194
|
|
|
* @param array List of decorator names |
|
195
|
|
|
* @since 2017.07 |
|
196
|
|
|
* @category Developer |
|
197
|
|
|
* @see admin/jqadm/common/decorators/default |
|
198
|
|
|
* @see admin/jqadm/catalog/product/decorators/excludes |
|
199
|
|
|
* @see admin/jqadm/catalog/product/decorators/local |
|
200
|
|
|
*/ |
|
201
|
|
|
|
|
202
|
|
|
/** admin/jqadm/catalog/product/decorators/local |
|
203
|
|
|
* Adds a list of local decorators only to the catalog JQAdm client |
|
204
|
|
|
* |
|
205
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
206
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
207
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
208
|
|
|
* modify what is returned to the caller. |
|
209
|
|
|
* |
|
210
|
|
|
* This option allows you to wrap local decorators |
|
211
|
|
|
* ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client. |
|
212
|
|
|
* |
|
213
|
|
|
* admin/jqadm/catalog/product/decorators/local = array( 'decorator2' ) |
|
214
|
|
|
* |
|
215
|
|
|
* This would add the decorator named "decorator2" defined by |
|
216
|
|
|
* "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client. |
|
217
|
|
|
* |
|
218
|
|
|
* @param array List of decorator names |
|
219
|
|
|
* @since 2017.07 |
|
220
|
|
|
* @category Developer |
|
221
|
|
|
* @see admin/jqadm/common/decorators/default |
|
222
|
|
|
* @see admin/jqadm/catalog/product/decorators/excludes |
|
223
|
|
|
* @see admin/jqadm/catalog/product/decorators/global |
|
224
|
|
|
*/ |
|
225
|
|
|
return $this->createSubClient( 'catalog/product/' . $type, $name ); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns the list of sub-client names configured for the client. |
|
231
|
|
|
* |
|
232
|
|
|
* @return array List of JQAdm client names |
|
233
|
|
|
*/ |
|
234
|
|
|
protected function getSubClientNames() : array |
|
235
|
|
|
{ |
|
236
|
|
|
/** admin/jqadm/catalog/product/subparts |
|
237
|
|
|
* List of JQAdm sub-clients rendered within the catalog product section |
|
238
|
|
|
* |
|
239
|
|
|
* The output of the frontend is composed of the code generated by the JQAdm |
|
240
|
|
|
* clients. Each JQAdm client can consist of serveral (or none) sub-clients |
|
241
|
|
|
* that are responsible for rendering certain sub-parts of the output. The |
|
242
|
|
|
* sub-clients can contain JQAdm clients themselves and therefore a |
|
243
|
|
|
* hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
|
244
|
|
|
* the output that is placed inside the container of its parent. |
|
245
|
|
|
* |
|
246
|
|
|
* At first, always the JQAdm code generated by the parent is printed, then |
|
247
|
|
|
* the JQAdm code of its sub-clients. The product of the JQAdm sub-clients |
|
248
|
|
|
* determines the product of the output of these sub-clients inside the parent |
|
249
|
|
|
* container. If the configured list of clients is |
|
250
|
|
|
* |
|
251
|
|
|
* array( "subclient1", "subclient2" ) |
|
252
|
|
|
* |
|
253
|
|
|
* you can easily change the product of the output by reproducting the subparts: |
|
254
|
|
|
* |
|
255
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
|
256
|
|
|
* |
|
257
|
|
|
* You can also remove one or more parts if they shouldn't be rendered: |
|
258
|
|
|
* |
|
259
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1" ) |
|
260
|
|
|
* |
|
261
|
|
|
* As the clients only generates structural JQAdm, the layout defined via CSS |
|
262
|
|
|
* should support adding, removing or reproducting content by a fluid like |
|
263
|
|
|
* design. |
|
264
|
|
|
* |
|
265
|
|
|
* @param array List of sub-client names |
|
266
|
|
|
* @since 2017.07 |
|
267
|
|
|
* @category Developer |
|
268
|
|
|
*/ |
|
269
|
|
|
return $this->context()->config()->get( 'admin/jqadm/catalog/product/subparts', [] ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Creates new and updates existing items using the data array |
|
275
|
|
|
* |
|
276
|
|
|
* @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object without referenced domain items |
|
277
|
|
|
* @param array $data Data array |
|
278
|
|
|
* @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item |
|
279
|
|
|
*/ |
|
280
|
|
|
protected function fromArray( \Aimeos\MShop\Catalog\Item\Iface $item, array $data ) : \Aimeos\MShop\Catalog\Item\Iface |
|
281
|
|
|
{ |
|
282
|
|
|
if( empty( $prodIds = $this->val( $data, 'product.lists.parentid', [] ) ) ) { |
|
283
|
|
|
return $item; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
|
287
|
|
|
$filter = $manager->filter()->add( ['product.id' => $prodIds] )->slice( 0, count( $prodIds ) ); |
|
288
|
|
|
$products = $manager->search( $filter, ['catalog'] ); |
|
289
|
|
|
|
|
290
|
|
|
$id = $item->getId(); |
|
291
|
|
|
$listItem = $manager->createListItem()->setRefId( $id ); |
|
|
|
|
|
|
292
|
|
|
$listItems = $products->getListItems( 'catalog', null, $id ) |
|
293
|
|
|
->flat( 1 )->col( null, 'product.lists.id' ); |
|
294
|
|
|
|
|
295
|
|
|
foreach( (array) $prodIds as $idx => $prodId ) |
|
296
|
|
|
{ |
|
297
|
|
|
if( ( $product = $products->get( $prodId ) ) === null ) { |
|
298
|
|
|
continue; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$conf = []; |
|
302
|
|
|
$type = $this->val( $data, 'product.lists.type/' . $idx, 'default' ); |
|
303
|
|
|
$listItem = $product->getListItem( 'catalog', $type, $id ) ?: clone $listItem; |
|
304
|
|
|
|
|
305
|
|
|
foreach( (array) $this->val( $data, 'config/' . $idx . '/key', [] ) as $pos => $key ) |
|
306
|
|
|
{ |
|
307
|
|
|
if( trim( $key ) !== '' && isset( $data['config'][$idx]['val'][$pos] ) ) { |
|
308
|
|
|
$conf[$key] = $data['config'][$idx]['val'][$pos]; |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$listItem->setType( $type )->setConfig( $conf ) |
|
313
|
|
|
->setPosition( (int) $this->val( $data, 'product.lists.position/' . $idx, 0 ) ) |
|
314
|
|
|
->setStatus( (int) $this->val( $data, 'product.lists.status/' . $idx, 1 ) ) |
|
315
|
|
|
->setDateStart( $this->val( $data, 'product.lists.datestart/' . $idx ) ) |
|
316
|
|
|
->setDateEnd( $this->val( $data, 'product.lists.dateend/' . $idx ) ); |
|
317
|
|
|
|
|
318
|
|
|
$product->addListItem( 'catalog', $listItem ); |
|
319
|
|
|
$listItems->remove( $listItem->getId() ); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
\Aimeos\MShop::create( $this->context(), 'index' )->save( $products ); |
|
323
|
|
|
|
|
324
|
|
|
return $item; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Constructs the data array for the view from the given item |
|
330
|
|
|
* |
|
331
|
|
|
* @param \Aimeos\Map $listItems Catalog list items implementing \Aimeos\MShop\Common\Item\Lists\Iface and referencing the products |
|
332
|
|
|
* @return string[] Multi-dimensional associative list of item data |
|
333
|
|
|
*/ |
|
334
|
|
|
protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, \Aimeos\Map $products ) : array |
|
335
|
|
|
{ |
|
336
|
|
|
$data = []; |
|
337
|
|
|
|
|
338
|
|
|
foreach( $products->getListItems( 'catalog', null, $item->getId() )->flat( 1 ) as $listItem ) |
|
339
|
|
|
{ |
|
340
|
|
|
foreach( $listItem->toArray( true ) as $key => $value ) { |
|
341
|
|
|
$data[$key][] = $value; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
return $data; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Returns the rendered template including the view data |
|
351
|
|
|
* |
|
352
|
|
|
* @param \Aimeos\MW\View\Iface $view View object with data assigned |
|
353
|
|
|
* @return string HTML output |
|
354
|
|
|
*/ |
|
355
|
|
|
protected function render( \Aimeos\MW\View\Iface $view ) : string |
|
356
|
|
|
{ |
|
357
|
|
|
/** admin/jqadm/catalog/product/template-item |
|
358
|
|
|
* Relative path to the HTML body template of the product subpart for catalogs. |
|
359
|
|
|
* |
|
360
|
|
|
* The template file contains the HTML code and processing instructions |
|
361
|
|
|
* to generate the result shown in the body of the frontend. The |
|
362
|
|
|
* configuration string is the path to the template file relative |
|
363
|
|
|
* to the templates directory (usually in admin/jqadm/templates). |
|
364
|
|
|
* |
|
365
|
|
|
* You can overwrite the template file configuration in extensions and |
|
366
|
|
|
* provide alternative templates. These alternative templates should be |
|
367
|
|
|
* named like the default one but with the string "default" replaced by |
|
368
|
|
|
* an unique name. You may use the name of your project for this. If |
|
369
|
|
|
* you've implemented an alternative client class as well, "default" |
|
370
|
|
|
* should be replaced by the name of the new class. |
|
371
|
|
|
* |
|
372
|
|
|
* @param string Relative path to the template creating the HTML code |
|
373
|
|
|
* @since 2016.04 |
|
374
|
|
|
* @category Developer |
|
375
|
|
|
*/ |
|
376
|
|
|
$tplconf = 'admin/jqadm/catalog/product/template-item'; |
|
377
|
|
|
$default = 'catalog/item-product-standard'; |
|
378
|
|
|
|
|
379
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
|
380
|
|
|
} |
|
381
|
|
|
} |
|
382
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.