Passed
Push — master ( 994b02...57b90d )
by Aimeos
03:27
created

Standard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
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), 2017-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Supplier\Product;
12
13
sprintf( 'product' ); // for translation
14
15
16
/**
17
 * Default implementation of supplier product 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/supplier/product/name
27
	 * Name of the product subpart used by the JQAdm supplier implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Supplier\Product\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 2017.07
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the 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 data( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$manager = \Aimeos\MShop::create( $this->context(), 'product/lists/type' );
47
48
		$search = $manager->filter( true )->slice( 0, 10000 )
49
			->add( ['product.lists.type.domain' => 'supplier'] )
50
			->order( 'product.lists.type.position' );
51
52
		$view->productListTypes = $manager->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->object()->data( $this->view() );
66
		$view->productBody = 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
		$view->productBody = parent::create();
81
82
		return $this->render( $view );
83
	}
84
85
86
	/**
87
	 * Returns a single resource
88
	 *
89
	 * @return string|null HTML output
90
	 */
91
	public function get() : ?string
92
	{
93
		$total = 0;
94
		$view = $this->object()->data( $this->view() );
95
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
96
97
		$filter = $manager->filter();
98
		$func = $filter->make( 'product:has', ['supplier', ['default', 'promotion'], $view->item->getId()] );
99
100
		$params = $this->storeFilter( $view->param( 'cp', [] ), 'supplierproduct' );
101
		$filter = $this->initCriteria( $filter, $params )->add( $func, '!=', null );
102
		$products = $manager->search( $filter, [], $total );
103
104
		$view->productItems = $products;
105
		$view->productData = $this->toArray( $view->item, $products );
106
		$view->productBody = parent::get();
107
		$view->productTotal = $total;
108
109
		return $this->render( $view );
110
	}
111
112
113
	/**
114
	 * Saves the data
115
	 *
116
	 * @return string|null HTML output
117
	 */
118
	public function save() : ?string
119
	{
120
		$view = $this->view();
121
122
		$manager = \Aimeos\MShop::create( $this->context(), 'index' );
123
		$manager->begin();
124
125
		try
126
		{
127
			$this->storeFilter( $view->param( 'cp', [] ), 'supplierproduct' );
128
			$this->fromArray( $view->item, $view->param( 'product', [] ) );
129
			$view->productBody = parent::save();
130
131
			$manager->commit();
132
		}
133
		catch( \Exception $e )
134
		{
135
			$manager->rollback();
136
			throw $e;
137
		}
138
139
		return null;
140
	}
141
142
143
	/**
144
	 * Returns the sub-client given by its name.
145
	 *
146
	 * @param string $type Name of the client type
147
	 * @param string|null $name Name of the sub-client (Default if null)
148
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
149
	 */
150
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
151
	{
152
		/** admin/jqadm/supplier/product/decorators/excludes
153
		 * Excludes decorators added by the "common" option from the supplier JQAdm client
154
		 *
155
		 * Decorators extend the functionality of a class by adding new aspects
156
		 * (e.g. log what is currently done), executing the methods of the underlying
157
		 * class only in certain conditions (e.g. only for logged in users) or
158
		 * modify what is returned to the caller.
159
		 *
160
		 * This option allows you to remove a decorator added via
161
		 * "admin/jqadm/common/decorators/default" before they are wrapped
162
		 * around the JQAdm client.
163
		 *
164
		 *  admin/jqadm/supplier/product/decorators/excludes = array( 'decorator1' )
165
		 *
166
		 * This would remove the decorator named "decorator1" from the list of
167
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
168
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
169
		 *
170
		 * @param array List of decorator names
171
		 * @since 2017.07
172
		 * @category Developer
173
		 * @see admin/jqadm/common/decorators/default
174
		 * @see admin/jqadm/supplier/product/decorators/global
175
		 * @see admin/jqadm/supplier/product/decorators/local
176
		 */
177
178
		/** admin/jqadm/supplier/product/decorators/global
179
		 * Adds a list of globally available decorators only to the supplier 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 wrap global decorators
187
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
188
		 *
189
		 *  admin/jqadm/supplier/product/decorators/global = array( 'decorator1' )
190
		 *
191
		 * This would add the decorator named "decorator1" defined by
192
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
193
		 *
194
		 * @param array List of decorator names
195
		 * @since 2017.07
196
		 * @category Developer
197
		 * @see admin/jqadm/common/decorators/default
198
		 * @see admin/jqadm/supplier/product/decorators/excludes
199
		 * @see admin/jqadm/supplier/product/decorators/local
200
		 */
201
202
		/** admin/jqadm/supplier/product/decorators/local
203
		 * Adds a list of local decorators only to the supplier JQAdm client
204
		 *
205
		 * Decorators extend the functionality of a class by adding new aspects
206
		 * (e.g. log what is currently done), executing the methods of the underlying
207
		 * class only in certain conditions (e.g. only for logged in users) or
208
		 * modify what is returned to the caller.
209
		 *
210
		 * This option allows you to wrap local decorators
211
		 * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client.
212
		 *
213
		 *  admin/jqadm/supplier/product/decorators/local = array( 'decorator2' )
214
		 *
215
		 * This would add the decorator named "decorator2" defined by
216
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
217
		 *
218
		 * @param array List of decorator names
219
		 * @since 2017.07
220
		 * @category Developer
221
		 * @see admin/jqadm/common/decorators/default
222
		 * @see admin/jqadm/supplier/product/decorators/excludes
223
		 * @see admin/jqadm/supplier/product/decorators/global
224
		 */
225
		return $this->createSubClient( 'supplier/product/' . $type, $name );
226
	}
227
228
229
	/**
230
	 * Returns the list of sub-client names configured for the client.
231
	 *
232
	 * @return array List of JQAdm client names
233
	 */
