Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 469
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 128
dl 0
loc 469
rs 9.84
c 0
b 0
f 0
wmc 32

11 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 30 4
A toArray() 0 11 2
A getSubClientNames() 0 36 1
A get() 0 30 4
A create() 0 29 4
A render() 0 25 1
A delete() 0 40 5
A search() 0 53 3
A getSubClient() 0 76 1
A fromArray() 0 13 3
A save() 0 31 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
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
	/**
27
	 * Copies a resource
28
	 *
29
	 * @return string|null HTML output
30
	 */
31
	public function copy() : ?string
32
	{
33
		$view = $this->getView();
34
		$context = $this->getContext();
35
36
		try
37
		{
38
			if( ( $id = $view->param( 'id' ) ) === null ) {
39
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
40
			}
41
42
			$manager = \Aimeos\MShop::create( $context, 'locale/language' );
43
			$view->item = $manager->getItem( $id );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemBody = '';
48
49
			foreach( $this->getSubClients() as $idx => $client )
50
			{
51
				$view->tabindex = ++$idx + 1;
52
				$view->itemBody .= $client->copy();
53
			}
54
		}
55
		catch( \Exception $e )
56
		{
57
			$this->report( $e, 'copy' );
58
		}
59
60
		return $this->render( $view );
61
	}
62
63
64
	/**
65
	 * Creates a new resource
66
	 *
67
	 * @return string|null HTML output
68
	 */
69
	public function create() : ?string
70
	{
71
		$view = $this->getView();
72
		$context = $this->getContext();
73
74
		try
75
		{
76
			$data = $view->param( 'item', [] );
77
78
			if( !isset( $view->item ) ) {
79
				$view->item = \Aimeos\MShop::create( $context, 'locale/language' )->createItem();
80
			}
81
82
			$view->itemSubparts = $this->getSubClientNames();
83
			$view->itemData = $data;
84
			$view->itemBody = '';
85
86
			foreach( $this->getSubClients() as $idx => $client )
87
			{
88
				$view->tabindex = ++$idx + 1;
89
				$view->itemBody .= $client->create();
90
			}
91
		}
92
		catch( \Exception $e )
93
		{
94
			$this->report( $e, 'create' );
95
		}
96
97
		return $this->render( $view );
98
	}
99
100
101
	/**
102
	 * Deletes a resource
103
	 *
104
	 * @return string|null HTML output
105
	 */
106
	public function delete() : ?string
107
	{
108
		$view = $this->getView();
109
		$context = $this->getContext();
110
111
		$manager = \Aimeos\MShop::create( $context, 'locale/language' );
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->createSearch()->setSlice( 0, count( (array) $ids ) );
121
			$search->setConditions( $search->compare( '==', 'locale.language.id', $ids ) );
122
			$items = $manager->searchItems( $search );
123
124
			foreach( $items as $item )
125
			{
126
				$view->item = $item;
127
128
				foreach( $this->getSubClients() as $client ) {
129
					$client->delete();
130
				}
131
			}
132
133
			$manager->deleteItems( $items->toArray() );
134
			$manager->commit();
135
136
			$this->nextAction( $view, 'search', 'locale/language', null, 'delete' );
137
			return null;
138
		}
139
		catch( \Exception $e )
140
		{
141
			$manager->rollback();
142
			$this->report( $e, 'delete' );
143
		}
144
145
		return $this->search();
146
	}
147
148
149
	/**
150
	 * Returns a single resource
151
	 *
152
	 * @return string|null HTML output
153
	 */
154
	public function get() : ?string
155
	{
156
		$view = $this->getView();
157
		$context = $this->getContext();
158
159
		try
160
		{
161
			if( ( $id = $view->param( 'id' ) ) === null ) {
162
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
163
			}
164
165
			$manager = \Aimeos\MShop::create( $context, 'locale/language' );
166
167
			$view->item = $manager->getItem( $id );
168
			$view->itemSubparts = $this->getSubClientNames();
169
			$view->itemData = $this->toArray( $view->item );
170
			$view->itemBody = '';
171
172
			foreach( $this->getSubClients() as $idx => $client )
173
			{
174
				$view->tabindex = ++$idx + 1;
175
				$view->itemBody .= $client->get();
176
			}
177
		}
178
		catch( \Exception $e )
179
		{
180
			$this->report( $e, 'get' );
181
		}
182
183
		return $this->render( $view );
184
	}
185
186
187
	/**
188
	 * Saves the data
189
	 *
190
	 * @return string|null HTML output
191
	 */
192
	public function save() : ?string
193
	{
194
		$view = $this->getView();
195
		$context = $this->getContext();
196
197
		$manager = \Aimeos\MShop::create( $context, 'locale/language' );
198
		$manager->begin();
199
200
		try
201
		{
202
			$item = $this->fromArray( $view->param( 'item', [] ) );
203
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
			$view->item = $item->getId() ? $item : $manager->/** @scrutinizer ignore-call */ saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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