Passed
Branch master (d032f7)
by Aimeos
05:30
created

Standard::copy()   A

Complexity

Conditions 5
Paths 25

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 25
nop 0
dl 0
loc 40
rs 9.2248
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-2018
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
	 * Copies a resource
28
	 *
29
	 * @return string HTML output
30
	 */
31
	public function copy()
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' );
43
			$view->item = $manager->getItem( $id );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemCurrencies = $this->getCurrencyItems();
48
			$view->itemLanguages = $this->getLanguageItems();
49
			$view->itemBody = '';
50
51
			foreach( $this->getSubClients() as $idx => $client )
52
			{
53
				$view->tabindex = ++$idx + 1;
54
				$view->itemBody .= $client->copy();
55
			}
56
		}
57
		catch( \Aimeos\MShop\Exception $e )
58
		{
59
			$error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
60
			$view->errors = $view->get( 'errors', [] ) + $error;
61
			$this->logException( $e );
62
		}
63
		catch( \Exception $e )
64
		{
65
			$error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
66
			$view->errors = $view->get( 'errors', [] ) + $error;
67
			$this->logException( $e );
68
		}
69
70
		return $this->render( $view );
71
	}
72
73
74
	/**
75
	 * Creates a new resource
76
	 *
77
	 * @return string HTML output
78
	 */
79
	public function create()
80
	{
81
		$view = $this->getView();
82
		$context = $this->getContext();
83
84
		try
85
		{
86
			$data = $view->param( 'item', [] );
87
88
			if( !isset( $view->item ) ) {
89
				$view->item = \Aimeos\MShop::create( $context, 'locale' )->createItem();
90
			}
91
92
			$data['locale.siteid'] = $view->item->getSiteId();
93
94
			$view->itemSubparts = $this->getSubClientNames();
95
			$view->itemCurrencies = $this->getCurrencyItems();
96
			$view->itemLanguages = $this->getLanguageItems();
97
			$view->itemData = $data;
98
			$view->itemBody = '';
99
100
			foreach( $this->getSubClients() as $idx => $client )
101
			{
102
				$view->tabindex = ++$idx + 1;
103
				$view->itemBody .= $client->create();
104
			}
105
		}
106
		catch( \Aimeos\MShop\Exception $e )
107
		{
108
			$error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
109
			$view->errors = $view->get( 'errors', [] ) + $error;
110
			$this->logException( $e );
111
		}
112
		catch( \Exception $e )
113
		{
114
			$error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
115
			$view->errors = $view->get( 'errors', [] ) + $error;
116
			$this->logException( $e );
117
		}
118
119
		return $this->render( $view );
120
	}
121
122
123
	/**
124
	 * Deletes a resource
125
	 *
126
	 * @return string|null HTML output
127
	 */
128
	public function delete()
129
	{
130
		$view = $this->getView();
131
		$context = $this->getContext();
132
133
		$manager = \Aimeos\MShop::create( $context, 'locale' );
134
		$manager->begin();
135
136
		try
137
		{
138
			if( ( $id = $view->param( 'id' ) ) === null ) {
139
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
140
			}
141
142
			$view->item = $manager->getItem( $id );
143
144
			foreach( $this->getSubClients() as $client ) {
145
				$client->delete();
146
			}
147
148
			$manager->deleteItem( $id );
149
			$manager->commit();
150
151
			$this->nextAction( $view, 'search', 'locale', null, 'delete' );
152
			return;
153
		}
154
		catch( \Aimeos\MShop\Exception $e )
155
		{
156
			$error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
157
			$view->errors = $view->get( 'errors', [] ) + $error;
158
			$this->logException( $e );
159
		}
160
		catch( \Exception $e )
161
		{
162
			$error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
163
			$view->errors = $view->get( 'errors', [] ) + $error;
164
			$this->logException( $e );
165
		}
166
167
		$manager->rollback();
168
169
		return $this->search();
170
	}
171
172
173
	/**
174
	 * Returns a single resource
175
	 *
176
	 * @return string HTML output
177
	 */
178
	public function get()
