Passed
Push — master ( 1ab0ac...0f6196 )
by Aimeos
03:16
created

Standard::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 25
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\Group;
12
13
sprintf( 'users' ); // for translation
14
sprintf( 'group' ); // for translation
15
16
17
/**
18
 * Default implementation of group 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/group/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\Group\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Group\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/group/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.07
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( 'customer/group', 'group' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('customer/group', 'group') 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
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( $this->context(), 'customer/group' );
104
			$view->item = $manager->get( $id );
105
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
	 * Creates a new resource
120
	 *
121
	 * @return string|null HTML output
122
	 */
123
	public function create() : ?string
124
	{
125
		$view = $this->object()->data( $this->view() );
126
127
		try
128
		{
129
			$data = $view->param( 'item', [] );
130
131
			if( !isset( $view->item ) ) {
132
				$view->item = \Aimeos\MShop::create( $this->context(), 'customer/group' )->create();
133
			}
134
135
			$data['customer.group.siteid'] = $view->item->getSiteId();
136
137
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
138
			$view->itemBody = parent::create();
139
		}
140
		catch( \Exception $e )
141
		{
142
			$this->report( $e, 'create' );
143
		}
144
145
		return $this->render( $view );
146
	}
147
148
149
	/**
150
	 * Deletes a resource
151
	 *
152
	 * @return string|null HTML output
153
	 */
154
	public function delete() : ?string
155
	{
156
		$view = $this->view();
157
158
		$manager = \Aimeos\MShop::create( $this->context(), 'customer/group' );
159
		$manager->begin();
160
161
		try
162
		{
163
			if( ( $ids = $view->param( 'id' ) ) === null )
164
			{
165
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
166
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
167
			}
168
169
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
170
			$search->setConditions( $search->compare( '==', 'customer.group.id', $ids ) );
171
			$items = $manager->search( $search );
172
173
			foreach( $items as $item )
174
			{
175
				$view->item = $item;
176
				parent::delete();
177
			}
178
179
			$manager->delete( $items->toArray() );
180
			$manager->commit();
181
182
			return $this->redirect( 'group', 'search', null, 'delete' );
183
		}
184
		catch( \Exception $e )
185
		{
186
			$manager->rollback();
187
			$this->report( $e, 'delete' );
188
		}
189
190
		return $this->search();
191
	}
192
193
194
	/**
195
	 * Returns a single resource
196
	 *
197
	 * @return string|null HTML output
198
	 */
199
	public function get() : ?string
200
	{
201
		$view = $this->object()->data( $this->view() );
202
203
		try
204
		{
205
			if( ( $id = $view->param( 'id' ) ) === null )
206
			{
207
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
208
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
209
			}
210
211
			$manager = \Aimeos\MShop::create( $this->context(), 'customer/group' );
212
213
			$view->item = $manager->get( $id );
214
			$view->itemData = $this->toArray( $view->item );
215
			$view->itemBody = parent::get();
216
		}
217
		catch( \Exception $e )
218
		{
219
			$this->report( $e, 'get' );
220
		}
221
222
		return $this->render( $view );
223
	}
224
225
226
	/**
227
	 * Saves the data
228
	 *
229
	 * @return string|null HTML output
230
	 */
231
	public function save() : ?string
