Standard   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 495
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 114
dl 0
loc 495
rs 10
c 0
b 0
f 0
wmc 28

13 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 14 3
A toArray() 0 11 2
A getSubClientNames() 0 35 1
A batch() 0 3 1
A copy() 0 24 3
A getSubClient() 0 73 1
A get() 0 24 3
A save() 0 25 3
A delete() 0 36 4
A render() 0 24 1
A create() 0 23 3
A search() 0 44 2
A data() 0 13 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;
12
13
sprintf( 'locale' ); // for translation
14
15
16
/**
17
 * Default implementation of locale 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/name
27
	 * Class name of the used locale 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\Standard
37
	 *
38
	 * and you want to replace it with your own version named
39
	 *
40
	 *  \Aimeos\Admin\JQAdm\Locale\Myfavorite
41
	 *
42
	 * then you have to set the this configuration option:
43
	 *
44
	 *  admin/jqadm/locale/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
		$curmanager = \Aimeos\MShop::create( $this->context(), 'locale/currency' );
69
		$langmanager = \Aimeos\MShop::create( $this->context(), 'locale/language' );
70
71
		$cursearch = $curmanager->filter( true )->order( 'locale.currency.id' )->slice( 0, 250 );
72
		$langsearch = $langmanager->filter( true )->order( ['locale.language.id'] )->slice( 0, 250 );
73
74
		$view->itemLanguages = $langmanager->search( $langsearch );
75
		$view->itemCurrencies = $curmanager->search( $cursearch );
76
		$view->itemSubparts = $this->getSubClientNames();
77
78
		return $view;
79
	}
80
81
82
	/**
83
	 * Batch update of a resource
84
	 *
85
	 * @return string|null Output to display
86
	 */
87
	public function batch() : ?string
88
	{
89
		return $this->batchBase( 'locale' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('locale') 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...
90
	}
91
92
93
	/**
94
	 * Copies a resource
95
	 *
96
	 * @return string|null HTML output
97
	 */
98
	public function copy() : ?string
99
	{
100
		$view = $this->object()->data( $this->view() );
101
102
		try
103
		{
104
			if( ( $id = $view->param( 'id' ) ) === null )
105
			{
106
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
107
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
108
			}
109
110
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
111
			$view->item = $manager->get( $id );
112
113
			$view->itemData = $this->toArray( $view->item, true );
114
			$view->itemBody = parent::copy();
115
		}
116
		catch( \Exception $e )
117
		{
118
			$this->report( $e, 'copy' );
119
		}
120
121
		return $this->render( $view );
122
	}
123
124
125
	/**
126
	 * Creates a new resource
127
	 *
128
	 * @return string|null HTML output
129
	 */
130
	public function create() : ?string
131
	{
132
		$view = $this->object()->data( $this->view() );
133
134
		try
135
		{
136
			$data = $view->param( 'item', [] );
137
138
			if( !isset( $view->item ) ) {
139
				$view->item = \Aimeos\MShop::create( $this->context(), 'locale' )->create();
140
			}
141
142
			$data['locale.siteid'] = $view->item->getSiteId();
143
144
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
145
			$view->itemBody = parent::create();
146
		}
147
		catch( \Exception $e )
148
		{
149
			$this->report( $e, 'create' );
150
		}
151
152
		return $this->render( $view );
153
	}
154
155
156
	/**
157
	 * Deletes a resource
158
	 *
159
	 * @return string|null HTML output
160
	 */
161
	public function delete() : ?string
162
	{
163
		$view = $this->view();
164
165
		$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
166
		$manager->begin();
167
168
		try
169
		{
170
			if( ( $ids = $view->param( 'id' ) ) === null )
171
			{
172
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
173
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
174
			}
175
176
			$search = $manager->filter()->add( 'locale.id', '==', $ids )->slice( 0, count( (array) $ids ) );
177
			$items = $manager->search( $search );
178
179
			foreach( $items as $item )
180
			{
181
				$view->item = $item;
182
				parent::delete();
183
			}
184
185
			$manager->delete( $items );
186
			$manager->commit();
187
188
			return $this->redirect( 'locale', 'search', null, 'delete' );
189
		}
190
		catch( \Exception $e )
191
		{
192
			$manager->rollback();
193
			$this->report( $e, 'delete' );
194
		}
195
196
		return $this->search();
197
	}
198
199
200
	/**
201
	 * Returns a single resource
202
	 *
203
	 * @return string|null HTML output
204
	 */
205
	public function get() : ?string
206
	{
207
		$view = $this->object()->data( $this->view() );
208
209
		try
210
		{
211
			if( ( $id = $view->param( 'id' ) ) === null )
212
			{
213
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
214
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
215
			}
216
217
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
218
219
			$view->item = $manager->get( $id );
220
			$view->itemData = $this->toArray( $view->item );
221
			$view->itemBody = parent::get();
222
		}
223
		catch( \Exception $e )
224
		{
225
			$this->report( $e, 'get' );
226
		}
227
228
		return $this->render( $view );
229
	}
230
231
232
	/**
233
	 * Saves the data
234
	 *
235
	 * @return string|null HTML output
236
	 */
237
	public function save() : ?string
238
	{
239
		$view = $this->view();
240
241
		$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
242
		$manager->begin();
243
244
		try
245
		{
246
			$item = $this->fromArray( $view->param( 'item', [] ) );
247
			$view->item = $item->getId() ? $item : $manager->save( $item );
248
			$view->itemBody = parent::save();
249
250
			$manager->save( $view->item );
251
			$manager->commit();
252
253
			return $this->redirect( 'locale', $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

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