Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard::delete()   A

Complexity

Conditions 5
Paths 25

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 25
nop 0
dl 0
loc 40
rs 9.2568
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-2020
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
	/**
27
	 * Copies a resource
28
	 *
29
	 * @return string|null HTML output
30
	 */
31
	public function copy() : ?string
32
	{
33
		$view = $this->getView();
34
		$context = $this->getContext();
35
36
		try
37
		{
38
			if( ( $id = $view->param( 'id' ) ) === null ) {
39
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
40
			}
41
42
			$manager = \Aimeos\MShop::create( $context, 'locale' );
43
			$view->item = $manager->getItem( $id );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemCurrencies = $this->getCurrencyItems();
48
			$view->itemLanguages = $this->getLanguageItems();
49
			$view->itemBody = '';
50
51
			foreach( $this->getSubClients() as $idx => $client )
52
			{
53
				$view->tabindex = ++$idx + 1;
54
				$view->itemBody .= $client->copy();
55
			}
56
		}
57
		catch( \Exception $e )
58
		{
59
			$this->report( $e, 'copy' );
60
		}
61
62
		return $this->render( $view );
63
	}
64
65
66
	/**
67
	 * Creates a new resource
68
	 *
69
	 * @return string|null HTML output
70
	 */
71
	public function create() : ?string
72
	{
73
		$view = $this->getView();
74
		$context = $this->getContext();
75
76
		try
77
		{
78
			$data = $view->param( 'item', [] );
79
80
			if( !isset( $view->item ) ) {
81
				$view->item = \Aimeos\MShop::create( $context, 'locale' )->createItem();
82
			}
83
84
			$data['locale.siteid'] = $view->item->getSiteId();
85
86
			$view->itemSubparts = $this->getSubClientNames();
87
			$view->itemCurrencies = $this->getCurrencyItems();
88
			$view->itemLanguages = $this->getLanguageItems();
89
			$view->itemData = $data;
90
			$view->itemBody = '';
91
92
			foreach( $this->getSubClients() as $idx => $client )
93
			{
94
				$view->tabindex = ++$idx + 1;
95
				$view->itemBody .= $client->create();
96
			}
97
		}
98
		catch( \Exception $e )
99
		{
100
			$this->report( $e, 'create' );
101
		}
102
103
		return $this->render( $view );
104
	}
105
106
107
	/**
108
	 * Deletes a resource
109
	 *
110
	 * @return string|null HTML output
111
	 */
112
	public function delete() : ?string
113
	{
114
		$view = $this->getView();
115
		$context = $this->getContext();
116
117
		$manager = \Aimeos\MShop::create( $context, 'locale' );
118
		$manager->begin();
119
120
		try
121
		{
122
			if( ( $ids = $view->param( 'id' ) ) === null ) {
123
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
124
			}
125
126
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
127
			$search->setConditions( $search->compare( '==', 'locale.id', $ids ) );
128
			$items = $manager->searchItems( $search );
129
130
			foreach( $items as $item )
131
			{
132
				$view->item = $item;
133
134
				foreach( $this->getSubClients() as $client ) {
135
					$client->delete();
136
				}
137
			}
138
139
			$manager->deleteItems( $items->toArray() );
140
			$manager->commit();
141
142
			$this->nextAction( $view, 'search', 'locale', null, 'delete' );
143
			return null;
144
		}
145
		catch( \Exception $e )
146
		{
147
			$manager->rollback();
148
			$this->report( $e, 'delete' );
149
		}
150
151
		return $this->search();
152
	}
153
154
155
	/**
156
	 * Returns a single resource
157
	 *
158
	 * @return string|null HTML output
159
	 */
160
	public function get() : ?string
161
	{
162
		$view = $this->getView();
163
		$context = $this->getContext();
164
165
		try
166
		{
167
			if( ( $id = $view->param( 'id' ) ) === null ) {
168
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
169
			}
170
171
			$manager = \Aimeos\MShop::create( $context, 'locale' );
172
173
			$view->item = $manager->getItem( $id );
174
			$view->itemSubparts = $this->getSubClientNames();
175
			$view->itemData = $this->toArray( $view->item );
176
			$view->itemCurrencies = $this->getCurrencyItems();
177
			$view->itemLanguages = $this->getLanguageItems();
178
			$view->itemBody = '';
179
180
			foreach( $this->getSubClients() as $idx => $client )
181
			{
182
				$view->tabindex = ++$idx + 1;
183
				$view->itemBody .= $client->get();
184
			}
185
		}
186
		catch( \Exception $e )
187
		{
188
			$this->report( $e, 'get' );
189
		}
190
191
		return $this->render( $view );
192
	}
193
194
195
	/**
196
	 * Saves the data
197
	 *
198
	 * @return string|null HTML output
199
	 */
200
	public function save() : ?string
