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

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 76
rs 10

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Text;
12
13
sprintf( 'text' ); // for translation
14
15
16
/**
17
 * Default implementation of supplier 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/supplier/text/name
27
	 * Name of the text subpart used by the JQAdm supplier implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Supplier\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 2018.07
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, 'supplier/lists/type' );
50
51
		$search = $textTypeManager->createSearch( true )->setSlice( 0, 10000 );
52
		$search->setConditions( $search->compare( '==', 'text.type.domain', 'supplier' ) );
53
		$search->setSortations( array( $search->sort( '+', 'text.type.position' ) ) );
54
55
		$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 );
56
		$listSearch->setConditions( $listSearch->compare( '==', 'supplier.lists.type.domain', 'text' ) );
57
		$listSearch->setSortations( array( $listSearch->sort( '+', 'supplier.lists.type.position' ) ) );
58
59
		$view->textListTypes = $listTypeManager->searchItems( $listSearch );
60
		$view->textTypes = $textTypeManager->searchItems( $search );
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]['supplier.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/supplier/text/decorators/excludes
178
		 * Excludes decorators added by the "common" option from the supplier 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/supplier/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 2018.07
197
		 * @category Developer
198
		 * @see admin/jqadm/common/decorators/default
199
		 * @see admin/jqadm/supplier/text/decorators/global
200
		 * @see admin/jqadm/supplier/text/decorators/local
201
		 */
202
203
		/** admin/jqadm/supplier/text/decorators/global
204
		 * Adds a list of globally available decorators only to the supplier 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/supplier/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 2018.07
221
		 * @category Developer
222
		 * @see admin/jqadm/common/decorators/default
223
		 * @see admin/jqadm/supplier/text/decorators/excludes
224
		 * @see admin/jqadm/supplier/text/decorators/local
225
		 */
226
227
		/** admin/jqadm/supplier/text/decorators/local
228
		 * Adds a list of local decorators only to the supplier 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\Supplier\Decorator\*") around the JQAdm client.
237
		 *
238
		 *  admin/jqadm/supplier/text/decorators/local = array( 'decorator2' )
239
		 *
240
		 * This would add the decorator named "decorator2" defined by
241
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
242
		 *
243
		 * @param array List of decorator names
244
		 * @since 2018.07
245
		 * @category Developer
246
		 * @see admin/jqadm/common/decorators/default
247
		 * @see admin/jqadm/supplier/text/decorators/excludes
248
		 * @see admin/jqadm/supplier/text/decorators/global
249
		 */
250
		return $this->createSubClient( 'supplier/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/supplier/text/standard/subparts
262
		 * List of JQAdm sub-clients rendered within the supplier 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 2018.07
292
		 * @category Developer
293
		 */
294
		return $this->getContext()->getConfig()->get( 'admin/jqadm/supplier/text/standard/subparts', [] );
295
	}
296
297
298
	/**
299
	 * Creates new and updates existing items using the data array
300
	 *
301
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object without referenced domain items
302
	 * @param array $data Data array
303
	 * @return \Aimeos\MShop\Service\Item\Iface Modified supplier item
304
	 */
305
	protected function fromArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data ) : \Aimeos\MShop\Supplier\Item\Iface
306
	{
307
		$context = $this->getContext();
308
309
		$textManager = \Aimeos\MShop::create( $context, 'text' );
310
		$listManager = \Aimeos\MShop::create( $context, 'supplier/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
			if( ( $listItem = $item->getListItem( 'text', $entry['supplier.lists.type'], $entry['text.id'], false ) ) === null ) {
322
				$listItem = $listManager->createItem();
323
			}
324
325
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
326
				$refItem = $textManager->createItem();
327
			}
328
329
			$refItem->fromArray( $entry, true );
330
			$conf = [];
331
332
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
333
			{
334
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
335
					$conf[$key] = trim( $cfg['val'] ?? '' );
336
				}
337
			}
338
339
			$listItem->fromArray( $entry, true );
340
			$listItem->setPosition( $idx );
341
			$listItem->setConfig( $conf );
342
343
			$item->addListItem( 'text', $listItem, $refItem );
344
345
			unset( $listItems[$listItem->getId()] );
346
		}
347
348
		return $item->deleteListItems( $listItems->toArray(), true );
349
	}
350
351
352
	/**
353
	 * Constructs the data array for the view from the given item
354
	 *
355
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object including referenced domain items
356
	 * @param bool $copy True if items should be copied, false if not
357
	 * @return string[] Multi-dimensional associative list of item data
358
	 */
359
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, bool $copy = false ) : array
360
	{
361
		$data = [];
362
		$siteId = $this->getContext()->getLocale()->getSiteId();
363
364
		foreach( $item->getListItems( 'text', null, null, false ) as $listItem )
365
		{
366
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
367
				continue;
368
			}
369
370
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
371
372
			if( $copy === true )
373
			{
374
				$list['supplier.lists.siteid'] = $siteId;
375
				$list['supplier.lists.id'] = '';
376
				$list['text.siteid'] = $siteId;
377
				$list['text.id'] = null;
378
			}
379
380
			$list['supplier.lists.datestart'] = str_replace( ' ', 'T', $list['supplier.lists.datestart'] );
381
			$list['supplier.lists.dateend'] = str_replace( ' ', 'T', $list['supplier.lists.dateend'] );
382
			$list['config'] = [];
383
384
			foreach( $listItem->getConfig() as $key => $value ) {
385
				$list['config'][] = ['key' => $key, 'val' => $value];
386
			}
387
388
			$data[] = $list;
389
		}
390
391
		return $data;
392
	}
393
394
395
	/**
396
	 * Returns the rendered template including the view data
397
	 *
398
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
399
	 * @return string|null HTML output
400
	 */
401
	protected function render( \Aimeos\MW\View\Iface $view ) : string
402
	{
403
		/** admin/jqadm/supplier/text/template-item
404
		 * Relative path to the HTML body template of the text subpart for suppliers.
405
		 *
406
		 * The template file contains the HTML code and processing instructions
407
		 * to generate the result shown in the body of the frontend. The
408
		 * configuration string is the path to the template file relative
409
		 * to the templates directory (usually in admin/jqadm/templates).
410
		 *
411
		 * You can overwrite the template file configuration in extensions and
412
		 * provide alternative templates. These alternative templates should be
413
		 * named like the default one but with the string "default" replaced by
414
		 * an unique name. You may use the name of your project for this. If
415
		 * you've implemented an alternative client class as well, "default"
416
		 * should be replaced by the name of the new class.
417
		 *
418
		 * @param string Relative path to the template creating the HTML code
419
		 * @since 2016.04
420
		 * @category Developer
421
		 */
422
		$tplconf = 'admin/jqadm/supplier/text/template-item';
423
		$default = 'supplier/item-text-standard';
424
425
		return $view->render( $view->config( $tplconf, $default ) );
426
	}
427
}
428