Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::get()   A

Complexity

Conditions 4
Paths 11

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

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