Passed
Push — master ( 7f8217...09e985 )
by Aimeos
04:00
created

Standard::getSubClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 35
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
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
	 */
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
		$codes = [];
70
71
		foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) {
72
			$codes[$code] = $view->translate( 'country', $code );
73
		}
74
75
		asort( $codes );
76
77
		$view->itemSubparts = $this->getSubClientNames();
78
		$view->itemGroups = $this->getGroupItems();
0 ignored issues
show
Bug introduced by
The method getGroupItems() does not exist on Aimeos\Admin\JQAdm\Customer\Standard. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

78
		/** @scrutinizer ignore-call */ 
79
  $view->itemGroups = $this->getGroupItems();
Loading history...
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
		$view = $this->view();
92
93
		if( !empty( $ids = $view->param( 'id' ) ) )
94
		{
95
			$context = $this->context();
96
			$manager = \Aimeos\MShop::create( $context, 'customer' );
97
			$filter = $manager->filter()->add( ['customer.id' => $ids] )->slice( 0, count( $ids ) );
98
			$items = $manager->search( $filter );
99
100
			$data = $view->param( 'item', [] );
101
102
			foreach( $items as $item )
103
			{
104
				if( $view->access( ['super', 'admin'] ) || $item->getId() === $context->user() )
105
				{
106
					!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
107
					!isset( $data['groups'] ) ?: $item->setGroups( array_filter( (array) $data['groups'] ) );
108
				}
109
110
				!isset( $data['customer.dateverified'] ) ?: $item->setDateVerified( $data['customer.dateverified'] );
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 templates/admin/jqadm).
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
		 */
358
		$tplconf = 'admin/jqadm/customer/template-list';
359
		$default = 'customer/list';
360
361
		return $view->render( $view->config( $tplconf, $default ) );
362
	}
363
364
365
	/**
366
	 * Returns the sub-client given by its name.
367
	 *
368
	 * @param string $type Name of the client type
369
	 * @param string|null $name Name of the sub-client (Default if null)
370
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
371
	 */
372
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
373
	{
374
		/** admin/jqadm/customer/decorators/excludes
375
		 * Excludes decorators added by the "common" option from the customer JQAdm client
376
		 *
377
		 * Decorators extend the functionality of a class by adding new aspects
378
		 * (e.g. log what is currently done), executing the methods of the underlying
379
		 * class only in certain conditions (e.g. only for logged in users) or
380
		 * modify what is returned to the caller.
381
		 *
382
		 * This option allows you to remove a decorator added via
383
		 * "client/jqadm/common/decorators/default" before they are wrapped
384
		 * around the JQAdm client.
385
		 *
386
		 *  admin/jqadm/customer/decorators/excludes = array( 'decorator1' )
387
		 *
388
		 * This would remove the decorator named "decorator1" from the list of
389
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
390
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
391
		 *
392
		 * @param array List of decorator names
393
		 * @since 2017.07
394
		 * @see admin/jqadm/common/decorators/default
395
		 * @see admin/jqadm/customer/decorators/global
396
		 * @see admin/jqadm/customer/decorators/local
397
		 */
398
399
		/** admin/jqadm/customer/decorators/global
400
		 * Adds a list of globally available decorators only to the customer JQAdm client
401
		 *
402
		 * Decorators extend the functionality of a class by adding new aspects
403
		 * (e.g. log what is currently done), executing the methods of the underlying
404
		 * class only in certain conditions (e.g. only for logged in users) or
405
		 * modify what is returned to the caller.
406
		 *
407
		 * This option allows you to wrap global decorators
408
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
409
		 *
410
		 *  admin/jqadm/customer/decorators/global = array( 'decorator1' )
411
		 *
412
		 * This would add the decorator named "decorator1" defined by
413
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
414
		 *
415
		 * @param array List of decorator names
416
		 * @since 2017.07
417
		 * @see admin/jqadm/common/decorators/default
418
		 * @see admin/jqadm/customer/decorators/excludes
419
		 * @see admin/jqadm/customer/decorators/local
420
		 */
421
422
		/** admin/jqadm/customer/decorators/local
423
		 * Adds a list of local decorators only to the customer JQAdm client
424
		 *
425
		 * Decorators extend the functionality of a class by adding new aspects
426
		 * (e.g. log what is currently done), executing the methods of the underlying
427
		 * class only in certain conditions (e.g. only for logged in users) or
428
		 * modify what is returned to the caller.
429
		 *
430
		 * This option allows you to wrap local decorators
431
		 * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client.
432
		 *
433
		 *  admin/jqadm/customer/decorators/local = array( 'decorator2' )
434
		 *
435
		 * This would add the decorator named "decorator2" defined by
436
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
437
		 *
438
		 * @param array List of decorator names
439
		 * @since 2017.07
440
		 * @see admin/jqadm/common/decorators/default
441
		 * @see admin/jqadm/customer/decorators/excludes
442
		 * @see admin/jqadm/customer/decorators/global
443
		 */
444
		return $this->createSubClient( 'customer/' . $type, $name );
445
	}
446
447
448
	/**
449
	 * Returns the domain names whose items should be fetched too
450
	 *
451
	 * @return string[] List of domain names
452
	 */
453
	protected function getDomains() : array
