Passed
Push — master ( 54ca64...d44c58 )
by Aimeos
04:37
created

Standard   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 99
c 1
b 0
f 0
dl 0
loc 419
rs 10
wmc 24

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubClientNames() 0 36 1
A toArray() 0 27 4
A getListItems() 0 12 1
A getSupplierItems() 0 9 1
A render() 0 25 1
A get() 0 11 2
A copy() 0 12 2
A fromArray() 0 22 3
A getSubClient() 0 76 1
A delete() 0 29 2
A save() 0 25 3
A create() 0 18 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Supplier;
12
13
sprintf( 'supplier' ); // for translation
14
15
16
/**
17
 * Default implementation of product supplier 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/supplier/name
27
	 * Name of the supplier subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Supplier\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 2020.04
34
	 * @supplier 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->getObject()->addData( $this->getView() );
46
47
		$view->supplierData = $this->toArray( $view->item, true );
48
		$view->supplierBody = '';
49
50
		foreach( $this->getSubClients() as $client ) {
51
			$view->supplierBody .= $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->getObject()->addData( $this->getView() );
66
		$siteid = $this->getContext()->getLocale()->getSiteId();
67
		$data = $view->param( 'supplier', [] );
68
69
		foreach( $view->value( $data, 'supplier.lists.id', [] ) as $idx => $value ) {
70
			$data['supplier.lists.siteid'][$idx] = $siteid;
71
		}
72
73
		$view->supplierData = $data;
74
		$view->supplierBody = '';
75
76
		foreach( $this->getSubClients() as $client ) {
77
			$view->supplierBody .= $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
		$view = $this->getView();
92
93
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/lists' );
94
95
		$search = $manager->createSearch();
96
		$expr = array(
97
			$search->compare( '==', 'supplier.lists.refid', $view->param( 'id' ) ),
98
			$search->compare( '==', 'supplier.lists.domain', 'product' )
99
		);
100
		$search->setConditions( $search->combine( '&&', $expr ) );
101
		$search->setSlice( 0, 0x7fffffff );
102
103
		$start = 0;
104
105
		do
106
		{
107
			$search->setSlice( $start );
108
109
			$result = $manager->searchItems( $search );
110
			$manager->deleteItems( $result->toArray() );
111
112
			$count = count( $result );
113
			$start += $count;
114
		}
115
		while( $count >= $search->getSliceSize() );
116
117
		return null;
118
	}
119
120
121
	/**
122
	 * Returns a single resource
123
	 *
124
	 * @return string|null HTML output
125
	 */
126
	public function get() : ?string
127
	{
128
		$view = $this->getObject()->addData( $this->getView() );
129
		$view->supplierData = $this->toArray( $view->item );
130
		$view->supplierBody = '';
131
132
		foreach( $this->getSubClients() as $client ) {
133
			$view->supplierBody .= $client->get();
134
		}
135
136
		return $this->render( $view );
137
	}
138
139
140
	/**
141
	 * Saves the data
142
	 *
143
	 * @return string|null HTML output
144
	 */
145
	public function save() : ?string
146
	{
147
		$view = $this->getView();
148
149
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/lists' );
150
		$manager->begin();
151
152
		try
153
		{
154
			$this->fromArray( $view->item, $view->param( 'supplier', [] ) );
155
			$view->supplierBody = '';
156
157
			foreach( $this->getSubClients() as $client ) {
158
				$view->supplierBody .= $client->save();
159
			}
160
161
			$manager->commit();
162
		}
163
		catch( \Exception $e )
164
		{
165
			$manager->rollback();
166
			throw $e;
167
		}
168
169
		return null;
170
	}
171
172
173
	/**
174
	 * Returns the sub-client given by its name.
175
	 *
176
	 * @param string $type Name of the client type
177
	 * @param string|null $name Name of the sub-client (Default if null)
178
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
179
	 */
