Standard::fromArray()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 23
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 44
rs 8.4444
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Cms\Seo;
12
13
sprintf( 'seo' ); // for translation
14
15
16
/**
17
 * Default implementation of cms SEO 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/cms/seo/name
27
	 * Name of the SEO subpart used by the JQAdm cms implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Cms\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 2020.10
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string|null HTML output
42
	 */
43
	public function copy() : ?string
44
	{
45
		$view = $this->object()->data( $this->view() );
46
		$view->seoData = $this->toArray( $view->item, true );
47
		$view->seoBody = parent::copy();
48
49
		return $this->render( $view );
50
	}
51
52
53
	/**
54
	 * Creates a new resource
55
	 *
56
	 * @return string|null HTML output
57
	 */
58
	public function create() : ?string
59
	{
60
		$view = $this->object()->data( $this->view() );
61
		$siteid = $this->context()->locale()->getSiteId();
62
		$data = $view->param( 'seo', [] );
63
64
		foreach( $data as $idx => $entry )
65
		{
66
			$data[$idx]['cms.lists.siteid'] = $siteid;
67
			$data[$idx]['text.siteid'] = $siteid;
68
		}
69
70
		$view->seoData = $data;
71
		$view->seoBody = parent::create();
72
73
		return $this->render( $view );
74
	}
75
76
77
	/**
78
	 * Deletes a resource
79
	 *
80
	 * @return string|null HTML output
81
	 */
82
	public function delete() : ?string
83
	{
84
		parent::delete();
85
86
		$item = $this->view()->item;
87
88
		$listItems = $item->getListItems( 'text', null, null, false )->filter( function( $item ) {
89
			return $item->getRefItem() === null || $item->getRefItem()->getType() !== 'content';
90
		} );
91
92
		$item->deleteListItems( $listItems, true );
93
94
		return null;
95
	}
96
97
98
	/**
99
	 * Returns a single resource
100
	 *
101
	 * @return string|null HTML output
102
	 */
103
	public function get() : ?string
104
	{
105
		$view = $this->object()->data( $this->view() );
106
		$view->seoData = $this->toArray( $view->item );
107
		$view->seoBody = parent::get();
108
109
		return $this->render( $view );
110
	}
111
112
113
	/**
114
	 * Saves the data
115
	 *
116
	 * @return string|null HTML output
117
	 */
118
	public function save() : ?string
119
	{
120
		$view = $this->view();
121
122
		$view->item = $this->fromArray( $view->item, $view->param( 'seo', [] ) );
123
		$view->seoBody = parent::save();
124
125
		return null;
126
	}
127
128
129
	/**
130
	 * Returns the sub-client given by its name.
131
	 *
132
	 * @param string $type Name of the client type
133
	 * @param string|null $name Name of the sub-client (Default if null)
134
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
135
	 */
136
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
137
	{
138
		/** admin/jqadm/cms/seo/decorators/excludes
139
		 * Excludes decorators added by the "common" option from the cms JQAdm client
140
		 *
141
		 * Decorators extend the functionality of a class by adding new aspects
142
		 * (e.g. log what is currently done), executing the methods of the underlying
143
		 * class only in certain conditions (e.g. only for logged in users) or
144
		 * modify what is returned to the caller.
145
		 *
146
		 * This option allows you to remove a decorator added via
147
		 * "admin/jqadm/common/decorators/default" before they are wrapped
148
		 * around the JQAdm client.
149
		 *
150
		 *  admin/jqadm/cms/seo/decorators/excludes = array( 'decorator1' )
151
		 *
152
		 * This would remove the decorator named "decorator1" from the list of
153
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
154
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
155
		 *
156
		 * @param array List of decorator names
157
		 * @since 2020.10
158
		 * @category Developer
159
		 * @see admin/jqadm/common/decorators/default
160
		 * @see admin/jqadm/cms/seo/decorators/global
161
		 * @see admin/jqadm/cms/seo/decorators/local
162
		 */
163
164
		/** admin/jqadm/cms/seo/decorators/global
165
		 * Adds a list of globally available decorators only to the cms JQAdm client
166
		 *
167
		 * Decorators extend the functionality of a class by adding new aspects
168
		 * (e.g. log what is currently done), executing the methods of the underlying
169
		 * class only in certain conditions (e.g. only for logged in users) or
170
		 * modify what is returned to the caller.
171
		 *
172
		 * This option allows you to wrap global decorators
173
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
174
		 *
175
		 *  admin/jqadm/cms/seo/decorators/global = array( 'decorator1' )
176
		 *
177
		 * This would add the decorator named "decorator1" defined by
178
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
179
		 *
180
		 * @param array List of decorator names
181
		 * @since 2020.10
182
		 * @category Developer
183
		 * @see admin/jqadm/common/decorators/default
184
		 * @see admin/jqadm/cms/seo/decorators/excludes
185
		 * @see admin/jqadm/cms/seo/decorators/local
186
		 */
187
188
		/** admin/jqadm/cms/seo/decorators/local
189
		 * Adds a list of local decorators only to the cms JQAdm client
190
		 *
191
		 * Decorators extend the functionality of a class by adding new aspects
192
		 * (e.g. log what is currently done), executing the methods of the underlying
193
		 * class only in certain conditions (e.g. only for logged in users) or
194
		 * modify what is returned to the caller.
195
		 *
196
		 * This option allows you to wrap local decorators
197
		 * ("\Aimeos\Admin\JQAdm\Cms\Decorator\*") around the JQAdm client.
198
		 *
199
		 *  admin/jqadm/cms/seo/decorators/local = array( 'decorator2' )
200
		 *
201
		 * This would add the decorator named "decorator2" defined by
202
		 * "\Aimeos\Admin\JQAdm\Cms\Decorator\Decorator2" only to the JQAdm client.
203
		 *
204
		 * @param array List of decorator names
205
		 * @since 2020.10
206
		 * @category Developer
207
		 * @see admin/jqadm/common/decorators/default
208
		 * @see admin/jqadm/cms/seo/decorators/excludes
209
		 * @see admin/jqadm/cms/seo/decorators/global
210
		 */
211
		return $this->createSubClient( 'cms/seo/' . $type, $name );
212
	}
