Standard::toArray()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
nc 6
nop 2
dl 0
loc 37
c 0
b 0
f 0
cc 7
rs 8.6666
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
		$typeManager = \Aimeos\MShop::create( $context, 'text/type' );
270
		$listTypeManager = \Aimeos\MShop::create( $context, 'cms/lists/type' );
271
272
		$search = $typeManager->filter( true )->add( 'text.type.code', '=~', 'meta-' )->order( 'text.type.code' )->slice( 0, 10000 );
273
		$listSearch = $listTypeManager->filter( true )->order( 'cms.lists.type.code' )->slice( 0, 10000 );
274
275
		$view->seoTypes = $typeManager->search( $search );
276
		$view->seoListTypes = $listTypeManager->search( $listSearch );
277
278
		return $view;
279
	}
280
281
282
	/**
283
	 * Creates new and updates existing items using the data array
284
	 *
285
	 * @param \Aimeos\MShop\Cms\Item\Iface $item Cms item object without referenced domain items
286
	 * @param array $data Data array
287
	 * @return \Aimeos\MShop\Cms\Item\Iface Modified cms item
288
	 */
289
	protected function fromArray( \Aimeos\MShop\Cms\Item\Iface $item, array $data ) : \Aimeos\MShop\Cms\Item\Iface
290
	{
291
		$context = $this->context();
292
293
		$textManager = \Aimeos\MShop::create( $context, 'text' );
294
		$manager = \Aimeos\MShop::create( $context, 'cms' );
295
296
		$listItems = $item->getListItems( 'text', null, null, false )->filter( function( $item ) {
297
			return $item->getRefItem() === null || $item->getRefItem()->getType() !== 'content';
298
		} );
299
300
301
		foreach( $data as $idx => $entry )
302
		{
303
			if( trim( $this->val( $entry, 'text.content', '' ) ) === '' ) {
304
				continue;
305
			}
306
307
			$id = $this->val( $entry, 'text.id', '' );
308
			$type = $this->val( $entry, 'cms.lists.type', 'default' );
309
310
			$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

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