Passed
Push — 2022.07 ( 3d6f84...16edbf )
by Aimeos
22:42 queued 14:52
created

Standard::getDomains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Cms;
12
13
sprintf( 'cms' ); // for translation
14
15
16
/**
17
 * Default implementation of cms 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/name
27
	 * Class name of the used account favorite client implementation
28
	 *
29
	 * Each default admin client can be replace by an alternative imlementation.
30
	 * To use this implementation, you have to set the last part of the class
31
	 * name as configuration value so the client factory knows which class it
32
	 * has to instantiate.
33
	 *
34
	 * For example, if the name of the default class is
35
	 *
36
	 *  \Aimeos\Admin\JQAdm\Cms\Standard
37
	 *
38
	 * and you want to replace it with your own version named
39
	 *
40
	 *  \Aimeos\Admin\JQAdm\Cms\Myfavorite
41
	 *
42
	 * then you have to set the this configuration option:
43
	 *
44
	 *  admin/jqadm/cms/name = Myfavorite
45
	 *
46
	 * The value is the last part of your own class name and it's case sensitive,
47
	 * so take care that the configuration value is exactly named like the last
48
	 * part of the class name.
49
	 *
50
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
51
	 * characters are possible! You should always start the last part of the class
52
	 * name with an upper case character and continue only with lower case characters
53
	 * or numbers. Avoid chamel case names like "MyFavorite"!
54
	 *
55
	 * @param string Last part of the class name
56
	 * @since 2020.10
57
	 * @category Developer
58
	 */
59
60
61
	/**
62
	 * Adds the required data used in the template
63
	 *
64
	 * @param \Aimeos\Base\View\Iface $view View object
65
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
66
	 */
67
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
68
	{
69
		$view->itemSubparts = $this->getSubClientNames();
70
		return $view;
71
	}
72
73
74
	/**
75
	 * Batch update of a resource
76
	 *
77
	 * @return string|null Output to display
78
	 */
79
	public function batch() : ?string
