Standard::batch()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 2
nop 0
dl 0
loc 31
rs 8.0555
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), 2017-2025
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 customer panel 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
		$manager = \Aimeos\MShop::create( $this->context(), 'group' );
78
		$filter = $manager->filter()->slice( 0, 1000 );
79
80
		$view->itemSubparts = $this->getSubClientNames();
81
		$view->itemGroups = $manager->search( $filter );
82
		$view->countries = $codes;
83
84
		return $view;
85
	}
86
87
88
	/**
89
	 * Batch update of a resource
90
	 *
91
	 * @return string|null Output to display
92
	 */
93
	public function batch() : ?string
94
	{
95
		$view = $this->view();
96
97
		if( !empty( $ids = $view->param( 'id' ) ) )
98
		{
99
			$context = $this->context();
100
			$manager = \Aimeos\MShop::create( $context, 'customer' );
101
			$filter = $manager->filter()->add( ['customer.id' => $ids] )->slice( 0, count( $ids ) );
102
			$items = $manager->search( $filter );
103
104
			$data = $view->param( 'item', [] );
105
106
			foreach( $items as $item )
107
			{
108
				if( $view->access( ['super', 'admin'] ) || $item->getId() === $context->user() )
109
				{
110
					!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
111
					!isset( $data['groups'] ) ?: $item->setGroups( array_filter( (array) $data['groups'] ) );
112
				}
113
114
				!isset( $data['customer.dateverified'] ) ?: $item->setDateVerified( $data['customer.dateverified'] );
115
				!isset( $data['customer.status'] ) ?: $item->setStatus( $data['customer.status'] );
116
117
				$temp = $data; $item->fromArray( $temp );
118
			}
119
120
			$manager->save( $items );
121
		}
122
123
		return $this->redirect( 'customer', 'search', null, 'save' );
124
	}
125
126
127
	/**
128
	 * Copies a resource
129
	 *
130
	 * @return string|null HTML output
131
	 */
132
	public function copy() : ?string
133
	{
134
		$view = $this->object()->data( $this->view() );
135
136
		try
137
		{
138
			if( ( $id = $view->param( 'id' ) ) === null )
139
			{
140
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
141
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
142
			}
143
144
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
145
			$view->item = $manager->get( $id, $this->getDomains() );
146
147
			$view->itemData = $this->toArray( $view->item, true );
148
			$view->itemBody = parent::copy();
149
		}
150
		catch( \Exception $e )
151
		{
152
			$this->report( $e, 'copy' );
153
		}
154
155
		return $this->render( $view );
156
	}
157
158
159
	/**
160
	 * Creates a new resource
161
	 *
162
	 * @return string|null HTML output
163
	 */
164
	public function create() : ?string
165
	{
166
		$view = $this->object()->data( $this->view() );
167
168
		try
169
		{
170
			$data = $view->param( 'item', [] );
171
172
			if( !isset( $view->item ) ) {
173
				$view->item = \Aimeos\MShop::create( $this->context(), 'customer' )->create();
174
			}
175
176
			$data['customer.siteid'] = $view->item->getSiteId();
177
178
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
179
			$view->itemBody = parent::create();
180
		}
181
		catch( \Exception $e )
182
		{
183
			$this->report( $e, 'create' );
184
		}
185
186
		return $this->render( $view );
187
	}
188
189
190
	/**
191
	 * Deletes a resource
192
	 *
193
	 * @return string|null HTML output
194
	 */
195
	public function delete() : ?string
196
	{
197
		$view = $this->view();
198
		$context = $this->context();
199
200
		$manager = \Aimeos\MShop::create( $context, 'customer' );
201
		$manager->begin();
202
203
		try
204
		{
205
			if( ( $ids = $view->param( 'id' ) ) === null )
206
			{
207
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
208
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
209
			}
210
211
			if( !$view->access( ['super', 'admin'] ) )
212
			{
213
				$msg = $context->translate( 'admin', 'Only super users and administrators can delete entries' );
214
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
215
			}
216
217
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
218
			$search->add( $search->and( [
219
				$search->compare( '==', 'customer.id', $ids ),
220
				$search->compare( '!=', 'customer.siteid', '' )
221
			] ) );
222
223
			$items = $manager->search( $search, $this->getDomains() );
224
225
			foreach( $items as $item )
226
			{
227
				$view->item = $item;
228
				parent::delete();
229
			}
230
231
			$manager->delete( $items );
232
			$manager->commit();
233
234
			if( $items->count() !== count( (array) $ids ) )
235
			{
236
				$msg = $context->translate( 'admin', 'Not all entries could be deleted' );
237
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
238
			}
239
240
			return $this->redirect( 'customer', 'search', null, 'delete' );
241
		}
242
		catch( \Exception $e )
243
		{
244
			$manager->rollback();
245
			$this->report( $e, 'delete' );
246
		}
247
248
		return $this->search();
249
	}
250
251
252
	/**
253
	 * Returns a single resource
254
	 *
255
	 * @return string|null HTML output
256
	 */
257
	public function get() : ?string
258
	{
259
		$view = $this->object()->data( $this->view() );
260
261
		try
262
		{
263
			if( ( $id = $view->param( 'id' ) ) === null )
264
			{
265
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
266
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
267
			}
268
269
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
270
271
			$view->item = $manager->get( $id, $this->getDomains() );
272
			$view->itemData = $this->toArray( $view->item );
273
			$view->itemBody = parent::get();
274
		}
275
		catch( \Exception $e )
276
		{
277
			$this->report( $e, 'get' );
278
		}
279
280
		return $this->render( $view );
281
	}
282
283
284
	/**
285
	 * Imports a file
286
	 *
287
	 * @return string|null Output to display or redirect
288
	 */
289
	public function import() : ?string
290
	{
291
		$context = $this->context();
292
		$fs = $context->fs( 'fs-import' );
293
		$site = $context->locale()->getSiteItem()->getCode();
294
		$dir = $context->config()->get( 'controller/jobs/customer/import/csv/location', 'customer' );
295
296
		if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $dir . '/' . $site ) === false ) {
297
			$fs->mkdir( $dir . '/' . $site );
298
		}
299
300
		$uploads = (array) $this->view()->request()->getUploadedFiles();
301
		$files = $this->val( $uploads, 'import', [] );
302
303
		foreach( is_array( $files ) ? $files : [$files] as $idx => $file )
304
		{
305
			$filename = date( 'YmdHis' ) . '_' . str_pad( $idx, 3, '0', STR_PAD_LEFT ) . '_' . substr( md5( microtime( true ) ), 0, 4 ) . '.csv';
306
			$fs->writes( $dir . '/' . $site . '/' . $filename, $file->getStream()->detach() );
307
		}
308
309
		return $this->redirect( 'customer', 'search', null, 'upload' );
310
	}
311
312
313
	/**
314
	 * Saves the data
315
	 *
316
	 * @return string|null HTML output
317
	 */
318
	public function save() : ?string
319
	{
320
		$view = $this->view();
321
322
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
323
		$manager->begin();
324
325
		try
326
		{
327
			$item = $this->fromArray( $view->param( 'item', [] ) );
328
			$view->item = $item->getId() ? $item : $manager->save( $item );
329
			$view->itemBody = parent::save();
330
331
			$manager->save( clone $view->item );
332
			$manager->commit();
333
334
			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

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