Passed
Push — master ( e80043...caca46 )
by Aimeos
04:27 queued 15s
created

Standard::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 12
rs 10
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-2024
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
	 */
35
36
37
	/**
38
	 * Adds the required data used in the 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
		$manager = \Aimeos\MShop::create( $this->context(), 'product/lists/type' );
46
47
		$search = $manager->filter( true )
48
			->add( ['product.lists.type.domain' => 'supplier'] )
49
			->order( 'product.lists.type.position' )
50
			->slice( 0, 10000 );
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
		$view = $this->object()->data( $this->view() );
94
		$view->productBody = parent::get();
95
96
		return $this->render( $view );
97
	}
98
99
100
	/**
101
	 * Saves the data
102
	 *
103
	 * @return string|null HTML output
104
	 */
105
	public function save() : ?string
106
	{
107
		$view = $this->view();
108
109
		$manager = \Aimeos\MShop::create( $this->context(), 'index' );
110
		$manager->begin();
111
112
		try
113
		{
114
			$this->storeFilter( $view->param( 'cp', [] ), 'supplierproduct' );
115
			$this->fromArray( $view->item, $view->param( 'product', [] ) );
116
			$view->productBody = parent::save();
117
118
			$manager->commit();
119
		}
120
		catch( \Exception $e )
121
		{
122
			$manager->rollback();
123
			throw $e;
124
		}
125
126
		return null;
127
	}
128
129
130
	/**
131
	 * Returns the sub-client given by its name.
132
	 *
133
	 * @param string $type Name of the client type
134
	 * @param string|null $name Name of the sub-client (Default if null)
135
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
136
	 */
137
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
138
	{
139
		/** admin/jqadm/supplier/product/decorators/excludes
140
		 * Excludes decorators added by the "common" option from the supplier JQAdm client
141
		 *
142
		 * Decorators extend the functionality of a class by adding new aspects
143
		 * (e.g. log what is currently done), executing the methods of the underlying
144
		 * class only in certain conditions (e.g. only for logged in users) or
145
		 * modify what is returned to the caller.
146
		 *
147
		 * This option allows you to remove a decorator added via
148
		 * "admin/jqadm/common/decorators/default" before they are wrapped
149
		 * around the JQAdm client.
150
		 *
151
		 *  admin/jqadm/supplier/product/decorators/excludes = array( 'decorator1' )
152
		 *
153
		 * This would remove the decorator named "decorator1" from the list of
154
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
155
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
156
		 *
157
		 * @param array List of decorator names
158
		 * @since 2017.07
159
		 * @see admin/jqadm/common/decorators/default
160
		 * @see admin/jqadm/supplier/product/decorators/global
161
		 * @see admin/jqadm/supplier/product/decorators/local
162
		 */
163
164
		/** admin/jqadm/supplier/product/decorators/global
165
		 * Adds a list of globally available decorators only to the supplier JQAdm client
166
		 *
167
		 * Decorators extend the functionality of a class by adding new aspects
168
		 * (e.g. log what is currently done), executing the methods of the underlying
169
		 * class only in certain conditions (e.g. only for logged in users) or
170
		 * modify what is returned to the caller.
171
		 *
172
		 * This option allows you to wrap global decorators
173
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
174
		 *
175
		 *  admin/jqadm/supplier/product/decorators/global = array( 'decorator1' )
176
		 *
177
		 * This would add the decorator named "decorator1" defined by
178
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
179
		 *
180
		 * @param array List of decorator names
181
		 * @since 2017.07
182
		 * @see admin/jqadm/common/decorators/default
183
		 * @see admin/jqadm/supplier/product/decorators/excludes
184
		 * @see admin/jqadm/supplier/product/decorators/local
185
		 */
186
187
		/** admin/jqadm/supplier/product/decorators/local
188
		 * Adds a list of local decorators only to the supplier JQAdm client
189
		 *
190
		 * Decorators extend the functionality of a class by adding new aspects
191
		 * (e.g. log what is currently done), executing the methods of the underlying
192
		 * class only in certain conditions (e.g. only for logged in users) or
193
		 * modify what is returned to the caller.
194
		 *
195
		 * This option allows you to wrap local decorators
196
		 * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client.
197
		 *
198
		 *  admin/jqadm/supplier/product/decorators/local = array( 'decorator2' )
199
		 *
200
		 * This would add the decorator named "decorator2" defined by
201
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
202
		 *
203
		 * @param array List of decorator names
204
		 * @since 2017.07
205
		 * @see admin/jqadm/common/decorators/default
206
		 * @see admin/jqadm/supplier/product/decorators/excludes
207
		 * @see admin/jqadm/supplier/product/decorators/global
208
		 */
209
		return $this->createSubClient( 'supplier/product/' . $type, $name );
210
	}
211
212
213
	/**
214
	 * Returns the list of sub-client names configured for the client.
215
	 *
216
	 * @return array List of JQAdm client names
217
	 */
218
	protected function getSubClientNames() : array
