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

Standard   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 406
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 94
dl 0
loc 406
rs 10
c 2
b 0
f 0
wmc 26

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubClientNames() 0 36 1
A getSubClient() 0 76 1
A save() 0 12 2
A render() 0 25 1
A delete() 0 8 1
A toArray() 0 33 5
B fromArray() 0 46 7
A get() 0 11 2
A addData() 0 19 1
A create() 0 20 3
A copy() 0 11 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Text;
12
13
sprintf( 'text' ); // for translation
14
15
16
/**
17
 * Default implementation of product text 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/product/text/name
27
	 * Name of the text subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Text\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 2016.04
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the text 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
		$context = $this->getContext();
47
48
		$textTypeManager = \Aimeos\MShop::create( $context, 'text/type' );
49
		$listTypeManager = \Aimeos\MShop::create( $context, 'product/lists/type' );
50
51
		$search = $textTypeManager->createSearch( true )->setSlice( 0, 10000 );
52
		$search->setConditions( $search->compare( '==', 'text.type.domain', 'product' ) );
53
		$search->setSortations( array( $search->sort( '+', 'text.type.position' ) ) );
54
55
		$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 );
56
		$listSearch->setConditions( $listSearch->compare( '==', 'product.lists.type.domain', 'text' ) );
57
		$listSearch->setSortations( array( $listSearch->sort( '+', 'product.lists.type.position' ) ) );
58
59
		$view->textTypes = $textTypeManager->searchItems( $search );
60
		$view->textListTypes = $listTypeManager->searchItems( $listSearch );
61
62
		return $view;
63
	}
64
65
66
	/**
67
	 * Copies a resource
68
	 *
69
	 * @return string|null HTML output
70
	 */
71
	public function copy() : ?string
