Completed
Push — master ( 0170f4...da2a64 )
by Aimeos
08:26
created

Standard::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 25
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Review;
12
13
sprintf( 'review' ); // for translation
14
15
16
/**
17
 * Default implementation of review 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
	 * Deletes a resource
28
	 *
29
	 * @return string|null HTML output
30
	 */
31
	public function delete() : ?string
32
	{
33
		$view = $this->getView();
34
35
		$manager = \Aimeos\MShop::create( $this->getContext(), 'review' );
36
		$manager->begin();
37
38
		try
39
		{
40
			if( ( $ids = $view->param( 'id' ) ) === null ) {
41
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
42
			}
43
44
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
45
			$search->setConditions( $search->compare( '==', 'review.id', $ids ) );
46
			$items = $manager->searchItems( $search );
47
48
			foreach( $items as $item )
49
			{
50
				$view->item = $item;
51
				parent::delete();
52
			}
53
54
			$manager->deleteItems( $items->toArray() );
55
			$manager->commit();
56
57
			$this->update( $items->toArray() )->redirect( 'review', 'search', null, 'delete' );
58
		}
59
		catch( \Exception $e )
60
		{
61
			$manager->rollback();
62
			$this->report( $e, 'delete' );
63
		}
64
65
		return $this->search();
66
	}
67
68
69
	/**
70
	 * Returns a single resource
71
	 *
72
	 * @return string|null HTML output
73
	 */
74
	public function get() : ?string
75
	{
76
		$view = $this->getObject()->addData( $this->getView() );
77
78
		try
79
		{
80
			if( ( $id = $view->param( 'id' ) ) === null ) {
81
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
82
			}
83
84
			$manager = \Aimeos\MShop::create( $this->getContext(), 'review' );
85
86
			$view->item = $manager->getItem( $id );
87
			$view->itemSubparts = $this->getSubClientNames();
88
			$view->itemData = $this->toArray( $view->item );
89
			$view->itemBody = parent::get();
90
		}
91
		catch( \Exception $e )
92
		{
93
			$this->report( $e, 'get' );
94
		}
95
96
		return $this->render( $view );
97
	}
98
99
100
	/**
101
	 * Saves the data
102
	 *
103
	 * @return string|null HTML output
104
	 */
105
	public function save() : ?string
106
	{
107
		$view = $this->getView();
108
109
		$manager = \Aimeos\MShop::create( $this->getContext(), 'review' );
110
		$manager->begin();
111
112
		try
113
		{
114
			$item = $this->fromArray( $view->param( 'item', [] ) );
115
			$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

115
			$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...
116
			$view->itemBody = parent::save();
117
118
			$item = $manager->saveItem( clone $view->item );
119
			$manager->commit();
120
121
			return $this->update( [$item] )->redirect( 'review', $view->param( 'next' ), $view->item->getId(), 'save' );
122
		}
123
		catch( \Exception $e )
124
		{
125
			$manager->rollback();
126
			$this->report( $e, 'save' );
127
		}
128
129
		return $this->create();
130
	}
131
132
133
	/**
134
	 * Returns a list of resource according to the conditions
135
	 *
136
	 * @return string|null HTML output
137
	 */
138
	public function search() : ?string
139
	{
140
		$view = $this->getView();
141
142
		try
143
		{
144
			$total = 0;
145
			$params = $this->storeSearchParams( $view->param(), 'review' );
146
			$manager = \Aimeos\MShop::create( $this->getContext(), 'review' );
147
148
			$search = $manager->createSearch();
149
			$search->setSortations( [$search->sort( '-', 'review.ctime' )] );
150
			$search = $this->initCriteria( $search, $params );
151
152
			$view->items = $manager->searchItems( $search, [], $total );
153
			$view->filterAttributes = $manager->getSearchAttributes( true );
154
			$view->filterOperators = $search->getOperators();
155
			$view->itemBody = parent::search();
156
			$view->total = $total;
157
		}
158
		catch( \Exception $e )
159
		{
160
			$this->report( $e, 'search' );
161
		}
162
163
		/** admin/jqadm/review/template-list
164
		 * Relative path to the HTML body template for the review list.
165
		 *
166
		 * The template file contains the HTML code and processing instructions
167
		 * to generate the result shown in the body of the frontend. The
168
		 * configuration string is the path to the template file relative
169
		 * to the templates directory (usually in admin/jqadm/templates).
170
		 *
171
		 * You can overwrite the template file configuration in extensions and
172
		 * provide alternative templates. These alternative templates should be
173
		 * named like the default one but with the string "default" replaced by
174
		 * an unique name. You may use the name of your project for this. If
175
		 * you've implemented an alternative client class as well, "default"
176
		 * should be replaced by the name of the new class.
177
		 *
178
		 * @param string Relative path to the template creating the HTML code
179
		 * @since 2016.04
180
		 * @category Developer
181
		 */
182
		$tplconf = 'admin/jqadm/review/template-list';
183
		$default = 'review/list-standard';
184
185
		return $view->render( $view->config( $tplconf, $default ) );
186
	}
