Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
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), 2018-2020
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
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the supplier 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(), 'media/property/type' );
47
48
		$search = $manager->createSearch( true )->setSlice( 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->searchItems( $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->getObject()->addData( $this->getView() );
66
		$view->mediaData = $this->toArray( $view->item, $view->get( 'mediaData', [] ), true );
67
		$view->propertyBody = '';
68
69
		foreach( $this->getSubClients() as $client ) {
70
			$view->propertyBody .= $client->copy();
71
		}
72
73
		return $this->render( $view );
74
	}
75
76
77
	/**
78
	 * Creates a new resource
79
	 *
80
	 * @return string|null HTML output
81
	 */
82
	public function create() : ?string
83
	{
84
		$view = $this->getObject()->addData( $this->getView() );
85
		$siteid = $this->getContext()->getLocale()->getSiteId();
86
		$data = $view->get( 'mediaData', [] );
87
88
		foreach( $data as $index => $entry )
89
		{
90
			foreach( $view->value( $entry, 'property', [] ) as $idx => $y ) {
91
				$data[$index]['property'][$idx]['media.property.siteid'] = $siteid;
92
			}
93
		}
94
95
		$view->propertyData = $data;
96
		$view->propertyBody = '';
97
98
		foreach( $this->getSubClients() as $client ) {
99
			$view->propertyBody .= $client->create();
100
		}
101
102
		return $this->render( $view );
103
	}
104
105
106
	/**
107
	 * Returns a single resource
108
	 *
109
	 * @return string|null HTML output
110
	 */
111
	public function get() : ?string
112
	{
113
		$view = $this->getObject()->addData( $this->getView() );
114
		$view->mediaData = $this->toArray( $view->item, $view->get( 'mediaData', [] ) );
115
		$view->propertyBody = '';
116
117
		foreach( $this->getSubClients() as $client ) {
118
			$view->propertyBody .= $client->get();
119
		}
120
121
		return $this->render( $view );
122
	}
123
124
125
	/**
126
	 * Saves the data
127
	 *
128
	 * @return string|null HTML output
129
	 */
130
	public function save() : ?string
131
	{
132
		$view = $this->getView();
133
134
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
135
		$view->propertyBody = '';
136
137
		foreach( $this->getSubClients() as $client ) {
138
			$view->propertyBody .= $client->save();
139
		}
140
141
		return null;
142
	}
143
144
145
	/**
146
	 * Returns the sub-client given by its name.
147
	 *
148
	 * @param string $type Name of the client type
149
	 * @param string|null $name Name of the sub-client (Default if null)
150
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
151
	 */
152
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
153
	{
154
		/** admin/jqadm/supplier/media/property/decorators/excludes
155
		 * Excludes decorators added by the "common" option from the supplier JQAdm client
156
		 *
157
		 * Decorators extend the functionality of a class by adding new aspects
158
		 * (e.g. log what is currently done), executing the methods of the underlying
159
		 * class only in certain conditions (e.g. only for logged in users) or
160
		 * modify what is returned to the caller.
161
		 *
162
		 * This option allows you to remove a decorator added via
163
		 * "admin/jqadm/common/decorators/default" before they are wrapped
164
		 * around the JQAdm client.
165
		 *
166
		 *  admin/jqadm/supplier/media/property/decorators/excludes = array( 'decorator1' )
167
		 *
168
		 * This would remove the decorator named "decorator1" from the list of
169
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
170
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
171
		 *
172
		 * @param array List of decorator names
173
		 * @since 2018.01
174
		 * @category Developer
175
		 * @see admin/jqadm/common/decorators/default
176
		 * @see admin/jqadm/supplier/media/property/decorators/global
177
		 * @see admin/jqadm/supplier/media/property/decorators/local
178
		 */
179
180
		/** admin/jqadm/supplier/media/property/decorators/global
181
		 * Adds a list of globally available decorators only to the supplier JQAdm client
182
		 *
183
		 * Decorators extend the functionality of a class by adding new aspects
184
		 * (e.g. log what is currently done), executing the methods of the underlying
185
		 * class only in certain conditions (e.g. only for logged in users) or
186
		 * modify what is returned to the caller.
187
		 *
188
		 * This option allows you to wrap global decorators
189
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
190
		 *
191
		 *  admin/jqadm/supplier/media/property/decorators/global = array( 'decorator1' )
192
		 *
193
		 * This would add the decorator named "decorator1" defined by
194
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
195
		 *
196
		 * @param array List of decorator names
197
		 * @since 2018.01
198
		 * @category Developer
199
		 * @see admin/jqadm/common/decorators/default
200
		 * @see admin/jqadm/supplier/media/property/decorators/excludes
201
		 * @see admin/jqadm/supplier/media/property/decorators/local
202
		 */
203
204
		/** admin/jqadm/supplier/media/property/decorators/local
205
		 * Adds a list of local decorators only to the supplier JQAdm client
206
		 *
207
		 * Decorators extend the functionality of a class by adding new aspects
208
		 * (e.g. log what is currently done), executing the methods of the underlying
209
		 * class only in certain conditions (e.g. only for logged in users) or
210
		 * modify what is returned to the caller.
211
		 *
212
		 * This option allows you to wrap local decorators
213
		 * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client.
214
		 *
215
		 *  admin/jqadm/supplier/media/property/decorators/local = array( 'decorator2' )
216
		 *
217
		 * This would add the decorator named "decorator2" defined by
218
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
219
		 *
220
		 * @param array List of decorator names
221
		 * @since 2018.01
222
		 * @category Developer
223
		 * @see admin/jqadm/common/decorators/default
224
		 * @see admin/jqadm/supplier/media/property/decorators/excludes
225
		 * @see admin/jqadm/supplier/media/property/decorators/global
226
		 */
227
		return $this->createSubClient( 'supplier/media/property/' . $type, $name );
228
	}
229
230
231
	/**
232
	 * Returns the list of sub-client names configured for the client.
233
	 *
234
	 * @return array List of JQAdm client names
235
	 */
236
	protected function getSubClientNames() : array
237
	{
238
		/** admin/jqadm/supplier/media/property/standard/subparts
239
		 * List of JQAdm sub-clients rendered within the supplier media property section
240
		 *
241
		 * The output of the frontend is composed of the code generated by the JQAdm
242
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
243
		 * that are responsible for rendering certain sub-parts of the output. The
244
		 * sub-clients can contain JQAdm clients themselves and therefore a
245
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
246
		 * the output that is placed inside the container of its parent.
247
		 *
248
		 * At first, always the JQAdm code generated by the parent is printed, then
249
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
250
		 * determines the order of the output of these sub-clients inside the parent
251
		 * container. If the configured list of clients is
252
		 *
253
		 *  array( "subclient1", "subclient2" )
254
		 *
255
		 * you can easily change the order of the output by reordering the subparts:
256
		 *
257
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
258
		 *
259
		 * You can also remove one or more parts if they shouldn't be rendered:
260
		 *
261
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
262
		 *
263
		 * As the clients only generates structural JQAdm, the layout defined via CSS
264
		 * should support adding, removing or reordering content by a fluid like
265
		 * design.
266
		 *
267
		 * @param array List of sub-client names
268
		 * @since 2018.01
269
		 * @category Developer
270
		 */
271
		return $this->getContext()->getConfig()->get( 'admin/jqadm/supplier/media/property/standard/subparts', [] );
272
	}
273
274
275
	/**
276
	 * Creates new and updates existing items using the data array
277
	 *
278
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object without referenced domain items
279
	 * @param array $data Data array
280
	 * @return \Aimeos\MShop\Service\Item\Iface Modified supplier item
281
	 */
282
	protected function fromArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data ) : \Aimeos\MShop\Supplier\Item\Iface
