Passed
Branch master (d032f7)
by Aimeos
05:30
created

Standard::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 19
rs 9.9666
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-2018
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
	 * Copies a resource
40
	 *
41
	 * @return string HTML output
42
	 */
43
	public function copy()
44
	{
45
		$view = $this->getView();
46
47
		$view->propertyData = $this->toArray( $view->item, true );
48
		$view->propertyTypes = $this->getPropertyTypes();
49
		$view->propertyBody = '';
50
51
		foreach( $this->getSubClients() as $client ) {
52
			$view->propertyBody .= $client->copy();
53
		}
54
55
		return $this->render( $view );
56
	}
57
58
59
	/**
60
	 * Creates a new resource
61
	 *
62
	 * @return string HTML output
63
	 */
64
	public function create()
65
	{
66
		$view = $this->getView();
67
		$siteid = $this->getContext()->getLocale()->getSiteId();
68
		$data = $view->param( 'property', [] );
69
70
		foreach( $data as $idx => $entry ) {
71
			$data[$idx]['attribute.lists.siteid'] = $siteid;
72
		}
73
74
		$view->propertyTypes = $this->getPropertyTypes();
75
		$view->propertyData = $data;
76
		$view->propertyBody = '';
77
78
		foreach( $this->getSubClients() as $client ) {
79
			$view->propertyBody .= $client->create();
80
		}
81
82
		return $this->render( $view );
83
	}
84
85
86
	/**
87
	 * Returns a single resource
88
	 *
89
	 * @return string HTML output
90
	 */
91
	public function get()
92
	{
93
		$view = $this->getView();
94
95
		$view->propertyData = $this->toArray( $view->item );
96
		$view->propertyTypes = $this->getPropertyTypes();
97
		$view->propertyBody = '';
98
99
		foreach( $this->getSubClients() as $client ) {
100
			$view->propertyBody .= $client->get();
101
		}
102
103
		return $this->render( $view );
104
	}
105
106
107
	/**
108
	 * Saves the data
109
	 */
110
	public function save()
