Standard   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 369
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 78
c 2
b 0
f 0
dl 0
loc 369
rs 10
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 7 1
A data() 0 14 1
A getSubClient() 0 73 1
A create() 0 16 2
A save() 0 8 1
A delete() 0 8 1
A getSubClientNames() 0 35 1
A get() 0 7 1
B fromArray() 0 36 8
A toArray() 0 33 5
A render() 0 24 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
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
	 */
35
36
37
	/**
38
	 * Adds the required data used in the text template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		$context = $this->context();
46
47
		$textTypeManager = \Aimeos\MShop::create( $context, 'text/type' );
48
		$listTypeManager = \Aimeos\MShop::create( $context, 'supplier/lists/type' );
49
50
		$search = $textTypeManager->filter( true )->order( 'text.type.code' )->slice( 0, 10000 );
51
		$listSearch = $listTypeManager->filter( true )->order( 'supplier.lists.type.code' )->slice( 0, 10000 );
52
53
		$view->textListTypes = $listTypeManager->search( $listSearch );
54
		$view->textTypes = $textTypeManager->search( $search );
55
56
		return $view;
57
	}
58
59
60
	/**
61
	 * Copies a resource
62
	 *
63
	 * @return string|null HTML output
64
	 */
65
	public function copy() : ?string
66
	{
67
		$view = $this->object()->data( $this->view() );
68
		$view->textData = $this->toArray( $view->item, true );
69
		$view->textBody = parent::copy();
70
71
		return $this->render( $view );
72
	}
73
74
75
	/**
76
	 * Creates a new resource
77
	 *
78
	 * @return string|null HTML output
79
	 */
80
	public function create() : ?string
81
	{
82
		$view = $this->object()->data( $this->view() );
83
		$siteid = $this->context()->locale()->getSiteId();
84
		$data = $view->param( 'text', [] );
85
86
		foreach( $data as $idx => $entry )
87
		{
88
			$data[$idx]['supplier.lists.siteid'] = $siteid;
89
			$data[$idx]['text.siteid'] = $siteid;
90
		}
91
92
		$view->textData = $data;
93
		$view->textBody = parent::create();
94
95
		return $this->render( $view );
96
	}
97
98
99
	/**
100
	 * Deletes a resource
101
	 *
102
	 * @return string|null HTML output
103
	 */
104
	public function delete() : ?string
105
	{
106
		parent::delete();
107
108
		$item = $this->view()->item;
109
		$item->deleteListItems( $item->getListItems( 'text', null, null, false ), true );
110
111
		return null;
112
	}
113
114
115
	/**
116
	 * Returns a single resource
117
	 *
118
	 * @return string|null HTML output
119
	 */
120
	public function get() : ?string
121
	{
122
		$view = $this->object()->data( $this->view() );
123
		$view->textData = $this->toArray( $view->item );
124
		$view->textBody = parent::get();
125
126
		return $this->render( $view );
127
	}
128
129
130
	/**
131
	 * Saves the data
132
	 *
133
	 * @return string|null HTML output
134
	 */
135
	public function save() : ?string
136
	{
137
		$view = $this->view();
138
139
		$view->item = $this->fromArray( $view->item, $view->param( 'text', [] ) );
140
		$view->textBody = parent::save();
141
142
		return null;
143
	}
144
145
146
	/**
147
	 * Returns the sub-client given by its name.
148
	 *
149
	 * @param string $type Name of the client type
150
	 * @param string|null $name Name of the sub-client (Default if null)
151
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
152
	 */
