Passed
Push — master ( d7021d...0bcbde )
by Aimeos
04:32 queued 54s
created

Standard::fromArray()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 38
rs 8.9777
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\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\MW\View\Iface $view View object
42
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
43
	 */
44
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$typeManager = \Aimeos\MShop::create( $this->getContext(), '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->getObject()->addData( $this->getView() );
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->getObject()->addData( $this->getView() );
81
		$siteid = $this->getContext()->getLocale()->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->getContext(), 'stock' );
105
		$filter = $manager->filter()->add( ['stock.productid' => $this->getView()->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->getObject()->addData( $this->getView() );
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->getView();
139
140
		$manager = \Aimeos\MShop::create( $this->getContext(), '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->getContext()->getConfig()->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->getContext(), 'stock' );
308
309
		if( !$ids->isEmpty() )
310
		{
311
			$filter = $manager->filter()->add( ['stock.id' => $ids->toArray()] );
312
			$stocks = $manager->search( $filter );
313
		}
314
315
		foreach( $data as $entry )
316
		{
317
			$id = $this->getValue( $entry, 'stock.id' );
318
319
			$stockItem = $stocks->get( $id, $manager->create() )
320
				->fromArray( $entry )->setProductId( $item->getId() );
321
322
			if( $entry['stock.stockflag'] ?? false ) {
323
				$stockItem->setStockLevel( $stockItem->getStockLevel() + $entry['stock.stockdiff'] ?? 0 );
324
			}
325
326
			if( $stockItem->getStockLevel() > 0 || $stockItem->getStockLevel() === null ) {
327
				$item->inStock( 1 );
328
			}
329
330
			$stockItems[] = $stockItem;
331
332
			$stocks->remove( $id );
333
		}
334
335
		$manager->delete( $stocks->toArray() );
336
		$manager->save( $stockItems, false );
337
338
		return $item;
339
	}
340
341
342
	/**
343
	 * Constructs the data array for the view from the given item
344
	 *
345
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
346
	 * @param bool $copy True if items should be copied, false if not
347
	 * @return string[] Multi-dimensional associative list of item data
348
	 */
349
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
350
	{
351
		$data = [];
352
		$context = $this->getContext();
353
		$siteId = $context->getLocale()->getSiteId();
354
355
		$manager = \Aimeos\MShop::create( $context, 'stock' );
356
		$filter = $manager->filter()->add( ['stock.productid' => $item->getId()] )->order( 'stock.type' );
357
358
		foreach( $manager->search( $filter ) as $stockItem )
359
		{
360
			$list = $stockItem->toArray( true );
361
362
			if( $copy === true )
363
			{
364
				$list['stock.siteid'] = $siteId;
365
				$list['stock.id'] = '';
366
			}
367
368
			$list['stock.dateback'] = str_replace( ' ', 'T', $list['stock.dateback'] );
369
370
			$data[] = $list;
371
		}
372
373
		return $data;
374
	}
375
376
377
	/**
378
	 * Returns the rendered template including the view data
379
	 *
380
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
381
	 * @return string HTML output
382
	 */
383
	protected function render( \Aimeos\MW\View\Iface $view ) : string
384
	{
385
		/** admin/jqadm/product/stock/template-item
386
		 * Relative path to the HTML body template of the stock subpart for products.
387
		 *
388
		 * The template file contains the HTML code and processing instructions
389
		 * to generate the result shown in the body of the frontend. The
390
		 * configuration string is the path to the template file relative
391
		 * to the templates directory (usually in admin/jqadm/templates).
392
		 *
393
		 * You can overwrite the template file configuration in extensions and
394
		 * provide alternative templates. These alternative templates should be
395
		 * named like the default one but with the string "default" replaced by
396
		 * an unique name. You may use the name of your project for this. If
397
		 * you've implemented an alternative client class as well, "default"
398
		 * should be replaced by the name of the new class.
399
		 *
400
		 * @param string Relative path to the template creating the HTML code
401
		 * @since 2016.04
402
		 * @category Developer
403
		 */
404
		$tplconf = 'admin/jqadm/product/stock/template-item';
405
		$default = 'product/item-stock-standard';
406
407
		return $view->render( $view->config( $tplconf, $default ) );
408
	}
409
}
410