Completed
Push — master ( 364a36...4d3f1f )
by Aimeos
05:28
created

Standard::fromArray()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
c 0
b 0
f 0
nc 18
nop 2
dl 0
loc 40
rs 8.9297
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
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
	 * Copies a resource
40
	 *
41
	 * @return string HTML output
42
	 */
43
	public function copy()
44
	{
45
		$view = $this->addViewData( $this->getView() );
46
47
		$view->stockData = $this->toArray( $view->item, true );
48
		$view->stockBody = '';
49
50
		foreach( $this->getSubClients() as $client ) {
51
			$view->stockBody .= $client->copy();
52
		}
53
54
		return $this->render( $view );
55
	}
56
57
58
	/**
59
	 * Creates a new resource
60
	 *
61
	 * @return string HTML output
62
	 */
63
	public function create()
64
	{
65
		$view = $this->addViewData( $this->getView() );
66
		$siteid = $this->getContext()->getLocale()->getSiteId();
67
		$data = $view->param( 'stock', [] );
68
69
		foreach( $view->value( $data, 'stock.id', [] ) as $idx => $value ) {
70
			$data[$idx]['stock.siteid'] = $siteid;
71
		}
72
73
		$view->stockData = $data;
74
		$view->stockBody = '';
75
76
		foreach( $this->getSubClients() as $client ) {
77
			$view->stockBody .= $client->create();
78
		}
79
80
		return $this->render( $view );
81
	}
82
83
84
	/**
85
	 * Deletes a resource
86
	 */
87
	public function delete()
88
	{
89
		parent::delete();
90
91
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
92
93
		$code = $this->getView()->item->getCode();
94
		$search = $manager->createSearch();
95
		$search->setConditions( $search->compare( '==', 'stock.productcode', $code ) );
96
97
		$manager->deleteItems( $manager->searchItems( $search ) );
98
	}
99
100
101
	/**
102
	 * Returns a single resource
103
	 *
104
	 * @return string HTML output
105
	 */
106
	public function get()
107
	{
108
		$view = $this->addViewData( $this->getView() );
109
110
		$view->stockData = $this->toArray( $view->item );
111
		$view->stockBody = '';
112
113
		foreach( $this->getSubClients() as $client ) {
114
			$view->stockBody .= $client->get();
115
		}
116
117
		return $this->render( $view );
118
	}
119
120
121
	/**
122
	 * Saves the data
123
	 */
124
	public function save()
