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