Passed
Push — master ( 815a26...2fe95b )
by Aimeos
03:46
created

Standard::delete()   A

Complexity

Conditions 4
Paths 16

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
c 0
b 0
f 0
nc 16
nop 0
dl 0
loc 38
rs 9.6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Supplier;
12
13
sprintf( 'goods' ); // for translation
14
sprintf( 'supplier' ); // for translation
15
16
17
/**
18
 * Default implementation of supplier JQAdm client.
19
 *
20
 * @package Admin
21
 * @subpackage JQAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
25
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
26
{
27
	/** admin/jqadm/supplier/name
28
	 * Class name of the used account favorite client implementation
29
	 *
30
	 * Each default admin client can be replace by an alternative imlementation.
31
	 * To use this implementation, you have to set the last part of the class
32
	 * name as configuration value so the client factory knows which class it
33
	 * has to instantiate.
34
	 *
35
	 * For example, if the name of the default class is
36
	 *
37
	 *  \Aimeos\Admin\JQAdm\Supplier\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Supplier\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/supplier/name = Myfavorite
46
	 *
47
	 * The value is the last part of your own class name and it's case sensitive,
48
	 * so take care that the configuration value is exactly named like the last
49
	 * part of the class name.
50
	 *
51
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
52
	 * characters are possible! You should always start the last part of the class
53
	 * name with an upper case character and continue only with lower case characters
54
	 * or numbers. Avoid chamel case names like "MyFavorite"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2017.10
58
	 * @category Developer
59
	 */
60
61
62
	/**
63
	 * Adds the required data used in the template
64
	 *
65
	 * @param \Aimeos\Base\View\Iface $view View object
66
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
67
	 */
68
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
69
	{
70
		$codes = [];
71
72
		foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) {
73
			$codes[$code] = $view->translate( 'country', $code );
74
		}
75
76
		asort( $codes );
77
78
		$view->itemSubparts = $this->getSubClientNames();
79
		$view->countries = $codes;
80
		return $view;
81
	}
82
83
84
	/**
85
	 * Batch update of a resource
86
	 *
87
	 * @return string|null Output to display
88
	 */
89
	public function batch() : ?string
90
	{
91
		return $this->batchBase( 'supplier' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('supplier') targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
	}
93
94
95
	/**
96
	 * Copies a resource
97
	 *
98
	 * @return string|null HTML output
99
	 */
100
	public function copy() : ?string
101
	{
102
		$view = $this->object()->data( $this->view() );
103
104
		try
105
		{
106
			if( ( $id = $view->param( 'id' ) ) === null )
107
			{
108
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
109
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
110
			}
111
112
			$manager = \Aimeos\MShop::create( $this->context(), 'supplier' );
113
			$view->item = $manager->get( $id, $this->getDomains() );
114
115
			$view->itemData = $this->toArray( $view->item, true );
116
			$view->itemBody = parent::copy();
117
		}
118
		catch( \Exception $e )
119
		{
120
			$this->report( $e, 'copy' );
121
		}
122
123
		return $this->render( $view );
124
	}
125
126
127
	/**
128
	 * Creates a new resource
129
	 *
130
	 * @return string|null HTML output
131
	 */
132
	public function create() : ?string
133
	{
134
		$view = $this->object()->data( $this->view() );
135
136
		try
137
		{
138
			$data = $view->param( 'item', [] );
139
140
			if( !isset( $view->item ) ) {
141
				$view->item = \Aimeos\MShop::create( $this->context(), 'supplier' )->create();
142
			}
143
144
			$data['supplier.siteid'] = $view->item->getSiteId();
145
146
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
147
			$view->itemBody = parent::create();
148
		}
149
		catch( \Exception $e )
150
		{
151
			$this->report( $e, 'create' );
152
		}
153
154
		return $this->render( $view );
155
	}
156
157
158
	/**
159
	 * Deletes a resource
160
	 *
161
	 * @return string|null HTML output
162
	 */
163
	public function delete() : ?string
164
	{
165
		$view = $this->view();
166
167
		$manager = \Aimeos\MShop::create( $this->context(), 'supplier' );
168
		$manager->begin();
169
170
		try
171
		{
172
			if( ( $ids = $view->param( 'id' ) ) === null )
173
			{
174
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
175
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
176
			}
177
178
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
179
			$search->setConditions( $search->compare( '==', 'supplier.id', $ids ) );
180
			$items = $manager->search( $search, $this->getDomains() );
181
182
			foreach( $items as $item )
183
			{
184
				$view->item = $item;
185
				parent::delete();
186
			}
187
188
			$manager->delete( $items->toArray() );
189
			$manager->commit();
190
191
			return $this->redirect( 'supplier', 'search', null, 'delete' );
192
		}
193
		catch( \Exception $e )
194
		{
195
			$manager->rollback();
196
			$this->report( $e, 'delete' );
197
		}
198
199
200
		return $this->search();
201
	}
202
203
204
	/**
205
	 * Returns a single resource
206
	 *
207
	 * @return string|null HTML output
208
	 */
209
	public function get() : ?string
210
	{
211
		$view = $this->object()->data( $this->view() );
212
213
		try
214
		{
215
			if( ( $id = $view->param( 'id' ) ) === null )
216
			{
217
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
218
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
219
			}
220
221
			$manager = \Aimeos\MShop::create( $this->context(), 'supplier' );
222
223
			$view->item = $manager->get( $id, $this->getDomains() );
224
			$view->itemData = $this->toArray( $view->item );
225
			$view->itemBody = parent::get();
226
		}
227
		catch( \Exception $e )
228
		{
229
			$this->report( $e, 'get' );
230
		}
231
232
		return $this->render( $view );
233
	}
234
235
236
	/**
237
	 * Saves the data
238
	 *
239
	 * @return string|null HTML output
240
	 */
241
	public function save() : ?string
242
	{
243
		$view = $this->view();
244
245
		$manager = \Aimeos\MShop::create( $this->context(), 'supplier' );
246
		$manager->begin();
247
248
		try
249
		{
250
			$item = $this->fromArray( $view->param( 'item', [] ) );
251
			$view->item = $item->getId() ? $item : $manager->save( $item );
252
			$view->itemBody = parent::save();
253
254
			$manager->save( clone $view->item );
255
			$manager->commit();
256
257
			return $this->redirect( 'supplier', $view->param( 'next' ), $view->item->getId(), 'save' );
0 ignored issues
show
Bug introduced by
It seems like $view->item->getId() can also be of type Aimeos\Map; however, parameter $id of Aimeos\Admin\JQAdm\Base::redirect() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

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