219
	{
220
		/** admin/jqadm/supplier/product/subparts
221
		 * List of JQAdm sub-clients rendered within the supplier product section
222
		 *
223
		 * The output of the frontend is composed of the code generated by the JQAdm
224
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
225
		 * that are responsible for rendering certain sub-parts of the output. The
226
		 * sub-clients can contain JQAdm clients themselves and therefore a
227
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
228
		 * the output that is placed inside the container of its parent.
229
		 *
230
		 * At first, always the JQAdm code generated by the parent is printed, then
231
		 * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients
232
		 * determines the product of the output of these sub-clients inside the parent
233
		 * container. If the configured list of clients is
234
		 *
235
		 *  array( "subclient1", "subclient2" )
236
		 *
237
		 * you can easily change the product of the output by reproducting the subparts:
238
		 *
239
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
240
		 *
241
		 * You can also remove one or more parts if they shouldn't be rendered:
242
		 *
243
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
244
		 *
245
		 * As the clients only generates structural JQAdm, the layout defined via CSS
246
		 * should support adding, removing or reproducting content by a fluid like
247
		 * design.
248
		 *
249
		 * @param array List of sub-client names
250
		 * @since 2017.07
251
		 */
252
		return $this->context()->config()->get( 'admin/jqadm/supplier/product/subparts', [] );
253
	}
254
255
256
	/**
257
	 * Creates new and updates existing items using the data array
258
	 *
259
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object without referenced domain items
260
	 * @param array $data Data array
261
	 * @return \Aimeos\MShop\Supplier\Item\Iface Modified supplier item
262
	 */
263
	protected function fromArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data ) : \Aimeos\MShop\Supplier\Item\Iface
264
	{
265
		if( empty( $prodIds = $this->val( $data, 'product.lists.parentid', [] ) ) ) {
266
			return $item;
267
		}
268
269
		$context = $this->context();
270
		$manager = \Aimeos\MShop::create( $context, 'product' );
271
272
		$filter = $manager->filter()->add( ['product.id' => $prodIds] )->slice( 0, count( $prodIds ) );
273
		$products = $manager->search( $filter, $context->config()->get( 'mshop/index/manager/domains', ['supplier'] ) );
274
275
		$id = $item->getId();
276
		$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

276
		$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...
277
		$listItems = $products->getListItems( 'supplier', null, null, false )
278
			->flat( 1 )->col( null, 'product.lists.id' );
279
280
		foreach( (array) $prodIds as $idx => $prodId )
281
		{
282
			if( ( $product = $products->get( $prodId ) ) === null ) {
283
				continue;
284
			}
285
286
			$listId = $this->val( $data, 'product.lists.id/' . $idx );
287
			$listItem = $listItems->get( $listId ) ?: clone $listItem;
288
289
			$listItem->setType( $this->val( $data, 'product.lists.type/' . $idx, 'default' ) )
290
				->setConfig( (array) json_decode( $this->val( $data, 'product.lists.config/' . $idx, '{}' ) ) )
291
				->setPosition( (int) $this->val( $data, 'product.lists.position/' . $idx, 0 ) )
292
				->setStatus( (int) $this->val( $data, 'product.lists.status/' . $idx, 1 ) )
293
				->setDateStart( $this->val( $data, 'product.lists.datestart/' . $idx ) )
294
				->setDateEnd( $this->val( $data, 'product.lists.dateend/' . $idx ) );
295
296
			$product->addListItem( 'supplier', $listItem );
297
			$listItems->remove( $listItem->getId() );
298
		}
299
300
		\Aimeos\MShop::create( $context, 'index' )->save( $products );
301
302
		return $item;
303
	}
304
305
306
	/**
307
	 * Returns the rendered template including the view data
308
	 *
309
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
310
	 * @return string HTML output
311
	 */
312
	protected function render( \Aimeos\Base\View\Iface $view ) : string
313
	{
314
		/** admin/jqadm/supplier/product/template-item
315
		 * Relative path to the HTML body template of the product subpart for suppliers.
316
		 *
317
		 * The template file contains the HTML code and processing instructions
318
		 * to generate the result shown in the body of the frontend. The
319
		 * configuration string is the path to the template file relative
320
		 * to the templates directory (usually in templates/admin/jqadm).
321
		 *
322
		 * You can overwrite the template file configuration in extensions and
323
		 * provide alternative templates. These alternative templates should be
324
		 * named like the default one but with the string "default" replaced by
325
		 * an unique name. You may use the name of your project for this. If
326
		 * you've implemented an alternative client class as well, "default"
327
		 * should be replaced by the name of the new class.
328
		 *
329
		 * @param string Relative path to the template creating the HTML code
330
		 * @since 2016.04
331
		 */
332
		$tplconf = 'admin/jqadm/supplier/product/template-item';
333
		$default = 'supplier/item-product';
334
335
		return $view->render( $view->config( $tplconf, $default ) );
336
	}
337
}
338