187
188
189
	/**
190
	 * Returns the sub-client given by its name.
191
	 *
192
	 * @param string $type Name of the client type
193
	 * @param string|null $name Name of the sub-client (Default if null)
194
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
195
	 */
196
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
197
	{
198
		/** admin/jqadm/review/decorators/excludes
199
		 * Excludes decorators added by the "common" option from the review JQAdm client
200
		 *
201
		 * Decorators extend the functionality of a class by adding new aspects
202
		 * (e.g. log what is currently done), executing the methods of the underlying
203
		 * class only in certain conditions (e.g. only for logged in users) or
204
		 * modify what is returned to the caller.
205
		 *
206
		 * This option allows you to remove a decorator added via
207
		 * "client/jqadm/common/decorators/default" before they are wrapped
208
		 * around the JQAdm client.
209
		 *
210
		 *  admin/jqadm/review/decorators/excludes = array( 'decorator1' )
211
		 *
212
		 * This would remove the decorator named "decorator1" from the list of
213
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
214
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
215
		 *
216
		 * @param array List of decorator names
217
		 * @since 2020.10
218
		 * @category Developer
219
		 * @see admin/jqadm/common/decorators/default
220
		 * @see admin/jqadm/review/decorators/global
221
		 * @see admin/jqadm/review/decorators/local
222
		 */
223
224
		/** admin/jqadm/review/decorators/global
225
		 * Adds a list of globally available decorators only to the review JQAdm client
226
		 *
227
		 * Decorators extend the functionality of a class by adding new aspects
228
		 * (e.g. log what is currently done), executing the methods of the underlying
229
		 * class only in certain conditions (e.g. only for logged in users) or
230
		 * modify what is returned to the caller.
231
		 *
232
		 * This option allows you to wrap global decorators
233
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
234
		 *
235
		 *  admin/jqadm/review/decorators/global = array( 'decorator1' )
236
		 *
237
		 * This would add the decorator named "decorator1" defined by
238
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
239
		 *
240
		 * @param array List of decorator names
241
		 * @since 2020.10
242
		 * @category Developer
243
		 * @see admin/jqadm/common/decorators/default
244
		 * @see admin/jqadm/review/decorators/excludes
245
		 * @see admin/jqadm/review/decorators/local
246
		 */
247
248
		/** admin/jqadm/review/decorators/local
249
		 * Adds a list of local decorators only to the review JQAdm client
250
		 *
251
		 * Decorators extend the functionality of a class by adding new aspects
252
		 * (e.g. log what is currently done), executing the methods of the underlying
253
		 * class only in certain conditions (e.g. only for logged in users) or
254
		 * modify what is returned to the caller.
255
		 *
256
		 * This option allows you to wrap local decorators
257
		 * ("\Aimeos\Admin\JQAdm\Review\Decorator\*") around the JQAdm client.
258
		 *
259
		 *  admin/jqadm/review/decorators/local = array( 'decorator2' )
260
		 *
261
		 * This would add the decorator named "decorator2" defined by
262
		 * "\Aimeos\Admin\JQAdm\Review\Decorator\Decorator2" only to the JQAdm client.
263
		 *
264
		 * @param array List of decorator names
265
		 * @since 2020.10
266
		 * @category Developer
267
		 * @see admin/jqadm/common/decorators/default
268
		 * @see admin/jqadm/review/decorators/excludes
269
		 * @see admin/jqadm/review/decorators/global
270
		 */
271
		return $this->createSubClient( 'review/' . $type, $name );
272
	}
273
274
275
	/**
276
	 * Returns the list of sub-client names configured for the client.
277
	 *
278
	 * @return array List of JQAdm client names
279
	 */
280
	protected function getSubClientNames() : array