80
	{
81
		return $this->batchBase( 'cms' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('cms') targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
	}
83
84
85
	/**
86
	 * Copies a resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function copy() : ?string
91
	{
92
		$view = $this->object()->data( $this->view() );
93
94
		try
95
		{
96
			if( ( $id = $view->param( 'id' ) ) === null ) {
97
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
98
			}
99
100
			$manager = \Aimeos\MShop::create( $this->context(), 'cms' );
101
			$view->item = $manager->get( $id, $this->getDomains() );
102
103
			$view->itemData = $this->toArray( $view->item, true );
104
			$view->itemBody = parent::copy();
105
		}
106
		catch( \Exception $e )
107
		{
108
			$this->report( $e, 'copy' );
109
		}
110
111
		return $this->render( $view );
112
	}
113
114
115
	/**
116
	 * Creates a new resource
117
	 *
118
	 * @return string|null HTML output
119
	 */
120
	public function create() : ?string
121
	{
122
		$view = $this->object()->data( $this->view() );
123
124
		try
125
		{
126
			$data = $view->param( 'item', [] );
127
128
			if( !isset( $view->item ) ) {
129
				$view->item = \Aimeos\MShop::create( $this->context(), 'cms' )->create();
130
			}
131
132
			$data['cms.siteid'] = $view->item->getSiteId();
133
134
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
135
			$view->itemBody = parent::create();
136
		}
137
		catch( \Exception $e )
138
		{
139
			$this->report( $e, 'create' );
140
		}
141
142
		return $this->render( $view );
143
	}
144
145
146
	/**
147
	 * Deletes a resource
148
	 *
149
	 * @return string|null HTML output
150
	 */
151
	public function delete() : ?string
152
	{
153
		$tags = ['cms'];
154
		$view = $this->view();
155
		$context = $this->context();
156
157
		$manager = \Aimeos\MShop::create( $context, 'cms' );
158
		$manager->begin();
159
160
		try
161
		{
162
			if( ( $ids = $view->param( 'id' ) ) === null ) {
163
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
164
			}
165
166
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
167
			$search->setConditions( $search->compare( '==', 'cms.id', $ids ) );
168
			$items = $manager->search( $search, $this->getDomains() );
169
170
			foreach( $items as $item )
171
			{
172
				$tags[] = 'cms-' . $item->getId();
173
				$view->item = $item;
174
				parent::delete();
175
			}
176
177
			$manager->delete( $items->toArray() );
178
			$manager->commit();
179
180
			$context->cache()->deleteByTags( $tags );
181
182
			return $this->redirect( 'cms', 'search', null, 'delete' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->redirect('cms', 'search', null, 'delete') targeting Aimeos\Admin\JQAdm\Base::redirect() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
183
		}
184
		catch( \Exception $e )
185
		{
186
			$manager->rollback();
187
			$this->report( $e, 'delete' );
188
		}
189
190
		return $this->search();
191
	}
192
193
194
	/**
195
	 * Returns a single resource
196
	 *
197
	 * @return string|null HTML output
198
	 */
199
	public function get() : ?string
200
	{
201
		$view = $this->object()->data( $this->view() );
202
203
		try
204
		{
205
			if( ( $id = $view->param( 'id' ) ) === null ) {
206
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
207
			}
208
209
			$manager = \Aimeos\MShop::create( $this->context(), 'cms' );
210
211
			$view->item = $manager->get( $id, $this->getDomains() );
212
			$view->itemData = $this->toArray( $view->item );
213
			$view->itemBody = parent::get();
214
		}
215
		catch( \Exception $e )
216
		{
217
			$this->report( $e, 'get' );
218
		}
219
220
		return $this->render( $view );
221
	}
222
223
224
	/**
225
	 * Saves the data
226
	 *
227
	 * @return string|null HTML output
228
	 */
229
	public function save() : ?string
230
	{
231
		$view = $this->view();
232
		$context = $this->context();
233
234
		$manager = \Aimeos\MShop::create( $context, 'cms' );
235
		$manager->begin();
236
237
		try
238
		{
239
			$item = $this->fromArray( $view->param( 'item', [] ) );
240
			$view->item = $item->getId() ? $item : $manager->save( $item );
241
			$view->itemBody = parent::save();
242
243
			$manager->save( clone $view->item );
244
			$manager->commit();
245
246
			$context->cache()->deleteByTags( ['cms', 'cms-' . $view->item->getId()] );
247
248
			return $this->redirect( 'cms', $view->param( 'next' ), $view->item->getId(), 'save' );
0 ignored issues
show
Bug introduced by
It seems like $view->item->getId() can also be of type Aimeos\Map; however, parameter $id of Aimeos\Admin\JQAdm\Base::redirect() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

248
			return $this->redirect( 'cms', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
Bug introduced by
Are you sure the usage of $this->redirect('cms', $...>item->getId(), 'save') targeting Aimeos\Admin\JQAdm\Base::redirect() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
249
		}
250
		catch( \Exception $e )
251
		{
252
			$manager->rollback();
253
			$this->report( $e, 'save' );
254
		}
255
256
		return $this->create();
257
	}
258
259
260
	/**
261
	 * Returns a list of resource according to the conditions
262
	 *
263
	 * @return string|null HTML output
264
	 */
265
	public function search() : ?string
266
	{
267
		$view = $this->view();
268
269
		try
270
		{
271
			$total = 0;
272
			$params = $this->storeFilter( $view->param(), 'cms' );
273
			$manager = \Aimeos\MShop::create( $this->context(), 'cms' );
274
			$search = $this->initCriteria( $manager->filter()->order( 'cms.url' ), $params );
275
276
			$view->items = $manager->search( $search, $this->getDomains(), $total );
277
			$view->filterAttributes = $manager->getSearchAttributes( true );
278
			$view->filterOperators = $search->getOperators();
279
			$view->itemBody = parent::search();
280
			$view->total = $total;
281
		}
282
		catch( \Exception $e )
283
		{
284
			$this->report( $e, 'search' );
285
		}
286
287
		/** admin/jqadm/cms/template-list
288
		 * Relative path to the HTML body template for the cms list.
289
		 *
290
		 * The template file contains the HTML code and processing instructions
291
		 * to generate the result shown in the body of the frontend. The
292
		 * configuration string is the path to the template file relative
293
		 * to the templates directory (usually in admin/jqadm/templates).
294
		 *
295
		 * You can overwrite the template file configuration in extensions and
296
		 * provide alternative templates. These alternative templates should be
297
		 * named like the default one but with the string "default" replaced by
298
		 * an unique name. You may use the name of your project for this. If
299
		 * you've implemented an alternative client class as well, "default"
300
		 * should be replaced by the name of the new class.
301
		 *
302
		 * @param string Relative path to the template creating the HTML code
303
		 * @since 2020.10
304
		 * @category Developer
305
		 */
306
		$tplconf = 'admin/jqadm/cms/template-list';
307
		$default = 'cms/list';
308
309
		return $view->render( $view->config( $tplconf, $default ) );
310
	}
311
312
313
	/**
314
	 * Returns the sub-client given by its name.
315
	 *
316
	 * @param string $type Name of the client type
317
	 * @param string|null $name Name of the sub-client (Default if null)
318
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
319
	 */
320
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
321
	{
322
		/** admin/jqadm/cms/decorators/excludes
323
		 * Excludes decorators added by the "common" option from the cms JQAdm client
324
		 *
325
		 * Decorators extend the functionality of a class by adding new aspects
326
		 * (e.g. log what is currently done), executing the methods of the underlying
327
		 * class only in certain conditions (e.g. only for logged in users) or
328
		 * modify what is returned to the caller.
329
		 *
330
		 * This option allows you to remove a decorator added via
331
		 * "client/jqadm/common/decorators/default" before they are wrapped
332
		 * around the JQAdm client.
333
		 *
334
		 *  admin/jqadm/cms/decorators/excludes = array( 'decorator1' )
335
		 *
336
		 * This would remove the decorator named "decorator1" from the list of
337
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
338
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
339
		 *
340
		 * @param array List of decorator names
341
		 * @since 2020.10
342
		 * @category Developer
343
		 * @see admin/jqadm/common/decorators/default
344
		 * @see admin/jqadm/cms/decorators/global
345
		 * @see admin/jqadm/cms/decorators/local
346
		 */
347
348
		/** admin/jqadm/cms/decorators/global
349
		 * Adds a list of globally available decorators only to the cms JQAdm client
350
		 *
351
		 * Decorators extend the functionality of a class by adding new aspects
352
		 * (e.g. log what is currently done), executing the methods of the underlying
353
		 * class only in certain conditions (e.g. only for logged in users) or
354
		 * modify what is returned to the caller.
355
		 *
356
		 * This option allows you to wrap global decorators
357
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
358
		 *
359
		 *  admin/jqadm/cms/decorators/global = array( 'decorator1' )
360
		 *
361
		 * This would add the decorator named "decorator1" defined by
362
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
363
		 *
364
		 * @param array List of decorator names
365
		 * @since 2020.10
366
		 * @category Developer
367
		 * @see admin/jqadm/common/decorators/default
368
		 * @see admin/jqadm/cms/decorators/excludes
369
		 * @see admin/jqadm/cms/decorators/local
370
		 */
371
372
		/** admin/jqadm/cms/decorators/local
373
		 * Adds a list of local decorators only to the cms JQAdm client
374
		 *
375
		 * Decorators extend the functionality of a class by adding new aspects
376
		 * (e.g. log what is currently done), executing the methods of the underlying
377
		 * class only in certain conditions (e.g. only for logged in users) or
378
		 * modify what is returned to the caller.
379
		 *
380
		 * This option allows you to wrap local decorators
381
		 * ("\Aimeos\Admin\JQAdm\Cms\Decorator\*") around the JQAdm client.
382
		 *
383
		 *  admin/jqadm/cms/decorators/local = array( 'decorator2' )
384
		 *
385
		 * This would add the decorator named "decorator2" defined by
386
		 * "\Aimeos\Admin\JQAdm\Cms\Decorator\Decorator2" only to the JQAdm client.
387
		 *
388
		 * @param array List of decorator names
389
		 * @since 2020.10
390
		 * @category Developer
391
		 * @see admin/jqadm/common/decorators/default
392
		 * @see admin/jqadm/cms/decorators/excludes
393
		 * @see admin/jqadm/cms/decorators/global
394
		 */
395
		return $this->createSubClient( 'cms/' . $type, $name );
396
	}
397
398
399
	/**
400
	 * Returns the domain names whose items should be fetched too
401
	 *
402
	 * @return string[] List of domain names
403
	 */
404
	protected function getDomains() : array
405
	{
406
		/** admin/jqadm/cms/domains
407
		 * List of domain items that should be fetched along with the cms
408
		 *
409
		 * If you need to display additional content, you can configure your own
410
		 * list of domains (cms, media, price, cms, text, etc. are
411
		 * domains) whose items are fetched from the storage.
412
		 *
413
		 * @param array List of domain names
414
		 * @since 2020.10
415
		 * @category Developer
416
		 */
417
		return $this->context()->config()->get( 'admin/jqadm/cms/domains', [] );
418
	}
419
420
421
	/**
422
	 * Returns the list of sub-client names configured for the client.
423
	 *
424
	 * @return array List of JQAdm client names
425
	 */
426
	protected function getSubClientNames() : array
427
	{
428
		/** admin/jqadm/cms/subparts
429
		 * List of JQAdm sub-clients rendered within the cms section
430
		 *
431
		 * The output of the frontend is composed of the code generated by the JQAdm
432
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
433
		 * that are responsible for rendering certain sub-parts of the output. The
434
		 * sub-clients can contain JQAdm clients themselves and therefore a
435
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
436
		 * the output that is placed inside the container of its parent.
437
		 *
438
		 * At first, always the JQAdm code generated by the parent is printed, then
439
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
440
		 * determines the order of the output of these sub-clients inside the parent
441
		 * container. If the configured list of clients is
442
		 *
443
		 *  array( "subclient1", "subclient2" )
444
		 *
445
		 * you can easily change the order of the output by reordering the subparts:
446
		 *
447
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
448
		 *
449
		 * You can also remove one or more parts if they shouldn't be rendered:
450
		 *
451
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
452
		 *
453
		 * As the clients only generates structural JQAdm, the layout defined via CSS
454
		 * should support adding, removing or reordering content by a fluid like
455
		 * design.
456
		 *
457
		 * @param array List of sub-client names
458
		 * @since 2020.10
459
		 * @category Developer
460
		 */
461
		return $this->context()->config()->get( 'admin/jqadm/cms/subparts', [] );
462
	}
463
464
465
	/**
466
	 * Creates new and updates existing items using the data array
467
	 *
468
	 * @param array $data Data array
469
	 * @return \Aimeos\MShop\Cms\Item\Iface New cms item object
470
	 */
471
	protected function fromArray( array $data ) : \Aimeos\MShop\Cms\Item\Iface
472
	{
473
		$manager = \Aimeos\MShop::create( $this->context(), 'cms' );
474
475
		if( isset( $data['cms.id'] ) && $data['cms.id'] != '' ) {
476
			$item = $manager->get( $data['cms.id'], $this->getDomains() );
477
		} else {
478
			$item = $manager->create();
479
		}
480
481
		$item->fromArray( $data, true );
482
483
		return $item;
484
	}
485
486
487
	/**
488
	 * Constructs the data array for the view from the given item
489
	 *
490
	 * @param \Aimeos\MShop\Cms\Item\Iface $item Cms item object
491
	 * @return string[] Multi-dimensional associative list of item data
492
	 */
493
	protected function toArray( \Aimeos\MShop\Cms\Item\Iface $item, bool $copy = false ) : array
494
	{
495
		$data = $item->toArray( true );
496
497
		if( $copy === true )
498
		{
499
			$data['cms.siteid'] = $this->context()->locale()->getSiteId();
500
			$data['cms.url'] = $data['cms.url'] . '_copy';
501
			$data['cms.id'] = '';
502
		}
503
504
		return $data;
505
	}
506
507
508
	/**
509
	 * Returns the rendered template including the view data
510
	 *
511
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
512
	 * @return string HTML output
513
	 */
514
	protected function render( \Aimeos\Base\View\Iface $view ) : string
515
	{
516
		/** admin/jqadm/cms/template-item
517
		 * Relative path to the HTML body template for the cms item.
518
		 *
519
		 * The template file contains the HTML code and processing instructions
520
		 * to generate the result shown in the body of the frontend. The
521
		 * configuration string is the path to the template file relative
522
		 * to the templates directory (usually in admin/jqadm/templates).
523
		 *
524
		 * You can overwrite the template file configuration in extensions and
525
		 * provide alternative templates. These alternative templates should be
526
		 * named like the default one but with the string "default" replaced by
527
		 * an unique name. You may use the name of your project for this. If
528
		 * you've implemented an alternative client class as well, "default"
529
		 * should be replaced by the name of the new class.
530
		 *
531
		 * @param string Relative path to the template creating the HTML code
532
		 * @since 2020.10
533
		 * @category Developer
534
		 */
535
		$tplconf = 'admin/jqadm/cms/template-item';
536
		$default = 'cms/item';
537
538
		return $view->render( $view->config( $tplconf, $default ) );
539
	}
540
}
541