Standard   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 545
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 128
dl 0
loc 545
rs 9.44
c 1
b 0
f 0
wmc 37

15 Methods

Rating   Name   Duplication   Size   Complexity  
A batch() 0 3 1
A create() 0 23 3
A copy() 0 26 3
A data() 0 5 1
A checkSite() 0 8 4
A get() 0 26 3
A search() 0 52 3
A getSubClient() 0 73 1
A delete() 0 37 4
A save() 0 25 3
A fromArray() 0 29 6
A toArray() 0 12 2
A render() 0 24 1
A getUserSiteId() 0 3 1
A getSubClientNames() 0 35 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Locale\Site;
12
13
sprintf( 'locale/site' ); // for translation
14
15
16
/**
17
 * Default implementation of locale site 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/locale/site/name
27
	 * Class name of the used locale site panel 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\Locale\Site\Standard
37
	 *
38
	 * and you want to replace it with your own version named
39
	 *
40
	 *  \Aimeos\Admin\JQAdm\Locale\Site\Myfavorite
41
	 *
42
	 * then you have to set the this configuration option:
43
	 *
44
	 *  admin/jqadm/locale/site/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 2017.10
57
	 */
58
59
60
	/**
61
	 * Adds the required data used in the template
62
	 *
63
	 * @param \Aimeos\Base\View\Iface $view View object
64
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
65
	 */
66
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
67
	{
68
		$view->themes = $this->context()->config()->get( 'client/html/themes', [] );
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( 'locale/site' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('locale/site') 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
			{
98
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
99
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
100
			}
101
102
			$this->checkSite( $view->access( 'super' ), $id );
103
104
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
105
			$view->item = $manager->get( $id );
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Aimeos\MShop\Common\Manager\Iface::get() does only seem to accept 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

105
			$view->item = $manager->get( /** @scrutinizer ignore-type */ $id );
Loading history...
106
107
			$view->itemData = $this->toArray( $view->item, true );
108
			$view->itemBody = parent::copy();
109
		}
110
		catch( \Exception $e )
111
		{
112
			$this->report( $e, 'copy' );
113
		}
114
115
		return $this->render( $view );
116
	}
117
118
119
	/**
120
	 * Creates a new resource
121
	 *
122
	 * @return string|null HTML output
123
	 */
124
	public function create() : ?string
125
	{
126
		$view = $this->object()->data( $this->view() );
127
128
		try
129
		{
130
			$this->checkSite( $view->access( 'super' ) );
131
132
			$data = $view->param( 'item', [] );
133
134
			if( !isset( $view->item ) ) {
135
				$view->item = \Aimeos\MShop::create( $this->context(), 'locale/site' )->create();
136
			}
137
138
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
139
			$view->itemBody = parent::create();
140
		}
141
		catch( \Exception $e )
142
		{
143
			$this->report( $e, 'create' );
144
		}
145
146
		return $this->render( $view );
147
	}
148
149
150
	/**
151
	 * Deletes a resource
152
	 *
153
	 * @return string|null HTML output
154
	 */
155
	public function delete() : ?string
156
	{
157
		$view = $this->view();
158
159
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
160
		$manager->begin();
161
162
		try
163
		{
164
			if( ( $ids = $view->param( 'id' ) ) === null )
165
			{
166
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
167
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
168
			}
169
170
			$search = $manager->filter()->add( 'locale.site.id', '==', $ids )->slice( 0, count( (array) $ids ) );
171
			$items = $manager->search( $search );
172
173
			foreach( $items as $id => $item )
174
			{
175
				$this->checkSite( $view->access( 'super' ), $item->getSiteId() );
176
				$view->item = $item;
177
				parent::delete();
178
			}
179
180
			$manager->delete( $items );
181
			$manager->commit();
182
183
			return $this->redirect( 'locale/site', 'search', null, 'delete' );
184
		}
185
		catch( \Exception $e )
186
		{
187
			$manager->rollback();
188
			$this->report( $e, 'delete' );
189
		}
190
191
		return $this->search();
192
	}
193
194
195
	/**
196
	 * Returns a single resource
197
	 *
198
	 * @return string|null HTML output
199
	 */
200
	public function get() : ?string
