Passed
Push — master ( 556742...733ade )
by Aimeos
04:56
created

Standard::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 10
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), 2018-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Subscription;
12
13
sprintf( 'sales' ); // for translation
14
sprintf( 'subscription' ); // for translation
15
16
17
/**
18
 * Default implementation of subscription 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/subscription/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/subscription/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 2018.04
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
		$view->itemSubparts = $this->getSubClientNames();
71
		return $view;
72
	}
73
74
75
	/**
76
	 * Batch update of a resource
77
	 *
78
	 * @return string|null Output to display
79
	 */
80
	public function batch() : ?string
81
	{
82
		return $this->batchBase( 'subscription' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('subscription') 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...
83
	}
84
85
86
	/**
87
	 * Copies a resource
88
	 *
89
	 * @return string|null HTML output
90
	 */
91
	public function copy() : ?string
92
	{
93
		$view = $this->object()->data( $this->view() );
94
		$context = $this->context();
95
96
		try
97
		{
98
			if( ( $id = $view->param( 'id' ) ) === null )
99
			{
100
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
101
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
102
			}
103
104
			$manager = \Aimeos\MShop::create( $context, 'subscription' );
105
			$baseManager = \Aimeos\MShop::create( $context, 'order/base' );
106
107
			$view->item = $manager->get( $id );
108
			$view->itemBase = $baseManager->get( $view->item->getOrderBaseId(), ['order/base/address', 'order/base/product'] );
109
			$view->itemData = $this->toArray( $view->item, true );
110
			$view->itemBody = parent::copy();
111
		}
112
		catch( \Exception $e )
113
		{
114
			$this->report( $e, 'copy' );
115
		}
116
117
		return $this->render( $view );
118
	}
119
120
121
	/**
122
	 * Creates a new resource
123
	 *
124
	 * @return string|null HTML output
125
	 */
126
	public function create() : ?string
127
	{
128
		$view = $this->object()->data( $this->view() );
129
		$context = $this->context();
130
131
		try
132
		{
133
			$data = $view->param( 'item', [] );
134
135
			if( !isset( $view->item ) ) {
136
				$view->item = \Aimeos\MShop::create( $context, 'subscription' )->create();
137
			}
138
139
			$baseManager = \Aimeos\MShop::create( $context, 'order/base' );
140
			$baseId = ( $view->item->getOrderBaseId() ?: $view->param( 'item/subscription.ordbaseid' ) );
141
142
			if( $baseId ) {
143
				$view->itemBase = $baseManager->get( $baseId, ['order/base/address', 'order/base/product'] );
144
			} else {
145
				$view->itemBase = $baseManager->create();
146
			}
147
148
			$data['subscription.siteid'] = $view->item->getSiteId();
149
150
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
151
			$view->itemBody = parent::create();
152
		}
153
		catch( \Exception $e )
154
		{
155
			$this->report( $e, 'create' );
156
		}
157
158
		return $this->render( $view );
159
	}
160
161
162
	/**
163
	 * Deletes a resource
164
	 *
165
	 * @return string|null HTML output
166
	 */
167
	public function delete() : ?string
168
	{
169
		$view = $this->view();
170
171
		$manager = \Aimeos\MShop::create( $this->context(), 'subscription' );
172
		$manager->begin();
173
174
		try
175
		{
176
			if( ( $ids = $view->param( 'id' ) ) === null )
177
			{
178
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
179
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
180
			}
181
182
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
183
			$search->setConditions( $search->compare( '==', 'subscription.id', $ids ) );
184
			$items = $manager->search( $search );
185
186
			foreach( $items as $item )
187
			{
188
				$view->item = $item;
189
				parent::delete();
190
			}
191
192
			$manager->delete( $items->toArray() );
193
			$manager->commit();
194
195
			return $this->redirect( 'subscription', 'search', null, 'delete' );
196
		}
197
		catch( \Exception $e )
198
		{
199
			$manager->rollback();
200
			$this->report( $e, 'delete' );
201
		}
202
203
		return $this->search();
204
	}
205
206
207
	/**
208
	 * Exports a resource
209
	 *
210
	 * @return string Admin output to display
211
	 */
212
	public function export() : ?string
213
	{
214
		$view = $this->view();
215
		$context = $this->context();
216
217
		try
218
		{
219
			$params = $this->storeFilter( $view->param(), 'subscription' );
220
			$msg = ['sitecode' => $context->locale()->getSiteItem()->getCode()];
221
222
			if( isset( $params['filter'] ) ) {
223
				$msg['filter'] = $this->getCriteriaConditions( (array) $params['filter'] );
224
			}
225
226
			if( isset( $params['sort'] ) ) {
227
				$msg['sort'] = (array) $params['sort'];
228
			}
229
230
			$mq = $context->queue( 'mq-admin', 'subscription-export' );
231
			$mq->add( json_encode( $msg ) );
232
233
			$msg = $context->translate( 'admin', 'Your export will be available in a few minutes for download' );
234
			$view->info = $view->get( 'info', [] ) + ['subscription-item' => $msg];
235
		}
236
		catch( \Exception $e )
237
		{
238
			$this->report( $e, 'export' );
239
		}
240
241
		return $this->search();
242
	}
243
244
245
	/**
246
	 * Returns a single resource
247
	 *
248
	 * @return string|null HTML output
249
	 */
250
	public function get() : ?string
251
	{
252
		$view = $this->object()->data( $this->view() );
253
		$context = $this->context();
254
255
		try
256
		{
257
			if( ( $id = $view->param( 'id' ) ) === null )
258
			{
259
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
260
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
261
			}
262
263
			$manager = \Aimeos\MShop::create( $context, 'subscription' );
264
			$baseManager = \Aimeos\MShop::create( $context, 'order/base' );
265
266
			$view->item = $manager->get( $id );
267
			$view->itemBase = $baseManager->get( $view->item->getOrderBaseId(), ['order/base/address', 'order/base/product'] );
268
			$view->itemData = $this->toArray( $view->item );
269
			$view->itemBody = parent::get();
270
		}
271
		catch( \Exception $e )
272
		{
273
			$this->report( $e, 'get' );
274
		}
275
276
		return $this->render( $view );
277
	}
278
279
280
	/**
281
	 * Saves the data
282
	 *
283
	 * @return string|null HTML output
284
	 */
285
	public function save() : ?string
286
	{
287
		$view = $this->view();
288
289
		$manager = \Aimeos\MShop::create( $this->context(), 'subscription' );
290
		$manager->begin();
291
292
		try
293
		{
294
			$item = $this->fromArray( $view->param( 'item', [] ) );
295
			$view->item = $item->getId() ? $item : $manager->save( $item );
296
			$view->itemBody = parent::save();
297
298
			$manager->save( clone $view->item );
299
			$manager->commit();
300
301
			return $this->redirect( 'subscription', $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

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