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 0
Metric Value
cc 5
eloc 22
nc 25
nop 0
dl 0
loc 40
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-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Attribute;
12
13
sprintf( 'attribute' ); // for translation
14
15
16
/**
17
 * Default implementation of 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|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, 'attribute' );
43
			$view->item = $manager->getItem( $id, $this->getDomains() );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemTypes = $this->getTypeItems();
48
			$view->itemBody = '';
49
50
			foreach( $this->getSubClients() as $idx => $client )
51
			{
52
				$view->tabindex = ++$idx + 1;
53
				$view->itemBody .= $client->copy();
54
			}
55
		}
56
		catch( \Exception $e )
57
		{
58
			$this->report( $e, 'copy' );
59
		}
60
61
		return $this->render( $view );
62
	}
63
64
65
	/**
66
	 * Creates a new resource
67
	 *
68
	 * @return string|null HTML output
69
	 */
70
	public function create() : ?string
71
	{
72
		$view = $this->getView();
73
		$context = $this->getContext();
74
75
		try
76
		{
77
			$data = $view->param( 'item', [] );
78
79
			if( !isset( $view->item ) ) {
80
				$view->item = \Aimeos\MShop::create( $context, 'attribute' )->createItem();
81
			}
82
83
			$data['attribute.siteid'] = $view->item->getSiteId();
84
85
			$view->itemSubparts = $this->getSubClientNames();
86
			$view->itemTypes = $this->getTypeItems();
87
			$view->itemData = $data;
88
			$view->itemBody = '';
89
90
			foreach( $this->getSubClients() as $idx => $client )
91
			{
92
				$view->tabindex = ++$idx + 1;
93
				$view->itemBody .= $client->create();
94
			}
95
		}
96
		catch( \Exception $e )
97
		{
98
			$this->report( $e, 'create' );
99
		}
100
101
		return $this->render( $view );
102
	}
103
104
105
	/**
106
	 * Deletes a resource
107
	 *
108
	 * @return string|null HTML output
109
	 */
110
	public function delete() : ?string
111
	{
112
		$view = $this->getView();
113
		$context = $this->getContext();
114
115
		$manager = \Aimeos\MShop::create( $context, 'attribute' );
116
		$manager->begin();
117
118
		try
119
		{
120
			if( ( $ids = $view->param( 'id' ) ) === null ) {
121
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
122
			}
123
124
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
125
			$search->setConditions( $search->compare( '==', 'attribute.id', $ids ) );
126
			$items = $manager->searchItems( $search, $this->getDomains() );
127
128
			foreach( $items as $item )
129
			{
130
				$view->item = $item;
131
132
				foreach( $this->getSubClients() as $client ) {
133
					$client->delete();
134
				}
135
			}
136
137
			$manager->deleteItems( $items->toArray() );
138
			$manager->commit();
139
140
			$this->nextAction( $view, 'search', 'attribute', null, 'delete' );
141
			return null;
142
		}
143
		catch( \Exception $e )
144
		{
145
			$manager->rollback();
146
			$this->report( $e, 'delete' );
147
		}
148
149
		return $this->search();
150
	}
151
152
153
	/**
154
	 * Returns a single resource
155
	 *
156
	 * @return string|null HTML output
157
	 */
158
	public function get() : ?string
159
	{
160
		$view = $this->getView();
161
		$context = $this->getContext();
162
163
		try
164
		{
165
			if( ( $id = $view->param( 'id' ) ) === null ) {
166
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
167
			}
168
169
			$manager = \Aimeos\MShop::create( $context, 'attribute' );
170
171
			$view->item = $manager->getItem( $id, $this->getDomains() );
172
			$view->itemSubparts = $this->getSubClientNames();
173
			$view->itemData = $this->toArray( $view->item );
174
			$view->itemTypes = $this->getTypeItems();
175
			$view->itemBody = '';
176
177
			foreach( $this->getSubClients() as $idx => $client )
178
			{
179
				$view->tabindex = ++$idx + 1;
180
				$view->itemBody .= $client->get();
181
			}
182
		}
183
		catch( \Exception $e )
184
		{
185
			$this->report( $e, 'get' );
186
		}
187
188
		return $this->render( $view );
189
	}
190
191
192
	/**
193
	 * Saves the data
194
	 *
195
	 * @return string|null HTML output
196
	 */
197
	public function save() : ?string
198
	{
199
		$view = $this->getView();
200
		$context = $this->getContext();
201
202
		$manager = \Aimeos\MShop::create( $context, 'attribute' );
203
		$manager->begin();
204
205
		try
206
		{
207
			$item = $this->fromArray( $view->param( 'item', [] ) );
208
			$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

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