72
	{
73
		$view = $this->getObject()->addData( $this->getView() );
74
		$view->textData = $this->toArray( $view->item, true );
75
		$view->textBody = '';
76
77
		foreach( $this->getSubClients() as $client ) {
78
			$view->textBody .= $client->copy();
79
		}
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Creates a new resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function create() : ?string
91
	{
92
		$view = $this->getObject()->addData( $this->getView() );
93
		$siteid = $this->getContext()->getLocale()->getSiteId();
94
		$data = $view->param( 'text', [] );
95
96
		foreach( $data as $idx => $entry )
97
		{
98
			$data[$idx]['product.lists.siteid'] = $siteid;
99
			$data[$idx]['text.siteid'] = $siteid;
100
		}
101
102
		$view->textData = $data;
103
		$view->textBody = '';
104
105
		foreach( $this->getSubClients() as $client ) {
106
			$view->textBody .= $client->create();
107
		}
108
109
		return $this->render( $view );
110
	}
111
112
113
	/**
114
	 * Deletes a resource
115
	 *
116
	 * @return string|null HTML output
117
	 */
118
	public function delete() : ?string
119
	{
120
		parent::delete();
121
122
		$item = $this->getView()->item;
123
		$item->deleteListItems( $item->getListItems( 'text', null, null, false )->toArray(), true );
124
125
		return null;
126
	}
127
128
129
	/**
130
	 * Returns a single resource
131
	 *
132
	 * @return string|null HTML output
133
	 */
134
	public function get() : ?string
135
	{
136
		$view = $this->getObject()->addData( $this->getView() );
137
		$view->textData = $this->toArray( $view->item );
138
		$view->textBody = '';
139
140
		foreach( $this->getSubClients() as $client ) {
141
			$view->textBody .= $client->get();
142
		}
143
144
		return $this->render( $view );
145
	}
146
147
148
	/**
149
	 * Saves the data
150
	 *
151
	 * @return string|null HTML output
152
	 */
153
	public function save() : ?string
154
	{
155
		$view = $this->getView();
156
157
		$view->item = $this->fromArray( $view->item, $view->param( 'text', [] ) );
158
		$view->textBody = '';
159
160
		foreach( $this->getSubClients() as $client ) {
161
			$view->textBody .= $client->save();
162
		}
163
164
		return null;
165
	}
166
167
168
	/**
169
	 * Returns the sub-client given by its name.
170
	 *
171
	 * @param string $type Name of the client type
172
	 * @param string|null $name Name of the sub-client (Default if null)
173
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
174
	 */
175
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
176
	{
177
		/** admin/jqadm/product/text/decorators/excludes
178
		 * Excludes decorators added by the "common" option from the product JQAdm client
179
		 *
180
		 * Decorators extend the functionality of a class by adding new aspects
181
		 * (e.g. log what is currently done), executing the methods of the underlying
182
		 * class only in certain conditions (e.g. only for logged in users) or
183
		 * modify what is returned to the caller.
184
		 *
185
		 * This option allows you to remove a decorator added via
186
		 * "admin/jqadm/common/decorators/default" before they are wrapped
187
		 * around the JQAdm client.
188
		 *
189
		 *  admin/jqadm/product/text/decorators/excludes = array( 'decorator1' )
190
		 *
191
		 * This would remove the decorator named "decorator1" from the list of
192
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
193
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
194
		 *
195
		 * @param array List of decorator names
196
		 * @since 2017.07
197
		 * @category Developer
198
		 * @see admin/jqadm/common/decorators/default
199
		 * @see admin/jqadm/product/text/decorators/global
200
		 * @see admin/jqadm/product/text/decorators/local
201
		 */
202
203
		/** admin/jqadm/product/text/decorators/global
204
		 * Adds a list of globally available decorators only to the product JQAdm client
205
		 *
206
		 * Decorators extend the functionality of a class by adding new aspects
207
		 * (e.g. log what is currently done), executing the methods of the underlying
208
		 * class only in certain conditions (e.g. only for logged in users) or
209
		 * modify what is returned to the caller.
210
		 *
211
		 * This option allows you to wrap global decorators
212
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
213
		 *
214
		 *  admin/jqadm/product/text/decorators/global = array( 'decorator1' )
215
		 *
216
		 * This would add the decorator named "decorator1" defined by
217
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
218
		 *
219
		 * @param array List of decorator names
220
		 * @since 2017.07
221
		 * @category Developer
222
		 * @see admin/jqadm/common/decorators/default
223
		 * @see admin/jqadm/product/text/decorators/excludes
224
		 * @see admin/jqadm/product/text/decorators/local
225
		 */
226
227
		/** admin/jqadm/product/text/decorators/local
228
		 * Adds a list of local decorators only to the product JQAdm client
229
		 *
230
		 * Decorators extend the functionality of a class by adding new aspects
231
		 * (e.g. log what is currently done), executing the methods of the underlying
232
		 * class only in certain conditions (e.g. only for logged in users) or
233
		 * modify what is returned to the caller.
234
		 *
235
		 * This option allows you to wrap local decorators
236
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
237
		 *
238
		 *  admin/jqadm/product/text/decorators/local = array( 'decorator2' )
239
		 *
240
		 * This would add the decorator named "decorator2" defined by
241
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
242
		 *
243
		 * @param array List of decorator names
244
		 * @since 2017.07
245
		 * @category Developer
246
		 * @see admin/jqadm/common/decorators/default
247
		 * @see admin/jqadm/product/text/decorators/excludes
248
		 * @see admin/jqadm/product/text/decorators/global
249
		 */
250
		return $this->createSubClient( 'product/text/' . $type, $name );
251
	}
252
253
254
	/**
255
	 * Returns the list of sub-client names configured for the client.
256
	 *
257
	 * @return array List of JQAdm client names
258
	 */
259
	protected function getSubClientNames() : array
260
	{
261
		/** admin/jqadm/product/text/standard/subparts
262
		 * List of JQAdm sub-clients rendered within the product text section
263
		 *
264
		 * The output of the frontend is composed of the code generated by the JQAdm
265
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
266
		 * that are responsible for rendering certain sub-parts of the output. The
267
		 * sub-clients can contain JQAdm clients themselves and therefore a
268
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
269
		 * the output that is placed inside the container of its parent.
270
		 *
271
		 * At first, always the JQAdm code generated by the parent is printed, then
272
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
273
		 * determines the order of the output of these sub-clients inside the parent
274
		 * container. If the configured list of clients is
275
		 *
276
		 *  array( "subclient1", "subclient2" )
277
		 *
278
		 * you can easily change the order of the output by reordering the subparts:
279
		 *
280
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
281
		 *
282
		 * You can also remove one or more parts if they shouldn't be rendered:
283
		 *
284
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
285
		 *
286
		 * As the clients only generates structural JQAdm, the layout defined via CSS
287
		 * should support adding, removing or reordering content by a fluid like
288
		 * design.
289
		 *
290
		 * @param array List of sub-client names
291
		 * @since 2017.07
292
		 * @category Developer
293
		 */
294
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/text/standard/subparts', [] );
295
	}
296
297
298
	/**
299
	 * Creates new and updates existing items using the data array
300
	 *
301
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
302
	 * @param array $data Data array
303
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
304
	 */
305
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
306
	{
307
		$context = $this->getContext();
308
309
		$textManager = \Aimeos\MShop::create( $context, 'text' );
310
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
311
312
		$listItems = $item->getListItems( 'text', null, null, false );
313
314
315
		foreach( $data as $idx => $entry )
316
		{
317
			if( trim( $this->getValue( $entry, 'text.content', '' ) ) === '' ) {
318
				continue;
319
			}
320
321
			$listType = $entry['product.lists.type'];
322
323
			if( ( $listItem = $item->getListItem( 'text', $listType, $entry['text.id'], false ) ) === null ) {
324
				$listItem = $listManager->createItem();
325
			}
326
327
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
328
				$refItem = $textManager->createItem();
329
			}
330
331
			$refItem->fromArray( $entry, true );
332
			$conf = [];
333
334
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
335
			{
336
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
337
					$conf[$key] = trim( $cfg['val'] ?? '' );
338
				}
339
			}
340
341
			$listItem->fromArray( $entry, true );
342
			$listItem->setPosition( $idx );
343
			$listItem->setConfig( $conf );
344
345
			$item->addListItem( 'text', $listItem, $refItem );
346
347
			unset( $listItems[$listItem->getId()] );
348
		}
