Passed
Push — master ( 4d137d...d931d2 )
by Aimeos
03:47
created

Standard   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 555
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 130
dl 0
loc 555
rs 9.52
c 0
b 0
f 0
wmc 36

15 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 4 1
A batch() 0 3 1
A fromArray() 0 29 5
A checkSite() 0 8 4
A toArray() 0 12 2
A render() 0 25 1
A get() 0 26 3
A search() 0 53 3
A getUserSiteId() 0 6 1
A getSubClient() 0 76 1
A getSubClientNames() 0 36 1
A create() 0 23 3
A copy() 0 26 3
A delete() 0 38 4
A save() 0 25 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
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 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\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
	 * @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( '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()->slice( 0, count( (array) $ids ) );
171
			$search->setConditions( $search->compare( '==', 'locale.site.id', $ids ) );
172
			$items = $manager->search( $search );
173
174
			foreach( $items as $id => $item )
175
			{
176
				$this->checkSite( $view->access( 'super' ), $item->getSiteId() );
177
				$view->item = $item;
178
				parent::delete();
179
			}
180
181
			$manager->delete( $items->toArray() );
182
			$manager->commit();
183
184
			return $this->redirect( 'locale/site', 'search', null, 'delete' );
185
		}
186
		catch( \Exception $e )
187
		{
188
			$manager->rollback();
189
			$this->report( $e, 'delete' );
190
		}
191
192
		return $this->search();
193
	}
194
195
196
	/**
197
	 * Returns a single resource
198
	 *
199
	 * @return string|null HTML output
200
	 */
201
	public function get() : ?string
202
	{
203
		$view = $this->object()->data( $this->view() );
204
205
		try
206
		{
207
			if( ( $id = $view->param( 'id' ) ) === null )
208
			{
209
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
210
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
211
			}
212
213
			$item = \Aimeos\MShop::create( $this->context(), 'locale/site' )->get( $id );
214
215
			$this->checkSite( $view->access( 'super' ), $item->getSiteId() );
216
217
			$view->item = $item;
218
			$view->itemData = $this->toArray( $item );
219
			$view->itemBody = parent::get();
220
		}
221
		catch( \Exception $e )
222
		{
223
			$this->report( $e, 'get' );
224
		}
225
226
		return $this->render( $view );
227
	}
228
229
230
	/**
231
	 * Saves the data
232
	 *
233
	 * @return string|null HTML output
234
	 */
235
	public function save() : ?string
