Standard::copy()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 8
nop 0
dl 0
loc 24
rs 9.8666
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), 2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Type;
12
13
sprintf( 'type' ); // for translation
14
15
16
/**
17
 * Default implementation of type JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/type/name
27
	 * Class name of the used type panel implementation
28
	 *
29
	 * Each default admin client can be replace by an alternative imlementation.
30
	 * To use this implementation, you have to set the last part of the class
31
	 * name as configuration value so the client factory knows which class it
32
	 * has to instantiate.
33
	 *
34
	 * For example, if the name of the default class is
35
	 *
36
	 *  \Aimeos\Admin\JQAdm\Type\Standard
37
	 *
38
	 * and you want to replace it with your own version named
39
	 *
40
	 *  \Aimeos\Admin\JQAdm\Type\Myfavorite
41
	 *
42
	 * then you have to set the this configuration option:
43
	 *
44
	 *  admin/jqadm/type/name = Myfavorite
45
	 *
46
	 * The value is the last part of your own class name and it's case sensitive,
47
	 * so take care that the configuration value is exactly named like the last
48
	 * part of the class name.
49
	 *
50
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
51
	 * characters are possible! You should always start the last part of the class
52
	 * name with an upper case character and continue only with lower case characters
53
	 * or numbers. Avoid chamel case names like "MyFavorite"!
54
	 *
55
	 * @param string Last part of the class name
56
	 * @since 2025.04
57
	 */
58
59
60
	/**
61
	 * Adds the required data used in the template
62
	 *
63
	 * @param \Aimeos\Base\View\Iface $view View object
64
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
65
	 */
66
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
67
	{
68
		/** admin/jqadm/type/domains
69
		 * List of domain names available for the items in the type JQAdm panel
70
		 *
71
		 * Type items are assigned to a domain which is used to group them and
72
		 * to be able to filter the items in the JQAdm panel. The configured
73
		 * domain names limits the domains in the panel to the available ones.
74
		 *
75
		 * @param array List of domain names
76
		 * @since 2025.04
77
		 */
78
		$domains = $view->config( 'admin/jqadm/type/domains', [] );
79
		$context = $this->context();
80
81
		$view->itemDomains = array_map( fn( $domain ) => $context->translate( 'admin/code', $domain ), $domains );
82
		$view->itemSubparts = $this->getSubClientNames();
83
		return $view;
84
	}
85
86
87
	/**
88
	 * Batch update of a resource
89
	 *
90
	 * @return string|null Output to display
91
	 */
92
	public function batch() : ?string
93
	{
94
		return $this->batchBase( 'type' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('type') 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...
95
	}
96
97
98
	/**
99
	 * Copies a resource
100
	 *
101
	 * @return string|null HTML output
102
	 */
103
	public function copy() : ?string
104
	{
105
		$view = $this->object()->data( $this->view() );
106
107
		try
108
		{
109
			if( ( $id = $view->param( 'id' ) ) === null )
110
			{
111
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
112
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
113
			}
114
115
			$manager = \Aimeos\MShop::create( $this->context(), 'type' );
116
			$view->item = $manager->get( $id );
117
118
			$view->itemData = $this->toArray( $view->item, true );
119
			$view->itemBody = parent::copy();
120
		}
121
		catch( \Exception $e )
122
		{
123
			$this->report( $e, 'copy' );
124
		}
125
126
		return $this->render( $view );
127
	}
128
129
130
	/**
131
	 * Creates a new resource
132
	 *
133
	 * @return string|null HTML output
134
	 */
135
	public function create() : ?string
136
	{
137
		$view = $this->object()->data( $this->view() );
138
139
		try
140
		{
141
			$data = $view->param( 'item', [] );
142
143
			if( !isset( $view->item ) ) {
144
				$view->item = \Aimeos\MShop::create( $this->context(), 'type' )->create();
145
			}
146
147
			$data['type.siteid'] = $view->item->getSiteId();
148
149
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
150
			$view->itemBody = parent::create();
151
		}
152
		catch( \Exception $e )
153
		{
154
			$this->report( $e, 'create' );
155
		}
156
157
		return $this->render( $view );
158
	}
159
160
161
	/**
162
	 * Deletes a resource
163
	 *
164
	 * @return string|null HTML output
165
	 */
166
	public function delete() : ?string
167
	{
168
		$view = $this->view();
169
170
		$manager = \Aimeos\MShop::create( $this->context(), 'type' );
171
		$manager->begin();
172
173
		try
174
		{
175
			if( ( $ids = $view->param( 'id' ) ) === null )
176
			{
177
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
178
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
179
			}
180
181
			$search = $manager->filter()->add( 'type.id', '==', $ids )->slice( 0, count( (array) $ids ) );
182
			$items = $manager->search( $search );
183
184
			foreach( $items as $item )
185
			{
186
				$view->item = $item;
187
				parent::delete();
188
			}
189
190
			$manager->delete( $items );
191
			$manager->commit();
192
193
			return $this->redirect( 'type', 'search', null, 'delete' );
194
		}
195
		catch( \Exception $e )
196
		{
197
			$manager->rollback();
198
			$this->report( $e, 'delete' );
199
		}
200
201
202
		return $this->search();
203
	}
204
205
206
	/**
207
	 * Returns a single resource
208
	 *
209
	 * @return string|null HTML output
210
	 */
211
	public function get() : ?string
212
	{
213
		$view = $this->object()->data( $this->view() );
214
215
		try
216
		{
217
			if( ( $id = $view->param( 'id' ) ) === null )
218
			{
219
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
220
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
221
			}
222
223
			$manager = \Aimeos\MShop::create( $this->context(), 'type' );
224
225
			$view->item = $manager->get( $id );
226
			$view->itemData = $this->toArray( $view->item );
227
			$view->itemBody = parent::get();
228
		}
229
		catch( \Exception $e )
230
		{
231
			$this->report( $e, 'get' );
232
		}
233
234
		return $this->render( $view );
235
	}
236
237
238
	/**
239
	 * Saves the data
240
	 *
241
	 * @return string|null HTML output
242
	 */
243
	public function save() : ?string
244
	{
245
		$view = $this->view();
246
247
		$manager = \Aimeos\MShop::create( $this->context(), 'type' );
248
		$manager->begin();
249
250
		try
251
		{
252
			$item = $this->fromArray( $view->param( 'item', [] ) );
253
			$view->item = $item->getId() ? $item : $manager->save( $item );
254
			$view->itemBody = parent::save();
255
256
			$manager->save( clone $view->item );
257
			$manager->commit();
258
259
			return $this->redirect( 'type', $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

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