Standard   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 402
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 97
dl 0
loc 402
rs 10
c 0
b 0
f 0
wmc 29

12 Methods

Rating   Name   Duplication   Size   Complexity  
B checkFileUpload() 0 26 9
A getSubClient() 0 73 1
A getCodeItems() 0 15 1
A render() 0 24 1
A toArray() 0 12 3
A get() 0 13 1
A save() 0 23 2
A getSubClientNames() 0 35 1
A storeFile() 0 19 4
A copy() 0 6 1
A fromArray() 0 30 4
A create() 0 6 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Coupon\Code;
12
13
sprintf( 'code' ); // for translation
14
15
16
/**
17
 * Default implementation of coupon code 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/coupon/code/name
27
	 * Name of the code subpart used by the JQAdm coupon implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Coupon\Code\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.07
34
	 */
35
36
37
	/**
38
	 * Copies a resource
39
	 *
40
	 * @return string|null HTML output
41
	 */
42
	public function copy() : ?string
43
	{
44
		$view = $this->object()->data( $this->view() );
45
		$view->codeBody = parent::copy();
46
47
		return $this->render( $view );
48
	}
49
50
51
	/**
52
	 * Creates a new resource
53
	 *
54
	 * @return string|null HTML output
55
	 */
56
	public function create() : ?string
57
	{
58
		$view = $this->object()->data( $this->view() );
59
		$view->codeBody = parent::create();
60
61
		return $this->render( $view );
62
	}
63
64
65
	/**
66
	 * Returns a single resource
67
	 *
68
	 * @return string|null HTML output
69
	 */
70
	public function get() : ?string
71
	{
72
		$view = $this->object()->data( $this->view() );
73
74
		$total = 0;
75
		$params = $this->storeFilter( $view->param( 'vc', [] ), 'couponcode' );
76
		$codeItems = $this->getCodeItems( $view->item, $params, $total );
77
78
		$view->codeData = $this->toArray( $codeItems );
79
		$view->codeBody = parent::search();
80
		$view->codeTotal = $total;
81
82
		return $this->render( $view );
83
	}
84
85
86
	/**
87
	 * Saves the data
88
	 *
89
	 * @return string|null HTML output
90
	 */
91
	public function save() : ?string
92
	{
93
		$view = $this->view();
94
95
		$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' );
96
		$manager->begin();
97
98
		try
99
		{
100
			$this->storeFilter( $view->param( 'vc', [] ), 'couponcode' );
101
			$this->storeFile( $view->item, (array) $view->request()->getUploadedFiles() );
102
			$this->fromArray( $view->item, $view->param( 'code', [] ) );
103
			$view->codeBody = parent::save();
104
105
			$manager->commit();
106
		}
107
		catch( \Exception $e )
108
		{
109
			$manager->rollback();
110
			throw $e;
111
		}
112
113
		return null;
114
	}
115
116
117
	/**
118
	 * Returns the sub-client given by its name.
119
	 *
120
	 * @param string $type Name of the client type
121
	 * @param string|null $name Name of the sub-client (Default if null)
122
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
123
	 */
