Passed
Push — master ( 51a54a...75bbef )
by Aimeos
03:47
created

Standard::copy()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 8
nop 0
dl 0
loc 24
rs 9.8666
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-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Customer;
12
13
sprintf( 'users' ); // for translation
14
sprintf( 'customer' ); // for translation
15
16
17
/**
18
 * Default implementation of customer 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/customer/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\Customer\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Customer\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/customer/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 2016.01
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->itemGroups = $this->getGroupItems();
80
		$view->countries = $codes;
81
		return $view;
82
	}
83
84
85
	/**
86
	 * Batch update of a resource
87
	 *
88
	 * @return string|null Output to display
89
	 */
90
	public function batch() : ?string
91
	{
92
		$view = $this->view();
93
94
		if( !empty( $ids = $view->param( 'id' ) ) )
95
		{
96
			$context = $this->context();
97
			$manager = \Aimeos\MShop::create( $context, 'customer' );
98
			$filter = $manager->filter()->add( ['customer.id' => $ids] )->slice( 0, count( $ids ) );
99
			$items = $manager->search( $filter );
100
101
			$data = $view->param( 'item', [] );
102
103
			foreach( $items as $item )
104
			{
105
				if( $view->access( ['super', 'admin'] ) || $item->getId() === $context->user() )
106
				{
107
					!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
108
					!isset( $data['customer.groups'] ) ?: $item->setGroups( (array) $data['customer.groups'] );
109
				}
110
111
				!isset( $data['customer.status'] ) ?: $item->setStatus( $data['customer.status'] );
112
113
				$temp = $data; $item->fromArray( $temp );
114
			}
115
116
			$manager->save( $items );
117
		}
118
119
		return $this->redirect( 'customer', 'search', null, 'save' );
120
	}
121
122
123
	/**
124
	 * Copies a resource
125
	 *
126
	 * @return string|null HTML output
127
	 */
128
	public function copy() : ?string
129
	{
130
		$view = $this->object()->data( $this->view() );
131
132
		try
133
		{
134
			if( ( $id = $view->param( 'id' ) ) === null )
135
			{
136
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
137
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
138
			}
139
140
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
141
			$view->item = $manager->get( $id, $this->getDomains() );
142
143
			$view->itemData = $this->toArray( $view->item, true );
144
			$view->itemBody = parent::copy();
145
		}
146
		catch( \Exception $e )
147
		{
148
			$this->report( $e, 'copy' );
149
		}
150
151
		return $this->render( $view );
152
	}
153
154
155
	/**
156
	 * Creates a new resource
157
	 *
158
	 * @return string|null HTML output
159
	 */
160
	public function create() : ?string
161
	{
162
		$view = $this->object()->data( $this->view() );
163
164
		try
165
		{
166
			$data = $view->param( 'item', [] );
167
168
			if( !isset( $view->item ) ) {
169
				$view->item = \Aimeos\MShop::create( $this->context(), 'customer' )->create();
170
			}
171
172
			$data['customer.siteid'] = $view->item->getSiteId();
173
174
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
175
			$view->itemBody = parent::create();
176
		}
177
		catch( \Exception $e )
178
		{
179
			$this->report( $e, 'create' );
180
		}
181
182
		return $this->render( $view );
183
	}
184
185
186
	/**
187
	 * Deletes a resource
188
	 *
189
	 * @return string|null HTML output
190
	 */
191
	public function delete() : ?string
192
	{
193
		$view = $this->view();
194
		$context = $this->context();
195
196
		$manager = \Aimeos\MShop::create( $context, 'customer' );
197
		$manager->begin();
198
199
		try
200
		{
201
			if( ( $ids = $view->param( 'id' ) ) === null )
202
			{
203
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
204
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
205
			}
206
207
			if( !$view->access( ['super', 'admin'] ) )
208
			{
209
				$msg = $context->translate( 'admin', 'Only super users and administrators can delete items' );
210
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
211
			}
212
213
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
214
			$search->add( $search->and( [
215
				$search->compare( '==', 'customer.id', $ids ),
216
				$search->compare( '!=', 'customer.siteid', '' )
217
			] ) );
218
219
			$items = $manager->search( $search, $this->getDomains() );
220
221
			foreach( $items as $item )
222
			{
223
				$view->item = $item;
224
				parent::delete();
225
			}
226
227
			$manager->delete( $items->toArray() );
228
			$manager->commit();
229
230
			if( $items->count() !== count( (array) $ids ) )
231
			{
232
				$msg = $context->translate( 'admin', 'Not all items could be deleted' );
233
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
234
			}
235
236
			return $this->redirect( 'customer', 'search', null, 'delete' );
237
		}
238
		catch( \Exception $e )
239
		{
240
			$manager->rollback();
241
			$this->report( $e, 'delete' );
242
		}
243
244
		return $this->search();
245
	}