232
	{
233
		$view = $this->view();
234
235
		$manager = \Aimeos\MShop::create( $this->context(), 'customer/group' );
236
		$manager->begin();
237
238
		try
239
		{
240
			$item = $this->fromArray( $view->param( 'item', [] ) );
241
			$view->item = $item->getId() ? $item : $manager->save( $item );
242
			$view->itemBody = parent::save();
243
244
			$manager->save( clone $view->item );
245
			$manager->commit();
246
247
			return $this->redirect( 'group', $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

247
			return $this->redirect( 'group', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
248
		}
249
		catch( \Exception $e )
250
		{
251
			$manager->rollback();
252
			$this->report( $e, 'save' );
253
		}
254
255
		return $this->create();
256
	}
257
258
259
	/**
260
	 * Returns a list of resource according to the conditions
261
	 *
262
	 * @return string|null HTML output
263
	 */
264
	public function search() : ?string
265
	{
266
		$view = $this->view();
267
268
		try
269
		{
270
			$total = 0;
271
			$params = $this->storeFilter( $view->param(), 'group' );
272
			$manager = \Aimeos\MShop::create( $this->context(), 'customer/group' );
273
			$search = $this->initCriteria( $manager->filter(), $params );
274
275
			$view->items = $manager->search( $search, [], $total );
276
			$view->filterAttributes = $manager->getSearchAttributes( true );
277
			$view->filterOperators = $search->getOperators();
278
			$view->itemBody = parent::search();
279
			$view->total = $total;
280
		}
281
		catch( \Exception $e )
282
		{
283
			$this->report( $e, 'search' );
284
		}
285
286
		/** admin/jqadm/group/template-list
287
		 * Relative path to the HTML body template for the group list.
288
		 *
289
		 * The template file contains the HTML code and processing instructions
290
		 * to generate the result shown in the body of the frontend. The
291
		 * configuration string is the path to the template file relative
292
		 * to the templates directory (usually in admin/jqadm/templates).
293
		 *
294
		 * You can overwrite the template file configuration in extensions and
295
		 * provide alternative templates. These alternative templates should be
296
		 * named like the default one but with the string "default" replaced by
297
		 * an unique name. You may use the name of your project for this. If
298
		 * you've implemented an alternative client class as well, "default"
299
		 * should be replaced by the name of the new class.
300
		 *
301
		 * @param string Relative path to the template creating the HTML code
302
		 * @since 2018.07
303
		 * @category Developer
304
		 */
305
		$tplconf = 'admin/jqadm/group/template-list';
306
		$default = 'group/list';
307
308
		return $view->render( $view->config( $tplconf, $default ) );
309
	}
310
311
312
	/**
313
	 * Returns the sub-client given by its name.
314
	 *
315
	 * @param string $type Name of the client type
316
	 * @param string|null $name Name of the sub-client (Default if null)
317
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
318
	 */
319
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
320
	{
321
		/** admin/jqadm/group/decorators/excludes
322
		 * Excludes decorators added by the "common" option from the group JQAdm client
323
		 *
324
		 * Decorators extend the functionality of a class by adding new aspects
325
		 * (e.g. log what is currently done), executing the methods of the underlying
326
		 * class only in certain conditions (e.g. only for logged in users) or
327
		 * modify what is returned to the caller.
328
		 *
329
		 * This option allows you to remove a decorator added via
330
		 * "client/jqadm/common/decorators/default" before they are wrapped
331
		 * around the JQAdm client.
332
		 *
333
		 *  admin/jqadm/group/decorators/excludes = array( 'decorator1' )
334
		 *
335
		 * This would remove the decorator named "decorator1" from the list of
336
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
337
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
338
		 *
339
		 * @param array List of decorator names
340
		 * @since 2018.07
341
		 * @category Developer
342
		 * @see admin/jqadm/common/decorators/default
343
		 * @see admin/jqadm/group/decorators/global
344
		 * @see admin/jqadm/group/decorators/local
345
		 */
346
347
		/** admin/jqadm/group/decorators/global
348
		 * Adds a list of globally available decorators only to the group JQAdm client
349
		 *
350
		 * Decorators extend the functionality of a class by adding new aspects
351
		 * (e.g. log what is currently done), executing the methods of the underlying
352
		 * class only in certain conditions (e.g. only for logged in users) or
353
		 * modify what is returned to the caller.
354
		 *
355
		 * This option allows you to wrap global decorators
356
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
357
		 *
358
		 *  admin/jqadm/group/decorators/global = array( 'decorator1' )
359
		 *
360
		 * This would add the decorator named "decorator1" defined by
361
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
362
		 *
363
		 * @param array List of decorator names
364
		 * @since 2018.07
365
		 * @category Developer
366
		 * @see admin/jqadm/common/decorators/default
367
		 * @see admin/jqadm/group/decorators/excludes
368
		 * @see admin/jqadm/group/decorators/local
369
		 */
370
371
		/** admin/jqadm/group/decorators/local
372
		 * Adds a list of local decorators only to the group JQAdm client
373
		 *
374
		 * Decorators extend the functionality of a class by adding new aspects
375
		 * (e.g. log what is currently done), executing the methods of the underlying
376
		 * class only in certain conditions (e.g. only for logged in users) or
377
		 * modify what is returned to the caller.
378
		 *
379
		 * This option allows you to wrap local decorators
380
		 * ("\Aimeos\Admin\JQAdm\Group\Decorator\*") around the JQAdm client.
381
		 *
382
		 *  admin/jqadm/group/decorators/local = array( 'decorator2' )
383
		 *
384
		 * This would add the decorator named "decorator2" defined by
385
		 * "\Aimeos\Admin\JQAdm\Group\Decorator\Decorator2" only to the JQAdm client.
386
		 *
387
		 * @param array List of decorator names
388
		 * @since 2018.07
389
		 * @category Developer
390
		 * @see admin/jqadm/common/decorators/default
391
		 * @see admin/jqadm/group/decorators/excludes
392
		 * @see admin/jqadm/group/decorators/global
393
		 */
394
		return $this->createSubClient( 'group/' . $type, $name );
395
	}
396
397
398
	/**
399
	 * Returns the list of sub-client names configured for the client.
400
	 *
401
	 * @return array List of JQAdm client names
402
	 */
403
	protected function getSubClientNames() : array
404
	{
405
		/** admin/jqadm/group/subparts
406
		 * List of JQAdm sub-clients rendered within the group section
407
		 *
408
		 * The output of the frontend is composed of the code generated by the JQAdm
409
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
410
		 * that are responsible for rendering certain sub-parts of the output. The
411
		 * sub-clients can contain JQAdm clients themselves and therefore a
412
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
413
		 * the output that is placed inside the container of its parent.
414
		 *
415
		 * At first, always the JQAdm code generated by the parent is printed, then
416
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
417
		 * determines the order of the output of these sub-clients inside the parent
418
		 * container. If the configured list of clients is
419
		 *
420
		 *  array( "subclient1", "subclient2" )
421
		 *
422
		 * you can easily change the order of the output by reordering the subparts:
423
		 *
424
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
425
		 *
426
		 * You can also remove one or more parts if they shouldn't be rendered:
427
		 *
428
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
429
		 *
430
		 * As the clients only generates structural JQAdm, the layout defined via CSS
431
		 * should support adding, removing or reordering content by a fluid like
432
		 * design.
433
		 *
434
		 * @param array List of sub-client names
435
		 * @since 2018.07
436
		 * @category Developer
437
		 */
438
		return $this->context()->config()->get( 'admin/jqadm/group/subparts', [] );
439
	}
440
441
442
443
	/**
444
	 * Creates new and updates existing items using the data array
445
	 *
446
	 * @param array $data Data array
447
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface New group item object
448
	 */
449
	protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Group\Iface
450
	{
451
		$manager = \Aimeos\MShop::create( $this->context(), 'customer/group' );
452
453
		if( isset( $data['customer.group.id'] ) && $data['customer.group.id'] != '' ) {
454
			$item = $manager->get( $data['customer.group.id'] );
455
		} else {
456
			$item = $manager->create();
457
		}
458
459
		$item->fromArray( $data, true );
460
461
		return $item;
462
	}
463
464
465
	/**
466
	 * Constructs the data array for the view from the given item
467
	 *
468
	 * @param \Aimeos\MShop\Customer\Item\Group\Iface $item Group item object
469
	 * @return string[] Multi-dimensional associative list of item data
470
	 */
471
	protected function toArray( \Aimeos\MShop\Customer\Item\Group\Iface $item, bool $copy = false ) : array
472
	{
473
		$data = $item->toArray( true );
474
475
		if( $copy === true )
476
		{
477
			$data['customer.group.siteid'] = $this->context()->locale()->getSiteId();
478
			$data['customer.group.id'] = '';
479
		}
480
481
		return $data;
482
	}
483
484
485
	/**
486
	 * Returns the rendered template including the view data
487
	 *
488
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
489
	 * @return string HTML output
490
	 */
491
	protected function render( \Aimeos\Base\View\Iface $view ) : string
492
	{
493
		/** admin/jqadm/group/template-item
494
		 * Relative path to the HTML body template for the group item.
495
		 *
496
		 * The template file contains the HTML code and processing instructions
497
		 * to generate the result shown in the body of the frontend. The
498
		 * configuration string is the path to the template file relative
499
		 * to the templates directory (usually in admin/jqadm/templates).
500
		 *
501
		 * You can overwrite the template file configuration in extensions and
502
		 * provide alternative templates. These alternative templates should be
503
		 * named like the default one but with the string "default" replaced by
504
		 * an unique name. You may use the name of your project for this. If
505
		 * you've implemented an alternative client class as well, "default"
506
		 * should be replaced by the name of the new class.
507
		 *
508
		 * @param string Relative path to the template creating the HTML code
509
		 * @since 2018.07
510
		 * @category Developer
511
		 */
512
		$tplconf = 'admin/jqadm/group/template-item';
513
		$default = 'group/item';
514
515
		return $view->render( $view->config( $tplconf, $default ) );
516
	}
517
}
518