201
	{
202
		$view = $this->object()->data( $this->view() );
203
204
		try
205
		{
206
			if( ( $id = $view->param( 'id' ) ) === null )
207
			{
208
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
209
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
210
			}
211
212
			$item = \Aimeos\MShop::create( $this->context(), 'locale/site' )->get( $id );
213
214
			$this->checkSite( $view->access( 'super' ), $item->getSiteId() );
215
216
			$view->item = $item;
217
			$view->itemData = $this->toArray( $item );
218
			$view->itemBody = parent::get();
219
		}
220
		catch( \Exception $e )
221
		{
222
			$this->report( $e, 'get' );
223
		}
224
225
		return $this->render( $view );
226
	}
227
228
229
	/**
230
	 * Saves the data
231
	 *
232
	 * @return string|null HTML output
233
	 */
234
	public function save() : ?string
235
	{
236
		$view = $this->view();
237
238
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
239
		$manager->begin();
240
241
		try
242
		{
243
			$item = $this->fromArray( $view->param( 'item', [] ), $view->access( 'super' ) );
244
			$view->item = $item->getId() ? $item : $manager->save( $item );
245
			$view->itemBody = parent::save();
246
247
			$manager->save( clone $view->item );
248
			$manager->commit();
249
250
			return $this->redirect( 'locale/site', $view->param( 'next' ), $view->item->getId(), 'save', ['site' => $item->getCode()] );
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

250
			return $this->redirect( 'locale/site', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save', ['site' => $item->getCode()] );
Loading history...
251
		}
252
		catch( \Exception $e )
253
		{
254
			$manager->rollback();
255
			$this->report( $e, 'save' );
256
		}
257
258
		return $this->create();
259
	}
260
261
262
	/**
263
	 * Returns a list of resource according to the conditions
264
	 *
265
	 * @return string|null HTML output
266
	 */
267
	public function search() : ?string
268
	{
269
		$view = $this->view();
270
271
		try
272
		{
273
			$total = 0;
274
			$params = $this->storeFilter( $view->param(), 'locale/site' );
275
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
276
			$search = $this->initCriteria( $manager->filter(), $params );
277
278
			if( $view->access( 'super' ) === false )
279
			{
280
				$search->setConditions( $search->and( [
281
					$search->compare( '==', 'locale.site.id', $this->getUserSiteId() ),
282
					$search->getConditions(),
283
				] ) );
284
			}
285
286
			$view->items = $manager->search( $search, [], $total );
287
			$view->filterAttributes = $manager->getSearchAttributes( true );
288
			$view->filterOperators = $search->getOperators();
289
			$view->itemBody = parent::search();
290
			$view->total = $total;
291
		}
292
		catch( \Exception $e )
293
		{
294
			$this->report( $e, 'search' );
295
		}
296
297
		/** admin/jqadm/locale/site/template-list
298
		 * Relative path to the HTML body template for the locale list.
299
		 *
300
		 * The template file contains the HTML code and processing instructions
301
		 * to generate the result shown in the body of the frontend. The
302
		 * configuration string is the path to the template file relative
303
		 * to the templates directory (usually in templates/admin/jqadm).
304
		 *
305
		 * You can overwrite the template file configuration in extensions and
306
		 * provide alternative templates. These alternative templates should be
307
		 * named like the default one but with the string "default" replaced by
308
		 * an unique name. You may use the name of your project for this. If
309
		 * you've implemented an alternative client class as well, "default"
310
		 * should be replaced by the name of the new class.
311
		 *
312
		 * @param string Relative path to the template creating the HTML code
313
		 * @since 2016.04
314
		 */
315
		$tplconf = 'admin/jqadm/locale/site/template-list';
316
		$default = 'locale/site/list';
317
318
		return $view->render( $view->config( $tplconf, $default ) );
319
	}
320
321
322
	/**
323
	 * Returns the sub-client given by its name.
324
	 *
325
	 * @param string $type Name of the client type
326
	 * @param string|null $name Name of the sub-client (Default if null)
327
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
328
	 */
329
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
330
	{
331
		/** admin/jqadm/locale/site/decorators/excludes
332
		 * Excludes decorators added by the "common" option from the locale JQAdm client
333
		 *
334
		 * Decorators extend the functionality of a class by adding new aspects
335
		 * (e.g. log what is currently done), executing the methods of the underlying
336
		 * class only in certain conditions (e.g. only for logged in users) or
337
		 * modify what is returned to the caller.
338
		 *
339
		 * This option allows you to remove a decorator added via
340
		 * "client/jqadm/common/decorators/default" before they are wrapped
341
		 * around the JQAdm client.
342
		 *
343
		 *  admin/jqadm/locale/site/decorators/excludes = array( 'decorator1' )
344
		 *
345
		 * This would remove the decorator named "decorator1" from the list of
346
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
347
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
348
		 *
349
		 * @param array List of decorator names
350
		 * @since 2017.10
351
		 * @see admin/jqadm/common/decorators/default
352
		 * @see admin/jqadm/locale/site/decorators/global
353
		 * @see admin/jqadm/locale/site/decorators/local
354
		 */
355
356
		/** admin/jqadm/locale/site/decorators/global
357
		 * Adds a list of globally available decorators only to the locale JQAdm client
358
		 *
359
		 * Decorators extend the functionality of a class by adding new aspects
360
		 * (e.g. log what is currently done), executing the methods of the underlying
361
		 * class only in certain conditions (e.g. only for logged in users) or
362
		 * modify what is returned to the caller.
363
		 *
364
		 * This option allows you to wrap global decorators
365
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
366
		 *
367
		 *  admin/jqadm/locale/site/decorators/global = array( 'decorator1' )
368
		 *
369
		 * This would add the decorator named "decorator1" defined by
370
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
371
		 *
372
		 * @param array List of decorator names
373
		 * @since 2017.10
374
		 * @see admin/jqadm/common/decorators/default
375
		 * @see admin/jqadm/locale/site/decorators/excludes
376
		 * @see admin/jqadm/locale/site/decorators/local
377
		 */
378
379
		/** admin/jqadm/locale/site/decorators/local
380
		 * Adds a list of local decorators only to the locale JQAdm client
381
		 *
382
		 * Decorators extend the functionality of a class by adding new aspects
383
		 * (e.g. log what is currently done), executing the methods of the underlying
384
		 * class only in certain conditions (e.g. only for logged in users) or
385
		 * modify what is returned to the caller.
386
		 *
387
		 * This option allows you to wrap local decorators
388
		 * ("\Aimeos\Admin\JQAdm\Locale\Site\Decorator\*") around the JQAdm client.
389
		 *
390
		 *  admin/jqadm/locale/site/decorators/local = array( 'decorator2' )
391
		 *
392
		 * This would add the decorator named "decorator2" defined by
393
		 * "\Aimeos\Admin\JQAdm\Locale\Site\Decorator\Decorator2" only to the JQAdm client.
394
		 *
395
		 * @param array List of decorator names
396
		 * @since 2017.10
397
		 * @see admin/jqadm/common/decorators/default
398
		 * @see admin/jqadm/locale/site/decorators/excludes
399
		 * @see admin/jqadm/locale/site/decorators/global
400
		 */
401
		return $this->createSubClient( 'locale/site/' . $type, $name );
402
	}
403
404
405
	/**
406
	 * Checks if the user is allowed to access the site item
407
	 *
408
	 * @param bool $super True if user is a super user
409
	 * @param string $id ID of the site to access
410
	 * @throws \Aimeos\Admin\JQAdm\Exception If user isn't allowed to access the site
411
	 */
412
	protected function checkSite( bool $super, ?string $id = null )
413
	{
414
		if( $super === true || $id === null || (string) $this->getUserSiteId() === (string) $id ) {
415
			return;
416
		}
417
418
		$msg = $this->context()->translate( 'admin', 'Permission denied' );
419
		throw new \Aimeos\Admin\JQAdm\Exception( $msg );
420
	}
421
422
423
	/**
424
	 * Returns the site ID of the current user
425
	 *
426
	 * @return string|null Site ID of the current user
427
	 */
428
	protected function getUserSiteId() : ?string
429
	{
430
		return $this->context()->user()?->getSiteId();
431
	}
432
433
434
	/**
435
	 * Returns the list of sub-client names configured for the client.
436
	 *
437
	 * @return array List of JQAdm client names
438
	 */
439
	protected function getSubClientNames() : array
440
	{
441
		/** admin/jqadm/locale/site/subparts
442
		 * List of JQAdm sub-clients rendered within the locale section
443
		 *
444
		 * The output of the frontend is composed of the code generated by the JQAdm
445
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
446
		 * that are responsible for rendering certain sub-parts of the output. The
447
		 * sub-clients can contain JQAdm clients themselves and therefore a
448
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
449
		 * the output that is placed inside the container of its parent.
450
		 *
451
		 * At first, always the JQAdm code generated by the parent is printed, then
452
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
453
		 * determines the order of the output of these sub-clients inside the parent
454
		 * container. If the configured list of clients is
455
		 *
456
		 *  array( "subclient1", "subclient2" )
457
		 *
458
		 * you can easily change the order of the output by reordering the subparts:
459
		 *
460
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
461
		 *
462
		 * You can also remove one or more parts if they shouldn't be rendered:
463
		 *
464
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
465
		 *
466
		 * As the clients only generates structural JQAdm, the layout defined via CSS
467
		 * should support adding, removing or reordering content by a fluid like
468
		 * design.
469
		 *
470
		 * @param array List of sub-client names
471
		 * @since 2017.10
472
		 */
473
		return $this->context()->config()->get( 'admin/jqadm/locale/site/subparts', [] );
474
	}
475
476
477
	/**
478
	 * Creates new and updates existing items using the data array
479
	 *
480
	 * @param array $data Data array
481
	 * @param bool $super If current user is a super user
482
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New locale site item object
483
	 */
484
	protected function fromArray( array $data, bool $super ) : \Aimeos\MShop\Locale\Item\Site\Iface
485
	{
486
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
487
488
		if( !empty( $data['locale.site.id'] ) )
489
		{
490
			$this->checkSite( $super, $data['locale.site.id'] );
491
			$item = $manager->get( $data['locale.site.id'] );
0 ignored issues
show
Bug introduced by
It seems like $data['locale.site.id'] can also be of type null; however, parameter $id of Aimeos\MShop\Common\Manager\Iface::get() does only seem to accept 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

491
			$item = $manager->get( /** @scrutinizer ignore-type */ $data['locale.site.id'] );
Loading history...
492
		}
493
		else
494
		{
495
			$this->checkSite( $super );
496
			$item = $manager->create();
497
		}
498
499
		$item->fromArray( $data, true )->setConfig( [] );
500
501
		foreach( (array) $this->val( $data, 'config', [] ) as $cfg )
502
		{
503
			if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
504
				$item->setConfigValue( $key, json_decode( $val, true ) ?? $val );
505
			}
506
		}
507
508
		if( empty( $item->getId() ) ) {
509
			return $manager->insert( $item );
510
		}
511
512
		return $item;
513
	}
