Passed
Push — master ( d92fa9...ba7df9 )
by Aimeos
03:30
created

Standard::fromArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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