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

Standard::copy()   A

Complexity

Conditions 5
Paths 21

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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