180
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
181
	{
182
		/** admin/jqadm/product/supplier/decorators/excludes
183
		 * Excludes decorators added by the "common" option from the product JQAdm client
184
		 *
185
		 * Decorators extend the functionality of a class by adding new aspects
186
		 * (e.g. log what is currently done), executing the methods of the underlying
187
		 * class only in certain conditions (e.g. only for logged in users) or
188
		 * modify what is returned to the caller.
189
		 *
190
		 * This option allows you to remove a decorator added via
191
		 * "admin/jqadm/common/decorators/default" before they are wrapped
192
		 * around the JQAdm client.
193
		 *
194
		 *  admin/jqadm/product/supplier/decorators/excludes = array( 'decorator1' )
195
		 *
196
		 * This would remove the decorator named "decorator1" from the list of
197
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
198
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
199
		 *
200
		 * @param array List of decorator names
201
		 * @since 2020.04
202
		 * @supplier Developer
203
		 * @see admin/jqadm/common/decorators/default
204
		 * @see admin/jqadm/product/supplier/decorators/global
205
		 * @see admin/jqadm/product/supplier/decorators/local
206
		 */
207
208
		/** admin/jqadm/product/supplier/decorators/global
209
		 * Adds a list of globally available decorators only to the product JQAdm client
210
		 *
211
		 * Decorators extend the functionality of a class by adding new aspects
212
		 * (e.g. log what is currently done), executing the methods of the underlying
213
		 * class only in certain conditions (e.g. only for logged in users) or
214
		 * modify what is returned to the caller.
215
		 *
216
		 * This option allows you to wrap global decorators
217
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
218
		 *
219
		 *  admin/jqadm/product/supplier/decorators/global = array( 'decorator1' )
220
		 *
221
		 * This would add the decorator named "decorator1" defined by
222
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
223
		 *
224
		 * @param array List of decorator names
225
		 * @since 2020.04
226
		 * @supplier Developer
227
		 * @see admin/jqadm/common/decorators/default
228
		 * @see admin/jqadm/product/supplier/decorators/excludes
229
		 * @see admin/jqadm/product/supplier/decorators/local
230
		 */
231
232
		/** admin/jqadm/product/supplier/decorators/local
233
		 * Adds a list of local decorators only to the product JQAdm client
234
		 *
235
		 * Decorators extend the functionality of a class by adding new aspects
236
		 * (e.g. log what is currently done), executing the methods of the underlying
237
		 * class only in certain conditions (e.g. only for logged in users) or
238
		 * modify what is returned to the caller.
239
		 *
240
		 * This option allows you to wrap local decorators
241
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
242
		 *
243
		 *  admin/jqadm/product/supplier/decorators/local = array( 'decorator2' )
244
		 *
245
		 * This would add the decorator named "decorator2" defined by
246
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
247
		 *
248
		 * @param array List of decorator names
249
		 * @since 2020.04
250
		 * @supplier Developer
251
		 * @see admin/jqadm/common/decorators/default
252
		 * @see admin/jqadm/product/supplier/decorators/excludes
253
		 * @see admin/jqadm/product/supplier/decorators/global
254
		 */
255
		return $this->createSubClient( 'product/supplier/' . $type, $name );
256
	}
257
258
259
	/**
260
	 * Returns the list of sub-client names configured for the client.
261
	 *
262
	 * @return array List of JQAdm client names
263
	 */
264
	protected function getSubClientNames() : array
265
	{
266
		/** admin/jqadm/product/supplier/standard/subparts
267
		 * List of JQAdm sub-clients rendered within the product supplier section
268
		 *
269
		 * The output of the frontend is composed of the code generated by the JQAdm
270
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
271
		 * that are responsible for rendering certain sub-parts of the output. The
272
		 * sub-clients can contain JQAdm clients themselves and therefore a
273
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
274
		 * the output that is placed inside the container of its parent.
275
		 *
276
		 * At first, always the JQAdm code generated by the parent is printed, then
277
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
278
		 * determines the order of the output of these sub-clients inside the parent
279
		 * container. If the configured list of clients is
280
		 *
281
		 *  array( "subclient1", "subclient2" )
282
		 *
283
		 * you can easily change the order of the output by reordering the subparts:
284
		 *
285
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
286
		 *
287
		 * You can also remove one or more parts if they shouldn't be rendered:
288
		 *
289
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
290
		 *
291
		 * As the clients only generates structural JQAdm, the layout defined via CSS
292
		 * should support adding, removing or reordering content by a fluid like
293
		 * design.
294
		 *
295
		 * @param array List of sub-client names
296
		 * @since 2020.04
297
		 * @supplier Developer
298
		 */
299
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/supplier/standard/subparts', [] );
300
	}
301
302
303
	/**
304
	 * Returns the supplier items for the given supplier list items
305
	 *
306
	 * @param \Aimeos\Map $listItems List of items implementing \Aimeos\Common\Item\Lists\Iface
307
	 * @return \Aimeos\Map List of supplier IDs as keys and items implementing \Aimeos\Catalog\Item\Iface
308
	 */
309
	protected function getSupplierItems( \Aimeos\Map $listItems ) : \Aimeos\Map
310
	{
311
		$ids = $listItems->getParentId()->toArray();
312
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' );
313
314
		$search = $manager->createSearch();
315
		$search->setConditions( $search->compare( '==', 'supplier.id', $ids ) );
316
317
		return $manager->searchItems( $search );
318
	}