124
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
125
	{
126
		/** admin/jqadm/coupon/code/decorators/excludes
127
		 * Excludes decorators added by the "common" option from the coupon JQAdm client
128
		 *
129
		 * Decorators extend the functionality of a class by adding new aspects
130
		 * (e.g. log what is currently done), executing the methods of the underlying
131
		 * class only in certain conditions (e.g. only for logged in users) or
132
		 * modify what is returned to the caller.
133
		 *
134
		 * This option allows you to remove a decorator added via
135
		 * "admin/jqadm/common/decorators/default" before they are wrapped
136
		 * around the JQAdm client.
137
		 *
138
		 *  admin/jqadm/coupon/code/decorators/excludes = array( 'decorator1' )
139
		 *
140
		 * This would remove the decorator named "decorator1" from the list of
141
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
142
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
143
		 *
144
		 * @param array List of decorator names
145
		 * @since 2017.07
146
		 * @see admin/jqadm/common/decorators/default
147
		 * @see admin/jqadm/coupon/code/decorators/global
148
		 * @see admin/jqadm/coupon/code/decorators/local
149
		 */
150
151
		/** admin/jqadm/coupon/code/decorators/global
152
		 * Adds a list of globally available decorators only to the coupon JQAdm client
153
		 *
154
		 * Decorators extend the functionality of a class by adding new aspects
155
		 * (e.g. log what is currently done), executing the methods of the underlying
156
		 * class only in certain conditions (e.g. only for logged in users) or
157
		 * modify what is returned to the caller.
158
		 *
159
		 * This option allows you to wrap global decorators
160
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
161
		 *
162
		 *  admin/jqadm/coupon/code/decorators/global = array( 'decorator1' )
163
		 *
164
		 * This would add the decorator named "decorator1" defined by
165
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
166
		 *
167
		 * @param array List of decorator names
168
		 * @since 2017.07
169
		 * @see admin/jqadm/common/decorators/default
170
		 * @see admin/jqadm/coupon/code/decorators/excludes
171
		 * @see admin/jqadm/coupon/code/decorators/local
172
		 */
173
174
		/** admin/jqadm/coupon/code/decorators/local
175
		 * Adds a list of local decorators only to the coupon JQAdm client
176
		 *
177
		 * Decorators extend the functionality of a class by adding new aspects
178
		 * (e.g. log what is currently done), executing the methods of the underlying
179
		 * class only in certain conditions (e.g. only for logged in users) or
180
		 * modify what is returned to the caller.
181
		 *
182
		 * This option allows you to wrap local decorators
183
		 * ("\Aimeos\Admin\JQAdm\Coupon\Decorator\*") around the JQAdm client.
184
		 *
185
		 *  admin/jqadm/coupon/code/decorators/local = array( 'decorator2' )
186
		 *
187
		 * This would add the decorator named "decorator2" defined by
188
		 * "\Aimeos\Admin\JQAdm\Coupon\Decorator\Decorator2" only to the JQAdm client.
189
		 *
190
		 * @param array List of decorator names
191
		 * @since 2017.07
192
		 * @see admin/jqadm/common/decorators/default
193
		 * @see admin/jqadm/coupon/code/decorators/excludes
194
		 * @see admin/jqadm/coupon/code/decorators/global
195
		 */
196
		return $this->createSubClient( 'coupon/code/' . $type, $name );
197
	}
198
199
200
	/**
201
	 * Checks if an error during upload occured
202
	 *
203
	 * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
204
	 * @throws \Aimeos\Admin\JQAdm\Exception If an error occured during upload
205
	 */
206
	protected function checkFileUpload( \Psr\Http\Message\UploadedFileInterface $file )
207
	{
208
		if( $file->getError() !== UPLOAD_ERR_OK )
209
		{
210
			$ctx = $this->context();
211
212
			switch( $file->getError() )
213
			{
214
				case UPLOAD_ERR_INI_SIZE:
215
				case UPLOAD_ERR_FORM_SIZE:
216
					$msg = $ctx->translate( 'admin', 'The uploaded file exceeds the max. allowed filesize' ); break;
217
				case UPLOAD_ERR_PARTIAL:
218
					$msg = $ctx->translate( 'admin', 'The uploaded file was only partially uploaded' ); break;
219
				case UPLOAD_ERR_NO_FILE:
220
					$msg = $ctx->translate( 'admin', 'No file was uploaded' ); break;
221
				case UPLOAD_ERR_NO_TMP_DIR:
222
					$msg = $ctx->translate( 'admin', 'Temporary folder is missing' ); break;
223
				case UPLOAD_ERR_CANT_WRITE:
224
					$msg = $ctx->translate( 'admin', 'Failed to write file to disk' ); break;
225
				case UPLOAD_ERR_EXTENSION:
226
					$msg = $ctx->translate( 'admin', 'File upload stopped by extension' ); break;
227
				default:
228
					$msg = $ctx->translate( 'admin', 'Unknown upload error' );
229
			}
230
231
			throw new \Aimeos\Admin\JQAdm\Exception( $msg );
232
		}
233
	}
