Standard   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 346
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 346
rs 10
c 0
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 24 1
A toArray() 0 26 4
A create() 0 17 3
A fromArray() 0 37 6
A getSubClient() 0 73 1
A copy() 0 7 1
A getSubClientNames() 0 35 1
A data() 0 11 1
A get() 0 7 1
A save() 0 8 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Supplier\Media\Property;
12
13
sprintf( 'property' ); // for translation
14
15
16
/**
17
 * Default implementation of supplier media 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/supplier/media/property/name
27
	 * Name of the property subpart used by the JQAdm supplier media implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Supplier\Media\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 2018.04
34
	 */
35
36
37
	/**
38
	 * Adds the required data used in the supplier 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(), 'media/property/type' );
46
47
		$search = $manager->filter( true )
48
			->order( 'media.property.type.position' )
49
			->slice( 0, 10000 );
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->object()->data( $this->view() );
65
		$view->mediaData = $this->toArray( $view->item, $view->get( 'mediaData', [] ), 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->object()->data( $this->view() );
80
		$siteid = $this->context()->locale()->getSiteId();
81
		$data = $view->get( 'mediaData', [] );
82
83
		foreach( $data as $index => $entry )
84
		{
85
			foreach( $view->value( $entry, 'property', [] ) as $idx => $y ) {
86
				$data[$index]['property'][$idx]['media.property.siteid'] = $siteid;
87
			}
88
		}
89
90
		$view->propertyData = $data;
91
		$view->propertyBody = parent::create();
92
93
		return $this->render( $view );
94
	}
95
96
97
	/**
98
	 * Returns a single resource
99
	 *
100
	 * @return string|null HTML output
101
	 */
102
	public function get() : ?string
103
	{
104
		$view = $this->object()->data( $this->view() );
105
		$view->mediaData = $this->toArray( $view->item, $view->get( 'mediaData', [] ) );
106
		$view->propertyBody = parent::get();
107
108
		return $this->render( $view );
109
	}
110
111
112
	/**
113
	 * Saves the data
114
	 *
115
	 * @return string|null HTML output
116
	 */
117
	public function save() : ?string
118
	{
119
		$view = $this->view();
120
121
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
122
		$view->propertyBody = parent::save();
123
124
		return null;
125
	}
126
127
128
	/**
129
	 * Returns the sub-client given by its name.
130
	 *
131
	 * @param string $type Name of the client type
132
	 * @param string|null $name Name of the sub-client (Default if null)
133
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
134
	 */
135
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
136
	{
137
		/** admin/jqadm/supplier/media/property/decorators/excludes
138
		 * Excludes decorators added by the "common" option from the supplier JQAdm client
139
		 *
140
		 * Decorators extend the functionality of a class by adding new aspects
141
		 * (e.g. log what is currently done), executing the methods of the underlying
142
		 * class only in certain conditions (e.g. only for logged in users) or
143
		 * modify what is returned to the caller.
144
		 *
145
		 * This option allows you to remove a decorator added via
146
		 * "admin/jqadm/common/decorators/default" before they are wrapped
147
		 * around the JQAdm client.
148
		 *
149
		 *  admin/jqadm/supplier/media/property/decorators/excludes = array( 'decorator1' )
150
		 *
151
		 * This would remove the decorator named "decorator1" from the list of
152
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
153
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2018.01
157
		 * @see admin/jqadm/common/decorators/default
158
		 * @see admin/jqadm/supplier/media/property/decorators/global
159
		 * @see admin/jqadm/supplier/media/property/decorators/local
160
		 */
161
162
		/** admin/jqadm/supplier/media/property/decorators/global
163
		 * Adds a list of globally available decorators only to the supplier JQAdm client
164
		 *
165
		 * Decorators extend the functionality of a class by adding new aspects
166
		 * (e.g. log what is currently done), executing the methods of the underlying
167
		 * class only in certain conditions (e.g. only for logged in users) or
168
		 * modify what is returned to the caller.
169
		 *
170
		 * This option allows you to wrap global decorators
171
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
172
		 *
173
		 *  admin/jqadm/supplier/media/property/decorators/global = array( 'decorator1' )
174
		 *
175
		 * This would add the decorator named "decorator1" defined by
176
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
177
		 *
178
		 * @param array List of decorator names
179
		 * @since 2018.01
180
		 * @see admin/jqadm/common/decorators/default
181
		 * @see admin/jqadm/supplier/media/property/decorators/excludes
182
		 * @see admin/jqadm/supplier/media/property/decorators/local
183
		 */
184
185
		/** admin/jqadm/supplier/media/property/decorators/local
186
		 * Adds a list of local decorators only to the supplier JQAdm client
187
		 *
188
		 * Decorators extend the functionality of a class by adding new aspects
189
		 * (e.g. log what is currently done), executing the methods of the underlying
190
		 * class only in certain conditions (e.g. only for logged in users) or
191
		 * modify what is returned to the caller.
192
		 *
193
		 * This option allows you to wrap local decorators
194
		 * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client.
195
		 *
196
		 *  admin/jqadm/supplier/media/property/decorators/local = array( 'decorator2' )
197
		 *
198
		 * This would add the decorator named "decorator2" defined by
199
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
200
		 *
201
		 * @param array List of decorator names
202
		 * @since 2018.01
203
		 * @see admin/jqadm/common/decorators/default
204
		 * @see admin/jqadm/supplier/media/property/decorators/excludes
205
		 * @see admin/jqadm/supplier/media/property/decorators/global
206
		 */
207
		return $this->createSubClient( 'supplier/media/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/supplier/media/property/subparts
219
		 * List of JQAdm sub-clients rendered within the supplier media 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
		 */
250
		return $this->context()->config()->get( 'admin/jqadm/supplier/media/property/subparts', [] );
251
	}
252
253
254
	/**
255
	 * Creates new and updates existing items using the data array
256
	 *
257
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object without referenced domain items
258
	 * @param array $data Data array
259
	 * @return \Aimeos\MShop\Service\Item\Iface Modified supplier item
260
	 */
261
	protected function fromArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data ) : \Aimeos\MShop\Supplier\Item\Iface
