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