111
	{
112
		$view = $this->getView();
113
114
		try
115
		{
116
			$this->fromArray( $view->item, $view->param( 'property', [] ) );
117
			$view->propertyBody = '';
118
119
			foreach( $this->getSubClients() as $client ) {
120
				$view->propertyBody .= $client->save();
121
			}
122
123
			return;
124
		}
125
		catch( \Aimeos\MShop\Exception $e )
126
		{
127
			$error = array( 'attribute-item-property' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
128
			$view->errors = $view->get( 'errors', [] ) + $error;
129
			$this->logException( $e );
130
		}
131
		catch( \Exception $e )
132
		{
133
			$error = array( 'attribute-item-property' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
134
			$view->errors = $view->get( 'errors', [] ) + $error;
135
			$this->logException( $e );
136
		}
137
138
		throw new \Aimeos\Admin\JQAdm\Exception();
139
	}
140
141
142
	/**
143
	 * Returns the sub-client given by its name.
144
	 *
145
	 * @param string $type Name of the client type
146
	 * @param string|null $name Name of the sub-client (Default if null)
147
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
148
	 */
149
	public function getSubClient( $type, $name = null )
150
	{
151
		/** admin/jqadm/attribute/property/decorators/excludes
152
		 * Excludes decorators added by the "common" option from the attribute JQAdm client
153
		 *
154
		 * Decorators extend the functionality of a class by adding new aspects
155
		 * (e.g. log what is currently done), executing the methods of the underlying
156
		 * class only in certain conditions (e.g. only for logged in users) or
157
		 * modify what is returned to the caller.
158
		 *
159
		 * This option allows you to remove a decorator added via
160
		 * "admin/jqadm/common/decorators/default" before they are wrapped
161
		 * around the JQAdm client.
162
		 *
163
		 *  admin/jqadm/attribute/property/decorators/excludes = array( 'decorator1' )
164
		 *
165
		 * This would remove the decorator named "decorator1" from the list of
166
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
167
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
168
		 *
169
		 * @param array List of decorator names
170
		 * @since 2018.01
171
		 * @category Developer
172
		 * @see admin/jqadm/common/decorators/default
173
		 * @see admin/jqadm/attribute/property/decorators/global
174
		 * @see admin/jqadm/attribute/property/decorators/local
175
		 */
176
177
		/** admin/jqadm/attribute/property/decorators/global
178
		 * Adds a list of globally available decorators only to the attribute 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 global decorators
186
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
187
		 *
188
		 *  admin/jqadm/attribute/property/decorators/global = array( 'decorator1' )
189
		 *
190
		 * This would add the decorator named "decorator1" defined by
191
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
192
		 *
193
		 * @param array List of decorator names
194
		 * @since 2018.01
195
		 * @category Developer
196
		 * @see admin/jqadm/common/decorators/default
197
		 * @see admin/jqadm/attribute/property/decorators/excludes
198
		 * @see admin/jqadm/attribute/property/decorators/local
199
		 */
200
201
		/** admin/jqadm/attribute/property/decorators/local
202
		 * Adds a list of local decorators only to the attribute JQAdm client
203
		 *
204
		 * Decorators extend the functionality of a class by adding new aspects
205
		 * (e.g. log what is currently done), executing the methods of the underlying
206
		 * class only in certain conditions (e.g. only for logged in users) or
207
		 * modify what is returned to the caller.
208
		 *
209
		 * This option allows you to wrap local decorators
210
		 * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client.
211
		 *
212
		 *  admin/jqadm/attribute/property/decorators/local = array( 'decorator2' )
213
		 *
214
		 * This would add the decorator named "decorator2" defined by
215
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
216
		 *
217
		 * @param array List of decorator names
218
		 * @since 2018.01
219
		 * @category Developer
220
		 * @see admin/jqadm/common/decorators/default
221
		 * @see admin/jqadm/attribute/property/decorators/excludes
222
		 * @see admin/jqadm/attribute/property/decorators/global
223
		 */
224
		return $this->createSubClient( 'attribute/property/' . $type, $name );
225
	}
226
227
228
	/**
229
	 * Returns the list of sub-client names configured for the client.
230
	 *
231
	 * @return array List of JQAdm client names
232
	 */
233
	protected function getSubClientNames()
234
	{
235
		/** admin/jqadm/attribute/property/standard/subparts
236
		 * List of JQAdm sub-clients rendered within the attribute property section
237
		 *
238
		 * The output of the frontend is composed of the code generated by the JQAdm
239
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
240
		 * that are responsible for rendering certain sub-parts of the output. The
241
		 * sub-clients can contain JQAdm clients themselves and therefore a
242
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
243
		 * the output that is placed inside the container of its parent.
244
		 *
245
		 * At first, always the JQAdm code generated by the parent is printed, then
246
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
247
		 * determines the order of the output of these sub-clients inside the parent
248
		 * container. If the configured list of clients is
249
		 *
250
		 *  array( "subclient1", "subclient2" )
251
		 *
252
		 * you can easily change the order of the output by reordering the subparts:
253
		 *
254
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
255
		 *
256
		 * You can also remove one or more parts if they shouldn't be rendered:
257
		 *
258
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
259
		 *
260
		 * As the clients only generates structural JQAdm, the layout defined via CSS
261
		 * should support adding, removing or reordering content by a fluid like
262
		 * design.
263
		 *
264
		 * @param array List of sub-client names
265
		 * @since 2018.01
266
		 * @category Developer
267
		 */
268
		return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/property/standard/subparts', [] );
269
	}
270
271
272
	/**
273
	 * Returns the available attribute property types
274
	 *
275
	 * @return array Associative list of property type IDs as keys and property type items as values
276
	 */
277
	protected function getPropertyTypes()
278
	{
279
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute/property/type' );
280
281
		$search = $manager->createSearch( true )->setSlice( 0, 10000 );
282
		$search->setSortations( [$search->sort( '+', 'attribute.property.type.position')] );
283
284
		return $manager->searchItems( $search );
285
	}
286
287
288
	/**
289
	 * Creates new and updates existing items using the data array
290
	 *
291
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
292
	 * @param string[] $data Data array
293
	 */
294
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data )
295
	{
296
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute/property' );
297
298
		$propItems = $item->getPropertyItems( null, false );
299
300
		foreach( $data as $entry )
301
		{
302
			if( isset( $propItems[$entry['attribute.property.id']] ) )
303
			{
304
				$propItem = $propItems[$entry['attribute.property.id']];
305
				unset( $propItems[$entry['attribute.property.id']] );
306
			}
307
			else
308
			{
309
				$propItem = $manager->createItem();
310
			}
311
312
			$propItem->fromArray( $entry );
0 ignored issues
show
Bug introduced by
$entry of type string is incompatible with the type array expected by parameter $list of Aimeos\MShop\Common\Item\Iface::fromArray(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

312
			$propItem->fromArray( /** @scrutinizer ignore-type */ $entry );
Loading history...
313
			$item->addPropertyItem( $propItem );
314
		}
315
316
		$item->deletePropertyItems( $propItems );
317
	}
318
319
320
	/**
321
	 * Constructs the data array for the view from the given item
322
	 *
323
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object including referenced domain items
324
	 * @param boolean $copy True if items should be copied, false if not
325
	 * @return string[] Multi-dimensional associative list of item data
326
	 */
327
	protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, $copy = false )
328
	{
329
		$siteId = $this->getContext()->getLocale()->getSiteId();
330
		$data = [];
331
332
		foreach( $item->getPropertyItems( null, false ) as $item )
333
		{
334
			$list = $item->toArray( true );
335
336
			if( $copy === true )
337
			{
338
				$list['attribute.property.siteid'] = $siteId;
339
				$list['attribute.property.id'] = '';
340
			}
341
342
			$data[] = $list;
343
		}
344
345
		return $data;
346
	}
347
348
349
	/**
350
	 * Returns the rendered template including the view data
351
	 *
352
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
353
	 * @return string HTML output
354
	 */
355
	protected function render( \Aimeos\MW\View\Iface $view )
356
	{
357
		/** admin/jqadm/attribute/property/template-item
358
		 * Relative path to the HTML body template of the property subpart for attributes.
359
		 *
360
		 * The template file contains the HTML code and processing instructions
361
		 * to generate the result shown in the body of the frontend. The
362
		 * configuration string is the path to the template file relative
363
		 * to the templates directory (usually in admin/jqadm/templates).
364
		 *
365
		 * You can overwrite the template file configuration in extensions and
366
		 * provide alternative templates. These alternative templates should be
367
		 * named like the default one but with the string "default" replaced by
368
		 * an unique name. You may use the name of your project for this. If
369
		 * you've implemented an alternative client class as well, "default"
370
		 * should be replaced by the name of the new class.
371
		 *
372
		 * @param string Relative path to the template creating the HTML code
373
		 * @since 2018.01
374
		 * @category Developer
375
		 */
376
		$tplconf = 'admin/jqadm/attribute/property/template-item';
377
		$default = 'attribute/item-property-standard';
378
379
		return $view->render( $view->config( $tplconf, $default ) );
380
	}
381
}
382