Passed
Push — master ( 596853...253ba3 )
by Aimeos
16:40
created

Standard::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
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), 2017-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Attribute\Property;
12
13
sprintf( 'property' ); // for translation
14
15
16
/**
17
 * Default implementation of attribute property 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/attribute/property/name
27
	 * Name of the property subpart used by the JQAdm attribute implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Attribute\Property\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
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the template
40
	 *
41
	 * @param \Aimeos\MW\View\Iface $view View object
42
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
43
	 */
44
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute/property/type' );
47
48
		$search = $manager->filter( true )->slice( 0, 10000 );
49
		$search->setSortations( [$search->sort( '+', 'attribute.property.type.position' )] );
50
51
		$view->propertyTypes = $manager->search( $search );
52
53
		return $view;
54
	}
55
56
57
	/**
58
	 * Copies a resource
59
	 *
60
	 * @return string|null HTML output
61
	 */
62
	public function copy() : ?string
63
	{
64
		$view = $this->getObject()->addData( $this->getView() );
65
		$view->propertyData = $this->toArray( $view->item, true );
66
		$view->propertyBody = 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->getObject()->addData( $this->getView() );
80
		$siteid = $this->getContext()->getLocale()->getSiteId();
81
		$data = $view->param( 'property', [] );
82
83
		foreach( $data as $idx => $entry ) {
84
			$data[$idx]['attribute.lists.siteid'] = $siteid;
85
		}
86
87
		$view->propertyData = $data;
88
		$view->propertyBody = parent::create();
89
90
		return $this->render( $view );
91
	}
92
93
94
	/**
95
	 * Returns a single resource
96
	 *
97
	 * @return string|null HTML output
98
	 */
99
	public function get() : ?string
100
	{
101
		$view = $this->getObject()->addData( $this->getView() );
102
		$view->propertyData = $this->toArray( $view->item );
103
		$view->propertyBody = parent::get();
104
105
		return $this->render( $view );
106
	}
107
108
109
	/**
110
	 * Saves the data
111
	 *
112
	 * @return string|null HTML output
113
	 */
114
	public function save() : ?string
115
	{
116
		$view = $this->getObject()->addData( $this->getView() );
117
118
		$this->fromArray( $view->item, $view->param( 'property', [] ) );
119
		$view->propertyBody = parent::save();
120
121
		return null;
122
	}
123
124
125
	/**
126
	 * Returns the sub-client given by its name.
127
	 *
128
	 * @param string $type Name of the client type
129
	 * @param string|null $name Name of the sub-client (Default if null)
130
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
131
	 */
