Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard::delete()   A

Complexity

Conditions 5
Paths 25

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
nc 25
nop 0
dl 0
loc 40
rs 9.2568
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
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|null HTML output
30
	 */
31
	public function copy() : ?string
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( \Exception $e )
56
		{
57
			$this->report( $e, 'copy' );
58
		}
59
60
		return $this->render( $view );
61
	}
62
63
64
	/**
65
	 * Creates a new resource
66
	 *
67
	 * @return string|null HTML output
68
	 */
69
	public function create() : ?string
70
	{
71
		$view = $this->getView();
72
		$context = $this->getContext();
73
74
		try
75
		{
76
			$data = $view->param( 'item', [] );
77
78
			if( !isset( $view->item ) ) {
79
				$view->item = \Aimeos\MShop::create( $context, 'customer/group' )->createItem();
80
			}
81
82
			$data['customer.group.siteid'] = $view->item->getSiteId();
83
84
			$view->itemSubparts = $this->getSubClientNames();
85
			$view->itemData = $data;
86
			$view->itemBody = '';
87
88
			foreach( $this->getSubClients() as $idx => $client )
89
			{
90
				$view->tabindex = ++$idx + 1;
91
				$view->itemBody .= $client->create();
92
			}
93
		}
94
		catch( \Exception $e )
95
		{
96
			$this->report( $e, 'create' );
97
		}
98
99
		return $this->render( $view );
100
	}
101
102
103
	/**
104
	 * Deletes a resource
105
	 *
106
	 * @return string|null HTML output
107
	 */
108
	public function delete() : ?string
109
	{
110
		$view = $this->getView();
111
		$context = $this->getContext();
112
113
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
114
		$manager->begin();
115
116
		try
117
		{
118
			if( ( $ids = $view->param( 'id' ) ) === null ) {
119
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
120
			}
121
122
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
123
			$search->setConditions( $search->compare( '==', 'customer.group.id', $ids ) );
124
			$items = $manager->searchItems( $search );
125
126
			foreach( $items as $item )
127
			{
128
				$view->item = $item;
129
130
				foreach( $this->getSubClients() as $client ) {
131
					$client->delete();
132
				}
133
			}
134
135
			$manager->deleteItems( $items->toArray() );
136
			$manager->commit();
137
138
			$this->nextAction( $view, 'search', 'group', null, 'delete' );
139
			return null;
140
		}
141
		catch( \Exception $e )
142
		{
143
			$manager->rollback();
144
			$this->report( $e, 'delete' );
145
		}
146
147
		return $this->search();
148
	}
149
150
151
	/**
152
	 * Returns a single resource
153
	 *
154
	 * @return string|null HTML output
155
	 */
156
	public function get() : ?string
157
	{
158
		$view = $this->getView();
159
		$context = $this->getContext();
160
161
		try
162
		{
163
			if( ( $id = $view->param( 'id' ) ) === null ) {
164
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
165
			}
166
167
			$manager = \Aimeos\MShop::create( $context, 'customer/group' );
168
169
			$view->item = $manager->getItem( $id );
170
			$view->itemSubparts = $this->getSubClientNames();
171
			$view->itemData = $this->toArray( $view->item );
172
			$view->itemBody = '';
173
174
			foreach( $this->getSubClients() as $idx => $client )
175
			{
176
				$view->tabindex = ++$idx + 1;
177
				$view->itemBody .= $client->get();
178
			}
179
		}
180
		catch( \Exception $e )
181
		{
182
			$this->report( $e, 'get' );
183
		}
184
185
		return $this->render( $view );
186
	}
187
188
189
	/**
190
	 * Saves the data
191
	 *
192
	 * @return string|null HTML output
193
	 */
194
	public function save() : ?string
195
	{
196
		$view = $this->getView();
197
		$context = $this->getContext();
198
199
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
200
		$manager->begin();
201
202
		try
203
		{
204
			$item = $this->fromArray( $view->param( 'item', [] ) );
205
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$view->item = $item->getId() ? $item : $manager->/** @scrutinizer ignore-call */ saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
206
			$view->itemBody = '';
207
208
			foreach( $this->getSubClients() as $client ) {
209
				$view->itemBody .= $client->save();
210
			}
211
212
			$manager->saveItem( clone $view->item );
213
			$manager->commit();
214
215
			$this->nextAction( $view, $view->param( 'next' ), 'group', $view->item->getId(), 'save' );
216
			return null;
217
		}
218
		catch( \Exception $e )
219
		{
220
			$manager->rollback();
221
			$this->report( $e, 'save' );
222
		}
223
224
		return $this->create();
225
	}
226
227
228
	/**
229
	 * Returns a list of resource according to the conditions
230
	 *
231
	 * @return string|null HTML output
232
	 */
