Standard::fromArray()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

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