246
247
248
	/**
249
	 * Returns a single resource
250
	 *
251
	 * @return string|null HTML output
252
	 */
253
	public function get() : ?string
254
	{
255
		$view = $this->object()->data( $this->view() );
256
257
		try
258
		{
259
			if( ( $id = $view->param( 'id' ) ) === null )
260
			{
261
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
262
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
263
			}
264
265
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
266
267
			$view->item = $manager->get( $id, $this->getDomains() );
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(), 'customer' );
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( 'customer', $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( 'customer', $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->object()->data( $this->view() );
321
322
		try
323
		{
324
			$total = 0;
325
			$params = $this->storeFilter( $view->param(), 'customer' );
326
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
327
			$search = $this->initCriteria( $manager->filter(), $params );
328
329
			$view->items = $manager->search( $search, $this->getDomains(), $total );
330
			$view->filterAttributes = $manager->getSearchAttributes( true );
331
			$view->filterOperators = $search->getOperators();
332
			$view->itemBody = parent::search();
333
			$view->total = $total;
334
		}
335
		catch( \Exception $e )
336
		{
337
			$this->report( $e, 'search' );
338
		}
339
340
		/** admin/jqadm/customer/template-list
341
		 * Relative path to the HTML body template for the customer list.
342
		 *
343
		 * The template file contains the HTML code and processing instructions
344
		 * to generate the result shown in the body of the frontend. The
345
		 * configuration string is the path to the template file relative
346
		 * to the templates directory (usually in admin/jqadm/templates).
347
		 *
348
		 * You can overwrite the template file configuration in extensions and
349
		 * provide alternative templates. These alternative templates should be
350
		 * named like the default one but with the string "default" replaced by
351
		 * an unique name. You may use the name of your project for this. If
352
		 * you've implemented an alternative client class as well, "default"
353
		 * should be replaced by the name of the new class.
354
		 *
355
		 * @param string Relative path to the template creating the HTML code
356
		 * @since 2016.04
357
		 * @category Developer
358
		 */
359
		$tplconf = 'admin/jqadm/customer/template-list';
360
		$default = 'customer/list';
361
362
		return $view->render( $view->config( $tplconf, $default ) );
363
	}
364
365
366
	/**
367
	 * Returns the sub-client given by its name.
368
	 *
369
	 * @param string $type Name of the client type
370
	 * @param string|null $name Name of the sub-client (Default if null)
371
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
372
	 */
373
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
374
	{
375
		/** admin/jqadm/customer/decorators/excludes
376
		 * Excludes decorators added by the "common" option from the customer JQAdm client
377
		 *
378
		 * Decorators extend the functionality of a class by adding new aspects
379
		 * (e.g. log what is currently done), executing the methods of the underlying
380
		 * class only in certain conditions (e.g. only for logged in users) or
381
		 * modify what is returned to the caller.
382
		 *
383
		 * This option allows you to remove a decorator added via
384
		 * "client/jqadm/common/decorators/default" before they are wrapped
385
		 * around the JQAdm client.
386
		 *
387
		 *  admin/jqadm/customer/decorators/excludes = array( 'decorator1' )
388
		 *
389
		 * This would remove the decorator named "decorator1" from the list of
390
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
391
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
392
		 *
393
		 * @param array List of decorator names
394
		 * @since 2017.07
395
		 * @category Developer
396
		 * @see admin/jqadm/common/decorators/default
397
		 * @see admin/jqadm/customer/decorators/global
398
		 * @see admin/jqadm/customer/decorators/local
399
		 */
400
401
		/** admin/jqadm/customer/decorators/global
402
		 * Adds a list of globally available decorators only to the customer JQAdm client
403
		 *
404
		 * Decorators extend the functionality of a class by adding new aspects
405
		 * (e.g. log what is currently done), executing the methods of the underlying
406
		 * class only in certain conditions (e.g. only for logged in users) or
407
		 * modify what is returned to the caller.
408
		 *
409
		 * This option allows you to wrap global decorators
410
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
411
		 *
412
		 *  admin/jqadm/customer/decorators/global = array( 'decorator1' )
413
		 *
414
		 * This would add the decorator named "decorator1" defined by
415
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
416
		 *
417
		 * @param array List of decorator names
418
		 * @since 2017.07
419
		 * @category Developer
420
		 * @see admin/jqadm/common/decorators/default
421
		 * @see admin/jqadm/customer/decorators/excludes
422
		 * @see admin/jqadm/customer/decorators/local
423
		 */
424
425
		/** admin/jqadm/customer/decorators/local
426
		 * Adds a list of local decorators only to the customer JQAdm client
427
		 *
428
		 * Decorators extend the functionality of a class by adding new aspects
429
		 * (e.g. log what is currently done), executing the methods of the underlying
430
		 * class only in certain conditions (e.g. only for logged in users) or
431
		 * modify what is returned to the caller.
432
		 *
433
		 * This option allows you to wrap local decorators
434
		 * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client.
435
		 *
436
		 *  admin/jqadm/customer/decorators/local = array( 'decorator2' )
437
		 *
438
		 * This would add the decorator named "decorator2" defined by
439
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
440
		 *
441
		 * @param array List of decorator names
442
		 * @since 2017.07
443
		 * @category Developer
444
		 * @see admin/jqadm/common/decorators/default
445
		 * @see admin/jqadm/customer/decorators/excludes
446
		 * @see admin/jqadm/customer/decorators/global
447
		 */
448
		return $this->createSubClient( 'customer/' . $type, $name );
449
	}
450
451
452
	/**
453
	 * Returns the domain names whose items should be fetched too
454
	 *
455
	 * @return string[] List of domain names
456
	 */
457
	protected function getDomains() : array
458
	{
459
		/** admin/jqadm/customer/domains
460
		 * List of domain items that should be fetched along with the customer
461
		 *
462
		 * If you need to display additional content, you can configure your own
463
		 * list of domains (attribute, media, price, customer, text, etc. are
464
		 * domains) whose items are fetched from the storage.
465
		 *
466
		 * @param array List of domain names
467
		 * @since 2017.07
468
		 * @category Developer
469
		 */
470
		return $this->context()->config()->get( 'admin/jqadm/customer/domains', [] );
471
	}
472
473
474
	/**
475
	 * Returns the available group items
476
	 *
477
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $item Customer item that should be updated
478
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of group IDs as keys and group items as values
479
	 */
480
	protected function getGroupItems( \Aimeos\MShop\Customer\Item\Iface $item = null ) : array
481
	{
482
		$list = [];
483
		$view = $this->view();
484
		$context = $this->context();
485
486
		$isSuper = $view->access( ['super'] );
487
		$isAdmin = $view->access( ['admin'] );
488
		$isEditor = $view->access( ['editor'] );
489
490
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
491
		$search = $manager->filter( true )->slice( 0, 10000 )->order( 'customer.group.label' );
492
493
		foreach( $manager->search( $search ) as $groupId => $groupItem )
494
		{
495
			if( !$isSuper && $groupItem->getCode() === 'super' ) {
496
				continue;
497
			}
498
499
			if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'admin' ) {
500
				continue;
501
			}
502
503
			if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'editor'
504
				&& ( !$isEditor || $item === null || (string) $context->user() !== (string) $item->getId() )
505
			) {
506
				continue;
507
			}
508
509
			$list[$groupId] = $groupItem;
510
		}
511
512
		return $list;
513
	}