262
	{
263
		$manager = \Aimeos\MShop::create( $this->context(), 'media' );
264
		$index = 0;
265
266
		foreach( $item->getRefItems( 'media', null, null, false ) as $refItem )
267
		{
268
			$propItems = $refItem->getPropertyItems( null, false );
269
270
			foreach( (array) $this->val( $data, $index . '/property', [] ) as $entry )
271
			{
272
				if( isset( $propItems[$entry['media.property.id']] ) )
273
				{
274
					$propItem = $propItems[$entry['media.property.id']];
275
					unset( $propItems[$entry['media.property.id']] );
276
				}
277
				else
278
				{
279
					$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

279
					/** @scrutinizer ignore-call */ 
280
     $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...
280
				}
281
282
				$propItem->fromArray( $entry, true );
283
				$refItem->addPropertyItem( $propItem );
284
			}
285
286
			foreach( $propItems as $propItem )
287
			{
288
				// Don't delete preview image URLs
289
				if( !ctype_digit( $propItem->getType() ) ) {
290
					$refItem->deletePropertyItem( $propItem );
291
				}
292
			}
293
294
			$index++;
295
		}
296
297
		return $item;
298
	}
299
300
301
	/**
302
	 * Constructs the data array for the view from the given item
303
	 *
304
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object including referenced domain items
305
	 * @param array $data Associative list of media data
306
	 * @param bool $copy True if items should be copied, false if not
307
	 * @return string[] Multi-dimensional associative list of item data
308
	 */
309
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data, bool $copy = false ) : array
310
	{
311
		$idx = 0;
312
		$siteId = $this->context()->locale()->getSiteId();
313
314
		foreach( $item->getRefItems( 'media', null, null, false ) as $mediaItem )
315
		{
316
			$data[$idx]['property'] = [];
317
318
			foreach( $mediaItem->getPropertyItems( null, false ) as $propItem )
319
			{
320
				$list = $propItem->toArray( true );
321
322
				if( $copy === true )
323
				{
324
					$list['media.property.siteid'] = $siteId;
325
					$list['media.property.id'] = '';
326
				}
327
328
				$data[$idx]['property'][] = $list;
329
			}
330
331
			$idx++;
332
		}
333
334
		return $data;
335
	}
336
337
338
	/**
339
	 * Returns the rendered template including the view data
340
	 *
341
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
342
	 * @return string HTML output
343
	 */
344
	protected function render( \Aimeos\Base\View\Iface $view ) : string
345
	{
346
		/** admin/jqadm/supplier/media/property/template-item
347
		 * Relative path to the HTML body template of the media subpart for suppliers.
348
		 *
349
		 * The template file contains the HTML code and processing instructions
350
		 * to generate the result shown in the body of the frontend. The
351
		 * configuration string is the path to the template file relative
352
		 * to the templates directory (usually in templates/admin/jqadm).
353
		 *
354
		 * You can overwrite the template file configuration in extensions and
355
		 * provide alternative templates. These alternative templates should be
356
		 * named like the default one but with the string "default" replaced by
357
		 * an unique name. You may use the name of your project for this. If
358
		 * you've implemented an alternative client class as well, "default"
359
		 * should be replaced by the name of the new class.
360
		 *
361
		 * @param string Relative path to the template creating the HTML code
362
		 * @since 2016.04
363
		 */
364
		$tplconf = 'admin/jqadm/supplier/media/property/template-item';
365
		$default = 'supplier/item-media-property';
366
367
		return $view->render( $view->config( $tplconf, $default ) );
368
	}
369
}
370