Passed
Push — master ( 331f12...0643a3 )
by Aimeos
03:43
created

Standard::create()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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