234
	protected function getSubClientNames() : array
235
	{
236
		/** admin/jqadm/supplier/product/subparts
237
		 * List of JQAdm sub-clients rendered within the supplier product section
238
		 *
239
		 * The output of the frontend is composed of the code generated by the JQAdm
240
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
241
		 * that are responsible for rendering certain sub-parts of the output. The
242
		 * sub-clients can contain JQAdm clients themselves and therefore a
243
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
244
		 * the output that is placed inside the container of its parent.
245
		 *
246
		 * At first, always the JQAdm code generated by the parent is printed, then
247
		 * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients
248
		 * determines the product of the output of these sub-clients inside the parent
249
		 * container. If the configured list of clients is
250
		 *
251
		 *  array( "subclient1", "subclient2" )
252
		 *
253
		 * you can easily change the product of the output by reproducting the subparts:
254
		 *
255
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
256
		 *
257
		 * You can also remove one or more parts if they shouldn't be rendered:
258
		 *
259
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
260
		 *
261
		 * As the clients only generates structural JQAdm, the layout defined via CSS
262
		 * should support adding, removing or reproducting content by a fluid like
263
		 * design.
264
		 *
265
		 * @param array List of sub-client names
266
		 * @since 2017.07
267
		 * @category Developer
268
		 */
269
		return $this->context()->config()->get( 'admin/jqadm/supplier/product/subparts', [] );
270
	}
271
272
273
	/**
274
	 * Creates new and updates existing items using the data array
275
	 *
276
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object without referenced domain items
277
	 * @param array $data Data array
278
	 * @return \Aimeos\MShop\Supplier\Item\Iface Modified supplier item
279
	 */
280
	protected function fromArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data ) : \Aimeos\MShop\Supplier\Item\Iface
281
	{
282
		if( empty( $prodIds = $this->val( $data, 'product.lists.parentid', [] ) ) ) {
283
			return $item;
284
		}
285
286
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
287
		$filter = $manager->filter()->add( ['product.id' => $prodIds] )->slice( 0, count( $prodIds ) );
288
		$products = $manager->search( $filter, ['supplier'] );
289
290
		$id = $item->getId();
291
		$listItem = $manager->createListItem()->setRefId( $id );
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

291
		$listItem = $manager->/** @scrutinizer ignore-call */ createListItem()->setRefId( $id );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
292
		$listItems = $products->getListItems( 'supplier', null, null, false )
293
			->flat( 1 )->col( null, 'product.lists.id' );
294
295
		foreach( (array) $prodIds as $idx => $prodId )
296
		{
297
			if( ( $product = $products->get( $prodId ) ) === null ) {
298
				continue;
299
			}
300
301
			$listId = $this->val( $data, 'product.lists.id/' . $idx );
302
			$listItem = $listItems->get( $listId ) ?: clone $listItem;
303
304
			$listItem->setType( $this->val( $data, 'product.lists.type/' . $idx, 'default' ) )
305
				->setConfig( (array) json_decode( $this->val( $data, 'product.lists.config/' . $idx, '{}' ) ) )
306
				->setPosition( (int) $this->val( $data, 'product.lists.position/' . $idx, 0 ) )
307
				->setStatus( (int) $this->val( $data, 'product.lists.status/' . $idx, 1 ) )
308
				->setDateStart( $this->val( $data, 'product.lists.datestart/' . $idx ) )
309
				->setDateEnd( $this->val( $data, 'product.lists.dateend/' . $idx ) );
310
311
			$product->addListItem( 'supplier', $listItem );
312
			$listItems->remove( $listItem->getId() );
313
		}
314
315
		\Aimeos\MShop::create( $this->context(), 'index' )->save( $products );
316
317
		return $item;
318
	}
319
320
321
	/**
322
	 * Constructs the data array for the view from the given item
323
	 *
324
	 * @param \Aimeos\Map $listItems Supplier list items implementing \Aimeos\MShop\Common\Item\Lists\Iface and referencing the products
325
	 * @return string[] Multi-dimensional associative list of item data
326
	 */
327
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, \Aimeos\Map $products ) : array
328
	{
329
		$data = [];
330
331
		foreach( $products->getListItems( 'supplier', null, $item->getId() )->flat( 1 ) as $listItem )
332
		{
333
			foreach( $listItem->toArray( true ) as $key => $value ) {
334
				$data[$key][] = $value;
335
			}
336
		}
337
338
		return $data;
339
	}
340
341
342
	/**
343
	 * Returns the rendered template including the view data
344
	 *
345
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
346
	 * @return string HTML output
347
	 */
348
	protected function render( \Aimeos\MW\View\Iface $view ) : string
349
	{
350
		/** admin/jqadm/supplier/product/template-item
351
		 * Relative path to the HTML body template of the product subpart for suppliers.
352
		 *
353
		 * The template file contains the HTML code and processing instructions
354
		 * to generate the result shown in the body of the frontend. The
355
		 * configuration string is the path to the template file relative
356
		 * to the templates directory (usually in admin/jqadm/templates).
357
		 *
358
		 * You can overwrite the template file configuration in extensions and
359
		 * provide alternative templates. These alternative templates should be
360
		 * named like the default one but with the string "default" replaced by
361
		 * an unique name. You may use the name of your project for this. If
362
		 * you've implemented an alternative client class as well, "default"
363
		 * should be replaced by the name of the new class.
364
		 *
365
		 * @param string Relative path to the template creating the HTML code
366
		 * @since 2016.04
367
		 * @category Developer
368
		 */
369
		$tplconf = 'admin/jqadm/supplier/product/template-item';
370
		$default = 'supplier/item-product-standard';
371
372
		return $view->render( $view->config( $tplconf, $default ) );
373
	}
374
}
375