514
515
516
	/**
517
	 * Returns the list of sub-client names configured for the client.
518
	 *
519
	 * @return array List of JQAdm client names
520
	 */
521
	protected function getSubClientNames() : array
522
	{
523
		/** admin/jqadm/customer/subparts
524
		 * List of JQAdm sub-clients rendered within the customer section
525
		 *
526
		 * The output of the frontend is composed of the code generated by the JQAdm
527
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
528
		 * that are responsible for rendering certain sub-parts of the output. The
529
		 * sub-clients can contain JQAdm clients themselves and therefore a
530
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
531
		 * the output that is placed inside the container of its parent.
532
		 *
533
		 * At first, always the JQAdm code generated by the parent is printed, then
534
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
535
		 * determines the order of the output of these sub-clients inside the parent
536
		 * container. If the configured list of clients is
537
		 *
538
		 *  array( "subclient1", "subclient2" )
539
		 *
540
		 * you can easily change the order of the output by reordering the subparts:
541
		 *
542
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
543
		 *
544
		 * You can also remove one or more parts if they shouldn't be rendered:
545
		 *
546
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
547
		 *
548
		 * As the clients only generates structural JQAdm, the layout defined via CSS
549
		 * should support adding, removing or reordering content by a fluid like
550
		 * design.
551
		 *
552
		 * @param array List of sub-client names
553
		 * @since 2017.07
554
		 * @category Developer
555
		 */
556
		return $this->context()->config()->get( 'admin/jqadm/customer/subparts', [] );
557
	}
