Passed
Push — master ( 596853...253ba3 )
by Aimeos
16:40
created

Standard::fromArray()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 12
nop 2
dl 0
loc 32
rs 9.1111
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), 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
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
52
			}
53
54
			$this->checkSite( $view->access( 'super' ), $id );
55
56
			$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/site' );
57
			$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

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

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

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

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