179
	{
180
		$view = $this->getView();
181
		$context = $this->getContext();
182
183
		try
184
		{
185
			if( ( $id = $view->param( 'id' ) ) === null ) {
186
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
187
			}
188
189
			$manager = \Aimeos\MShop::create( $context, 'locale' );
190
191
			$view->item = $manager->getItem( $id );
192
			$view->itemSubparts = $this->getSubClientNames();
193
			$view->itemData = $this->toArray( $view->item );
194
			$view->itemCurrencies = $this->getCurrencyItems();
195
			$view->itemLanguages = $this->getLanguageItems();
196
			$view->itemBody = '';
197
198
			foreach( $this->getSubClients() as $idx => $client )
199
			{
200
				$view->tabindex = ++$idx + 1;
201
				$view->itemBody .= $client->get();
202
			}
203
		}
204
		catch( \Aimeos\MShop\Exception $e )
205
		{
206
			$error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
207
			$view->errors = $view->get( 'errors', [] ) + $error;
208
			$this->logException( $e );
209
		}
210
		catch( \Exception $e )
211
		{
212
			$error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
213
			$view->errors = $view->get( 'errors', [] ) + $error;
214
			$this->logException( $e );
215
		}
216
217
		return $this->render( $view );
218
	}
219
220
221
	/**
222
	 * Saves the data
223
	 *
224
	 * @return string HTML output
225
	 */
226
	public function save()
227
	{
228
		$view = $this->getView();
229
		$context = $this->getContext();
230
231
		$manager = \Aimeos\MShop::create( $context, 'locale' );
232
		$manager->begin();
233
234
		try
235
		{
236
			$item = $this->fromArray( $view->param( 'item', [] ) );
237
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
238
			$view->itemBody = '';
239
240
			foreach( $this->getSubClients() as $client ) {
241
				$view->itemBody .= $client->save();
242
			}
243
244
			$manager->saveItem( $view->item );
245
			$manager->commit();
246
247
			$this->nextAction( $view, $view->param( 'next' ), 'locale', $view->item->getId(), 'save' );
248
			return;
249
		}
250
		catch( \Aimeos\Admin\JQAdm\Exception $e )
251
		{
252
			// fall through to create
253
		}
254
		catch( \Aimeos\MShop\Exception $e )
255
		{
256
			$error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
257
			$view->errors = $view->get( 'errors', [] ) + $error;
258
			$this->logException( $e );
259
		}
260
		catch( \Exception $e )
261
		{
262
			$error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
263
			$view->errors = $view->get( 'errors', [] ) + $error;
264
			$this->logException( $e );
265
		}
266
267
		$manager->rollback();
268
269
		return $this->create();
270
	}
271
272
273
	/**
274
	 * Returns a list of resource according to the conditions
275
	 *
276
	 * @return string HTML output
277
	 */
278
	public function search()