213
214
215
	/**
216
	 * Returns the list of sub-client names configured for the client.
217
	 *
218
	 * @return array List of JQAdm client names
219
	 */
220
	protected function getSubClientNames() : array
221
	{
222
		/** admin/jqadm/cms/seo/subparts
223
		 * List of JQAdm sub-clients rendered within the cms seo section
224
		 *
225
		 * The output of the frontend is composed of the code generated by the JQAdm
226
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
227
		 * that are responsible for rendering certain sub-parts of the output. The
228
		 * sub-clients can contain JQAdm clients themselves and therefore a
229
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
230
		 * the output that is placed inside the container of its parent.
231
		 *
232
		 * At first, always the JQAdm code generated by the parent is printed, then
233
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
234
		 * determines the order of the output of these sub-clients inside the parent
235
		 * container. If the configured list of clients is
236
		 *
237
		 *  array( "subclient1", "subclient2" )
238
		 *
239
		 * you can easily change the order of the output by reordering the subparts:
240
		 *
241
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
242
		 *
243
		 * You can also remove one or more parts if they shouldn't be rendered:
244
		 *
245
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
246
		 *
247
		 * As the clients only generates structural JQAdm, the layout defined via CSS
248
		 * should support adding, removing or reordering content by a fluid like
249
		 * design.
250
		 *
251
		 * @param array List of sub-client names
252
		 * @since 2020.10
253
		 * @category Developer
254
		 */
255
		return $this->context()->config()->get( 'admin/jqadm/cms/seo/subparts', [] );
256
	}
257
258
259
	/**
260
	 * Adds the required data used in the seo template
261
	 *
262
	 * @param \Aimeos\Base\View\Iface $view View object
263
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
264
	 */
265
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
266
	{
267
		$context = $this->context();
268
269
		$textTypeManager = \Aimeos\MShop::create( $context, 'text/type' );
270
		$listTypeManager = \Aimeos\MShop::create( $context, 'cms/lists/type' );
271
272
		$search = $textTypeManager->filter( true )->slice( 0, 10000 );
273
		$search->add( $search->and( [
274
			$search->is( 'text.type.domain', '==', 'cms' ),
275
			$search->is( 'text.type.code', '!=', 'content' )
276
		] ) );
277
		$search->setSortations( [$search->sort( '+', 'text.type.position' )] );
278
279
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
280
		$listSearch->setConditions( $listSearch->compare( '==', 'cms.lists.type.domain', 'text' ) );
281
		$listSearch->setSortations( [$listSearch->sort( '+', 'cms.lists.type.position' )] );
282
283
		$view->seoTypes = $textTypeManager->search( $search );
284
		$view->seoListTypes = $listTypeManager->search( $listSearch );
285
286
		return $view;
287
	}
288
289
290
	/**
291
	 * Creates new and updates existing items using the data array
292
	 *
293
	 * @param \Aimeos\MShop\Cms\Item\Iface $item Cms item object without referenced domain items
294
	 * @param array $data Data array
295
	 * @return \Aimeos\MShop\Cms\Item\Iface Modified cms item
296
	 */