234
235
236
	/**
237
	 * Returns the coupon code items associated by the coupon item
238
	 *
239
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
240
	 * @param array $params Associative list of GET/POST parameters
241
	 * @param int $total Value/result parameter that will contain the item total afterwards
242
	 * @return \Aimeos\Map Coupon code items implementing \Aimeos\MShop\Coupon\Item\Code\Iface associated to the coupon item
243
	 */
244
	protected function getCodeItems( \Aimeos\MShop\Coupon\Item\Iface $item, array $params = [], ?int &$total = null ) : \Aimeos\Map
245
	{
246
		$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' );
247
248
		$search = $manager->filter();
249
		$search->setSortations( [$search->sort( '+', 'coupon.code.code' )] );
250
251
		$search = $this->initCriteria( $search, $params );
252
		$expr = [
253
			$search->compare( '==', 'coupon.code.parentid', $item->getId() ),
254
			$search->getConditions(),
255
		];
256
		$search->setConditions( $search->and( $expr ) );
257
258
		return $manager->search( $search, [], $total );
259
	}
260
261
262
	/**
263
	 * Returns the list of sub-client names configured for the client.
264
	 *
265
	 * @return array List of JQAdm client names
266
	 */
267
	protected function getSubClientNames() : array
268
	{
269
		/** admin/jqadm/coupon/code/subparts
270
		 * List of JQAdm sub-clients rendered within the coupon code section
271
		 *
272
		 * The output of the frontend is composed of the code generated by the JQAdm
273
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
274
		 * that are responsible for rendering certain sub-parts of the output. The
275
		 * sub-clients can contain JQAdm clients themselves and therefore a
276
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
277
		 * the output that is placed inside the container of its parent.
278
		 *
279
		 * At first, always the JQAdm code generated by the parent is printed, then
280
		 * the JQAdm code of its sub-clients. The code of the JQAdm sub-clients
281
		 * determines the code of the output of these sub-clients inside the parent
282
		 * container. If the configured list of clients is
283
		 *
284
		 *  array( "subclient1", "subclient2" )
285
		 *
286
		 * you can easily change the code of the output by recodeing the subparts:
287
		 *
288
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
289
		 *
290
		 * You can also remove one or more parts if they shouldn't be rendered:
291
		 *
292
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
293
		 *
294
		 * As the clients only generates structural JQAdm, the layout defined via CSS
295
		 * should support adding, removing or recodeing content by a fluid like
296
		 * design.
297
		 *
298
		 * @param array List of sub-client names
299
		 * @since 2017.07
300
		 */
301
		return $this->context()->config()->get( 'admin/jqadm/coupon/code/subparts', [] );
302
	}
303
304
305
	/**
306
	 * Creates new and updates existing items using the data array
307
	 *
308
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
309
	 * @param array $data Data array
310
	 * @return \Aimeos\MShop\Coupon\Item\Iface Modified coupon item
311
	 */
312
	protected function fromArray( \Aimeos\MShop\Coupon\Item\Iface $item, array $data ) : \Aimeos\MShop\Coupon\Item\Iface