201
	{
202
		$view = $this->getView();
203
		$context = $this->getContext();
204
205
		$manager = \Aimeos\MShop::create( $context, 'locale' );
206
		$manager->begin();
207
208
		try
209
		{
210
			$item = $this->fromArray( $view->param( 'item', [] ) );
211
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

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

211
			$view->item = $item->getId() ? $item : $manager->/** @scrutinizer ignore-call */ saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
			$view->itemBody = '';
213
214
			foreach( $this->getSubClients() as $client ) {
215
				$view->itemBody .= $client->save();
216
			}
217
218
			$manager->saveItem( $view->item );
219
			$manager->commit();
220
221
			$this->nextAction( $view, $view->param( 'next' ), 'locale', $view->item->getId(), 'save' );
222
			return null;
223
		}
224
		catch( \Exception $e )
225
		{
226
			$manager->rollback();
227
			$this->report( $e, 'save' );
228
		}
229
230
		return $this->create();
231
	}
232
233
234
	/**
235
	 * Returns a list of resource according to the conditions
236
	 *
237
	 * @return string|null HTML output
238
	 */
239
	public function search() : ?string
240
	{
241
		$view = $this->getView();
242
		$context = $this->getContext();
243
244
		try
245
		{
246
			$total = 0;
247
			$params = $this->storeSearchParams( $view->param(), 'locale' );
248
			$manager = \Aimeos\MShop::create( $context, 'locale' );
249
			$search = $this->initCriteria( $manager->createSearch(), $params );
250
251
			$view->items = $manager->searchItems( $search, [], $total );
252
			$view->filterAttributes = $manager->getSearchAttributes( true );
253
			$view->filterOperators = $search->getOperators();
254
			$view->total = $total;
255
			$view->itemBody = '';
256
257
			foreach( $this->getSubClients() as $client ) {
258
				$view->itemBody .= $client->search();
259
			}
260
		}
261
		catch( \Exception $e )
262
		{
263
			$this->report( $e, 'search' );
264
		}
265
266
		/** admin/jqadm/locale/template-list
267
		 * Relative path to the HTML body template for the locale list.
268
		 *
269
		 * The template file contains the HTML code and processing instructions
270
		 * to generate the result shown in the body of the frontend. The
271
		 * configuration string is the path to the template file relative
272
		 * to the templates directory (usually in admin/jqadm/templates).
273
		 *
274
		 * You can overwrite the template file configuration in extensions and
275
		 * provide alternative templates. These alternative templates should be
276
		 * named like the default one but with the string "default" replaced by
277
		 * an unique name. You may use the name of your project for this. If
278
		 * you've implemented an alternative client class as well, "default"
279
		 * should be replaced by the name of the new class.
280
		 *
281
		 * @param string Relative path to the template creating the HTML code
282
		 * @since 2017.10
283
		 * @category Developer
284
		 */
285
		$tplconf = 'admin/jqadm/locale/template-list';
286
		$default = 'locale/list-standard';
287
288
		return $view->render( $view->config( $tplconf, $default ) );
289
	}
290
291
292
	/**
293
	 * Returns the sub-client given by its name.
294
	 *
295
	 * @param string $type Name of the client type
296
	 * @param string|null $name Name of the sub-client (Default if null)
297
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
298
	 */
299
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
300
	{
301
		/** admin/jqadm/locale/decorators/excludes
302
		 * Excludes decorators added by the "common" option from the locale JQAdm client
303
		 *
304
		 * Decorators extend the functionality of a class by adding new aspects
305
		 * (e.g. log what is currently done), executing the methods of the underlying
306
		 * class only in certain conditions (e.g. only for logged in users) or
307
		 * modify what is returned to the caller.
308
		 *
309
		 * This option allows you to remove a decorator added via
310
		 * "client/jqadm/common/decorators/default" before they are wrapped
311
		 * around the JQAdm client.
312
		 *
313
		 *  admin/jqadm/locale/decorators/excludes = array( 'decorator1' )
314
		 *
315
		 * This would remove the decorator named "decorator1" from the list of
316
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
317
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
318
		 *
319
		 * @param array List of decorator names
320
		 * @since 2017.10
321
		 * @category Developer
322
		 * @see admin/jqadm/common/decorators/default
323
		 * @see admin/jqadm/locale/decorators/global
324
		 * @see admin/jqadm/locale/decorators/local
325
		 */
326
327
		/** admin/jqadm/locale/decorators/global
328
		 * Adds a list of globally available decorators only to the locale JQAdm client
329
		 *
330
		 * Decorators extend the functionality of a class by adding new aspects
331
		 * (e.g. log what is currently done), executing the methods of the underlying
332
		 * class only in certain conditions (e.g. only for logged in users) or
333
		 * modify what is returned to the caller.
334
		 *
335
		 * This option allows you to wrap global decorators
336
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
337
		 *
338
		 *  admin/jqadm/locale/decorators/global = array( 'decorator1' )
339
		 *
340
		 * This would add the decorator named "decorator1" defined by
341
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
342
		 *
343
		 * @param array List of decorator names
344
		 * @since 2017.10
345
		 * @category Developer
346
		 * @see admin/jqadm/common/decorators/default
347
		 * @see admin/jqadm/locale/decorators/excludes
348
		 * @see admin/jqadm/locale/decorators/local
349
		 */
350
351
		/** admin/jqadm/locale/decorators/local
352
		 * Adds a list of local decorators only to the locale JQAdm client
353
		 *
354
		 * Decorators extend the functionality of a class by adding new aspects
355
		 * (e.g. log what is currently done), executing the methods of the underlying
356
		 * class only in certain conditions (e.g. only for logged in users) or
357
		 * modify what is returned to the caller.
358
		 *
359
		 * This option allows you to wrap local decorators
360
		 * ("\Aimeos\Admin\JQAdm\Locale\Decorator\*") around the JQAdm client.
361
		 *
362
		 *  admin/jqadm/locale/decorators/local = array( 'decorator2' )
363
		 *
364
		 * This would add the decorator named "decorator2" defined by
365
		 * "\Aimeos\Admin\JQAdm\Locale\Decorator\Decorator2" only to the JQAdm client.
366
		 *
367
		 * @param array List of decorator names
368
		 * @since 2017.10
369
		 * @category Developer
370
		 * @see admin/jqadm/common/decorators/default
371
		 * @see admin/jqadm/locale/decorators/excludes
372
		 * @see admin/jqadm/locale/decorators/global
373
		 */
374
		return $this->createSubClient( 'locale/' . $type, $name );
375
	}
