Passed
Push — master ( aac277...9774ed )
by Aimeos
09:25 queued 05:17
created

Standard::flatten()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
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
	/**
27
	 * Adds the required data used in the template
28
	 *
29
	 * @param \Aimeos\MW\View\Iface $view View object
30
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
31
	 */
32
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
33
	{
34
		$view->itemSubparts = $this->getSubClientNames();
35
		return $view;
36
	}
37
38
39
	/**
40
	 * Copies a resource
41
	 *
42
	 * @return string|null HTML output
43
	 */
44
	public function copy() : ?string
45
	{
46
		$view = $this->getObject()->addData( $this->getView() );
47
48
		try
49
		{
50
			if( ( $id = $view->param( 'id' ) ) === null )
51
			{
52
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
53
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
54
			}
55
56
			$this->checkSite( $view->access( 'super' ), $id );
57
58
			$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/site' );
59
			$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

59
			$view->item = $manager->get( /** @scrutinizer ignore-type */ $id );
Loading history...
60
61
			$view->itemData = $this->toArray( $view->item, true );
62
			$view->itemBody = parent::copy();
63
		}
64
		catch( \Exception $e )
65
		{
66
			$this->report( $e, 'copy' );
67
		}
68
69
		return $this->render( $view );
70
	}
71
72
73
	/**
74
	 * Creates a new resource
75
	 *
76
	 * @return string|null HTML output
77
	 */
78
	public function create() : ?string
79
	{
80
		$view = $this->getObject()->addData( $this->getView() );
81
82
		try
83
		{
84
			$this->checkSite( $view->access( 'super' ) );
85
86
			$data = $view->param( 'item', [] );
87
88
			if( !isset( $view->item ) ) {
89
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'locale/site' )->create();
90
			}
91
92
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
93
			$view->itemBody = parent::create();
94
		}
95
		catch( \Exception $e )
96
		{
97
			$this->report( $e, 'create' );
98
		}
99
100
		return $this->render( $view );
101
	}
102
103
104
	/**
105
	 * Deletes a resource
106
	 *
107
	 * @return string|null HTML output
108
	 */
109
	public function delete() : ?string
110
	{
111
		$view = $this->getView();
112
113
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/site' );
114
		$manager->begin();
115
116
		try
117
		{
118
			if( ( $ids = $view->param( 'id' ) ) === null )
119
			{
120
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
121
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
122
			}
123
124
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
125
			$search->setConditions( $search->compare( '==', 'locale.site.id', $ids ) );
126
			$items = $manager->search( $search );
127
128
			foreach( $items as $id => $item )
129
			{
130
				$this->checkSite( $view->access( 'super' ), $item->getSiteId() );
131
				$view->item = $item;
132
				parent::delete();
133
			}
134
135
			$manager->delete( $items->toArray() );
136
			$manager->commit();
137
138
			return $this->redirect( 'locale/site', 'search', null, 'delete' );
139
		}
140
		catch( \Exception $e )
141
		{
142
			$manager->rollback();
143
			$this->report( $e, 'delete' );
144
		}
145
146
		return $this->search();
147
	}
148
149
150
	/**
151
	 * Returns a single resource
152
	 *
153
	 * @return string|null HTML output
154
	 */
155
	public function get() : ?string
156
	{
157
		$view = $this->getObject()->addData( $this->getView() );
158
159
		try
160
		{
161
			if( ( $id = $view->param( 'id' ) ) === null )
162
			{
163
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
164
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
165
			}
166
167
			$item = \Aimeos\MShop::create( $this->getContext(), 'locale/site' )->get( $id );
168
169
			$this->checkSite( $view->access( 'super' ), $item->getSiteId() );
170
171
			$view->item = $item;
172
			$view->itemData = $this->toArray( $item );
173
			$view->itemBody = parent::get();
174
		}
175
		catch( \Exception $e )
176
		{
177
			$this->report( $e, 'get' );
178
		}
179
180
		return $this->render( $view );
181
	}
182
183
184
	/**
185
	 * Saves the data
186
	 *
187
	 * @return string|null HTML output
188
	 */
189
	public function save() : ?string