514
515
516
	/**
517
	 * Constructs the data array for the view from the given item
518
	 *
519
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $item Locale site item object
520
	 * @return string[] Multi-dimensional associative list of item data
521
	 */
522
	protected function toArray( \Aimeos\MShop\Locale\Item\Site\Iface $item, bool $copy = false ) : array
523
	{
524
		$data = $item->toArray( true );
525
		$data['config'] = $this->flatten( $item->getConfig() );
526
527
		if( $copy === true )
528
		{
529
			$data['locale.site.code'] = $data['locale.site.code'] . '_' . substr( md5( microtime( true ) ), -5 );
530
			$data['locale.site.id'] = '';
531
		}
532
533
		return $data;
534
	}
535
536
537
	/**
538
	 * Returns the rendered template including the view data
539
	 *
540
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
541
	 * @return string HTML output
542
	 */
543
	protected function render( \Aimeos\Base\View\Iface $view ) : string
544
	{
545
		/** admin/jqadm/locale/site/template-item
546
		 * Relative path to the HTML body template for the locale item.
547
		 *
548
		 * The template file contains the HTML code and processing instructions
549
		 * to generate the result shown in the body of the frontend. The
550
		 * configuration string is the path to the template file relative
551
		 * to the templates directory (usually in templates/admin/jqadm).
552
		 *
553
		 * You can overwrite the template file configuration in extensions and
554
		 * provide alternative templates. These alternative templates should be
555
		 * named like the default one but with the string "default" replaced by
556
		 * an unique name. You may use the name of your project for this. If
557
		 * you've implemented an alternative client class as well, "default"
558
		 * should be replaced by the name of the new class.
559
		 *
560
		 * @param string Relative path to the template creating the HTML code
561
		 * @since 2017.10
562
		 */
563
		$tplconf = 'admin/jqadm/locale/site/template-item';
564
		$default = 'locale/site/item';
565
566
		return $view->render( $view->config( $tplconf, $default ) );
567
	}
568
}
569