283
	{
284
		$propManager = \Aimeos\MShop::create( $this->getContext(), 'media/property' );
285
		$index = 0;
286
287
		foreach( $item->getRefItems( 'media', null, null, false ) as $refItem )
288
		{
289
			$propItems = $refItem->getPropertyItems( null, false );
290
291
			foreach( (array) $this->getValue( $data, $index . '/property', [] ) as $entry )
292
			{
293
				if( isset( $propItems[$entry['media.property.id']] ) )
294
				{
295
					$propItem = $propItems[$entry['media.property.id']];
296
					unset( $propItems[$entry['media.property.id']] );
297
				}
298
				else
299
				{
300
					$propItem = $propManager->createItem();
301
				}
302
303
				$propItem->fromArray( $entry, true );
304
				$refItem->addPropertyItem( $propItem );
305
			}
306
307
			foreach( $propItems as $propItem )
308
			{
309
				// Don't delete preview image URLs
310
				if( !ctype_digit( $propItem->getType() ) ) {
311
					$refItem->deletePropertyItem( $propItem );
312
				}
313
			}
314
315
			$index++;
316
		}
317
318
		return $item;
319
	}
320
321
322
	/**
323
	 * Constructs the data array for the view from the given item
324
	 *
325
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object including referenced domain items
326
	 * @param array $data Associative list of media data
327
	 * @param bool $copy True if items should be copied, false if not
328
	 * @return string[] Multi-dimensional associative list of item data
329
	 */
330
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data, bool $copy = false ) : array
331
	{
332
		$idx = 0;
333
		$siteId = $this->getContext()->getLocale()->getSiteId();
334
335
		foreach( $item->getRefItems( 'media', null, null, false ) as $mediaItem )
336
		{
337
			$data[$idx]['property'] = [];
338
339
			foreach( $mediaItem->getPropertyItems( null, false ) as $propItem )
340
			{
341
				$list = $propItem->toArray( true );
342
343
				if( $copy === true )
344
				{
345
					$list['media.property.siteid'] = $siteId;
346
					$list['media.property.id'] = '';
347
				}
348
349
				$data[$idx]['property'][] = $list;
350
			}
351
352
			$idx++;
353
		}
354
355
		return $data;
356
	}
357
358
359
	/**
360
	 * Returns the rendered template including the view data
361
	 *
362
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
363
	 * @return string|null HTML output
364
	 */
365
	protected function render( \Aimeos\MW\View\Iface $view ) : string
366
	{
367
		/** admin/jqadm/supplier/media/property/template-item
368
		 * Relative path to the HTML body template of the media subpart for suppliers.
369
		 *
370
		 * The template file contains the HTML code and processing instructions
371
		 * to generate the result shown in the body of the frontend. The
372
		 * configuration string is the path to the template file relative
373
		 * to the templates directory (usually in admin/jqadm/templates).
374
		 *
375
		 * You can overwrite the template file configuration in extensions and
376
		 * provide alternative templates. These alternative templates should be
377
		 * named like the default one but with the string "default" replaced by
378
		 * an unique name. You may use the name of your project for this. If
379
		 * you've implemented an alternative client class as well, "default"
380
		 * should be replaced by the name of the new class.
381
		 *
382
		 * @param string Relative path to the template creating the HTML code
383
		 * @since 2016.04
384
		 * @category Developer
385
		 */
386
		$tplconf = 'admin/jqadm/supplier/media/property/template-item';
387
		$default = 'supplier/item-media-property-standard';
388
389
		return $view->render( $view->config( $tplconf, $default ) );
390
	}
391
}
392