279
	{
280
		$view = $this->getView();
281
		$context = $this->getContext();
282
283
		try
284
		{
285
			$total = 0;
286
			$params = $this->storeSearchParams( $view->param(), 'locale' );
287
			$manager = \Aimeos\MShop::create( $context, 'locale' );
288
			$search = $this->initCriteria( $manager->createSearch(), $params );
289
290
			$view->items = $manager->searchItems( $search, [], $total );
291
			$view->filterAttributes = $manager->getSearchAttributes( true );
292
			$view->filterOperators = $search->getOperators();
293
			$view->total = $total;
294
			$view->itemBody = '';
295
296
			foreach( $this->getSubClients() as $client ) {
297
				$view->itemBody .= $client->search();
298
			}
299
		}
300
		catch( \Aimeos\MShop\Exception $e )
301
		{
302
			$error = array( 'locale-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
303
			$view->errors = $view->get( 'errors', [] ) + $error;
304
			$this->logException( $e );
305
		}
306
		catch( \Exception $e )
307
		{
308
			$error = array( 'locale-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
309
			$view->errors = $view->get( 'errors', [] ) + $error;
310
			$this->logException( $e );
311
		}
312
313
		/** admin/jqadm/locale/template-list
314
		 * Relative path to the HTML body template for the locale list.
315
		 *
316
		 * The template file contains the HTML code and processing instructions
317
		 * to generate the result shown in the body of the frontend. The
318
		 * configuration string is the path to the template file relative
319
		 * to the templates directory (usually in admin/jqadm/templates).
320
		 *
321
		 * You can overwrite the template file configuration in extensions and
322
		 * provide alternative templates. These alternative templates should be
323
		 * named like the default one but with the string "default" replaced by
324
		 * an unique name. You may use the name of your project for this. If
325
		 * you've implemented an alternative client class as well, "default"
326
		 * should be replaced by the name of the new class.
327
		 *
328
		 * @param string Relative path to the template creating the HTML code
329
		 * @since 2017.10
330
		 * @category Developer
331
		 */
332
		$tplconf = 'admin/jqadm/locale/template-list';
333
		$default = 'locale/list-standard';
334
335
		return $view->render( $view->config( $tplconf, $default ) );
336
	}
337
338
339
	/**
340
	 * Returns the sub-client given by its name.
341
	 *
342
	 * @param string $type Name of the client type
343
	 * @param string|null $name Name of the sub-client (Default if null)
344
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
345
	 */
346
	public function getSubClient( $type, $name = null )
347
	{
348
		/** admin/jqadm/locale/decorators/excludes
349
		 * Excludes decorators added by the "common" option from the locale JQAdm client
350
		 *
351
		 * Decorators extend the functionality of a class by adding new aspects
352
		 * (e.g. log what is currently done), executing the methods of the underlying
353
		 * class only in certain conditions (e.g. only for logged in users) or
354
		 * modify what is returned to the caller.
355
		 *
356
		 * This option allows you to remove a decorator added via
357
		 * "client/jqadm/common/decorators/default" before they are wrapped
358
		 * around the JQAdm client.
359
		 *
360
		 *  admin/jqadm/locale/decorators/excludes = array( 'decorator1' )
361
		 *
362
		 * This would remove the decorator named "decorator1" from the list of
363
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
364
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
365
		 *
366
		 * @param array List of decorator names
367
		 * @since 2017.10
368
		 * @category Developer
369
		 * @see admin/jqadm/common/decorators/default
370
		 * @see admin/jqadm/locale/decorators/global
371
		 * @see admin/jqadm/locale/decorators/local
372
		 */
373
374
		/** admin/jqadm/locale/decorators/global
375
		 * Adds a list of globally available decorators only to the locale JQAdm client
376
		 *
377
		 * Decorators extend the functionality of a class by adding new aspects
378
		 * (e.g. log what is currently done), executing the methods of the underlying
379
		 * class only in certain conditions (e.g. only for logged in users) or
380
		 * modify what is returned to the caller.
381
		 *
382
		 * This option allows you to wrap global decorators
383
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
384
		 *
385
		 *  admin/jqadm/locale/decorators/global = array( 'decorator1' )
386
		 *
387
		 * This would add the decorator named "decorator1" defined by
388
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
389
		 *
390
		 * @param array List of decorator names
391
		 * @since 2017.10
392
		 * @category Developer
393
		 * @see admin/jqadm/common/decorators/default
394
		 * @see admin/jqadm/locale/decorators/excludes
395
		 * @see admin/jqadm/locale/decorators/local
396
		 */
397
398
		/** admin/jqadm/locale/decorators/local
399
		 * Adds a list of local decorators only to the locale JQAdm client
400
		 *
401
		 * Decorators extend the functionality of a class by adding new aspects
402
		 * (e.g. log what is currently done), executing the methods of the underlying
403
		 * class only in certain conditions (e.g. only for logged in users) or
404
		 * modify what is returned to the caller.
405
		 *
406
		 * This option allows you to wrap local decorators
407
		 * ("\Aimeos\Admin\JQAdm\Locale\Decorator\*") around the JQAdm client.
408
		 *
409
		 *  admin/jqadm/locale/decorators/local = array( 'decorator2' )
410
		 *
411
		 * This would add the decorator named "decorator2" defined by
412
		 * "\Aimeos\Admin\JQAdm\Locale\Decorator\Decorator2" only to the JQAdm client.
413
		 *
414
		 * @param array List of decorator names
415
		 * @since 2017.10
416
		 * @category Developer
417
		 * @see admin/jqadm/common/decorators/default
418
		 * @see admin/jqadm/locale/decorators/excludes
419
		 * @see admin/jqadm/locale/decorators/global
420
		 */
421
		return $this->createSubClient( 'locale/' . $type, $name );
422
	}
423
424
425
	/**
426
	 * Returns the available currencies
427
	 *
428
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface[] List of currency items
429
	 */
430
	protected function getCurrencyItems()
431
	{
432
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/currency' );
433
434
		$search = $manager->createSearch( true );
435
		$search->setSortations( [$search->sort( '+', 'locale.currency.id')] );
436
		$search->setSlice( 0, 250 );
437
438
		return $manager->searchItems( $search );
439
	}
440
441
442
	/**
443
	 * Returns the available languages
444
	 *
445
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface[] List of language items
446
	 */
447
	protected function getLanguageItems()
448
	{
449
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/language' );
450
451
		$search = $manager->createSearch( true );
452
		$search->setSortations( [$search->sort( '+', 'locale.language.id')] );
453
		$search->setSlice( 0, 250 );
454
455
		return $manager->searchItems( $search );
456
	}
457
458
459
	/**
460
	 * Returns the list of sub-client names configured for the client.
461
	 *
462
	 * @return array List of JQAdm client names
463
	 */
464
	protected function getSubClientNames()
465
	{
466
		/** admin/jqadm/locale/standard/subparts
467
		 * List of JQAdm sub-clients rendered within the locale section
468
		 *
469
		 * The output of the frontend is composed of the code generated by the JQAdm
470
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
471
		 * that are responsible for rendering certain sub-parts of the output. The
472
		 * sub-clients can contain JQAdm clients themselves and therefore a
473
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
474
		 * the output that is placed inside the container of its parent.
475
		 *
476
		 * At first, always the JQAdm code generated by the parent is printed, then
477
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
478
		 * determines the order of the output of these sub-clients inside the parent
479
		 * container. If the configured list of clients is
480
		 *
481
		 *  array( "subclient1", "subclient2" )
482
		 *
483
		 * you can easily change the order of the output by reordering the subparts:
484
		 *
485
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
486
		 *
487
		 * You can also remove one or more parts if they shouldn't be rendered:
488
		 *
489
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
490
		 *
491
		 * As the clients only generates structural JQAdm, the layout defined via CSS
492
		 * should support adding, removing or reordering content by a fluid like
493
		 * design.
494
		 *
495
		 * @param array List of sub-client names
496
		 * @since 2017.10
497
		 * @category Developer
498
		 */
499
		return $this->getContext()->getConfig()->get( 'admin/jqadm/locale/standard/subparts', [] );
500
	}
501
502
503
504
	/**
505
	 * Creates new and updates existing items using the data array
506
	 *
507
	 * @param string[] Data array
0 ignored issues
show
Bug introduced by
The type Aimeos\Admin\JQAdm\Locale\Data was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
508
	 * @return \Aimeos\MShop\Locale\Item\Iface New locale item object
509
	 */
510
	protected function fromArray( array $data )
511
	{
512
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale' );
513
514
		if( isset( $data['locale.id'] ) && $data['locale.id'] != '' ) {
515
			$item = $manager->getItem( $data['locale.id'] );
516
		} else {
517
			$item = $manager->createItem();
518
		}
519
520
		$item->fromArray( $data );
521
522
		return $item;
523
	}
524
525
526
	/**
527
	 * Constructs the data array for the view from the given item
528
	 *
529
	 * @param \Aimeos\MShop\Locale\Item\Iface $item Locale item object
530
	 * @return string[] Multi-dimensional associative list of item data
531
	 */
532
	protected function toArray( \Aimeos\MShop\Locale\Item\Iface $item, $copy = false )
533
	{
534
		$data = $item->toArray( true );
535
536
		if( $copy === true )
537
		{
538
			$data['locale.siteid'] = $this->getContext()->getLocale()->getSiteId();
539
			$data['locale.id'] = '';
540
		}
541
542
		return $data;
543
	}
544
545
546
	/**
547
	 * Returns the rendered template including the view data
548
	 *
549
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
550
	 * @return string HTML output
551
	 */
552
	protected function render( \Aimeos\MW\View\Iface $view )
553
	{
554
		/** admin/jqadm/locale/template-item
555
		 * Relative path to the HTML body template for the locale item.
556
		 *
557
		 * The template file contains the HTML code and processing instructions
558
		 * to generate the result shown in the body of the frontend. The
559
		 * configuration string is the path to the template file relative
560
		 * to the templates directory (usually in admin/jqadm/templates).
561
		 *
562
		 * You can overwrite the template file configuration in extensions and
563
		 * provide alternative templates. These alternative templates should be
564
		 * named like the default one but with the string "default" replaced by
565
		 * an unique name. You may use the name of your project for this. If
566
		 * you've implemented an alternative client class as well, "default"
567
		 * should be replaced by the name of the new class.
568
		 *
569
		 * @param string Relative path to the template creating the HTML code
570
		 * @since 2017.10
571
		 * @category Developer
572
		 */
573
		$tplconf = 'admin/jqadm/locale/template-item';
574
		$default = 'locale/item-standard';
575
576
		return $view->render( $view->config( $tplconf, $default ) );
577
	}
578
}
579