132
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
133
	{
134
		/** admin/jqadm/attribute/property/decorators/excludes
135
		 * Excludes decorators added by the "common" option from the attribute JQAdm client
136
		 *
137
		 * Decorators extend the functionality of a class by adding new aspects
138
		 * (e.g. log what is currently done), executing the methods of the underlying
139
		 * class only in certain conditions (e.g. only for logged in users) or
140
		 * modify what is returned to the caller.
141
		 *
142
		 * This option allows you to remove a decorator added via
143
		 * "admin/jqadm/common/decorators/default" before they are wrapped
144
		 * around the JQAdm client.
145
		 *
146
		 *  admin/jqadm/attribute/property/decorators/excludes = array( 'decorator1' )
147
		 *
148
		 * This would remove the decorator named "decorator1" from the list of
149
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
150
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
151
		 *
152
		 * @param array List of decorator names
153
		 * @since 2018.01
154
		 * @category Developer
155
		 * @see admin/jqadm/common/decorators/default
156
		 * @see admin/jqadm/attribute/property/decorators/global
157
		 * @see admin/jqadm/attribute/property/decorators/local
158
		 */
159
160
		/** admin/jqadm/attribute/property/decorators/global
161
		 * Adds a list of globally available decorators only to the attribute 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/attribute/property/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 2018.01
178
		 * @category Developer
179
		 * @see admin/jqadm/common/decorators/default
180
		 * @see admin/jqadm/attribute/property/decorators/excludes
181
		 * @see admin/jqadm/attribute/property/decorators/local
182
		 */
183
184
		/** admin/jqadm/attribute/property/decorators/local
185
		 * Adds a list of local decorators only to the attribute JQAdm client
186
		 *
187
		 * Decorators extend the functionality of a class by adding new aspects
188
		 * (e.g. log what is currently done), executing the methods of the underlying
189
		 * class only in certain conditions (e.g. only for logged in users) or
190
		 * modify what is returned to the caller.
191
		 *
192
		 * This option allows you to wrap local decorators
193
		 * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client.
194
		 *
195
		 *  admin/jqadm/attribute/property/decorators/local = array( 'decorator2' )
196
		 *
197
		 * This would add the decorator named "decorator2" defined by
198
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
199
		 *
200
		 * @param array List of decorator names
201
		 * @since 2018.01
202
		 * @category Developer
203
		 * @see admin/jqadm/common/decorators/default
204
		 * @see admin/jqadm/attribute/property/decorators/excludes
205
		 * @see admin/jqadm/attribute/property/decorators/global
206
		 */
207
		return $this->createSubClient( 'attribute/property/' . $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/attribute/property/subparts
219
		 * List of JQAdm sub-clients rendered within the attribute property 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 order of the JQAdm sub-clients
230
		 * determines the order 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 order of the output by reordering 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 reordering content by a fluid like
245
		 * design.
246
		 *
247
		 * @param array List of sub-client names
248
		 * @since 2018.01
249
		 * @category Developer
250
		 */
251
		return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/property/subparts', [] );
252
	}
253
254
255
	/**
256
	 * Creates new and updates existing items using the data array
257
	 *
258
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
259
	 * @param array $data Data array
260
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item
261
	 */
262
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\Item\Iface
263
	{
264
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute/property' );
265
266
		$propItems = $item->getPropertyItems( null, false );
267
268
		foreach( $data as $entry )
269
		{
270
			if( isset( $propItems[$entry['attribute.property.id']] ) )
271
			{
272
				$propItem = $propItems[$entry['attribute.property.id']];
273
				unset( $propItems[$entry['attribute.property.id']] );
274
			}
275
			else
276
			{
277
				$propItem = $manager->create();
278
			}
279
280
			$propItem->fromArray( $entry, true );
281
			$item->addPropertyItem( $propItem );
282
		}
283
284
		return $item->deletePropertyItems( $propItems->toArray() );
285
	}
286
287
288
	/**
289
	 * Constructs the data array for the view from the given item
290
	 *
291
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object including referenced domain items
292
	 * @param bool $copy True if items should be copied, false if not
293
	 * @return string[] Multi-dimensional associative list of item data
294
	 */
295
	protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, bool $copy = false ) : array
296
	{
297
		$siteId = $this->getContext()->getLocale()->getSiteId();
298
		$data = [];
299
300
		foreach( $item->getPropertyItems( null, false ) as $item )
301
		{
302
			$list = $item->toArray( true );
303
304
			if( $copy === true )
305
			{
306
				$list['attribute.property.siteid'] = $siteId;
307
				$list['attribute.property.id'] = '';
308
			}
309
310
			$data[] = $list;
311
		}
312
313
		return $data;
314
	}
315
316
317
	/**
318
	 * Returns the rendered template including the view data
319
	 *
320
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
321
	 * @return string HTML output
322
	 */
323
	protected function render( \Aimeos\MW\View\Iface $view ) : string
324
	{
325
		/** admin/jqadm/attribute/property/template-item
326
		 * Relative path to the HTML body template of the property subpart for attributes.
327
		 *
328
		 * The template file contains the HTML code and processing instructions
329
		 * to generate the result shown in the body of the frontend. The
330
		 * configuration string is the path to the template file relative
331
		 * to the templates directory (usually in admin/jqadm/templates).
332
		 *
333
		 * You can overwrite the template file configuration in extensions and
334
		 * provide alternative templates. These alternative templates should be
335
		 * named like the default one but with the string "default" replaced by
336
		 * an unique name. You may use the name of your project for this. If
337
		 * you've implemented an alternative client class as well, "default"
338
		 * should be replaced by the name of the new class.
339
		 *
340
		 * @param string Relative path to the template creating the HTML code
341
		 * @since 2018.01
342
		 * @category Developer
343
		 */
344
		$tplconf = 'admin/jqadm/attribute/property/template-item';
345
		$default = 'attribute/item-property-standard';
346
347
		return $view->render( $view->config( $tplconf, $default ) );
348
	}
349
}
350