153
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
154
	{
155
		/** admin/jqadm/supplier/text/decorators/excludes
156
		 * Excludes decorators added by the "common" option from the supplier JQAdm client
157
		 *
158
		 * Decorators extend the functionality of a class by adding new aspects
159
		 * (e.g. log what is currently done), executing the methods of the underlying
160
		 * class only in certain conditions (e.g. only for logged in users) or
161
		 * modify what is returned to the caller.
162
		 *
163
		 * This option allows you to remove a decorator added via
164
		 * "admin/jqadm/common/decorators/default" before they are wrapped
165
		 * around the JQAdm client.
166
		 *
167
		 *  admin/jqadm/supplier/text/decorators/excludes = array( 'decorator1' )
168
		 *
169
		 * This would remove the decorator named "decorator1" from the list of
170
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
171
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
172
		 *
173
		 * @param array List of decorator names
174
		 * @since 2018.07
175
		 * @see admin/jqadm/common/decorators/default
176
		 * @see admin/jqadm/supplier/text/decorators/global
177
		 * @see admin/jqadm/supplier/text/decorators/local
178
		 */
179
180
		/** admin/jqadm/supplier/text/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/text/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.07
198
		 * @see admin/jqadm/common/decorators/default
199
		 * @see admin/jqadm/supplier/text/decorators/excludes
200
		 * @see admin/jqadm/supplier/text/decorators/local
201
		 */
202
203
		/** admin/jqadm/supplier/text/decorators/local
204
		 * Adds a list of local 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 local decorators
212
		 * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client.
213
		 *
214
		 *  admin/jqadm/supplier/text/decorators/local = array( 'decorator2' )
215
		 *
216
		 * This would add the decorator named "decorator2" defined by
217
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
218
		 *
219
		 * @param array List of decorator names
220
		 * @since 2018.07
221
		 * @see admin/jqadm/common/decorators/default
222
		 * @see admin/jqadm/supplier/text/decorators/excludes
223
		 * @see admin/jqadm/supplier/text/decorators/global
224
		 */
225
		return $this->createSubClient( 'supplier/text/' . $type, $name );
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() : array
235
	{
236
		/** admin/jqadm/supplier/text/subparts
237
		 * List of JQAdm sub-clients rendered within the supplier text 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.07
267
		 */
268
		return $this->context()->config()->get( 'admin/jqadm/supplier/text/subparts', [] );
269
	}
270
271
272
	/**
273
	 * Creates new and updates existing items using the data array
274
	 *
275
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object without referenced domain items
276
	 * @param array $data Data array
277
	 * @return \Aimeos\MShop\Service\Item\Iface Modified supplier item
278
	 */
279
	protected function fromArray( \Aimeos\MShop\Supplier\Item\Iface $item, array $data ) : \Aimeos\MShop\Supplier\Item\Iface
280
	{
281
		$context = $this->context();
282
283
		$textManager = \Aimeos\MShop::create( $context, 'text' );
284
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
285
286
		$listItems = $item->getListItems( 'text', null, null, false );
287
288
		foreach( $data as $idx => $entry )
289
		{
290
			if( trim( $this->val( $entry, 'text.content', '' ) ) === '' ) {
291
				continue;
292
			}
293
294
			$id = $this->val( $entry, 'text.id', '' );
295
			$type = $this->val( $entry, 'supplier.lists.type', 'default' );
296
297
			$listItem = $item->getListItem( 'text', $type, $id, false ) ?: $manager->createListItem();
0 ignored issues
show
Bug introduced by
The method createListItem() 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

297
			$listItem = $item->getListItem( 'text', $type, $id, false ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

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...
298
			$refItem = $listItem->getRefItem() ?: $textManager->create();
299
300
			$refItem->fromArray( $entry, true );
301
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
302
303
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
304
			{
305
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
306
					$listItem->setConfigValue( $key, json_decode( $val, true ) ?? $val );
307
				}
308
			}
309
310
			$item->addListItem( 'text', $listItem, $refItem );
311
			unset( $listItems[$listItem->getId()] );
312
		}
313
314
		return $item->deleteListItems( $listItems, true );
315
	}
316
317
318
	/**
319
	 * Constructs the data array for the view from the given item
320
	 *
321
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object including referenced domain items
322
	 * @param bool $copy True if items should be copied, false if not
323
	 * @return string[] Multi-dimensional associative list of item data
324
	 */
325
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, bool $copy = false ) : array
326
	{
327
		$data = [];
328
		$siteId = $this->context()->locale()->getSiteId();
329
330
		foreach( $item->getListItems( 'text', null, null, false ) as $listItem )
331
		{
332
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
333
				continue;
334
			}
335
336
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
337
338
			if( $copy === true )
339
			{
340
				$list['supplier.lists.siteid'] = $siteId;
341
				$list['supplier.lists.id'] = '';
342
				$list['text.siteid'] = $siteId;
343
				$list['text.id'] = null;
344
			}
345
346
			$list['supplier.lists.datestart'] = str_replace( ' ', 'T', $list['supplier.lists.datestart'] ?? '' );
347
			$list['supplier.lists.dateend'] = str_replace( ' ', 'T', $list['supplier.lists.dateend'] ?? '' );
348
			$list['config'] = [];
349
350
			foreach( $listItem->getConfig() as $key => $value ) {
351
				$list['config'][] = ['key' => $key, 'val' => $value];
352
			}
353
354
			$data[] = $list;
355
		}
356
357
		return $data;
358
	}
359
360
361
	/**
362
	 * Returns the rendered template including the view data
363
	 *
364
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
365
	 * @return string HTML output
366
	 */
367
	protected function render( \Aimeos\Base\View\Iface $view ) : string
368
	{
369
		/** admin/jqadm/supplier/text/template-item
370
		 * Relative path to the HTML body template of the text subpart for suppliers.
371
		 *
372
		 * The template file contains the HTML code and processing instructions
373
		 * to generate the result shown in the body of the frontend. The
374
		 * configuration string is the path to the template file relative
375
		 * to the templates directory (usually in templates/admin/jqadm).
376
		 *
377
		 * You can overwrite the template file configuration in extensions and
378
		 * provide alternative templates. These alternative templates should be
379
		 * named like the default one but with the string "default" replaced by
380
		 * an unique name. You may use the name of your project for this. If
381
		 * you've implemented an alternative client class as well, "default"
382
		 * should be replaced by the name of the new class.
383
		 *
384
		 * @param string Relative path to the template creating the HTML code
385
		 * @since 2016.04
386
		 */
387
		$tplconf = 'admin/jqadm/supplier/text/template-item';
388
		$default = 'supplier/item-text';
389
390
		return $view->render( $view->config( $tplconf, $default ) );
391
	}
392
}
393