Passed
Push — master ( 154e9f...c95a25 )
by Aimeos
03:44
created

Standard::delete()   B

Complexity

Conditions 6
Paths 46

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 46
nop 0
dl 0
loc 51
rs 8.8337
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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