Passed
Push — master ( cff44b...e8ea19 )
by Aimeos
03:24
created

Standard::fromArrayIcon()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 23
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), 2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Settings;
12
13
sprintf( 'settings' ); // for translation
14
15
16
/**
17
 * Default implementation of settings 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->themes = $this->getContext()->config()->get( 'client/html/themes', [] );
35
		$view->itemSubparts = $this->getSubClientNames();
36
		return $view;
37
	}
38
39
40
	/**
41
	 * Saves the data
42
	 *
43
	 * @return string|null HTML output
44
	 */
45
	public function save() : ?string
46
	{
47
		$view = $this->getView();
48
		$context = $this->getContext();
49
50
		$manager = \Aimeos\MShop::create( $context, 'locale/site' );
51
		$manager->begin();
52
53
		try
54
		{
55
			$view->item = $this->fromArray( $view->param( 'item', [] ) );
56
			$view->itemBody = parent::save();
57
58
			$manager->save( clone $view->item );
59
			$manager->commit();
60
61
			$target = $view->config( 'admin/jqadm/url/search/target' );
62
			$cntl = $view->config( 'admin/jqadm/url/search/controller', 'Jqadm' );
63
			$action = $view->config( 'admin/jqadm/url/search/action', 'search' );
64
			$conf = $view->config( 'admin/jqadm/url/search/config', [] );
65
66
			$params = $this->getClientParams();
67
			$params['site'] = $view->item->getCode();
68
			$url = $view->url( $target, $cntl, $action, $params, [], $conf );
69
70
			$context->getSession()->set( 'info', [$context->getI18n()->dt( 'admin', 'Item saved successfully' )] );
71
72
			$view->response()->withStatus( 302 );
73
			$view->response()->withHeader( 'Location', $url );
74
			$view->response()->withHeader( 'Cache-Control', 'no-store' );
75
76
			return null;
77
		}
78
		catch( \Exception $e )
79
		{
80
			$manager->rollback();
81
			$this->report( $e, 'save' );
82
		}
83
84
		return $this->search();
85
	}
86
87
88
	/**
89
	 * Returns the settings root node
90
	 *
91
	 * @return string|null HTML output
92
	 */
93
	public function search() : ?string
94
	{
95
		$view = $this->getView();
96
97
		try
98
		{
99
			$view->itemData = $this->toArray( $this->getContext()->getLocale()->getSiteItem() );
100
			$view->itemBody = parent::search();
101
		}
102
		catch( \Exception $e )
103
		{
104
			$this->report( $e, 'search' );
105
		}
106
107
		return $this->render( $view );
108
	}
109
110
111
	/**
112
	 * Returns the sub-client given by its name.
113
	 *
114
	 * @param string $type Name of the client type
115
	 * @param string|null $name Name of the sub-client (Default if null)
116
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
117
	 */
118
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
119
	{
120
		/** admin/jqadm/settings/decorators/excludes
121
		 * Excludes decorators added by the "common" option from the settings JQAdm client
122
		 *
123
		 * Decorators extend the functionality of a class by adding new aspects
124
		 * (e.g. log what is currently done), executing the methods of the underlying
125
		 * class only in certain conditions (e.g. only for logged in users) or
126
		 * modify what is returned to the caller.
127
		 *
128
		 * This option allows you to remove a decorator added via
129
		 * "client/jqadm/common/decorators/default" before they are wrapped
130
		 * around the JQAdm client.
131
		 *
132
		 *  admin/jqadm/settings/decorators/excludes = array( 'decorator1' )
133
		 *
134
		 * This would remove the decorator named "decorator1" from the list of
135
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
136
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
137
		 *
138
		 * @param array List of decorator names
139
		 * @since 2021.07
140
		 * @category Developer
141
		 * @see admin/jqadm/common/decorators/default
142
		 * @see admin/jqadm/settings/decorators/global
143
		 * @see admin/jqadm/settings/decorators/local
144
		 */
145
146
		/** admin/jqadm/settings/decorators/global
147
		 * Adds a list of globally available decorators only to the settings JQAdm client
148
		 *
149
		 * Decorators extend the functionality of a class by adding new aspects
150
		 * (e.g. log what is currently done), executing the methods of the underlying
151
		 * class only in certain conditions (e.g. only for logged in users) or
152
		 * modify what is returned to the caller.
153
		 *
154
		 * This option allows you to wrap global decorators
155
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
156
		 *
157
		 *  admin/jqadm/settings/decorators/global = array( 'decorator1' )
158
		 *
159
		 * This would add the decorator named "decorator1" defined by
160
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
161
		 *
162
		 * @param array List of decorator names
163
		 * @since 2021.07
164
		 * @category Developer
165
		 * @see admin/jqadm/common/decorators/default
166
		 * @see admin/jqadm/settings/decorators/excludes
167
		 * @see admin/jqadm/settings/decorators/local
168
		 */
169
170
		/** admin/jqadm/settings/decorators/local
171
		 * Adds a list of local decorators only to the settings JQAdm client
172
		 *
173
		 * Decorators extend the functionality of a class by adding new aspects
174
		 * (e.g. log what is currently done), executing the methods of the underlying
175
		 * class only in certain conditions (e.g. only for logged in users) or
176
		 * modify what is returned to the caller.
177
		 *
178
		 * This option allows you to wrap local decorators
179
		 * ("\Aimeos\Admin\JQAdm\Settings\Decorator\*") around the JQAdm client.
180
		 *
181
		 *  admin/jqadm/settings/decorators/local = array( 'decorator2' )
182
		 *
183
		 * This would add the decorator named "decorator2" defined by
184
		 * "\Aimeos\Admin\JQAdm\Settings\Decorator\Decorator2" only to the JQAdm client.
185
		 *
186
		 * @param array List of decorator names
187
		 * @since 2021.07
188
		 * @category Developer
189
		 * @see admin/jqadm/common/decorators/default
190
		 * @see admin/jqadm/settings/decorators/excludes
191
		 * @see admin/jqadm/settings/decorators/global
192
		 */
193
		return $this->createSubClient( 'settings/' . $type, $name );
194
	}
