Passed
Push — master ( c734ec...eff6db )
by Aimeos
12:57
created

Standard::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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