297
	protected function fromArray( \Aimeos\MShop\Cms\Item\Iface $item, array $data ) : \Aimeos\MShop\Cms\Item\Iface
298
	{
299
		$context = $this->context();
300
301
		$textManager = \Aimeos\MShop::create( $context, 'text' );
302
		$manager = \Aimeos\MShop::create( $context, 'cms' );
303
304
		$listItems = $item->getListItems( 'text', null, null, false )->filter( function( $item ) {
305
			return $item->getRefItem() === null || $item->getRefItem()->getType() !== 'content';
306
		} );
307
308
309
		foreach( $data as $idx => $entry )
310
		{
311
			if( trim( $this->val( $entry, 'text.content', '' ) ) === '' ) {
312
				continue;
313
			}
314
315
			$id = $this->val( $entry, 'text.id', '' );
316
			$type = $this->val( $entry, 'cms.lists.type', 'default' );
317
318
			$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

318
			$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...
319
			$refItem = $listItem->getRefItem() ?: $textManager->create();
320
321
			$refItem->fromArray( $entry, true );
322
			$conf = [];
323
324
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
325
			{
326
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
327
					$conf[$key] = trim( $cfg['val'] ?? '' );
328
				}
329
			}
330
331
			$listItem->fromArray( $entry, true );
332
			$listItem->setPosition( $idx );
333
			$listItem->setConfig( $conf );
334
335
			$item->addListItem( 'text', $listItem, $refItem );
336
337
			unset( $listItems[$listItem->getId()] );
338
		}
339
340
		return $item->deleteListItems( $listItems->toArray(), true );
341
	}
342
343
344
	/**
345
	 * Constructs the data array for the view from the given item
346
	 *
347
	 * @param \Aimeos\MShop\Cms\Item\Iface $item Cms item object including referenced domain items
348
	 * @param bool $copy True if items should be copied, false if not
349
	 * @return string[] Multi-dimensional associative list of item data
350
	 */
351
	protected function toArray( \Aimeos\MShop\Cms\Item\Iface $item, bool $copy = false ) : array
352
	{
353
		$data = [];
354
		$siteId = $this->context()->locale()->getSiteId();
355
356
		$listItems = $item->getListItems( 'text', null, null, false )->filter( function( $item ) {
357
			return $item->getRefItem() === null || $item->getRefItem()->getType() !== 'content';
358
		} );
359
360
		foreach( $listItems as $listItem )
361
		{
362
			if( ( $refItem = $listItem->getRefItem() ) === null || $refItem->getType() === 'content' ) {
363
				continue;
364
			}
365
366
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
367
368
			if( $copy === true )
369
			{
370
				$list['cms.lists.siteid'] = $siteId;
371
				$list['cms.lists.id'] = '';
372
				$list['text.siteid'] = $siteId;
373
				$list['text.id'] = null;
374
			}
375
376
			$list['cms.lists.datestart'] = str_replace( ' ', 'T', $list['cms.lists.datestart'] ?? '' );
377
			$list['cms.lists.dateend'] = str_replace( ' ', 'T', $list['cms.lists.dateend'] ?? '' );
378
			$list['config'] = [];
379
380
			foreach( $listItem->getConfig() as $key => $value ) {
381
				$list['config'][] = ['key' => $key, 'val' => $value];
382
			}
383
384
			$data[] = $list;
385
		}
386
387
		return $data;
388
	}
389
390
391
	/**
392
	 * Returns the rendered template including the view data
393
	 *
394
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
395
	 * @return string HTML output
396
	 */
397
	protected function render( \Aimeos\Base\View\Iface $view ) : string
398
	{
399
		/** admin/jqadm/cms/seo/template-item
400
		 * Relative path to the HTML body template of the seo subpart for cmss.
401
		 *
402
		 * The template file contains the HTML code and processing instructions
403
		 * to generate the result shown in the body of the frontend. The
404
		 * configuration string is the path to the template file relative
405
		 * to the templates directory (usually in admin/jqadm/templates).
406
		 *
407
		 * You can overwrite the template file configuration in extensions and
408
		 * provide alternative templates. These alternative templates should be
409
		 * named like the default one but with the string "default" replaced by
410
		 * an unique name. You may use the name of your project for this. If
411
		 * you've implemented an alternative client class as well, "default"
412
		 * should be replaced by the name of the new class.
413
		 *
414
		 * @param string Relative path to the template creating the HTML code
415
		 * @since 2020.10
416
		 * @category Developer
417
		 */
418
		$tplconf = 'admin/jqadm/cms/seo/template-item';
419
		$default = 'cms/item-seo';
420
421
		return $view->render( $view->config( $tplconf, $default ) );
422
	}
423
}
424