195
196
197
	/**
198
	 * Returns the list of sub-client names configured for the client.
199
	 *
200
	 * @return array List of JQAdm client names
201
	 */
202
	protected function getSubClientNames() : array
203
	{
204
		/** admin/jqadm/settings/subparts
205
		 * List of JQAdm sub-clients rendered within the settings section
206
		 *
207
		 * The output of the frontend is composed of the code generated by the JQAdm
208
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
209
		 * that are responsible for rendering certain sub-parts of the output. The
210
		 * sub-clients can contain JQAdm clients themselves and therefore a
211
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
212
		 * the output that is placed inside the container of its parent.
213
		 *
214
		 * At first, always the JQAdm code generated by the parent is printed, then
215
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
216
		 * determines the order of the output of these sub-clients inside the parent
217
		 * container. If the configured list of clients is
218
		 *
219
		 *  array( "subclient1", "subclient2" )
220
		 *
221
		 * you can easily change the order of the output by reordering the subparts:
222
		 *
223
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
224
		 *
225
		 * You can also remove one or more parts if they shouldn't be rendered:
226
		 *
227
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
228
		 *
229
		 * As the clients only generates structural JQAdm, the layout defined via CSS
230
		 * should support adding, removing or reordering content by a fluid like
231
		 * design.
232
		 *
233
		 * @param array List of sub-client names
234
		 * @since 2021.07
235
		 * @category Developer
236
		 */
237
		return $this->getContext()->getConfig()->get( 'admin/jqadm/settings/subparts', [] );
238
	}
239
240
241
	/**
242
	 * Creates new and updates existing items using the data array
243
	 *
244
	 * @param array $data Data array
245
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New settings item object
246
	 */
247
	protected function fromArray( array $data ) : \Aimeos\MShop\Locale\Item\Site\Iface
248
	{
249
		$item = $this->getContext()->getLocale()->getSiteItem();
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
250
251
		$config = $data['locale.site.config'] ?? [];
252
		$config['resource']['email']['email-name'] = $data['locale.site.label'];
253
254
		$files = (array) $this->getView()->request()->getUploadedFiles();
255
256
		$item = $this->fromArrayIcon( $files );
257
		$item = $this->fromArrayLogo( $files );
258
259
		return $item->setConfig( $config )
260
			->setTheme( $data['locale.site.theme'] ?? '' )
261
			->setLabel( $data['locale.site.label'] ?? '' )
262
			->setCode( $data['locale.site.code'] ?? '' );
263
	}
264
265
266
	/**
267
	 * Creates new and updates existing items using the data array
268
	 *
269
	 * @param array $files Uploaded files
270
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New settings item object
271
	 */
272
	protected function fromArrayIcon( array $files ) : \Aimeos\MShop\Locale\Item\Site\Iface
273
	{
274
		$file = $this->getValue( $files, 'media/icon' );
275
276
		if( $file && $file->getError() === UPLOAD_ERR_OK )
277
		{
278
			$context = $this->getContext();
279
			$siteId = $context->getLocale()->getSiteId();
280
			$image = \Aimeos\MW\Media\Factory::get( $file->getStream(), $options );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
281
282
			if( !in_array( $image->getMimetype(), ['image/jpeg', 'image/png', 'image/gif'] ) )
283
			{
284
				$msg = $context->i18n()->dt( 'admin', 'Only .jpg, .png and .gif are allowed for icons' );
285
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
286
			}
287
288
			$filepath = 'aimeos/' . $siteId . '/icon.' . $ext;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ext seems to be never defined.
Loading history...
289
			$context->getFilesystemManager()->get( 'fs-media' )->write( $filepath, $image->save() );
0 ignored issues
show
Bug introduced by
It seems like $image->save() can also be of type null; however, parameter $content of Aimeos\MW\Filesystem\Iface::write() does only seem to accept 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

289
			$context->getFilesystemManager()->get( 'fs-media' )->write( $filepath, /** @scrutinizer ignore-type */ $image->save() );
Loading history...
290
291
			$item->setIcon( $filepath );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be never defined.
Loading history...
292
		}
293
294
		return $item;
295
	}
