Standard::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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