Passed
Push — master ( d3a4a1...d29683 )
by Aimeos
04:23
created

Standard::delete()   A

Complexity

Conditions 4
Paths 16

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 37
rs 9.6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Basket;
12
13
sprintf( 'sales' ); // for translation
14
sprintf( 'basket' ); // for translation
15
16
17
/**
18
 * Default implementation of basket 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/basket/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\Subscription\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Subscription\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/basket/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 2023.10
58
	 */
59
60
61
	/**
62
	 * Adds the required data used in the template
63
	 *
64
	 * @param \Aimeos\Base\View\Iface $view View object
65
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
66
	 */
67
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
68
	{
69
		$view->itemSubparts = $this->getSubClientNames();
70
		return $view;
71
	}
72
73
74
	/**
75
	 * Batch update of a resource
76
	 *
77
	 * @return string|null Output to display
78
	 */
79
	public function batch() : ?string
80
	{
81
		return $this->batchBase( 'basket' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('basket') 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...
82
	}
83
84
85
	/**
86
	 * Copies a resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function copy() : ?string
91
	{
92
		$view = $this->object()->data( $this->view() );
93
		$context = $this->context();
94
95
		try
96
		{
97
			if( ( $id = $view->param( 'id' ) ) === null )
98
			{
99
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
100
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
101
			}
102
103
			$manager = \Aimeos\MShop::create( $context, 'order/basket' );
104
105
			$view->item = $manager->get( $id );
106
			$view->itemData = $this->toArray( $view->item, true );
107
			$view->itemBody = parent::copy();
108
		}
109
		catch( \Exception $e )
110
		{
111
			$this->report( $e, 'copy' );
112
		}
113
114
		return $this->render( $view );
115
	}
116
117
118
	/**
119
	 * Deletes a resource
120
	 *
121
	 * @return string|null HTML output
122
	 */
123
	public function delete() : ?string
124
	{
125
		$view = $this->view();
126
127
		$manager = \Aimeos\MShop::create( $this->context(), 'order/basket' );
128
		$manager->begin();
129
130
		try
131
		{
132
			if( ( $ids = $view->param( 'id' ) ) === null )
133
			{
134
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
135
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
136
			}
137
138
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
139
			$search->setConditions( $search->compare( '==', 'order.basket.id', $ids ) );
140
			$items = $manager->search( $search );
141
142
			foreach( $items as $item )
143
			{
144
				$view->item = $item;
145
				parent::delete();
146
			}
147
148
			$manager->delete( $items );
149
			$manager->commit();
150
151
			return $this->redirect( 'basket', 'search', null, 'delete' );
152
		}
153
		catch( \Exception $e )
154
		{
155
			$manager->rollback();
156
			$this->report( $e, 'delete' );
157
		}
158
159
		return $this->search();
160
	}
161
162
163
	/**
164
	 * Returns a single resource
165
	 *
166
	 * @return string|null HTML output
167
	 */
168
	public function get() : ?string
169
	{
170
		$view = $this->object()->data( $this->view() );
171
		$context = $this->context();
172
173
		try
174
		{
175
			if( ( $id = $view->param( 'id' ) ) === null )
176
			{
177
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
178
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
179
			}
180
181
			$manager = \Aimeos\MShop::create( $context, 'order/basket' );
182
183
			$view->item = $manager->get( $id );
184
			$view->itemData = $this->toArray( $view->item );
185
			$view->itemBody = parent::get();
186
		}
187
		catch( \Exception $e )
188
		{
189
			$this->report( $e, 'get' );
190
		}
191
192
		return $this->render( $view );
193
	}
194
195
196
	/**
197
	 * Returns a list of resource according to the conditions
198
	 *
199
	 * @return string|null HTML output
200
	 */
201
	public function search() : ?string
202
	{
203
		$view = $this->view();
204
205
		try
206
		{
207
			$total = 0;
208
			$params = $this->storeFilter( $view->param(), 'basket' );
209
			$manager = \Aimeos\MShop::create( $this->context(), 'order/basket' );
210
211
			$search = $manager->filter( false, true );
212
			$search->setSortations( [$search->sort( '-', 'order.basket.ctime' )] );
213
			$search = $this->initCriteria( $search, $params );
214
215
			$view->items = $manager->search( $search, [], $total );
216
			$view->filterAttributes = $manager->getSearchAttributes( true );
217
			$view->filterOperators = $search->getOperators();
218
			$view->itemBody = parent::search();
219
			$view->total = $total;
220
		}
221
		catch( \Exception $e )
222
		{
223
			$this->report( $e, 'search' );
224
		}
225
226
		/** admin/jqadm/basket/template-list
227
		 * Relative path to the HTML body template for the basket list.
228
		 *
229
		 * The template file contains the HTML code and processing instructions
230
		 * to generate the result shown in the body of the frontend. The
231
		 * configuration string is the path to the template file relative
232
		 * to the templates directory (usually in templates/admin/jqadm).
233
		 *
234
		 * You can overwrite the template file configuration in extensions and
235
		 * provide alternative templates. These alternative templates should be
236
		 * named like the default one but with the string "default" replaced by
237
		 * an unique name. You may use the name of your project for this. If
238
		 * you've implemented an alternative client class as well, "default"
239
		 * should be replaced by the name of the new class.
240
		 *
241
		 * @param string Relative path to the template creating the HTML code
242
		 * @since 2023.10
243
		 */
244
		$tplconf = 'admin/jqadm/basket/template-list';
245
		$default = 'basket/list';
246
247
		return $view->render( $view->config( $tplconf, $default ) );
248
	}
249
250
251
	/**
252
	 * Returns the sub-client given by its name.
253
	 *
254
	 * @param string $type Name of the client type
255
	 * @param string|null $name Name of the sub-client (Default if null)
256
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
257
	 */
258
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
259
	{
260
		/** admin/jqadm/basket/decorators/excludes
261
		 * Excludes decorators added by the "common" option from the basket JQAdm client
262
		 *
263
		 * Decorators extend the functionality of a class by adding new aspects
264
		 * (e.g. log what is currently done), executing the methods of the underlying
265
		 * class only in certain conditions (e.g. only for logged in users) or
266
		 * modify what is returned to the caller.
267
		 *
268
		 * This option allows you to remove a decorator added via
269
		 * "client/jqadm/common/decorators/default" before they are wrapped
270
		 * around the JQAdm client.
271
		 *
272
		 *  admin/jqadm/basket/decorators/excludes = array( 'decorator1' )
273
		 *
274
		 * This would remove the decorator named "decorator1" from the list of
275
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
276
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
277
		 *
278
		 * @param array List of decorator names
279
		 * @since 2023.10
280
		 * @see admin/jqadm/common/decorators/default
281
		 * @see admin/jqadm/basket/decorators/global
282
		 * @see admin/jqadm/basket/decorators/local
283
		 */
284
285
		/** admin/jqadm/basket/decorators/global
286
		 * Adds a list of globally available decorators only to the basket JQAdm client
287
		 *
288
		 * Decorators extend the functionality of a class by adding new aspects
289
		 * (e.g. log what is currently done), executing the methods of the underlying
290
		 * class only in certain conditions (e.g. only for logged in users) or
291
		 * modify what is returned to the caller.
292
		 *
293
		 * This option allows you to wrap global decorators
294
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
295
		 *
296
		 *  admin/jqadm/basket/decorators/global = array( 'decorator1' )
297
		 *
298
		 * This would add the decorator named "decorator1" defined by
299
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
300
		 *
301
		 * @param array List of decorator names
302
		 * @since 2023.10
303
		 * @see admin/jqadm/common/decorators/default
304
		 * @see admin/jqadm/basket/decorators/excludes
305
		 * @see admin/jqadm/basket/decorators/local
306
		 */
307
308
		/** admin/jqadm/basket/decorators/local
309
		 * Adds a list of local decorators only to the basket JQAdm client
310
		 *
311
		 * Decorators extend the functionality of a class by adding new aspects
312
		 * (e.g. log what is currently done), executing the methods of the underlying
313
		 * class only in certain conditions (e.g. only for logged in users) or
314
		 * modify what is returned to the caller.
315
		 *
316
		 * This option allows you to wrap local decorators
317
		 * ("\Aimeos\Admin\JQAdm\Subscription\Decorator\*") around the JQAdm client.
318
		 *
319
		 *  admin/jqadm/basket/decorators/local = array( 'decorator2' )
320
		 *
321
		 * This would add the decorator named "decorator2" defined by
322
		 * "\Aimeos\Admin\JQAdm\Subscription\Decorator\Decorator2" only to the JQAdm client.
323
		 *
324
		 * @param array List of decorator names
325
		 * @since 2023.10
326
		 * @see admin/jqadm/common/decorators/default
327
		 * @see admin/jqadm/basket/decorators/excludes
328
		 * @see admin/jqadm/basket/decorators/global
329
		 */
330
		return $this->createSubClient( 'basket/' . $type, $name );
331
	}
332
333
334
	/**
335
	 * Returns the list of sub-client names configured for the client.
336
	 *
337
	 * @return array List of JQAdm client names
338
	 */
339
	protected function getSubClientNames() : array
340
	{
341
		/** admin/jqadm/basket/subparts
342
		 * List of JQAdm sub-clients rendered within the basket section
343
		 *
344
		 * The output of the frontend is composed of the code generated by the JQAdm
345
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
346
		 * that are responsible for rendering certain sub-parts of the output. The
347
		 * sub-clients can contain JQAdm clients themselves and therefore a
348
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
349
		 * the output that is placed inside the container of its parent.
350
		 *
351
		 * At first, always the JQAdm code generated by the parent is printed, then
352
		 * the JQAdm code of its sub-clients. The basket of the JQAdm sub-clients
353
		 * determines the basket of the output of these sub-clients inside the parent
354
		 * container. If the configured list of clients is
355
		 *
356
		 *  array( "subclient1", "subclient2" )
357
		 *
358
		 * you can easily change the basket of the output by rebasketing the subparts:
359
		 *
360
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
361
		 *
362
		 * You can also remove one or more parts if they shouldn't be rendered:
363
		 *
364
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
365
		 *
366
		 * As the clients only generates structural JQAdm, the layout defined via CSS
367
		 * should support adding, removing or rebasketing content by a fluid like
368
		 * design.
369
		 *
370
		 * @param array List of sub-client names
371
		 * @since 2023.10
372
		 */
373
		return $this->context()->config()->get( 'admin/jqadm/basket/subparts', [] );
374
	}
375
376
377
	/**
378
	 * Constructs the data array for the view from the given item
379
	 *
380
	 * @param \Aimeos\MShop\Order\Item\Basket\Iface $item Basket item object
381
	 * @return string[] Multi-dimensional associative list of item data
382
	 */
383
	protected function toArray( \Aimeos\MShop\Order\Item\Basket\Iface $item, bool $copy = false ) : array
384
	{
385
		$siteId = $this->context()->locale()->getSiteId();
386
		$data = $item->toArray( true );
387
388
		if( $copy === true )
389
		{
390
			$data['order.basket.siteid'] = $siteId;
391
			$data['order.basket.id'] = '';
392
		}
393
394
		return $data;
395
	}
396
397
398
	/**
399
	 * Returns the rendered template including the view data
400
	 *
401
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
402
	 * @return string HTML output
403
	 */
404
	protected function render( \Aimeos\Base\View\Iface $view ) : string
405
	{
406
		/** admin/jqadm/basket/template-item
407
		 * Relative path to the HTML body template for the basket item.
408
		 *
409
		 * The template file contains the HTML code and processing instructions
410
		 * to generate the result shown in the body of the frontend. The
411
		 * configuration string is the path to the template file relative
412
		 * to the templates directory (usually in templates/admin/jqadm).
413
		 *
414
		 * You can overwrite the template file configuration in extensions and
415
		 * provide alternative templates. These alternative templates should be
416
		 * named like the default one but with the string "default" replaced by
417
		 * an unique name. You may use the name of your project for this. If
418
		 * you've implemented an alternative client class as well, "default"
419
		 * should be replaced by the name of the new class.
420
		 *
421
		 * @param string Relative path to the template creating the HTML code
422
		 * @since 2023.10
423
		 */
424
		$tplconf = 'admin/jqadm/basket/template-item';
425
		$default = 'basket/item';
426
427
		return $view->render( $view->config( $tplconf, $default ) );
428
	}
429
}
430