296
297
298
	/**
299
	 * Creates new and updates existing items using the data array
300
	 *
301
	 * @param array $files Uploaded files
302
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New settings item object
303
	 */
304
	protected function fromArrayLogo( array $files ) : \Aimeos\MShop\Locale\Item\Site\Iface
305
	{
306
		$file = $this->getValue( $files, 'media/logo' );
307
308
		if( $file && $file->getError() === UPLOAD_ERR_OK )
309
		{
310
			$context = $this->getContext();
311
			$siteId = $context->getLocale()->getSiteId();
312
313
			$options = $context->getConfig()->get( 'controller/common/media/options', [] );
314
			$image = \Aimeos\MW\Media\Factory::get( $file->getStream(), $options );
315
			$ext = pathinfo( $file->getClientFilename(), PATHINFO_EXTENSION );
316
			$filepaths = [];
317
318
			if( !in_array( $image->getMimetype(), ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'] ) )
319
			{
320
				$msg = $context->i18n()->dt( 'admin', 'Only .jpg, .png, .gif or .svg are allowed for logos' );
321
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
322
			}
323
324
			foreach( $cfg->get( 'admin/jqadm/settings/logo-size', ['maxwidth' => null, 'maxheight' => null] ) as $size )
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cfg seems to be never defined.
Loading history...
325
			{
326
				$w = $size['maxwidth'] ?? null;
327
				$h = $size['maxheight'] ?? null;
328
329
				$filepath = 'aimeos/' . $siteId . '/logo' . $w . '.' . $ext;
0 ignored issues
show
Bug introduced by
Are you sure $ext of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

329
				$filepath = 'aimeos/' . $siteId . '/logo' . $w . '.' . /** @scrutinizer ignore-type */ $ext;
Loading history...
330
				$context->getFilesystemManager()->get( 'fs-media' )->write( $filepath, $image->scale( $w, $h )->save() );
0 ignored issues
show
Bug introduced by
The method scale() does not exist on Aimeos\MW\Media\Iface. It seems like you code against a sub-type of Aimeos\MW\Media\Iface such as Aimeos\MW\Media\Image\Iface. ( Ignorable by Annotation )

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

330
				$context->getFilesystemManager()->get( 'fs-media' )->write( $filepath, $image->/** @scrutinizer ignore-call */ scale( $w, $h )->save() );
Loading history...
331
				$filepaths[] = $filepath;
332
			}
333
334
			$item->setLogos( $filepaths );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be never defined.
Loading history...
335
		}
336
337
		return $item;
338
	}
339
340
341
	/**
342
	 * Constructs the data array for the view from the given item
343
	 *
344
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $item Settings item object
345
	 * @return string[] Multi-dimensional associative list of item data
346
	 */
347
	protected function toArray( \Aimeos\MShop\Locale\Item\Site\Iface $item, bool $copy = false ) : array
348
	{
349
		return [
350
			'locale.site.code' => $item->getCode(),
351
			'locale.site.logo' => $item->getLogos(),
352
			'locale.site.label' => $item->getLabel(),
353
			'locale.site.config' => $item->getConfig(),
354
			'locale.site.supplierid' => $item->getSupplierId(),
355
			'locale.site.ctime' => $item->getTimeCreated(),
356
			'locale.site.mtime' => $item->getTimeModified(),
357
			'locale.site.editor' => $item->getEditor(),
358
		];
359
	}
360
361
362
	/**
363
	 * Returns the rendered template including the view data
364
	 *
365
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
366
	 * @return string HTML output
367
	 */
368
	protected function render( \Aimeos\MW\View\Iface $view ) : string
369
	{
370
		/** admin/jqadm/settings/template-item
371
		 * Relative path to the HTML body template for the settings item.
372
		 *
373
		 * The template file contains the HTML code and processing instructions
374
		 * to generate the result shown in the body of the frontend. The
375
		 * configuration string is the path to the template file relative
376
		 * to the templates directory (usually in admin/jqadm/templates).
377
		 *
378
		 * You can overwrite the template file configuration in extensions and
379
		 * provide alternative templates. These alternative templates should be
380
		 * named like the default one but with the string "default" replaced by
381
		 * an unique name. You may use the name of your project for this. If
382
		 * you've implemented an alternative client class as well, "default"
383
		 * should be replaced by the name of the new class.
384
		 *
385
		 * @param string Relative path to the template creating the HTML code
386
		 * @since 2021.07
387
		 * @category Developer
388
		 */
389
		$tplconf = 'admin/jqadm/settings/template-item';
390
		$default = 'settings/item-standard';
391
392
		return $view->render( $view->config( $tplconf, $default ) );
393
	}
394
}
395