313
	{
314
		if( ( $ids = $this->val( $data, 'coupon.code.id', [] ) ) === [] ) {
315
			return $item;
316
		}
317
318
		$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' );
319
		$filter = $manager->filter()->add( ['coupon.code.id' => $ids] )->slice( 0, count( $ids ) );
320
		$items = $manager->search( $filter );
321
322
		foreach( $ids as $idx => $id )
323
		{
324
			if( !isset( $items[$id] ) ) {
325
				$citem = $manager->create();
326
			} else {
327
				$citem = $items[$id];
328
			}
329
330
			$citem->setId( $id );
331
			$citem->setParentId( $item->getId() );
332
			$citem->setCode( $this->val( $data, 'coupon.code.code/' . $idx, $citem->getCode() ) );
333
			$citem->setCount( $this->val( $data, 'coupon.code.count/' . $idx, $citem->getCount() ) );
334
			$citem->setDateStart( $this->val( $data, 'coupon.code.datestart/' . $idx, $citem->getDateStart() ) );
335
			$citem->setDateEnd( $this->val( $data, 'coupon.code.dateend/' . $idx, $citem->getDateEnd() ) );
336
			$citem->setRef( $this->val( $data, 'coupon.code.ref/' . $idx, $citem->getRef() ) );
337
338
			$manager->save( $citem, false );
339
		}
340
341
		return $item;
342
	}
343
344
345
	/**
346
	 * Stores the uploaded CSV file containing the coupon codes
347
	 *
348
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
349
	 * @param array $files File upload array including the PSR-7 file upload objects
350
	 */
351
	protected function storeFile( \Aimeos\MShop\Coupon\Item\Iface $item, array $files )
352
	{
353
		$file = $this->val( $files, 'code/file' );
354
355
		if( $file == null || $file->getError() === UPLOAD_ERR_NO_FILE ) {
356
			return;
357
		}
358
359
		$this->checkFileUpload( $file );
360
361
		$context = $this->context();
362
		$fs = $context->fs( 'fs-import' );
363
		$dir = 'couponcode/' . $context->locale()->getSiteItem()->getCode();
364
365
		if( $fs->isdir( $dir ) === false ) {
366
			$fs->mkdir( $dir );
367
		}
368
369
		$fs->writes( $dir . '/' . $item->getId() . '.csv', $file->getStream()->detach() );
370
	}
371
372
373
	/**
374
	 * Constructs the data array for the view from the given item
375
	 *
376
	 * @param \Aimeos\Map $items Coupon code items implementing \Aimeos\MShop\Coupon\Item\Code\Iface
377
	 * @return string[] Multi-dimensional associative list of item data
378
	 */
379
	protected function toArray( \Aimeos\Map $items )
380
	{
381
		$data = [];
382
383
		foreach( $items as $item )
384
		{
385
			foreach( $item->toArray( true ) as $key => $value ) {
386
				$data[$key][] = $value;
387
			}
388
		}
389
390
		return $data;
391
	}
392
393
394
	/**
395
	 * Returns the rendered template including the view data
396
	 *
397
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
398
	 * @return string HTML output
399
	 */
400
	protected function render( \Aimeos\Base\View\Iface $view ) : string
401
	{
402
		/** admin/jqadm/coupon/code/template-item
403
		 * Relative path to the HTML body template of the code subpart for coupons.
404
		 *
405
		 * The template file contains the HTML code and processing instructions
406
		 * to generate the result shown in the body of the frontend. The
407
		 * configuration string is the path to the template file relative
408
		 * to the templates directory (usually in templates/admin/jqadm).
409
		 *
410
		 * You can overwrite the template file configuration in extensions and
411
		 * provide alternative templates. These alternative templates should be
412
		 * named like the default one but with the string "default" replaced by
413
		 * an unique name. You may use the name of your project for this. If
414
		 * you've implemented an alternative client class as well, "default"
415
		 * should be replaced by the name of the new class.
416
		 *
417
		 * @param string Relative path to the template creating the HTML code
418
		 * @since 2016.04
419
		 */
420
		$tplconf = 'admin/jqadm/coupon/code/template-item';
421
		$default = 'coupon/item-code';
422
423
		return $view->render( $view->config( $tplconf, $default ) );
424
	}
425
}
426