Standard   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
c 0
b 0
f 0
dl 0
loc 317
rs 10
wmc 15

10 Methods

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

269
				/** @scrutinizer ignore-call */ 
270
    $propItem = $manager->createPropertyItem();

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