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

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