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

Standard::delete()   A

Complexity

Conditions 5
Paths 25

Size

Total Lines 41
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 41
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), 2017-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Supplier;
12
13
sprintf( 'supplier' ); // for translation
14
15
16
/**
17
 * Default implementation of supplier 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, 'supplier' );
43
			$view->item = $manager->getItem( $id, $this->getDomains() );
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, 'supplier' )->createItem();
80
			}
81
82
			$data['supplier.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, 'supplier' );
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( '==', 'supplier.id', $ids ) );
124
			$items = $manager->searchItems( $search, $this->getDomains() );
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', 'supplier', null, 'delete' );
139
			return null;
140
		}
141
		catch( \Exception $e )
142
		{
143
			$manager->rollback();
144
			$this->report( $e, 'delete' );
145
		}
146
147
148
		return $this->search();
149
	}
150
151
152
	/**
153
	 * Returns a single resource
154
	 *
155
	 * @return string|null HTML output
156
	 */
157
	public function get() : ?string
158
	{
159
		$view = $this->getView();
160
		$context = $this->getContext();
161
162
		try
163
		{
164
			if( ( $id = $view->param( 'id' ) ) === null ) {
165
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
166
			}
167
168
			$manager = \Aimeos\MShop::create( $context, 'supplier' );
169
170
			$view->item = $manager->getItem( $id, $this->getDomains() );
171
			$view->itemSubparts = $this->getSubClientNames();
172
			$view->itemData = $this->toArray( $view->item );
173
			$view->itemBody = '';
174
175
			foreach( $this->getSubClients() as $idx => $client )
176
			{
177
				$view->tabindex = ++$idx + 1;
178
				$view->itemBody .= $client->get();
179
			}
180
		}
181
		catch( \Exception $e )
182
		{
183
			$this->report( $e, 'get' );
184
		}
185
186
		return $this->render( $view );
187
	}
188
189
190
	/**
191
	 * Saves the data
192
	 *
193
	 * @return string|null HTML output
194
	 */
195
	public function save() : ?string
196
	{
197
		$view = $this->getView();
198
		$context = $this->getContext();
199
200
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
201
		$manager->begin();
202
203
		try
204
		{
205
			$item = $this->fromArray( $view->param( 'item', [] ) );
206
			$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

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