Passed
Push — master ( d931d2...a33bf6 )
by Aimeos
04:32
created

Standard::batch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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), 2017-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Locale;
12
13
sprintf( 'locale' ); // for translation
14
15
16
/**
17
 * Default implementation of locale 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/locale/name
27
	 * Class name of the used account favorite client 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\Locale\Standard
37
	 *
38
	 * and you want to replace it with your own version named
39
	 *
40
	 *  \Aimeos\Admin\JQAdm\Locale\Myfavorite
41
	 *
42
	 * then you have to set the this configuration option:
43
	 *
44
	 *  admin/jqadm/locale/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 2017.10
57
	 * @category Developer
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
		$curmanager = \Aimeos\MShop::create( $this->context(), 'locale/currency' );
70
		$langmanager = \Aimeos\MShop::create( $this->context(), 'locale/language' );
71
72
		$cursearch = $curmanager->filter( true );
73
		$cursearch->setSortations( [$cursearch->sort( '+', 'locale.currency.id' )] );
74
		$cursearch->slice( 0, 250 );
75
76
		$langsearch = $langmanager->filter( true );
77
		$langsearch->setSortations( [$langsearch->sort( '+', 'locale.language.id' )] );
78
		$langsearch->slice( 0, 250 );
79
80
		$view->itemLanguages = $langmanager->search( $langsearch );
81
		$view->itemCurrencies = $curmanager->search( $cursearch );
82
		$view->itemSubparts = $this->getSubClientNames();
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
		return $this->batchBase( 'locale' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('locale') 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...
96
	}
97
98
99
	/**
100
	 * Copies a resource
101
	 *
102
	 * @return string|null HTML output
103
	 */
104
	public function copy() : ?string
105
	{
106
		$view = $this->object()->data( $this->view() );
107
108
		try
109
		{
110
			if( ( $id = $view->param( 'id' ) ) === null )
111
			{
112
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
113
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
114
			}
115
116
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
117
			$view->item = $manager->get( $id );
118
119
			$view->itemData = $this->toArray( $view->item, true );
120
			$view->itemBody = parent::copy();
121
		}
122
		catch( \Exception $e )
123
		{
124
			$this->report( $e, 'copy' );
125
		}
126
127
		return $this->render( $view );
128
	}
129
130
131
	/**
132
	 * Creates a new resource
133
	 *
134
	 * @return string|null HTML output
135
	 */
136
	public function create() : ?string
137
	{
138
		$view = $this->object()->data( $this->view() );
139
140
		try
141
		{
142
			$data = $view->param( 'item', [] );
143
144
			if( !isset( $view->item ) ) {
145
				$view->item = \Aimeos\MShop::create( $this->context(), 'locale' )->create();
146
			}
147
148
			$data['locale.siteid'] = $view->item->getSiteId();
149
150
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
151
			$view->itemBody = parent::create();
152
		}
153
		catch( \Exception $e )
154
		{
155
			$this->report( $e, 'create' );
156
		}
157
158
		return $this->render( $view );
159
	}
160
161
162
	/**
163
	 * Deletes a resource
164
	 *
165
	 * @return string|null HTML output
166
	 */
167
	public function delete() : ?string
168
	{
169
		$view = $this->view();
170
171
		$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
172
		$manager->begin();
173
174
		try
175
		{
176
			if( ( $ids = $view->param( 'id' ) ) === null )
177
			{
178
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
179
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
180
			}
181
182
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
183
			$search->setConditions( $search->compare( '==', 'locale.id', $ids ) );
184
			$items = $manager->search( $search );
185
186
			foreach( $items as $item )
187
			{
188
				$view->item = $item;
189
				parent::delete();
190
			}
191
192
			$manager->delete( $items->toArray() );
193
			$manager->commit();
194
195
			return $this->redirect( 'locale', 'search', null, 'delete' );
196
		}
197
		catch( \Exception $e )
198
		{
199
			$manager->rollback();
200
			$this->report( $e, 'delete' );
201
		}
202
203
		return $this->search();
204
	}
205
206
207
	/**
208
	 * Returns a single resource
209
	 *
210
	 * @return string|null HTML output
211
	 */
212
	public function get() : ?string
213
	{
214
		$view = $this->object()->data( $this->view() );
215
216
		try
217
		{
218
			if( ( $id = $view->param( 'id' ) ) === null )
219
			{
220
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
221
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
222
			}
223
224
			$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
225
226
			$view->item = $manager->get( $id );
227
			$view->itemData = $this->toArray( $view->item );
228
			$view->itemBody = parent::get();
229
		}
230
		catch( \Exception $e )
231
		{
232
			$this->report( $e, 'get' );
233
		}
234
235
		return $this->render( $view );
236
	}
237
238
239
	/**
240
	 * Saves the data
241
	 *
242
	 * @return string|null HTML output
243
	 */
244
	public function save() : ?string
245
	{
246
		$view = $this->view();
247
248
		$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
249
		$manager->begin();
250
251
		try
252
		{
253
			$item = $this->fromArray( $view->param( 'item', [] ) );
254
			$view->item = $item->getId() ? $item : $manager->save( $item );
255
			$view->itemBody = parent::save();
256
257
			$manager->save( $view->item );
258
			$manager->commit();
259
260
			return $this->redirect( 'locale', $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

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