376
377
378
	/**
379
	 * Returns the available currencies
380
	 *
381
	 * @return \Aimeos\Map List of currency items implementing \Aimeos\MShop\Locale\Item\Currency\Iface
382
	 */
383
	protected function getCurrencyItems() : \Aimeos\Map
384
	{
385
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/currency' );
386
387
		$search = $manager->createSearch( true );
388
		$search->setSortations( [$search->sort( '+', 'locale.currency.id' )] );
389
		$search->setSlice( 0, 250 );
390
391
		return $manager->searchItems( $search );
392
	}
393
394
395
	/**
396
	 * Returns the available languages
397
	 *
398
	 * @return \Aimeos\Map List of language items implementing \Aimeos\MShop\Locale\Item\Language\Iface
399
	 */
400
	protected function getLanguageItems() : \Aimeos\Map
401
	{
402
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/language' );
403
404
		$search = $manager->createSearch( true );
405
		$search->setSortations( [$search->sort( '+', 'locale.language.id' )] );
406
		$search->setSlice( 0, 250 );
407
408
		return $manager->searchItems( $search );
409
	}
410
411
412
	/**
413
	 * Returns the list of sub-client names configured for the client.
414
	 *
415
	 * @return array List of JQAdm client names
416
	 */
417
	protected function getSubClientNames() : array
418
	{
419
		/** admin/jqadm/locale/standard/subparts
420
		 * List of JQAdm sub-clients rendered within the locale section
421
		 *
422
		 * The output of the frontend is composed of the code generated by the JQAdm
423
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
424
		 * that are responsible for rendering certain sub-parts of the output. The
425
		 * sub-clients can contain JQAdm clients themselves and therefore a
426
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
427
		 * the output that is placed inside the container of its parent.
428
		 *
429
		 * At first, always the JQAdm code generated by the parent is printed, then
430
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
431
		 * determines the order of the output of these sub-clients inside the parent
432
		 * container. If the configured list of clients is
433
		 *
434
		 *  array( "subclient1", "subclient2" )
435
		 *
436
		 * you can easily change the order of the output by reordering the subparts:
437
		 *
438
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
439
		 *
440
		 * You can also remove one or more parts if they shouldn't be rendered:
441
		 *
442
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
443
		 *
444
		 * As the clients only generates structural JQAdm, the layout defined via CSS
445
		 * should support adding, removing or reordering content by a fluid like
446
		 * design.
447
		 *
448
		 * @param array List of sub-client names
449
		 * @since 2017.10
450
		 * @category Developer
451
		 */
452
		return $this->getContext()->getConfig()->get( 'admin/jqadm/locale/standard/subparts', [] );
453
	}
454
455
456
457
	/**
458
	 * Creates new and updates existing items using the data array
459
	 *
460
	 * @param array $data Data array
461
	 * @return \Aimeos\MShop\Locale\Item\Iface New locale item object
462
	 */
463
	protected function fromArray( array $data ) : \Aimeos\MShop\Locale\Item\Iface
464
	{
465
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale' );
466
467
		if( isset( $data['locale.id'] ) && $data['locale.id'] != '' ) {
468
			$item = $manager->getItem( $data['locale.id'] );
469
		} else {
470
			$item = $manager->createItem();
471
		}
472
473
		$item->fromArray( $data, true );
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->getContext()->getLocale()->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\MW\View\Iface $view View object with data assigned
503
	 * @return string|null HTML output
504
	 */
505
	protected function render( \Aimeos\MW\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-standard';
528
529
		return $view->render( $view->config( $tplconf, $default ) );
530
	}
531
}
532