Standard   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 372
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 80
c 4
b 0
f 0
dl 0
loc 372
rs 10
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 7 1
A render() 0 24 1
A toArray() 0 25 3
A getSubClientNames() 0 35 1
A data() 0 11 1
A save() 0 25 3
A create() 0 16 2
A get() 0 11 2
A delete() 0 9 1
A getSubClient() 0 73 1
A fromArray() 0 28 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
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
	 */
35
36
37
	/**
38
	 * Adds the required data used in the stock template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		$typeManager = \Aimeos\MShop::create( $this->context(), 'stock/type' );
46
47
		$search = $typeManager->filter( true )
48
			->order( 'stock.type.code' )
49
			->slice( 0, 10000 );
50
51
		$view->stockTypes = $typeManager->search( $search );
52
53
		return $view;
54
	}
55
56
57
	/**
58
	 * Copies a resource
59
	 *
60
	 * @return string|null HTML output
61
	 */
62
	public function copy() : ?string
63
	{
64
		$view = $this->object()->data( $this->view() );
65
		$view->stockData = $this->toArray( $view->item, true );
66
		$view->stockBody = 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
		$siteid = $this->context()->locale()->getSiteId();
81
82
		$itemData = $this->toArray( $view->item );
83
		$data = array_replace_recursive( $itemData, $view->param( 'stock', [] ) );
84
85
		foreach( $data as $idx => $entry ) {
86
			$data[$idx]['stock.siteid'] = $siteid;
87
		}
88
89
		$view->stockData = $data;
90
		$view->stockBody = parent::copy();
91
92
		return $this->render( $view );
93
	}
94
95
96
	/**
97
	 * Deletes a resource
98
	 *
99
	 * @return string|null HTML output
100
	 */
101
	public function delete() : ?string
102
	{
103
		parent::delete();
104
105
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
106
		$filter = $manager->filter()->add( ['stock.productid' => $this->view()->item->getId()] );
107
		$manager->delete( $manager->search( $filter ) );
108
109
		return null;
110
	}
111
112
113
	/**
114
	 * Returns a single resource
115
	 *
116
	 * @return string|null HTML output
117
	 */
118
	public function get() : ?string
119
	{
120
		$view = $this->object()->data( $this->view() );
121
		$view->stockData = $this->toArray( $view->item );
122
		$view->stockBody = '';
123
124
		foreach( $this->getSubClients() as $client ) {
125
			$view->stockBody .= $client->get();
126
		}
127
128
		return $this->render( $view );
129
	}
130
131
132
	/**
133
	 * Saves the data
134
	 *
135
	 * @return string|null HTML output
136
	 */
137
	public function save() : ?string
138
	{
139
		$view = $this->view();
140
141
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
142
		$manager->begin();
143
144
		try
145
		{
146
			$this->fromArray( $view->item, $view->param( 'stock', [] ) );
147
			$view->stockBody = '';
148
149
			foreach( $this->getSubClients() as $client ) {
150
				$view->stockBody .= $client->save();
151
			}
152
153
			$manager->commit();
154
		}
155
		catch( \Exception $e )
156
		{
157
			$manager->rollback();
158
			throw $e;
159
		}
160
161
		return null;
162
	}
163
164
165
	/**
166
	 * Returns the sub-client given by its name.
167
	 *
168
	 * @param string $type Name of the client type
169
	 * @param string|null $name Name of the sub-client (Default if null)
170
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
171
	 */
172
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
173
	{
174
		/** admin/jqadm/product/stock/decorators/excludes
175
		 * Excludes decorators added by the "common" option from the product JQAdm client
176
		 *
177
		 * Decorators extend the functionality of a class by adding new aspects
178
		 * (e.g. log what is currently done), executing the methods of the underlying
179
		 * class only in certain conditions (e.g. only for logged in users) or
180
		 * modify what is returned to the caller.
181
		 *
182
		 * This option allows you to remove a decorator added via
183
		 * "admin/jqadm/common/decorators/default" before they are wrapped
184
		 * around the JQAdm client.
185
		 *
186
		 *  admin/jqadm/product/stock/decorators/excludes = array( 'decorator1' )
187
		 *
188
		 * This would remove the decorator named "decorator1" from the list of
189
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
190
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
191
		 *
192
		 * @param array List of decorator names
193
		 * @since 2016.01
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
		 * @see admin/jqadm/common/decorators/default
218
		 * @see admin/jqadm/product/stock/decorators/excludes
219
		 * @see admin/jqadm/product/stock/decorators/local
220
		 */
221
222
		/** admin/jqadm/product/stock/decorators/local
223
		 * Adds a list of local decorators only to the product JQAdm client
224
		 *
225
		 * Decorators extend the functionality of a class by adding new aspects
226
		 * (e.g. log what is currently done), executing the methods of the underlying
227
		 * class only in certain conditions (e.g. only for logged in users) or
228
		 * modify what is returned to the caller.
229
		 *
230
		 * This option allows you to wrap local decorators
231
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
232
		 *
233
		 *  admin/jqadm/product/stock/decorators/local = array( 'decorator2' )
234
		 *
235
		 * This would add the decorator named "decorator2" defined by
236
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
237
		 *
238
		 * @param array List of decorator names
239
		 * @since 2016.01
240
		 * @see admin/jqadm/common/decorators/default
241
		 * @see admin/jqadm/product/stock/decorators/excludes
242
		 * @see admin/jqadm/product/stock/decorators/global
243
		 */
244
		return $this->createSubClient( 'product/stock/' . $type, $name );
245
	}
