Passed
Push — master ( 41c9aa...3eceb3 )
by Aimeos
03:14
created

Standard::toArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

251
			$litem = $listItems->pull( $listid ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

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...
252
253
			$litem->setType( $this->val( $entry, 'product.lists.type' ) )
254
				->setRefId( $this->val( $entry, 'catalog.id' ) );
255
256
			$item->addListItem( 'catalog', $litem );
257
		}
258
259
		$item->deleteListItems( $listItems );
260
	}
261
262
263
	/**
264
	 * Constructs the data array for the view from the given item
265
	 *
266
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
267
	 * @param bool $copy True if items should be copied, false if not
268
	 * @return string[] Multi-dimensional associative list of item data
269
	 */
270
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
271
	{
272
		$data = [];
273
		$siteId = $this->context()->locale()->getSiteId();
274
275
		foreach( $item->getListItems( 'catalog' ) as $listItem )
276
		{
277
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
278
				continue;
279
			}
280
281
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
282
283
			if( $copy === true )
284
			{
285
				$list['product.lists.siteid'] = $siteId;
286
				$list['product.lists.id'] = '';
287
			}
288
289
			$data[] = $list;
290
		}
291
292
		return $data;
293
	}
294
295
296
	/**
297
	 * Returns the rendered template including the view data
298
	 *
299
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
300
	 * @return string HTML output
301
	 */
302
	protected function render( \Aimeos\Base\View\Iface $view ) : string
303
	{
304
		/** admin/jqadm/product/category/template-item
305
		 * Relative path to the HTML body template of the category subpart for products.
306
		 *
307
		 * The template file contains the HTML code and processing instructions
308
		 * to generate the result shown in the body of the frontend. The
309
		 * configuration string is the path to the template file relative
310
		 * to the templates directory (usually in admin/jqadm/templates).
311
		 *
312
		 * You can overwrite the template file configuration in extensions and
313
		 * provide alternative templates. These alternative templates should be
314
		 * named like the default one but with the string "default" replaced by
315
		 * an unique name. You may use the name of your project for this. If
316
		 * you've implemented an alternative client class as well, "default"
317
		 * should be replaced by the name of the new class.
318
		 *
319
		 * @param string Relative path to the template creating the HTML code
320
		 * @since 2016.04
321
		 * @category Developer
322
		 */
323
		$tplconf = 'admin/jqadm/product/category/template-item';
324
		$default = 'product/item-category';
325
326
		return $view->render( $view->config( $tplconf, $default ) );
327
	}
328
}
329