Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::create()   A

Complexity

Conditions 4
Paths 14

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

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