281
	{
282
		/** admin/jqadm/review/standard/subparts
283
		 * List of JQAdm sub-clients rendered within the review section
284
		 *
285
		 * The output of the frontend is composed of the code generated by the JQAdm
286
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
287
		 * that are responsible for rendering certain sub-parts of the output. The
288
		 * sub-clients can contain JQAdm clients themselves and therefore a
289
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
290
		 * the output that is placed inside the container of its parent.
291
		 *
292
		 * At first, always the JQAdm code generated by the parent is printed, then
293
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
294
		 * determines the order of the output of these sub-clients inside the parent
295
		 * container. If the configured list of clients is
296
		 *
297
		 *  array( "subclient1", "subclient2" )
298
		 *
299
		 * you can easily change the order of the output by reordering the subparts:
300
		 *
301
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
302
		 *
303
		 * You can also remove one or more parts if they shouldn't be rendered:
304
		 *
305
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
306
		 *
307
		 * As the clients only generates structural JQAdm, the layout defined via CSS
308
		 * should support adding, removing or reordering content by a fluid like
309
		 * design.
310
		 *
311
		 * @param array List of sub-client names
312
		 * @since 2020.10
313
		 * @category Developer
314
		 */
315
		return $this->getContext()->getConfig()->get( 'admin/jqadm/review/standard/subparts', [] );
316
	}
317
318
319
	/**
320
	 * Creates new and updates existing items using the data array
321
	 *
322
	 * @param array $data Data array
323
	 * @return \Aimeos\MShop\Review\Item\Iface New review item object
324
	 */
325
	protected function fromArray( array $data ) : \Aimeos\MShop\Review\Item\Iface
326
	{
327
		$manager = \Aimeos\MShop::create( $this->getContext(), 'review' );
328
329
		if( isset( $data['review.id'] ) && $data['review.id'] != '' ) {
330
			$item = $manager->getItem( $data['review.id'] );
331
		} else {
332
			$item = $manager->createItem();
333
		}
334
335
		return $item->fromArray( $data, true );
336
	}
337
338
339
	/**
340
	 * Constructs the data array for the view from the given item
341
	 *
342
	 * @param \Aimeos\MShop\Review\Item\Iface $item Review item object
343
	 * @return string[] Multi-dimensional associative list of item data
344
	 */
345
	protected function toArray( \Aimeos\MShop\Review\Item\Iface $item, bool $copy = false ) : array
346
	{
347
		return $item->toArray( true );
348
	}
349
350
351
	/**
352
	 * Returns the rendered template including the view data
353
	 *
354
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
355
	 * @return string HTML output
356
	 */
357
	protected function render( \Aimeos\MW\View\Iface $view ) : string
358
	{
359
		/** admin/jqadm/review/template-item
360
		 * Relative path to the HTML body template for the review item.
361
		 *
362
		 * The template file contains the HTML code and processing instructions
363
		 * to generate the result shown in the body of the frontend. The
364
		 * configuration string is the path to the template file relative
365
		 * to the templates directory (usually in admin/jqadm/templates).
366
		 *
367
		 * You can overwrite the template file configuration in extensions and
368
		 * provide alternative templates. These alternative templates should be
369
		 * named like the default one but with the string "default" replaced by
370
		 * an unique name. You may use the name of your project for this. If
371
		 * you've implemented an alternative client class as well, "default"
372
		 * should be replaced by the name of the new class.
373
		 *
374
		 * @param string Relative path to the template creating the HTML code
375
		 * @since 2016.04
376
		 * @category Developer
377
		 */
378
		$tplconf = 'admin/jqadm/review/template-item';
379
		$default = 'review/item-standard';
380
381
		return $view->render( $view->config( $tplconf, $default ) );
382
	}
383
384
385
	/**
386
	 * Updates the average rating and the number of ratings in the domain item
387
	 *
388
	 * @param \Aimeos\MShop\Review\Item\Iface[] $item List of review items with domain and refid
389
	 * @return \Aimeos\Admin\JQAdm\Iface Admin client for fluent interface
390
	 */
391
	protected function update( array $items ) : \Aimeos\Admin\JQAdm\Iface
392
	{
393
		$context = $this->getContext();
394
		$manager = \Aimeos\MShop::create( $context, 'review' );
395
396
		foreach( $items as $item )
397
		{
398
			$filter = $manager->filter( true )->add( [
399
				'review.domain' => $item->getDomain(),
400
				'review.refid' => $item->getRefId(),
401
				'review.status' => 1
402
			] );
403
404
			if( $entry = $manager->aggregate( $filter, 'review.refid', 'review.rating', 'rate' )->first() )
405
			{
406
				$rateManager = \Aimeos\MShop::create( $context, $item->getDomain() );
407
				$rateManager->rate( $item->getRefId(), $entry['sum'], $entry['count'] );
408
409
				$context->cache()->deleteByTags( [$item->getDomain() . '-' . $item->getRefId()] );
410
			}
411
		}
412
413
		return $this;
414
	}
415
}
416