Passed
Push — master ( 85cd01...c58492 )
by Aimeos
03:18
created

Standard::fromArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

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