349
350
		return $item->deleteListItems( $listItems->toArray(), true );
351
	}
352
353
354
	/**
355
	 * Constructs the data array for the view from the given item
356
	 *
357
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
358
	 * @param bool $copy True if items should be copied, false if not
359
	 * @return string[] Multi-dimensional associative list of item data
360
	 */
361
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
362
	{
363
		$data = [];
364
		$siteId = $this->getContext()->getLocale()->getSiteId();
365
366
		foreach( $item->getListItems( 'text', null, null, false ) as $listItem )
367
		{
368
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
369
				continue;
370
			}
371
372
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
373
374
			if( $copy === true )
375
			{
376
				$list['product.lists.siteid'] = $siteId;
377
				$list['product.lists.id'] = '';
378
				$list['text.siteid'] = $siteId;
379
				$list['text.id'] = null;
380
			}
381
382
			$list['product.lists.datestart'] = str_replace( ' ', 'T', $list['product.lists.datestart'] );
383
			$list['product.lists.dateend'] = str_replace( ' ', 'T', $list['product.lists.dateend'] );
384
			$list['config'] = [];
385
386
			foreach( $listItem->getConfig() as $key => $value ) {
387
				$list['config'][] = ['key' => $key, 'val' => $value];
388
			}
389
390
			$data[] = $list;
391
		}
392
393
		return $data;
394
	}
395
396
397
	/**
398
	 * Returns the rendered template including the view data
399
	 *
400
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
401
	 * @return string|null HTML output
402
	 */
403
	protected function render( \Aimeos\MW\View\Iface $view ) : string
404
	{
405
		/** admin/jqadm/product/text/template-item
406
		 * Relative path to the HTML body template of the text subpart for products.
407
		 *
408
		 * The template file contains the HTML code and processing instructions
409
		 * to generate the result shown in the body of the frontend. The
410
		 * configuration string is the path to the template file relative
411
		 * to the templates directory (usually in admin/jqadm/templates).
412
		 *
413
		 * You can overwrite the template file configuration in extensions and
414
		 * provide alternative templates. These alternative templates should be
415
		 * named like the default one but with the string "default" replaced by
416
		 * an unique name. You may use the name of your project for this. If
417
		 * you've implemented an alternative client class as well, "default"
418
		 * should be replaced by the name of the new class.
419
		 *
420
		 * @param string Relative path to the template creating the HTML code
421
		 * @since 2016.04
422
		 * @category Developer
423
		 */
424
		$tplconf = 'admin/jqadm/product/text/template-item';
425
		$default = 'product/item-text-standard';
426
427
		return $view->render( $view->config( $tplconf, $default ) );
428
	}
429
}
430