319
320
321
	/**
322
	 * Returns the supplier list items for the given product ID
323
	 *
324
	 * @param string $prodid Unique product ID
325
	 * @return \Aimeos\Map Associative list of supplier list IDs as keys and list items as values
326
	 */
327
	protected function getListItems( string $prodid ) : \Aimeos\Map
328
	{
329
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/lists' );
330
331
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
332
		$expr = array(
333
			$search->compare( '==', 'supplier.lists.refid', $prodid ),
334
			$search->compare( '==', 'supplier.lists.domain', 'product' ),
335
		);
336
		$search->setConditions( $search->combine( '&&', $expr ) );
337
338
		return $manager->searchItems( $search );
339
	}
340
341
342
	/**
343
	 * Creates new and updates existing items using the data array
344
	 *
345
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
346
	 * @param array $data Data array
347
	 */
348
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data )
349
	{
350
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/lists' );
351
		$listItems = $this->getListItems( $item->getId() );
0 ignored issues
show
Bug introduced by
It seems like $item->getId() can also be of type null; however, parameter $prodid of Aimeos\Admin\JQAdm\Produ...tandard::getListItems() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

351
		$listItems = $this->getListItems( /** @scrutinizer ignore-type */ $item->getId() );
Loading history...
352
		$list = [];
353
354
		foreach( $data as $idx => $entry )
355
		{
356
			if( isset( $listItems[$entry['supplier.lists.id']] ) ) {
357
				$litem = $listItems[$entry['supplier.lists.id']];
358
			} else {
359
				$litem = $manager->createItem();
360
			}
361
362
			$list[] = $litem->setParentId( $this->getValue( $entry, 'supplier.id' ) )->setDomain( 'product' )
363
				->setType( $this->getValue( $entry, 'supplier.lists.type' ) )->setRefId( $item->getId() );
364
365
			unset( $listItems[$litem->getId()] );
366
		}
367
368
		$manager->deleteItems( $listItems->toArray() );
369
		$manager->saveItems( $list );
370
	}
371
372
373
	/**
374
	 * Constructs the data array for the view from the given item
375
	 *
376
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
377
	 * @param bool $copy True if items should be copied, false if not
378
	 * @return string[] Multi-dimensional associative list of item data
379
	 */
380
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
381
	{
382
		$siteId = $this->getContext()->getLocale()->getSiteId();
383
		$listItems = $this->getListItems( $item->getId() );
0 ignored issues
show
Bug introduced by
It seems like $item->getId() can also be of type null; however, parameter $prodid of Aimeos\Admin\JQAdm\Produ...tandard::getListItems() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

383
		$listItems = $this->getListItems( /** @scrutinizer ignore-type */ $item->getId() );
Loading history...
384
		$supItems = $this->getSupplierItems( $listItems );
385
		$data = [];
386
387
		foreach( $listItems as $listItem )
388
		{
389
			$catId = $listItem->getParentId();
390
391
			if( ( $supItem = $supItems->get( $catId ) ) === null ) {
392
				continue;
393
			}
394
395
			$list = $listItem->toArray( true ) + $supItem->toArray( true );
396
397
			if( $copy === true )
398
			{
399
				$list['supplier.lists.siteid'] = $siteId;
400
				$list['supplier.lists.id'] = '';
401
			}
402
403
			$data[] = $list;
404
		}
405
406
		return $data;
407
	}
408
409
410
	/**
411
	 * Returns the rendered template including the view data
412
	 *
413
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
414
	 * @return string|null HTML output
415
	 */
416
	protected function render( \Aimeos\MW\View\Iface $view ) : string
417
	{
418
		/** admin/jqadm/product/supplier/template-item
419
		 * Relative path to the HTML body template of the supplier subpart for products.
420
		 *
421
		 * The template file contains the HTML code and processing instructions
422
		 * to generate the result shown in the body of the frontend. The
423
		 * configuration string is the path to the template file relative
424
		 * to the templates directory (usually in admin/jqadm/templates).
425
		 *
426
		 * You can overwrite the template file configuration in extensions and
427
		 * provide alternative templates. These alternative templates should be
428
		 * named like the default one but with the string "default" replaced by
429
		 * an unique name. You may use the name of your project for this. If
430
		 * you've implemented an alternative client class as well, "default"
431
		 * should be replaced by the name of the new class.
432
		 *
433
		 * @param string Relative path to the template creating the HTML code
434
		 * @since 2020.04
435
		 * @supplier Developer
436
		 */
437
		$tplconf = 'admin/jqadm/product/supplier/template-item';
438
		$default = 'product/item-supplier-standard';
439
440
		return $view->render( $view->config( $tplconf, $default ) );
441
	}
442
}
443