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

Standard::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 18
rs 9.8666
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;
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
	/**
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
		$curmanager = \Aimeos\MShop::create( $this->getContext(), 'locale/currency' );
35
		$langmanager = \Aimeos\MShop::create( $this->getContext(), 'locale/language' );
36
37
		$cursearch = $curmanager->filter( true );
38
		$cursearch->setSortations( [$cursearch->sort( '+', 'locale.currency.id' )] );
39
		$cursearch->slice( 0, 250 );
40
41
		$langsearch = $langmanager->filter( true );
42
		$langsearch->setSortations( [$langsearch->sort( '+', 'locale.language.id' )] );
43
		$langsearch->slice( 0, 250 );
44
45
		$view->itemLanguages = $langmanager->search( $langsearch );
46
		$view->itemCurrencies = $curmanager->search( $cursearch );
47
		$view->itemSubparts = $this->getSubClientNames();
48
49
		return $view;
50
	}
51
52
53
	/**
54
	 * Copies a resource
55
	 *
56
	 * @return string|null HTML output
57
	 */
58
	public function copy() : ?string
59
	{
60
		$view = $this->getObject()->data( $this->view() );
61
62
		try
63
		{
64
			if( ( $id = $view->param( 'id' ) ) === null )
65
			{
66
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
67
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
68
			}
69
70
			$manager = \Aimeos\MShop::create( $this->getContext(), 'locale' );
71
			$view->item = $manager->get( $id );
72
73
			$view->itemData = $this->toArray( $view->item, true );
74
			$view->itemBody = parent::copy();
75
		}
76
		catch( \Exception $e )
77
		{
78
			$this->report( $e, 'copy' );
79
		}
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Creates a new resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function create() : ?string
91
	{
92
		$view = $this->getObject()->data( $this->view() );
93
94
		try
95
		{
96
			$data = $view->param( 'item', [] );
97
98
			if( !isset( $view->item ) ) {
99
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'locale' )->create();
100
			}
101
102
			$data['locale.siteid'] = $view->item->getSiteId();
103
104
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
105
			$view->itemBody = parent::create();
106
		}
107
		catch( \Exception $e )
108
		{
109
			$this->report( $e, 'create' );
110
		}
111
112
		return $this->render( $view );
113
	}
114
115
116
	/**
117
	 * Deletes a resource
118
	 *
119
	 * @return string|null HTML output
120
	 */
121
	public function delete() : ?string
122
	{
123
		$view = $this->view();
124
125
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale' );
126
		$manager->begin();
127
128
		try
129
		{
130
			if( ( $ids = $view->param( 'id' ) ) === null )
131
			{
132
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
133
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
134
			}
135
136
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
137
			$search->setConditions( $search->compare( '==', 'locale.id', $ids ) );
138
			$items = $manager->search( $search );
139
140
			foreach( $items as $item )
141
			{
142
				$view->item = $item;
143
				parent::delete();
144
			}
145
146
			$manager->delete( $items->toArray() );
147
			$manager->commit();
148
149
			return $this->redirect( 'locale', 'search', null, 'delete' );
150
		}
151
		catch( \Exception $e )
152
		{
153
			$manager->rollback();
154
			$this->report( $e, 'delete' );
155
		}
156
157
		return $this->search();
158
	}
159
160
161
	/**
162
	 * Returns a single resource
163
	 *
164
	 * @return string|null HTML output
165
	 */
166
	public function get() : ?string
167
	{
168
		$view = $this->getObject()->data( $this->view() );
169
170
		try
171
		{
172
			if( ( $id = $view->param( 'id' ) ) === null )
173
			{
174
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
175
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
176
			}
177
178
			$manager = \Aimeos\MShop::create( $this->getContext(), 'locale' );
179
180
			$view->item = $manager->get( $id );
181
			$view->itemData = $this->toArray( $view->item );
182
			$view->itemBody = parent::get();
183
		}
184
		catch( \Exception $e )
185
		{
186
			$this->report( $e, 'get' );
187
		}
188
189
		return $this->render( $view );
190
	}
191
192
193
	/**
194
	 * Saves the data
195
	 *
196
	 * @return string|null HTML output
197
	 */
198
	public function save() : ?string
199
	{
200
		$view = $this->view();
201
202
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale' );
203
		$manager->begin();
204
205
		try
206
		{
207
			$item = $this->fromArray( $view->param( 'item', [] ) );
208
			$view->item = $item->getId() ? $item : $manager->save( $item );
209
			$view->itemBody = parent::save();
210
211
			$manager->save( $view->item );
212
			$manager->commit();
213
214
			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

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