125
	{
126
		$view = $this->getView();
127
		$context = $this->getContext();
128
129
		$manager = \Aimeos\MShop::create( $context, 'stock' );
130
		$manager->begin();
131
132
		try
133
		{
134
			$this->fromArray( $view->item, $view->param( 'stock', [] ) );
135
			$view->stockBody = '';
136
137
			foreach( $this->getSubClients() as $client ) {
138
				$view->stockBody .= $client->save();
139
			}
140
141
			$manager->commit();
142
			return;
143
		}
144
		catch( \Aimeos\MShop\Exception $e )
145
		{
146
			$error = array( 'product-item-stock' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
147
			$view->errors = $view->get( 'errors', [] ) + $error;
148
			$this->logException( $e );
149
		}
150
		catch( \Exception $e )
151
		{
152
			$error = array( 'product-item-stock' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
153
			$view->errors = $view->get( 'errors', [] ) + $error;
154
			$this->logException( $e );
155
		}
156
157
		$manager->rollback();
158
159
		throw new \Aimeos\Admin\JQAdm\Exception();
160
	}
161
162
163
	/**
164
	 * Returns the sub-client given by its name.
165
	 *
166
	 * @param string $type Name of the client type
167
	 * @param string|null $name Name of the sub-client (Default if null)
168
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
169
	 */
170
	public function getSubClient( $type, $name = null )
171
	{
172
		/** admin/jqadm/product/stock/decorators/excludes
173
		 * Excludes decorators added by the "common" option from the product JQAdm client
174
		 *
175
		 * Decorators extend the functionality of a class by adding new aspects
176
		 * (e.g. log what is currently done), executing the methods of the underlying
177
		 * class only in certain conditions (e.g. only for logged in users) or
178
		 * modify what is returned to the caller.
179
		 *
180
		 * This option allows you to remove a decorator added via
181
		 * "admin/jqadm/common/decorators/default" before they are wrapped
182
		 * around the JQAdm client.
183
		 *
184
		 *  admin/jqadm/product/stock/decorators/excludes = array( 'decorator1' )
185
		 *
186
		 * This would remove the decorator named "decorator1" from the list of
187
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
188
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
189
		 *
190
		 * @param array List of decorator names
191
		 * @since 2016.01
192
		 * @category Developer
193
		 * @see admin/jqadm/common/decorators/default
194
		 * @see admin/jqadm/product/stock/decorators/global
195
		 * @see admin/jqadm/product/stock/decorators/local
196
		 */
197
198
		/** admin/jqadm/product/stock/decorators/global
199
		 * Adds a list of globally available decorators only to the product JQAdm client
200
		 *
201
		 * Decorators extend the functionality of a class by adding new aspects
202
		 * (e.g. log what is currently done), executing the methods of the underlying
203
		 * class only in certain conditions (e.g. only for logged in users) or
204
		 * modify what is returned to the caller.
205
		 *
206
		 * This option allows you to wrap global decorators
207
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
208
		 *
209
		 *  admin/jqadm/product/stock/decorators/global = array( 'decorator1' )
210
		 *
211
		 * This would add the decorator named "decorator1" defined by
212
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
213
		 *
214
		 * @param array List of decorator names
215
		 * @since 2016.01
216
		 * @category Developer
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
		 * @category Developer
241
		 * @see admin/jqadm/common/decorators/default
242
		 * @see admin/jqadm/product/stock/decorators/excludes
243
		 * @see admin/jqadm/product/stock/decorators/global
244
		 */
245
		return $this->createSubClient( 'product/stock/' . $type, $name );
246
	}
247
248
249
	/**
250
	 * Adds the required data used in the stock template
251
	 *
252
	 * @param \Aimeos\MW\View\Iface $view View object
253
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
254
	 */
255
	protected function addViewData( \Aimeos\MW\View\Iface $view )
256
	{
257
		$typeManager = \Aimeos\MShop::create( $this->getContext(), 'stock/type' );
258
259
		$search = $typeManager->createSearch( true )->setSlice( 0, 10000 );
260
		$search->setConditions( $search->compare( '==', 'stock.type.domain', 'product' ) );
261
		$search->setSortations( [$search->sort( '+', 'stock.type.position' )] );
262
263
		$view->stockTypes = $this->map( $typeManager->searchItems( $search ) );
264
265
		return $view;
266
	}
267
268
269
	/**
270
	 * Returns the list of sub-client names configured for the client.
271
	 *
272
	 * @return array List of JQAdm client names
273
	 */
274
	protected function getSubClientNames()
275
	{
276
		/** admin/jqadm/product/stock/standard/subparts
277
		 * List of JQAdm sub-clients rendered within the product stock section
278
		 *
279
		 * The output of the frontend is composed of the code generated by the JQAdm
280
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
281
		 * that are responsible for rendering certain sub-parts of the output. The
282
		 * sub-clients can contain JQAdm clients themselves and therefore a
283
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
284
		 * the output that is placed inside the container of its parent.
285
		 *
286
		 * At first, always the JQAdm code generated by the parent is printed, then
287
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
288
		 * determines the order of the output of these sub-clients inside the parent
289
		 * container. If the configured list of clients is
290
		 *
291
		 *  array( "subclient1", "subclient2" )
292
		 *
293
		 * you can easily change the order of the output by reordering the subparts:
294
		 *
295
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
296
		 *
297
		 * You can also remove one or more parts if they shouldn't be rendered:
298
		 *
299
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
300
		 *
301
		 * As the clients only generates structural JQAdm, the layout defined via CSS
302
		 * should support adding, removing or reordering content by a fluid like
303
		 * design.
304
		 *
305
		 * @param array List of sub-client names
306
		 * @since 2016.01
307
		 * @category Developer
308
		 */
309
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/stock/standard/subparts', [] );
310
	}
311
312
313
	/**
314
	 * Creates new and updates existing items using the data array
315
	 *
316
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
317
	 * @param array $data Data array
318
	 */
319
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data )
320
	{
321
		$ids = $stocks = $stockItems = [];
322
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
323
324
		foreach( $data as $entry )
325
		{
326
			if( ( $id = $this->getValue( $entry, 'stock.id' ) ) !== null ) {
327
				$ids[] = $id;
328
			}
329
		}
330
331
		if( !empty( $ids ) )
332
		{
333
			$search = $manager->createSearch();
334
			$search->setConditions( $search->compare( '==', 'stock.id', $ids ) );
335
			$stocks = $manager->searchitems( $search );
336
		}
337
338
		foreach( $data as $entry )
339
		{
340
			$id = $this->getValue( $entry, 'stock.id' );
341
342
			if( !isset( $stocks[$id] ) ) {
343
				$stockItem = $manager->createItem();
344
			} else {
345
				$stockItem = $stocks[$id];
346
			}
347
348
			$stockItems[] = $stockItem->setProductCode( $item->getCode() )
0 ignored issues
show
Bug introduced by
The method setProductCode() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Item\Iface such as Aimeos\MShop\Order\Item\Base\Product\Iface or Aimeos\MShop\Stock\Item\Iface or Aimeos\MShop\Stock\Item\Standard or Aimeos\MShop\Order\Item\Base\Product\Standard or Aimeos\MShop\Order\Item\Base\Product\Iface. ( Ignorable by Annotation )

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

348
			$stockItems[] = $stockItem->/** @scrutinizer ignore-call */ setProductCode( $item->getCode() )
Loading history...
349
				->setType( $this->getValue( $entry, 'stock.type', 'default' ) )
350
				->setStockLevel( $this->getValue( $entry, 'stock.stocklevel' ) )
351
				->setTimeFrame( $this->getValue( $entry, 'stock.timeframe' ) )
352
				->setDateBack( $this->getValue( $entry, 'stock.dateback' ) );
353
354
			unset( $stocks[$id] );
355
		}
356
357
		$manager->deleteItems( $stocks );
358
		$manager->saveItems( $stockItems, false );
359
	}
360
361
362
	/**
363
	 * Constructs the data array for the view from the given item
364
	 *
365
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
366
	 * @param boolean $copy True if items should be copied, false if not
367
	 * @return string[] Multi-dimensional associative list of item data
368
	 */
369
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, $copy = false )
370
	{
371
		$data = [];
372
		$context = $this->getContext();
373
		$siteId = $context->getLocale()->getSiteId();
374
		$manager = \Aimeos\MShop::create( $context, 'stock' );
375
376
		$search = $manager->createSearch();
377
		$search->setConditions( $search->compare( '==', 'stock.productcode', $item->getCode() ) );
378
		$search->setSortations( array( $search->sort( '+', 'stock.type' ) ) );
379
380
		foreach( $manager->searchItems( $search ) as $stockItem )
381
		{
382
			$list = $stockItem->toArray( true );
383
384
			if( $copy === true )
385
			{
386
				$list['stock.siteid'] = $siteId;
387
				$list['stock.id'] = '';
388
			}
389
390
			$list['stock.dateback'] = str_replace( ' ', 'T', $list['stock.dateback'] );
391
392
			$data[] = $list;
393
		}
394
395
		return $data;
396
	}
