Passed
Push — master ( 1d53d5...4d137d )
by Aimeos
03:38
created

Standard   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 494
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 109
c 0
b 0
f 0
dl 0
loc 494
rs 10
wmc 28

13 Methods

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

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