558
559
560
561
	/**
562
	 * Creates new and updates existing items using the data array
563
	 *
564
	 * @param array $data Data array
565
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
566
	 */
567
	protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface
568
	{
569
		$context = $this->context();
570
		$manager = \Aimeos\MShop::create( $context, 'customer' );
571
572
		if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) {
573
			$item = $manager->get( $data['customer.id'], $this->getDomains() );
574
		} else {
575
			$item = $manager->create();
576
		}
577
578
		$addr = $item->getPaymentAddress();
579
		$label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname();
580
		$label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' );
581
582
		$item->setLabel( $label )->setStatus( $data['customer.status'] ?? 0 );
583
584
		if( $this->view()->access( ['super', 'admin'] ) )
585
		{
586
			$groupIds = $this->val( $data, 'customer.groups', [] );
587
			$gids = array_keys( $this->getGroupItems( $item ) );
588
589
			$item->setGroups( array_intersect( $gids, $groupIds ) );
590
		}
591
592
		if( $this->view()->access( ['super', 'admin'] ) || $item->getId() === $context->user() )
593
		{
594
			!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
595
			!isset( $data['customer.code'] ) ?: $item->setCode( $data['customer.code'] );
596
		}
597
598
		return $item->fromArray( $data );
599
	}
600
601
602
	/**
603
	 * Constructs the data array for the view from the given item
604
	 *
605
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
606
	 * @return string[] Multi-dimensional associative list of item data
607
	 */
608
	protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array
609
	{
610
		$data = $item->toArray( true );
611
612
		if( $this->view()->access( ['super'] ) || $item->getId() === $this->context()->user() ) {
613
			$data['.modify'] = true;
614
		}
615
616
		if( $copy === true )
617
		{
618
			$data['customer.siteid'] = $this->context()->locale()->getSiteId();
619
			$data['customer.code'] = '';
620
			$data['customer.id'] = '';
621
		}
622
623
		return $data;
624
	}
625
626
627
	/**
628
	 * Returns the rendered template including the view data
629
	 *
630
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
631
	 * @return string HTML output
632
	 */
633
	protected function render( \Aimeos\Base\View\Iface $view ) : string
634
	{
635
		/** admin/jqadm/customer/template-item
636
		 * Relative path to the HTML body template for the customer item.
637
		 *
638
		 * The template file contains the HTML code and processing instructions
639
		 * to generate the result shown in the body of the frontend. The
640
		 * configuration string is the path to the template file relative
641
		 * to the templates directory (usually in admin/jqadm/templates).
642
		 *
643
		 * You can overwrite the template file configuration in extensions and
644
		 * provide alternative templates. These alternative templates should be
645
		 * named like the default one but with the string "default" replaced by
646
		 * an unique name. You may use the name of your project for this. If
647
		 * you've implemented an alternative client class as well, "default"
648
		 * should be replaced by the name of the new class.
649
		 *
650
		 * @param string Relative path to the template creating the HTML code
651
		 * @since 2016.04
652
		 * @category Developer
653
		 */
654
		$tplconf = 'admin/jqadm/customer/template-item';
655
		$default = 'customer/item';
656
657
		return $view->render( $view->config( $tplconf, $default ) );
658
	}
659
}
660