233
	public function search() : ?string
234
	{
235
		$view = $this->getView();
236
		$context = $this->getContext();
237
238
		try
239
		{
240
			$total = 0;
241
			$params = $this->storeSearchParams( $view->param(), 'group' );
242
			$manager = \Aimeos\MShop::create( $context, 'customer/group' );
243
			$search = $this->initCriteria( $manager->createSearch(), $params );
244
245
			$view->items = $manager->searchItems( $search, [], $total );
246
			$view->filterAttributes = $manager->getSearchAttributes( true );
247
			$view->filterOperators = $search->getOperators();
248
			$view->total = $total;
249
			$view->itemBody = '';
250
251
			foreach( $this->getSubClients() as $client ) {
252
				$view->itemBody .= $client->search();
253
			}
254
		}
255
		catch( \Exception $e )
256
		{
257
			$this->report( $e, 'search' );
258
		}
259
260
		/** admin/jqadm/group/template-list
261
		 * Relative path to the HTML body template for the group list.
262
		 *
263
		 * The template file contains the HTML code and processing instructions
264
		 * to generate the result shown in the body of the frontend. The
265
		 * configuration string is the path to the template file relative
266
		 * to the templates directory (usually in admin/jqadm/templates).
267
		 *
268
		 * You can overwrite the template file configuration in extensions and
269
		 * provide alternative templates. These alternative templates should be
270
		 * named like the default one but with the string "default" replaced by
271
		 * an unique name. You may use the name of your project for this. If
272
		 * you've implemented an alternative client class as well, "default"
273
		 * should be replaced by the name of the new class.
274
		 *
275
		 * @param string Relative path to the template creating the HTML code
276
		 * @since 2018.07
277
		 * @category Developer
278
		 */
279
		$tplconf = 'admin/jqadm/group/template-list';
280
		$default = 'group/list-standard';
281
282
		return $view->render( $view->config( $tplconf, $default ) );
283
	}
284
285
286
	/**
287
	 * Returns the sub-client given by its name.
288
	 *
289
	 * @param string $type Name of the client type
290
	 * @param string|null $name Name of the sub-client (Default if null)
291
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
292
	 */
293
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
294
	{
295
		/** admin/jqadm/group/decorators/excludes
296
		 * Excludes decorators added by the "common" option from the group JQAdm client
297
		 *
298
		 * Decorators extend the functionality of a class by adding new aspects
299
		 * (e.g. log what is currently done), executing the methods of the underlying
300
		 * class only in certain conditions (e.g. only for logged in users) or
301
		 * modify what is returned to the caller.
302
		 *
303
		 * This option allows you to remove a decorator added via
304
		 * "client/jqadm/common/decorators/default" before they are wrapped
305
		 * around the JQAdm client.
306
		 *
307
		 *  admin/jqadm/group/decorators/excludes = array( 'decorator1' )
308
		 *
309
		 * This would remove the decorator named "decorator1" from the list of
310
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
311
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
312
		 *
313
		 * @param array List of decorator names
314
		 * @since 2018.07
315
		 * @category Developer
316
		 * @see admin/jqadm/common/decorators/default
317
		 * @see admin/jqadm/group/decorators/global
318
		 * @see admin/jqadm/group/decorators/local
319
		 */
320
321
		/** admin/jqadm/group/decorators/global
322
		 * Adds a list of globally available decorators only to the group JQAdm client
323
		 *
324
		 * Decorators extend the functionality of a class by adding new aspects
325
		 * (e.g. log what is currently done), executing the methods of the underlying
326
		 * class only in certain conditions (e.g. only for logged in users) or
327
		 * modify what is returned to the caller.
328
		 *
329
		 * This option allows you to wrap global decorators
330
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
331
		 *
332
		 *  admin/jqadm/group/decorators/global = array( 'decorator1' )
333
		 *
334
		 * This would add the decorator named "decorator1" defined by
335
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
336
		 *
337
		 * @param array List of decorator names
338
		 * @since 2018.07
339
		 * @category Developer
340
		 * @see admin/jqadm/common/decorators/default
341
		 * @see admin/jqadm/group/decorators/excludes
342
		 * @see admin/jqadm/group/decorators/local
343
		 */
344
345
		/** admin/jqadm/group/decorators/local
346
		 * Adds a list of local decorators only to the group JQAdm client
347
		 *
348
		 * Decorators extend the functionality of a class by adding new aspects
349
		 * (e.g. log what is currently done), executing the methods of the underlying
350
		 * class only in certain conditions (e.g. only for logged in users) or
351
		 * modify what is returned to the caller.
352
		 *
353
		 * This option allows you to wrap local decorators
354
		 * ("\Aimeos\Admin\JQAdm\Group\Decorator\*") around the JQAdm client.
355
		 *
356
		 *  admin/jqadm/group/decorators/local = array( 'decorator2' )
357
		 *
358
		 * This would add the decorator named "decorator2" defined by
359
		 * "\Aimeos\Admin\JQAdm\Group\Decorator\Decorator2" only to the JQAdm client.
360
		 *
361
		 * @param array List of decorator names
362
		 * @since 2018.07
363
		 * @category Developer
364
		 * @see admin/jqadm/common/decorators/default
365
		 * @see admin/jqadm/group/decorators/excludes
366
		 * @see admin/jqadm/group/decorators/global
367
		 */
368
		return $this->createSubClient( 'group/' . $type, $name );
369
	}