397
398
399
	/**
400
	 * Returns the rendered template including the view data
401
	 *
402
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
403
	 * @return string HTML output
404
	 */
405
	protected function render( \Aimeos\MW\View\Iface $view )
406
	{
407
		/** admin/jqadm/product/stock/template-item
408
		 * Relative path to the HTML body template of the stock subpart for products.
409
		 *
410
		 * The template file contains the HTML code and processing instructions
411
		 * to generate the result shown in the body of the frontend. The
412
		 * configuration string is the path to the template file relative
413
		 * to the templates directory (usually in admin/jqadm/templates).
414
		 *
415
		 * You can overwrite the template file configuration in extensions and
416
		 * provide alternative templates. These alternative templates should be
417
		 * named like the default one but with the string "default" replaced by
418
		 * an unique name. You may use the name of your project for this. If
419
		 * you've implemented an alternative client class as well, "default"
420
		 * should be replaced by the name of the new class.
421
		 *
422
		 * @param string Relative path to the template creating the HTML code
423
		 * @since 2016.04
424
		 * @category Developer
425
		 */
426
		$tplconf = 'admin/jqadm/product/stock/template-item';
427
		$default = 'product/item-stock-standard';
428
429
		return $view->render( $view->config( $tplconf, $default ) );
430
	}
431
}
432