236
	{
237
		$view = $this->view();
238
239
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
240
		$manager->begin();
241
242
		try
243
		{
244
			$item = $this->fromArray( $view->param( 'item', [] ), $view->access( 'super' ) );
245
			$view->item = $item->getId() ? $item : $manager->save( $item );
246
			$view->itemBody = parent::save();
247
248
			$manager->save( clone $view->item );
249
			$manager->commit();
250
251
			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

251
			return $this->redirect( 'locale/site', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save', ['site' => $item->getCode()] );
Loading history...
252
		}
253
		catch( \Exception $e )
254
		{
255
			$manager->rollback();
256
			$this->report( $e, 'save' );
257
		}
258
259
		return $this->create();
260
	}
261
262
263
	/**
264
	 * Returns a list of resource according to the conditions
265
	 *
266
	 * @return string|null HTML output
267
	 */
268
	public function search() : ?string
269
	{
270
		$view = $this->view();
271
272
		try
273
		{
274
			$total = 0;
275
			$params = $this->storeFilter( $view->param(), 'locale/site' );
276
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
277
			$search = $this->initCriteria( $manager->filter(), $params );
278
279
			if( $view->access( 'super' ) === false )
280
			{
281
				$search->setConditions( $search->and( [
282
					$search->compare( '==', 'locale.site.id', $this->getUserSiteId() ),
283
					$search->getConditions(),
284
				] ) );
285
			}
286
287
			$view->items = $manager->search( $search, [], $total );
288
			$view->filterAttributes = $manager->getSearchAttributes( true );
289
			$view->filterOperators = $search->getOperators();
290
			$view->itemBody = parent::search();
291
			$view->total = $total;
292
		}
293
		catch( \Exception $e )
294
		{
295
			$this->report( $e, 'search' );
296
		}
297
298
		/** admin/jqadm/locale/site/template-list
299
		 * Relative path to the HTML body template for the locale list.
300
		 *
301
		 * The template file contains the HTML code and processing instructions
302
		 * to generate the result shown in the body of the frontend. The
303
		 * configuration string is the path to the template file relative
304
		 * to the templates directory (usually in admin/jqadm/templates).
305
		 *
306
		 * You can overwrite the template file configuration in extensions and
307
		 * provide alternative templates. These alternative templates should be
308
		 * named like the default one but with the string "default" replaced by
309
		 * an unique name. You may use the name of your project for this. If
310
		 * you've implemented an alternative client class as well, "default"
311
		 * should be replaced by the name of the new class.
312
		 *
313
		 * @param string Relative path to the template creating the HTML code
314
		 * @since 2016.04
315
		 * @category Developer
316
		 */
317
		$tplconf = 'admin/jqadm/locale/site/template-list';
318
		$default = 'locale/site/list';
319
320
		return $view->render( $view->config( $tplconf, $default ) );
321
	}
322
323
324
	/**
325
	 * Returns the sub-client given by its name.
326
	 *
327
	 * @param string $type Name of the client type
328
	 * @param string|null $name Name of the sub-client (Default if null)
329
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
330
	 */
331
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
332
	{
333
		/** admin/jqadm/locale/site/decorators/excludes
334
		 * Excludes decorators added by the "common" option from the locale JQAdm client
335
		 *
336
		 * Decorators extend the functionality of a class by adding new aspects
337
		 * (e.g. log what is currently done), executing the methods of the underlying
338
		 * class only in certain conditions (e.g. only for logged in users) or
339
		 * modify what is returned to the caller.
340
		 *
341
		 * This option allows you to remove a decorator added via
342
		 * "client/jqadm/common/decorators/default" before they are wrapped
343
		 * around the JQAdm client.
344
		 *
345
		 *  admin/jqadm/locale/site/decorators/excludes = array( 'decorator1' )
346
		 *
347
		 * This would remove the decorator named "decorator1" from the list of
348
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
349
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
350
		 *
351
		 * @param array List of decorator names
352
		 * @since 2017.10
353
		 * @category Developer
354
		 * @see admin/jqadm/common/decorators/default
355
		 * @see admin/jqadm/locale/site/decorators/global
356
		 * @see admin/jqadm/locale/site/decorators/local
357
		 */
358
359
		/** admin/jqadm/locale/site/decorators/global
360
		 * Adds a list of globally available decorators only to the locale JQAdm client
361
		 *
362
		 * Decorators extend the functionality of a class by adding new aspects
363
		 * (e.g. log what is currently done), executing the methods of the underlying
364
		 * class only in certain conditions (e.g. only for logged in users) or
365
		 * modify what is returned to the caller.
366
		 *
367
		 * This option allows you to wrap global decorators
368
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
369
		 *
370
		 *  admin/jqadm/locale/site/decorators/global = array( 'decorator1' )
371
		 *
372
		 * This would add the decorator named "decorator1" defined by
373
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
374
		 *
375
		 * @param array List of decorator names
376
		 * @since 2017.10
377
		 * @category Developer
378
		 * @see admin/jqadm/common/decorators/default
379
		 * @see admin/jqadm/locale/site/decorators/excludes
380
		 * @see admin/jqadm/locale/site/decorators/local
381
		 */
382
383
		/** admin/jqadm/locale/site/decorators/local
384
		 * Adds a list of local decorators only to the locale JQAdm client
385
		 *
386
		 * Decorators extend the functionality of a class by adding new aspects
387
		 * (e.g. log what is currently done), executing the methods of the underlying
388
		 * class only in certain conditions (e.g. only for logged in users) or
389
		 * modify what is returned to the caller.
390
		 *
391
		 * This option allows you to wrap local decorators
392
		 * ("\Aimeos\Admin\JQAdm\Locale\Site\Decorator\*") around the JQAdm client.
393
		 *
394
		 *  admin/jqadm/locale/site/decorators/local = array( 'decorator2' )
395
		 *
396
		 * This would add the decorator named "decorator2" defined by
397
		 * "\Aimeos\Admin\JQAdm\Locale\Site\Decorator\Decorator2" only to the JQAdm client.
398
		 *
399
		 * @param array List of decorator names
400
		 * @since 2017.10
401
		 * @category Developer
402
		 * @see admin/jqadm/common/decorators/default
403
		 * @see admin/jqadm/locale/site/decorators/excludes
404
		 * @see admin/jqadm/locale/site/decorators/global
405
		 */
406
		return $this->createSubClient( 'locale/site' . $type, $name );
407
	}
408
409
410
	/**
411
	 * Checks if the user is allowed to access the site item
412
	 *
413
	 * @param bool $super True if user is a super user
414
	 * @param string $id ID of the site to access
415
	 * @throws \Aimeos\Admin\JQAdm\Exception If user isn't allowed to access the site
416
	 */
417
	protected function checkSite( bool $super, string $id = null )
418
	{
419
		if( $super === true || $id === null || (string) $this->getUserSiteId() === (string) $id ) {
420
			return;
421
		}
422
423
		$msg = $this->context()->translate( 'admin', 'Permission denied' );
424
		throw new \Aimeos\Admin\JQAdm\Exception( $msg );
425
	}
426
427
428
	/**
429
	 * Returns the site ID of the current user
430
	 *
431
	 * @return string|null Site ID of the current user
432
	 */
433
	protected function getUserSiteId() : ?string
434
	{
435
		$context = $this->context();
436
		$manager = \Aimeos\MShop::create( $context, 'customer' );
437
438
		return $manager->get( $context->user() )->getSiteId();
0 ignored issues
show
Bug introduced by
It seems like $context->user() 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

438
		return $manager->get( /** @scrutinizer ignore-type */ $context->user() )->getSiteId();
Loading history...
439
	}
440
441
442
	/**
443
	 * Returns the list of sub-client names configured for the client.
444
	 *
445
	 * @return array List of JQAdm client names
446
	 */
447
	protected function getSubClientNames() : array
448
	{
449
		/** admin/jqadm/locale/site/subparts
450
		 * List of JQAdm sub-clients rendered within the locale section
451
		 *
452
		 * The output of the frontend is composed of the code generated by the JQAdm
453
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
454
		 * that are responsible for rendering certain sub-parts of the output. The
455
		 * sub-clients can contain JQAdm clients themselves and therefore a
456
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
457
		 * the output that is placed inside the container of its parent.
458
		 *
459
		 * At first, always the JQAdm code generated by the parent is printed, then
460
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
461
		 * determines the order of the output of these sub-clients inside the parent
462
		 * container. If the configured list of clients is
463
		 *
464
		 *  array( "subclient1", "subclient2" )
465
		 *
466
		 * you can easily change the order of the output by reordering the subparts:
467
		 *
468
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
469
		 *
470
		 * You can also remove one or more parts if they shouldn't be rendered:
471
		 *
472
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
473
		 *
474
		 * As the clients only generates structural JQAdm, the layout defined via CSS
475
		 * should support adding, removing or reordering content by a fluid like
476
		 * design.
477
		 *
478
		 * @param array List of sub-client names
479
		 * @since 2017.10
480
		 * @category Developer
481
		 */
482
		return $this->context()->config()->get( 'admin/jqadm/locale/site/subparts', [] );
483
	}
484
485
486
	/**
487
	 * Creates new and updates existing items using the data array
488
	 *
489
	 * @param array $data Data array
490
	 * @param bool $super If current user is a super user
491
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New locale site item object
492
	 */
493
	protected function fromArray( array $data, bool $super ) : \Aimeos\MShop\Locale\Item\Site\Iface
494
	{
495
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
496
497
		if( !empty( $data['locale.site.id'] ) )
498
		{
499
			$this->checkSite( $super, $data['locale.site.id'] );
500
			$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

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