370
371
372
	/**
373
	 * Returns the list of sub-client names configured for the client.
374
	 *
375
	 * @return array List of JQAdm client names
376
	 */
377
	protected function getSubClientNames() : array
378
	{
379
		/** admin/jqadm/group/standard/subparts
380
		 * List of JQAdm sub-clients rendered within the group section
381
		 *
382
		 * The output of the frontend is composed of the code generated by the JQAdm
383
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
384
		 * that are responsible for rendering certain sub-parts of the output. The
385
		 * sub-clients can contain JQAdm clients themselves and therefore a
386
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
387
		 * the output that is placed inside the container of its parent.
388
		 *
389
		 * At first, always the JQAdm code generated by the parent is printed, then
390
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
391
		 * determines the order of the output of these sub-clients inside the parent
392
		 * container. If the configured list of clients is
393
		 *
394
		 *  array( "subclient1", "subclient2" )
395
		 *
396
		 * you can easily change the order of the output by reordering the subparts:
397
		 *
398
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
399
		 *
400
		 * You can also remove one or more parts if they shouldn't be rendered:
401
		 *
402
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
403
		 *
404
		 * As the clients only generates structural JQAdm, the layout defined via CSS
405
		 * should support adding, removing or reordering content by a fluid like
406
		 * design.
407
		 *
408
		 * @param array List of sub-client names
409
		 * @since 2018.07
410
		 * @category Developer
411
		 */
412
		return $this->getContext()->getConfig()->get( 'admin/jqadm/group/standard/subparts', [] );
413
	}
414
415
416
417
	/**
418
	 * Creates new and updates existing items using the data array
419
	 *
420
	 * @param array $data Data array
421
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface New group item object
422
	 */
423
	protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Group\Iface
424
	{
425
		$manager = \Aimeos\MShop::create( $this->getContext(), 'customer/group' );
426
427
		if( isset( $data['customer.group.id'] ) && $data['customer.group.id'] != '' ) {
428
			$item = $manager->getItem( $data['customer.group.id'] );
429
		} else {
430
			$item = $manager->createItem();
431
		}
432
433
		$item->fromArray( $data, true );
434
435
		return $item;
436
	}
437
438
439
	/**
440
	 * Constructs the data array for the view from the given item
441
	 *
442
	 * @param \Aimeos\MShop\Customer\Item\Group\Iface $item Group item object
443
	 * @return string[] Multi-dimensional associative list of item data
444
	 */
445
	protected function toArray( \Aimeos\MShop\Customer\Item\Group\Iface $item, bool $copy = false ) : array
446
	{
447
		$data = $item->toArray( true );
448
449
		if( $copy === true )
450
		{
451
			$data['customer.group.siteid'] = $this->getContext()->getLocale()->getSiteId();
452
			$data['customer.group.id'] = '';
453
		}
454
455
		return $data;
456
	}
457
458
459
	/**
460
	 * Returns the rendered template including the view data
461
	 *
462
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
463
	 * @return string|null HTML output
464
	 */
465
	protected function render( \Aimeos\MW\View\Iface $view ) : string
466
	{
467
		/** admin/jqadm/group/template-item
468
		 * Relative path to the HTML body template for the group item.
469
		 *
470
		 * The template file contains the HTML code and processing instructions
471
		 * to generate the result shown in the body of the frontend. The
472
		 * configuration string is the path to the template file relative
473
		 * to the templates directory (usually in admin/jqadm/templates).
474
		 *
475
		 * You can overwrite the template file configuration in extensions and
476
		 * provide alternative templates. These alternative templates should be
477
		 * named like the default one but with the string "default" replaced by
478
		 * an unique name. You may use the name of your project for this. If
479
		 * you've implemented an alternative client class as well, "default"
480
		 * should be replaced by the name of the new class.
481
		 *
482
		 * @param string Relative path to the template creating the HTML code
483
		 * @since 2018.07
484
		 * @category Developer
485
		 */
486
		$tplconf = 'admin/jqadm/group/template-item';
487
		$default = 'group/item-standard';
488
489
		return $view->render( $view->config( $tplconf, $default ) );
490
	}
491
}
492