246
247
248
	/**
249
	 * Returns the list of sub-client names configured for the client.
250
	 *
251
	 * @return array List of JQAdm client names
252
	 */
253
	protected function getSubClientNames() : array
254
	{
255
		/** admin/jqadm/product/stock/subparts
256
		 * List of JQAdm sub-clients rendered within the product stock section
257
		 *
258
		 * The output of the frontend is composed of the code generated by the JQAdm
259
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
260
		 * that are responsible for rendering certain sub-parts of the output. The
261
		 * sub-clients can contain JQAdm clients themselves and therefore a
262
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
263
		 * the output that is placed inside the container of its parent.
264
		 *
265
		 * At first, always the JQAdm code generated by the parent is printed, then
266
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
267
		 * determines the order of the output of these sub-clients inside the parent
268
		 * container. If the configured list of clients is
269
		 *
270
		 *  array( "subclient1", "subclient2" )
271
		 *
272
		 * you can easily change the order of the output by reordering the subparts:
273
		 *
274
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
275
		 *
276
		 * You can also remove one or more parts if they shouldn't be rendered:
277
		 *
278
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
279
		 *
280
		 * As the clients only generates structural JQAdm, the layout defined via CSS
281
		 * should support adding, removing or reordering content by a fluid like
282
		 * design.
283
		 *
284
		 * @param array List of sub-client names
285
		 * @since 2016.01
286
		 */
287
		return $this->context()->config()->get( 'admin/jqadm/product/stock/subparts', [] );
288
	}
289
290
291
	/**
292
	 * Creates new and updates existing items using the data array
293
	 *
294
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
295
	 * @param array $data Data array
296
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
297
	 */
298
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
299
	{
300
		$stockItems = [];
301
		$ids = map( $data )->col( 'stock.id' )->filter();
0 ignored issues
show
Unused Code introduced by
The assignment to $ids is dead and can be removed.
Loading history...
302
303
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
304
		$filter = $manager->filter()->add( 'stock.productid', '==', $item->getId() );
305
		$stocks = $manager->search( $filter );
306
307
		foreach( $data as $entry )
308
		{
309
			$id = $this->val( $entry, 'stock.id' );
310
			$stockItem = $stocks->pull( $id ) ?: $manager->create();
311
			$stockItem->fromArray( $entry )->setProductId( $item->getId() );
312
313
			if( $entry['stock.stockflag'] ?? false ) {
314
				$stockItem->setStockLevel( $stockItem->getStockLevel() + $entry['stock.stockdiff'] ?? 0 );
315
			}
316
317
			$item->setInStock( (int) $stockItem->getStockLevel() > 0 || $stockItem->getStockLevel() === null );
0 ignored issues
show
Bug introduced by
(int)$stockItem->getStoc...etStockLevel() === null of type boolean is incompatible with the type integer expected by parameter $value of Aimeos\MShop\Product\Item\Iface::setInStock(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

317
			$item->setInStock( /** @scrutinizer ignore-type */ (int) $stockItem->getStockLevel() > 0 || $stockItem->getStockLevel() === null );
Loading history...
318
319
			$stockItems[] = $stockItem;
320
		}
321
322
		$manager->delete( $stocks );
323
		$manager->save( $stockItems, false );
324
325
		return $item;
326
	}
327
328
329
	/**
330
	 * Constructs the data array for the view from the given item
331
	 *
332
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
333
	 * @param bool $copy True if items should be copied, false if not
334
	 * @return string[] Multi-dimensional associative list of item data
335
	 */
336
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
337
	{
338
		$data = [];
339
		$context = $this->context();
340
		$siteId = $context->locale()->getSiteId();
341
342
		$manager = \Aimeos\MShop::create( $context, 'stock' );
343
		$filter = $manager->filter()->add( ['stock.productid' => $item->getId()] )->order( 'stock.type' );
344
345
		foreach( $manager->search( $filter ) as $stockItem )
346
		{
347
			$list = $stockItem->toArray( true );
348
349
			if( $copy === true )
350
			{
351
				$list['stock.siteid'] = $siteId;
352
				$list['stock.id'] = '';
353
			}
354
355
			$list['stock.dateback'] = str_replace( ' ', 'T', $list['stock.dateback'] ?? '' );
356
357
			$data[] = $list;
358
		}
359
360
		return $data;
361
	}
362
363
364
	/**
365
	 * Returns the rendered template including the view data
366
	 *
367
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
368
	 * @return string HTML output
369
	 */
370
	protected function render( \Aimeos\Base\View\Iface $view ) : string
371
	{
372
		/** admin/jqadm/product/stock/template-item
373
		 * Relative path to the HTML body template of the stock subpart for products.
374
		 *
375
		 * The template file contains the HTML code and processing instructions
376
		 * to generate the result shown in the body of the frontend. The
377
		 * configuration string is the path to the template file relative
378
		 * to the templates directory (usually in templates/admin/jqadm).
379
		 *
380
		 * You can overwrite the template file configuration in extensions and
381
		 * provide alternative templates. These alternative templates should be
382
		 * named like the default one but with the string "default" replaced by
383
		 * an unique name. You may use the name of your project for this. If
384
		 * you've implemented an alternative client class as well, "default"
385
		 * should be replaced by the name of the new class.
386
		 *
387
		 * @param string Relative path to the template creating the HTML code
388
		 * @since 2016.04
389
		 */
390
		$tplconf = 'admin/jqadm/product/stock/template-item';
391
		$default = 'product/item-stock';
392
393
		return $view->render( $view->config( $tplconf, $default ) );
394
	}
395
}
396