Passed
Push — master ( f25c12...e9f33e )
by Aimeos
03:32
created

Standard::delete()   A

Complexity

Conditions 5
Paths 17

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 17
nop 0
dl 0
loc 44
rs 9.2248
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), 2020-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Review;
12
13
sprintf( 'marketing' ); // for translation
14
sprintf( 'review' ); // for translation
15
16
17
/**
18
 * Default implementation of review JQAdm client.
19
 *
20
 * @package Admin
21
 * @subpackage JQAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
25
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
26
{
27
	/** admin/jqadm/review/name
28
	 * Class name of the used account favorite client implementation
29
	 *
30
	 * Each default admin client can be replace by an alternative imlementation.
31
	 * To use this implementation, you have to set the last part of the class
32
	 * name as configuration value so the client factory knows which class it
33
	 * has to instantiate.
34
	 *
35
	 * For example, if the name of the default class is
36
	 *
37
	 *  \Aimeos\Admin\JQAdm\Review\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Review\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/review/name = Myfavorite
46
	 *
47
	 * The value is the last part of your own class name and it's case sensitive,
48
	 * so take care that the configuration value is exactly named like the last
49
	 * part of the class name.
50
	 *
51
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
52
	 * characters are possible! You should always start the last part of the class
53
	 * name with an upper case character and continue only with lower case characters
54
	 * or numbers. Avoid chamel case names like "MyFavorite"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2017.07
58
	 * @category Developer
59
	 */
60
61
62
	/**
63
	 * Batch update of a resource
64
	 *
65
	 * @return string|null Output to display
66
	 */
67
	public function batch() : ?string
68
	{
69
		return $this->batchBase( 'plugin' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('plugin') 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...
70
	}
71
72
73
	/**
74
	 * Deletes a resource
75
	 *
76
	 * @return string|null HTML output
77
	 */
78
	public function delete() : ?string
79
	{
80
		$view = $this->view();
81
		$context = $this->context();
82
83
		if( !$view->access( ['super', 'admin'] ) )
84
		{
85
			$msg = $context->translate( 'admin', 'Deleting reviews is not allowed' );
86
			throw new \Aimeos\Admin\JQAdm\Exception( $msg );
87
		}
88
89
		$manager = \Aimeos\MShop::create( $context, 'review' );
90
		$manager->begin();
91
92
		try
93
		{
94
			if( ( $ids = $view->param( 'id' ) ) === null )
95
			{
96
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
97
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
98
			}
99
100
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
101
			$search->setConditions( $search->compare( '==', 'review.id', $ids ) );
102
			$items = $manager->search( $search );
103
104
			foreach( $items as $item )
105
			{
106
				$view->item = $item;
107
				parent::delete();
108
			}
109
110
			$manager->delete( $items->toArray() );
111
			$manager->commit();
112
113
			$this->update( $items->toArray() )->redirect( 'review', 'search', null, 'delete' );
114
		}
115
		catch( \Exception $e )
116
		{
117
			$manager->rollback();
118
			$this->report( $e, 'delete' );
119
		}
120
121
		return $this->search();
122
	}
123
124
125
	/**
126
	 * Returns a single resource
127
	 *
128
	 * @return string|null HTML output
129
	 */
130
	public function get() : ?string
131
	{
132
		$view = $this->object()->data( $this->view() );
133
134
		try
135
		{
136
			if( ( $id = $view->param( 'id' ) ) === null )
137
			{
138
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
139
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
140
			}
141
142
			$manager = \Aimeos\MShop::create( $this->context(), 'review' );
143
144
			$view->item = $manager->get( $id );
145
			$view->itemSubparts = $this->getSubClientNames();
146
			$view->itemData = $this->toArray( $view->item );
147
			$view->itemBody = parent::get();
148
		}
149
		catch( \Exception $e )
150
		{
151
			$this->report( $e, 'get' );
152
		}
153
154
		return $this->render( $view );
155
	}
156
157
158
	/**
159
	 * Saves the data
160
	 *
161
	 * @return string|null HTML output
162
	 */
163
	public function save() : ?string
164
	{
165
		$view = $this->view();
166
167
		$manager = \Aimeos\MShop::create( $this->context(), 'review' );
168
		$manager->begin();
169
170
		try
171
		{
172
			$item = $this->fromArray( $view->param( 'item', [] ) );
173
			$view->item = $item->getId() ? $item : $manager->save( $item );
174
			$view->itemBody = parent::save();
175
176
			$item = $manager->save( clone $view->item );
177
			$manager->commit();
178
179
			return $this->update( [$item] )->redirect( 'review', $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

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