454
	{
455
		/** admin/jqadm/customer/domains
456
		 * List of domain items that should be fetched along with the customer
457
		 *
458
		 * If you need to display additional content, you can configure your own
459
		 * list of domains (attribute, media, price, customer, text, etc. are
460
		 * domains) whose items are fetched from the storage.
461
		 *
462
		 * @param array List of domain names
463
		 * @since 2017.07
464
		 */
465
		return $this->context()->config()->get( 'admin/jqadm/customer/domains', [] );
466
	}
467
468
469
	/**
470
	 * Returns the list of sub-client names configured for the client.
471
	 *
472
	 * @return array List of JQAdm client names
473
	 */
474
	protected function getSubClientNames() : array
475
	{
476
		/** admin/jqadm/customer/subparts
477
		 * List of JQAdm sub-clients rendered within the customer section
478
		 *
479
		 * The output of the frontend is composed of the code generated by the JQAdm
480
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
481
		 * that are responsible for rendering certain sub-parts of the output. The
482
		 * sub-clients can contain JQAdm clients themselves and therefore a
483
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
484
		 * the output that is placed inside the container of its parent.
485
		 *
486
		 * At first, always the JQAdm code generated by the parent is printed, then
487
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
488
		 * determines the order of the output of these sub-clients inside the parent
489
		 * container. If the configured list of clients is
490
		 *
491
		 *  array( "subclient1", "subclient2" )
492
		 *
493
		 * you can easily change the order of the output by reordering the subparts:
494
		 *
495
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
496
		 *
497
		 * You can also remove one or more parts if they shouldn't be rendered:
498
		 *
499
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
500
		 *
501
		 * As the clients only generates structural JQAdm, the layout defined via CSS
502
		 * should support adding, removing or reordering content by a fluid like
503
		 * design.
504
		 *
505
		 * @param array List of sub-client names
506
		 * @since 2017.07
507
		 */
508
		return $this->context()->config()->get( 'admin/jqadm/customer/subparts', [] );
509
	}
510
511
512
513
	/**
514
	 * Creates new and updates existing items using the data array
515
	 *
516
	 * @param array $data Data array
517
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
518
	 */
519
	protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface
520
	{
521
		$context = $this->context();
522
		$manager = \Aimeos\MShop::create( $context, 'customer' );
523
524
		if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) {
525
			$item = $manager->get( $data['customer.id'], $this->getDomains() );
526
		} else {
527
			$item = $manager->create();
528
		}
529
530
		$addr = $item->getPaymentAddress();
531
		$label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname();
532
		$label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' );
533
534
		$item->setLabel( $label )->setStatus( $data['customer.status'] ?? 0 )
535
			->setDateVerified( $data['customer.dateverified'] ?? null );
536
537
		if( $this->view()->access( ['super', 'admin'] ) ) {
538
			$item->setGroups( array_unique( $this->val( $data, 'groups', [] ) ) );
539
		}
540
541
		if( $this->view()->access( ['super', 'admin'] ) || $item->getId() === $context->user() )
542
		{
543
			!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
544
			!isset( $data['customer.code'] ) ?: $item->setCode( $data['customer.code'] );
545
		}
546
547
		return $item->fromArray( $data );
548
	}
549
550
551
	/**
552
	 * Constructs the data array for the view from the given item
553
	 *
554
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
555
	 * @return string[] Multi-dimensional associative list of item data
556
	 */
557
	protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array
558
	{
559
		$data = $item->toArray( true );
560
561
		if( $this->view()->access( ['super', 'admin'] )
562
			|| $this->view()->access( ['editor'] ) && $item->getId() === null
563
			|| $item->getId() === $this->context()->user()
564
		) {
565
			$data['.modify'] = true;
566
		}
567
568
		if( $copy === true )
569
		{
570
			$data['customer.siteid'] = $this->context()->locale()->getSiteId();
571
			$data['customer.code'] = '';
572
			$data['customer.id'] = '';
573
		}
574
575
		return $data;
576
	}
577
578
579
	/**
580
	 * Returns the rendered template including the view data
581
	 *
582
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
583
	 * @return string HTML output
584
	 */
585
	protected function render( \Aimeos\Base\View\Iface $view ) : string
586
	{
587
		/** admin/jqadm/customer/template-item
588
		 * Relative path to the HTML body template for the customer item.
589
		 *
590
		 * The template file contains the HTML code and processing instructions
591
		 * to generate the result shown in the body of the frontend. The
592
		 * configuration string is the path to the template file relative
593
		 * to the templates directory (usually in templates/admin/jqadm).
594
		 *
595
		 * You can overwrite the template file configuration in extensions and
596
		 * provide alternative templates. These alternative templates should be
597
		 * named like the default one but with the string "default" replaced by
598
		 * an unique name. You may use the name of your project for this. If
599
		 * you've implemented an alternative client class as well, "default"
600
		 * should be replaced by the name of the new class.
601
		 *
602
		 * @param string Relative path to the template creating the HTML code
603
		 * @since 2016.04
604
		 */
605
		$tplconf = 'admin/jqadm/customer/template-item';
606
		$default = 'customer/item';
607
608
		return $view->render( $view->config( $tplconf, $default ) );
609
	}
610
}
611