190
	{
191
		$view = $this->getView();
192
193
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/site' );
194
		$manager->begin();
195
196
		try
197
		{
198
			$item = $this->fromArray( $view->param( 'item', [] ), $view->access( 'super' ) );
199
			$view->item = $item->getId() ? $item : $manager->save( $item );
200
			$view->itemBody = parent::save();
201
202
			$manager->save( clone $view->item );
203
			$manager->commit();
204
205
			return $this->redirect( 'locale/site', $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

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

392
		return $manager->get( /** @scrutinizer ignore-type */ $context->getUserId() )->getSiteId();
Loading history...
393
	}
394
395
396
	/**
397
	 * Returns the list of sub-client names configured for the client.
398
	 *
399
	 * @return array List of JQAdm client names
400
	 */
401
	protected function getSubClientNames() : array
402
	{
403
		/** admin/jqadm/locale/site/subparts
404
		 * List of JQAdm sub-clients rendered within the locale section
405
		 *
406
		 * The output of the frontend is composed of the code generated by the JQAdm
407
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
408
		 * that are responsible for rendering certain sub-parts of the output. The
409
		 * sub-clients can contain JQAdm clients themselves and therefore a
410
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
411
		 * the output that is placed inside the container of its parent.
412
		 *
413
		 * At first, always the JQAdm code generated by the parent is printed, then
414
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
415
		 * determines the order of the output of these sub-clients inside the parent
416
		 * container. If the configured list of clients is
417
		 *
418
		 *  array( "subclient1", "subclient2" )
419
		 *
420
		 * you can easily change the order of the output by reordering the subparts:
421
		 *
422
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
423
		 *
424
		 * You can also remove one or more parts if they shouldn't be rendered:
425
		 *
426
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
427
		 *
428
		 * As the clients only generates structural JQAdm, the layout defined via CSS
429
		 * should support adding, removing or reordering content by a fluid like
430
		 * design.
431
		 *
432
		 * @param array List of sub-client names
433
		 * @since 2017.10
434
		 * @category Developer
435
		 */
436
		return $this->getContext()->getConfig()->get( 'admin/jqadm/locale/site/subparts', [] );
437
	}
438
439
440
	/**
441
	 * Creates new and updates existing items using the data array
442
	 *
443
	 * @param array $data Data array
444
	 * @param bool $super If current user is a super user
445
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New locale site item object
446
	 */
447
	protected function fromArray( array $data, bool $super ) : \Aimeos\MShop\Locale\Item\Site\Iface
448
	{
449
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/site' );
450
451
		if( !empty( $data['locale.site.id'] ) )
452
		{
453
			$this->checkSite( $super, $data['locale.site.id'] );
454
			$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

454
			$item = $manager->get( /** @scrutinizer ignore-type */ $data['locale.site.id'] );
Loading history...
455
		}
456
		else
457
		{
458
			$this->checkSite( $super );
459
			$item = $manager->create();
460
		}
461
462
		$conf = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
463
		$item->fromArray( $data, true )->setConfig( [] );
464
465
		foreach( (array) $this->getValue( $data, 'config', [] ) as $entry )
466
		{
467
			if( ( $key = trim( $entry['key'] ?? '' ) ) !== '' ) {
468
				$item->setConfigValue( $key, trim( $entry['val'] ?? '' ) );
469
			}
470
		}
471
472
		if( $item->getId() == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $item->getId() of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
473
			return $manager->insert( $item );
474
		}
475
476
		return $item;
477
	}
478
479
480
	/**
481
	 * Constructs the data array for the view from the given item
482
	 *
483
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $item Locale site item object
484
	 * @return string[] Multi-dimensional associative list of item data
485
	 */
486
	protected function toArray( \Aimeos\MShop\Locale\Item\Site\Iface $item, bool $copy = false ) : array
487
	{
488
		$data = $item->toArray( true );
489
		$data['config'] = $this->flatten( $item->getConfig() );
490
491
		if( $copy === true )
492
		{
493
			$data['locale.site.code'] = $data['locale.site.code'] . '_' . substr( md5( microtime( true ) ), -5 );
494
			$data['locale.site.id'] = '';
495
		}
496
497
		return $data;
498
	}
499
500
501
	/**
502
	 * Returns the rendered template including the view data
503
	 *
504
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
505
	 * @return string HTML output
506
	 */
507
	protected function render( \Aimeos\MW\View\Iface $view ) : string
508
	{
509
		/** admin/jqadm/locale/site/template-item
510
		 * Relative path to the HTML body template for the locale item.
511
		 *
512
		 * The template file contains the HTML code and processing instructions
513
		 * to generate the result shown in the body of the frontend. The
514
		 * configuration string is the path to the template file relative
515
		 * to the templates directory (usually in admin/jqadm/templates).
516
		 *
517
		 * You can overwrite the template file configuration in extensions and
518
		 * provide alternative templates. These alternative templates should be
519
		 * named like the default one but with the string "default" replaced by
520
		 * an unique name. You may use the name of your project for this. If
521
		 * you've implemented an alternative client class as well, "default"
522
		 * should be replaced by the name of the new class.
523
		 *
524
		 * @param string Relative path to the template creating the HTML code
525
		 * @since 2017.10
526
		 * @category Developer
527
		 */
528
		$tplconf = 'admin/jqadm/locale/site/template-item';
529
		$default = 'locale/site/item-standard';
530
531
		return $view->render( $view->config( $tplconf, $default ) );
532
	}
533
}
534