Passed
Push — master ( 0d6ae3...0fb3b0 )
by Aimeos
05:00
created

Standard::save()   A

Complexity

Conditions 3
Paths 12

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 12
nop 0
dl 0
loc 25
rs 9.7998
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
		$view->itemForDomains = $view->config( 'admin/jqadm/type/domains-for', [] );
69
		$view->itemDomains = $view->config( 'admin/jqadm/type/domains', [] );
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( '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...
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(), 'type' );
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(), 'type' )->create();
133
			}
134
135
			$data['type.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(), 'type' );
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()->add( 'type.id', '==', $ids )->slice( 0, count( (array) $ids ) );
170
			$items = $manager->search( $search );
171
172
			foreach( $items as $item )
173
			{
174
				$view->item = $item;
175
				parent::delete();
176
			}
177
178
			$manager->delete( $items );
179
			$manager->commit();
180
181
			return $this->redirect( 'type', 'search', null, 'delete' );
182
		}
183
		catch( \Exception $e )
184
		{
185
			$manager->rollback();
186
			$this->report( $e, 'delete' );
187
		}
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(), 'type' );
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(), 'type' );
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( '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

247
			return $this->redirect( 'type', $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(), 'type' );
272
			$manager = \Aimeos\MShop::create( $this->context(), 'type' );
273
			$search = $this->initCriteria( $manager->filter(), $params );
274
275
			$view->items = $manager->search( $search, $total );
0 ignored issues
show
Bug introduced by
$total of type integer is incompatible with the type array expected by parameter $ref of Aimeos\MShop\Common\Manager\Iface::search(). ( Ignorable by Annotation )

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

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