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\Product\Stock; |
12
|
|
|
|
13
|
|
|
sprintf( 'stock' ); // for translation |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default implementation of product stock 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/stock/name |
27
|
|
|
* Name of the stock subpart used by the JQAdm product implementation |
28
|
|
|
* |
29
|
|
|
* Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Stock\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
|
|
|
* Adds the required data used in the stock template |
40
|
|
|
* |
41
|
|
|
* @param \Aimeos\Base\View\Iface $view View object |
42
|
|
|
* @return \Aimeos\Base\View\Iface View object with assigned parameters |
43
|
|
|
*/ |
44
|
|
|
public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
45
|
|
|
{ |
46
|
|
|
$typeManager = \Aimeos\MShop::create( $this->context(), 'stock/type' ); |
47
|
|
|
|
48
|
|
|
$search = $typeManager->filter( true )->slice( 0, 10000 ); |
49
|
|
|
$search->setConditions( $search->compare( '==', 'stock.type.domain', 'product' ) ); |
50
|
|
|
$search->setSortations( [$search->sort( '+', 'stock.type.position' )] ); |
51
|
|
|
|
52
|
|
|
$view->stockTypes = $typeManager->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->stockData = $this->toArray( $view->item, true ); |
67
|
|
|
$view->stockBody = parent::copy(); |
68
|
|
|
|
69
|
|
|
return $this->render( $view ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Creates a new resource |
75
|
|
|
* |
76
|
|
|
* @return string|null HTML output |
77
|
|
|
*/ |
78
|
|
|
public function create() : ?string |
79
|
|
|
{ |
80
|
|
|
$view = $this->object()->data( $this->view() ); |
81
|
|
|
$siteid = $this->context()->locale()->getSiteId(); |
82
|
|
|
$data = $view->param( 'stock', [] ); |
83
|
|
|
|
84
|
|
|
foreach( $view->value( $data, 'stock.id', [] ) as $idx => $value ) { |
85
|
|
|
$data[$idx]['stock.siteid'] = $siteid; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$view->stockData = $data; |
89
|
|
|
$view->stockBody = parent::copy(); |
90
|
|
|
|
91
|
|
|
return $this->render( $view ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Deletes a resource |
97
|
|
|
* |
98
|
|
|
* @return string|null HTML output |
99
|
|
|
*/ |
100
|
|
|
public function delete() : ?string |
101
|
|
|
{ |
102
|
|
|
parent::delete(); |
103
|
|
|
|
104
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'stock' ); |
105
|
|
|
$filter = $manager->filter()->add( ['stock.productid' => $this->view()->item->getId()] ); |
106
|
|
|
$manager->delete( $manager->search( $filter )->toArray() ); |
107
|
|
|
|
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns a single resource |
114
|
|
|
* |
115
|
|
|
* @return string|null HTML output |
116
|
|
|
*/ |
117
|
|
|
public function get() : ?string |
118
|
|
|
{ |
119
|
|
|
$view = $this->object()->data( $this->view() ); |
120
|
|
|
$view->stockData = $this->toArray( $view->item ); |
121
|
|
|
$view->stockBody = ''; |
122
|
|
|
|
123
|
|
|
foreach( $this->getSubClients() as $client ) { |
124
|
|
|
$view->stockBody .= $client->get(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->render( $view ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Saves the data |
133
|
|
|
* |
134
|
|
|
* @return string|null HTML output |
135
|
|
|
*/ |
136
|
|
|
public function save() : ?string |
137
|
|
|
{ |
138
|
|
|
$view = $this->view(); |
139
|
|
|
|
140
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'stock' ); |
141
|
|
|
$manager->begin(); |
142
|
|
|
|
143
|
|
|
try |
144
|
|
|
{ |
145
|
|
|
$this->fromArray( $view->item, $view->param( 'stock', [] ) ); |
146
|
|
|
$view->stockBody = ''; |
147
|
|
|
|
148
|
|
|
foreach( $this->getSubClients() as $client ) { |
149
|
|
|
$view->stockBody .= $client->save(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$manager->commit(); |
153
|
|
|
} |
154
|
|
|
catch( \Exception $e ) |
155
|
|
|
{ |
156
|
|
|
$manager->rollback(); |
157
|
|
|
throw $e; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns the sub-client given by its name. |
166
|
|
|
* |
167
|
|
|
* @param string $type Name of the client type |
168
|
|
|
* @param string|null $name Name of the sub-client (Default if null) |
169
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
170
|
|
|
*/ |
171
|
|
|
public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
172
|
|
|
{ |
173
|
|
|
/** admin/jqadm/product/stock/decorators/excludes |
174
|
|
|
* Excludes decorators added by the "common" option from the product JQAdm client |
175
|
|
|
* |
176
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
177
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
178
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
179
|
|
|
* modify what is returned to the caller. |
180
|
|
|
* |
181
|
|
|
* This option allows you to remove a decorator added via |
182
|
|
|
* "admin/jqadm/common/decorators/default" before they are wrapped |
183
|
|
|
* around the JQAdm client. |
184
|
|
|
* |
185
|
|
|
* admin/jqadm/product/stock/decorators/excludes = array( 'decorator1' ) |
186
|
|
|
* |
187
|
|
|
* This would remove the decorator named "decorator1" from the list of |
188
|
|
|
* common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
189
|
|
|
* "admin/jqadm/common/decorators/default" to the JQAdm client. |
190
|
|
|
* |
191
|
|
|
* @param array List of decorator names |
192
|
|
|
* @since 2016.01 |
193
|
|
|
* @category Developer |
194
|
|
|
* @see admin/jqadm/common/decorators/default |
195
|
|
|
* @see admin/jqadm/product/stock/decorators/global |
196
|
|
|
* @see admin/jqadm/product/stock/decorators/local |
197
|
|
|
*/ |
198
|
|
|
|
199
|
|
|
/** admin/jqadm/product/stock/decorators/global |
200
|
|
|
* Adds a list of globally available decorators only to the product JQAdm client |
201
|
|
|
* |
202
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
203
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
204
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
205
|
|
|
* modify what is returned to the caller. |
206
|
|
|
* |
207
|
|
|
* This option allows you to wrap global decorators |
208
|
|
|
* ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
209
|
|
|
* |
210
|
|
|
* admin/jqadm/product/stock/decorators/global = array( 'decorator1' ) |
211
|
|
|
* |
212
|
|
|
* This would add the decorator named "decorator1" defined by |
213
|
|
|
* "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
214
|
|
|
* |
215
|
|
|
* @param array List of decorator names |
216
|
|
|
* @since 2016.01 |
217
|
|
|
* @category Developer |
218
|
|
|
* @see admin/jqadm/common/decorators/default |
219
|
|
|
* @see admin/jqadm/product/stock/decorators/excludes |
220
|
|
|
* @see admin/jqadm/product/stock/decorators/local |
221
|
|
|
*/ |
222
|
|
|
|
223
|
|
|
/** admin/jqadm/product/stock/decorators/local |
224
|
|
|
* Adds a list of local decorators only to the product JQAdm client |
225
|
|
|
* |
226
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
227
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
228
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
229
|
|
|
* modify what is returned to the caller. |
230
|
|
|
* |
231
|
|
|
* This option allows you to wrap local decorators |
232
|
|
|
* ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client. |
233
|
|
|
* |
234
|
|
|
* admin/jqadm/product/stock/decorators/local = array( 'decorator2' ) |
235
|
|
|
* |
236
|
|
|
* This would add the decorator named "decorator2" defined by |
237
|
|
|
* "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client. |
238
|
|
|
* |
239
|
|
|
* @param array List of decorator names |
240
|
|
|
* @since 2016.01 |
241
|
|
|
* @category Developer |
242
|
|
|
* @see admin/jqadm/common/decorators/default |
243
|
|
|
* @see admin/jqadm/product/stock/decorators/excludes |
244
|
|
|
* @see admin/jqadm/product/stock/decorators/global |
245
|
|
|
*/ |
246
|
|
|
return $this->createSubClient( 'product/stock/' . $type, $name ); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Returns the list of sub-client names configured for the client. |
252
|
|
|
* |
253
|
|
|
* @return array List of JQAdm client names |
254
|
|
|
*/ |
255
|
|
|
protected function getSubClientNames() : array |
256
|
|
|
{ |
257
|
|
|
/** admin/jqadm/product/stock/subparts |
258
|
|
|
* List of JQAdm sub-clients rendered within the product stock section |
259
|
|
|
* |
260
|
|
|
* The output of the frontend is composed of the code generated by the JQAdm |
261
|
|
|
* clients. Each JQAdm client can consist of serveral (or none) sub-clients |
262
|
|
|
* that are responsible for rendering certain sub-parts of the output. The |
263
|
|
|
* sub-clients can contain JQAdm clients themselves and therefore a |
264
|
|
|
* hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
265
|
|
|
* the output that is placed inside the container of its parent. |
266
|
|
|
* |
267
|
|
|
* At first, always the JQAdm code generated by the parent is printed, then |
268
|
|
|
* the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
269
|
|
|
* determines the order of the output of these sub-clients inside the parent |
270
|
|
|
* container. If the configured list of clients is |
271
|
|
|
* |
272
|
|
|
* array( "subclient1", "subclient2" ) |
273
|
|
|
* |
274
|
|
|
* you can easily change the order of the output by reordering the subparts: |
275
|
|
|
* |
276
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
277
|
|
|
* |
278
|
|
|
* You can also remove one or more parts if they shouldn't be rendered: |
279
|
|
|
* |
280
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1" ) |
281
|
|
|
* |
282
|
|
|
* As the clients only generates structural JQAdm, the layout defined via CSS |
283
|
|
|
* should support adding, removing or reordering content by a fluid like |
284
|
|
|
* design. |
285
|
|
|
* |
286
|
|
|
* @param array List of sub-client names |
287
|
|
|
* @since 2016.01 |
288
|
|
|
* @category Developer |
289
|
|
|
*/ |
290
|
|
|
return $this->context()->config()->get( 'admin/jqadm/product/stock/subparts', [] ); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Creates new and updates existing items using the data array |
296
|
|
|
* |
297
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items |
298
|
|
|
* @param array $data Data array |
299
|
|
|
* @return \Aimeos\MShop\Product\Item\Iface Modified product item |
300
|
|
|
*/ |
301
|
|
|
protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface |
302
|
|
|
{ |
303
|
|
|
$stocks = map(); |
304
|
|
|
$stockItems = []; |
305
|
|
|
|
306
|
|
|
$ids = map( $data )->col( 'stock.id' )->filter(); |
307
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'stock' ); |
308
|
|
|
|
309
|
|
|
if( !$ids->isEmpty() ) { |
310
|
|
|
$stocks = $manager->search( $manager->filter()->add( ['stock.id' => $ids->all()] ) ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
foreach( $data as $entry ) |
314
|
|
|
{ |
315
|
|
|
$id = $this->val( $entry, 'stock.id' ); |
316
|
|
|
$stockItem = $stocks->get( $id ) ?: $manager->create(); |
317
|
|
|
$stockItem->fromArray( $entry )->setProductId( $item->getId() ); |
318
|
|
|
|
319
|
|
|
if( $entry['stock.stockflag'] ?? false ) { |
320
|
|
|
$stockItem->setStockLevel( $stockItem->getStockLevel() + $entry['stock.stockdiff'] ?? 0 ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$item->setInStock( (int) $stockItem->getStockLevel() > 0 || $stockItem->getStockLevel() === null ); |
|
|
|
|
324
|
|
|
|
325
|
|
|
$stockItems[] = $stockItem; |
326
|
|
|
$stocks->remove( $id ); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$manager->save( $stockItems, false ); |
330
|
|
|
$manager->delete( $stocks ); |
331
|
|
|
|
332
|
|
|
return $item; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Constructs the data array for the view from the given item |
338
|
|
|
* |
339
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items |
340
|
|
|
* @param bool $copy True if items should be copied, false if not |
341
|
|
|
* @return string[] Multi-dimensional associative list of item data |
342
|
|
|
*/ |
343
|
|
|
protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array |
344
|
|
|
{ |
345
|
|
|
$data = []; |
346
|
|
|
$context = $this->context(); |
347
|
|
|
$siteId = $context->locale()->getSiteId(); |
348
|
|
|
|
349
|
|
|
$manager = \Aimeos\MShop::create( $context, 'stock' ); |
350
|
|
|
$filter = $manager->filter()->add( ['stock.productid' => $item->getId()] )->order( 'stock.type' ); |
351
|
|
|
|
352
|
|
|
foreach( $manager->search( $filter ) as $stockItem ) |
353
|
|
|
{ |
354
|
|
|
$list = $stockItem->toArray( true ); |
355
|
|
|
|
356
|
|
|
if( $copy === true ) |
357
|
|
|
{ |
358
|
|
|
$list['stock.siteid'] = $siteId; |
359
|
|
|
$list['stock.id'] = ''; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
$list['stock.dateback'] = str_replace( ' ', 'T', $list['stock.dateback'] ?? '' ); |
363
|
|
|
|
364
|
|
|
$data[] = $list; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return $data; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Returns the rendered template including the view data |
373
|
|
|
* |
374
|
|
|
* @param \Aimeos\Base\View\Iface $view View object with data assigned |
375
|
|
|
* @return string HTML output |
376
|
|
|
*/ |
377
|
|
|
protected function render( \Aimeos\Base\View\Iface $view ) : string |
378
|
|
|
{ |
379
|
|
|
/** admin/jqadm/product/stock/template-item |
380
|
|
|
* Relative path to the HTML body template of the stock subpart for products. |
381
|
|
|
* |
382
|
|
|
* The template file contains the HTML code and processing instructions |
383
|
|
|
* to generate the result shown in the body of the frontend. The |
384
|
|
|
* configuration string is the path to the template file relative |
385
|
|
|
* to the templates directory (usually in templates/admin/jqadm). |
386
|
|
|
* |
387
|
|
|
* You can overwrite the template file configuration in extensions and |
388
|
|
|
* provide alternative templates. These alternative templates should be |
389
|
|
|
* named like the default one but with the string "default" replaced by |
390
|
|
|
* an unique name. You may use the name of your project for this. If |
391
|
|
|
* you've implemented an alternative client class as well, "default" |
392
|
|
|
* should be replaced by the name of the new class. |
393
|
|
|
* |
394
|
|
|
* @param string Relative path to the template creating the HTML code |
395
|
|
|
* @since 2016.04 |
396
|
|
|
* @category Developer |
397
|
|
|
*/ |
398
|
|
|
$tplconf = 'admin/jqadm/product/stock/template-item'; |
399
|
|
|
$default = 'product/item-stock'; |
400
|
|
|
|
401
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|