1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2018-2020 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Frontend |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Frontend\Review; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default implementation of the review frontend controller. |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Frontend |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Frontend\Base |
22
|
|
|
implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
23
|
|
|
{ |
24
|
|
|
private $conditions = []; |
25
|
|
|
private $filter; |
26
|
|
|
private $manager; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Common initialization for controller classes |
31
|
|
|
* |
32
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
33
|
|
|
*/ |
34
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
35
|
|
|
{ |
36
|
|
|
parent::__construct( $context ); |
37
|
|
|
|
38
|
|
|
$this->manager = \Aimeos\MShop::create( $context, 'review' ); |
39
|
|
|
$this->filter = $this->manager->filter(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Clones objects in controller and resets values |
45
|
|
|
*/ |
46
|
|
|
public function __clone() |
47
|
|
|
{ |
48
|
|
|
$this->filter = clone $this->filter; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Adds generic condition for filtering |
54
|
|
|
* |
55
|
|
|
* @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
56
|
|
|
* @param string $key Search key defined by the review manager, e.g. "review.status" |
57
|
|
|
* @param array|string $value Value or list of values to compare to |
58
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
59
|
|
|
* @since 2020.10 |
60
|
|
|
*/ |
61
|
|
|
public function compare( string $operator, string $key, $value ) : Iface |
62
|
|
|
{ |
63
|
|
|
$this->conditions[] = $this->filter->compare( $operator, $key, $value ); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns a new rating item |
70
|
|
|
* |
71
|
|
|
* @param array $vals Associative list of key/value pairs to initialize the item |
72
|
|
|
* @return \Aimeos\MShop\Review\Item\Iface New review item |
73
|
|
|
*/ |
74
|
|
|
public function create( array $vals = [] ) : \Aimeos\MShop\Review\Item\Iface |
75
|
|
|
{ |
76
|
|
|
return $this->manager->create()->setOrderProductId( $vals['review.orderproductid'] ?? '' )->fromArray( $vals ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Deletes the review item for the given ID or IDs |
82
|
|
|
* |
83
|
|
|
* @param array|string $id Unique review ID or list of IDs |
84
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
85
|
|
|
* @since 2020.10 |
86
|
|
|
*/ |
87
|
|
|
public function delete( $ids ) : Iface |
88
|
|
|
{ |
89
|
|
|
$ids = (array) $ids; |
90
|
|
|
$filter = $this->manager->filter()->add( ['review.id' => $ids] ); |
91
|
|
|
$this->manager->delete( $this->manager->search( $filter->slice( 0, count( $ids ) ) )->toArray() ); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the review domain for filtering |
99
|
|
|
* |
100
|
|
|
* @param string $domain Domain (e.g. "product") of the reviewed items |
101
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
102
|
|
|
* @since 2020.10 |
103
|
|
|
*/ |
104
|
|
|
public function domain( string $domain ) : Iface |
105
|
|
|
{ |
106
|
|
|
$this->conditions['domain'] = $this->filter->compare( '==', 'review.domain', $domain ); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Restricts the reviews to a specific domain item |
113
|
|
|
* |
114
|
|
|
* @param string $domain Domain the reviews belong to (e.g. "product") |
115
|
|
|
* @param string|null $refid Id of the item the reviews belong to or NULL for all reviews from the domain |
116
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
117
|
|
|
* @since 2020.10 |
118
|
|
|
*/ |
119
|
|
|
public function for( string $domain, ?string $refid ) : Iface |
120
|
|
|
{ |
121
|
|
|
$this->conditions['domain'] = $this->filter->compare( '==', 'review.domain', $domain ); |
122
|
|
|
|
123
|
|
|
if( $refid !== null ) { |
124
|
|
|
$this->conditions['refid'] = $this->filter->compare( '==', 'review.refid', $refid ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the review item for the given ID |
133
|
|
|
* |
134
|
|
|
* @param string $id Unique review ID |
135
|
|
|
* @return \Aimeos\MShop\Review\Item\Iface Review object |
136
|
|
|
*/ |
137
|
|
|
public function get( string $id ) : \Aimeos\MShop\Review\Item\Iface |
138
|
|
|
{ |
139
|
|
|
return $this->manager->getItem( $id, [], true ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the reviews for the logged-in user |
145
|
|
|
* |
146
|
|
|
* @param int &$total Parameter where the total number of found reviews will be stored in |
147
|
|
|
* @return \Aimeos\Map Ordered list of review items implementing \Aimeos\MShop\Review\Item\Iface |
148
|
|
|
* @since 2020.10 |
149
|
|
|
*/ |
150
|
|
|
public function list( int &$total = null ) : \Aimeos\Map |
151
|
|
|
{ |
152
|
|
|
$filter = clone $this->filter; |
153
|
|
|
$cond = $filter->is( 'review.customerid', '==', $this->getContext()->getUserId() ); |
154
|
|
|
|
155
|
|
|
$filter->setConditions( $filter->combine( '&&', array_merge( $this->conditions, [$cond] ) ) ); |
156
|
|
|
return $this->manager->searchItems( $filter, [], $total ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Parses the given array and adds the conditions to the list of conditions |
162
|
|
|
* |
163
|
|
|
* @param array $conditions List of conditions, e.g. ['>' => ['review.rating' => 3]] |
164
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
165
|
|
|
* @since 2020.10 |
166
|
|
|
*/ |
167
|
|
|
public function parse( array $conditions ) : Iface |
168
|
|
|
{ |
169
|
|
|
if( ( $cond = $this->filter->toConditions( $conditions ) ) !== null ) { |
170
|
|
|
$this->conditions[] = $cond; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Adds or updates a review |
179
|
|
|
* |
180
|
|
|
* @param \Aimeos\MShop\Review\Item\Iface $item Review item including required data |
181
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
182
|
|
|
* @since 2020.10 |
183
|
|
|
*/ |
184
|
|
|
public function save( \Aimeos\MShop\Review\Item\Iface $item ) : \Aimeos\MShop\Review\Item\Iface |
185
|
|
|
{ |
186
|
|
|
if( !in_array( $item->getDomain(), ['product'] ) ) |
187
|
|
|
{ |
188
|
|
|
$msg = sprintf( 'Domain "%1$s" is not supported', $item->getDomain() ); |
189
|
|
|
throw new \Aimeos\Controller\Frontend\Review\Exception( $msg ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$context = $this->getContext(); |
193
|
|
|
$manager = \Aimeos\MShop::create( $context, 'order/base' ); |
194
|
|
|
|
195
|
|
|
$filter = $manager->filter( true )->add( [ |
196
|
|
|
'order.base.product.id' => $item->getOrderProductId(), |
197
|
|
|
'order.base.customerid' => $context->getUserId() |
198
|
|
|
] ); |
199
|
|
|
$manager->search( $filter->slice( 0, 1 ) )->first( new \Aimeos\Controller\Frontend\Review\Exception( |
200
|
|
|
sprintf( 'You can only add a review if you have ordered a product' ) |
201
|
|
|
) ); |
202
|
|
|
|
203
|
|
|
$orderProductItem = \Aimeos\MShop::create( $context, 'order/base/product' )->get( $item->getOrderProductId() ); |
|
|
|
|
204
|
|
|
|
205
|
|
|
$filter = $this->manager->filter()->add( [ |
206
|
|
|
'review.customerid' => $context->getUserId(), |
207
|
|
|
'review.id' => $item->getId() |
208
|
|
|
] ); |
209
|
|
|
|
210
|
|
|
$real = $this->manager->search( $filter->slice( 0, 1 ) )->first( $this->manager->create() ); |
211
|
|
|
|
212
|
|
|
$real = $real->setCustomerId( $context->getUserId() ) |
213
|
|
|
->setOrderProductId( $orderProductItem->getId() ) |
214
|
|
|
->setRefId( $orderProductItem->getProductId() ) |
215
|
|
|
->setComment( $item->getComment() ) |
216
|
|
|
->setRating( $item->getRating() ) |
217
|
|
|
->setName( $item->getName() ) |
218
|
|
|
->setDomain( 'product' ); |
219
|
|
|
|
220
|
|
|
$item = $this->manager->save( $real ); |
221
|
|
|
|
222
|
|
|
$filter = $this->manager->filter( true )->add( [ |
223
|
|
|
'review.domain' => $item->getDomain(), |
224
|
|
|
'review.refid' => $item->getRefId() |
|
|
|
|
225
|
|
|
] ); |
226
|
|
|
|
227
|
|
|
if( $entry = $this->manager->aggregate( $filter, 'review.refid', 'review.rating', 'rate' )->first() ) |
228
|
|
|
{ |
229
|
|
|
$rateManager = \Aimeos\MShop::create( $context, $item->getDomain() ); |
230
|
|
|
$rateManager->rate( $item->getId(), $entry['sum'], $entry['count'] ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $item; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Returns the reviews filtered by the previously assigned conditions |
239
|
|
|
* |
240
|
|
|
* @param int &$total Parameter where the total number of found reviews will be stored in |
241
|
|
|
* @return \Aimeos\Map Ordered list of review items implementing \Aimeos\MShop\Review\Item\Iface |
242
|
|
|
* @since 2020.10 |
243
|
|
|
*/ |
244
|
|
|
public function search( int &$total = null ) : \Aimeos\Map |
245
|
|
|
{ |
246
|
|
|
$filter = clone $this->filter; |
247
|
|
|
$cond = $filter->is( 'review.status', '>', 0 ); |
248
|
|
|
|
249
|
|
|
$filter->setConditions( $filter->combine( '&&', array_merge( $this->conditions, [$cond] ) ) ); |
250
|
|
|
return $this->manager->searchItems( $filter, [], $total ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Sets the start value and the number of returned review items for slicing the list of found review items |
256
|
|
|
* |
257
|
|
|
* @param int $start Start value of the first review item in the list |
258
|
|
|
* @param int $limit Number of returned review items |
259
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
260
|
|
|
* @since 2020.10 |
261
|
|
|
*/ |
262
|
|
|
public function slice( int $start, int $limit ) : Iface |
263
|
|
|
{ |
264
|
|
|
$this->filter->setSlice( $start, $limit ); |
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Sets the sorting of the result list |
271
|
|
|
* |
272
|
|
|
* @param string|null $key Sorting key of the result list like "mtime" or "rating", null for no sorting |
273
|
|
|
* @return \Aimeos\Controller\Frontend\Review\Iface Review controller for fluent interface |
274
|
|
|
* @since 2020.10 |
275
|
|
|
*/ |
276
|
|
|
public function sort( string $key = null ) : Iface |
277
|
|
|
{ |
278
|
|
|
$sort = []; |
279
|
|
|
$list = ( $key ? explode( ',', $key ) : [] ); |
280
|
|
|
|
281
|
|
|
foreach( $list as $sortkey ) |
282
|
|
|
{ |
283
|
|
|
$direction = ( $sortkey[0] === '-' ? '-' : '+' ); |
284
|
|
|
$sortkey = ltrim( $sortkey, '+-' ); |
285
|
|
|
|
286
|
|
|
switch( $sortkey ) |
287
|
|
|
{ |
288
|
|
|
case 'ctime': |
289
|
|
|
$sort[] = $this->filter->sort( $direction, 'review.ctime' ); |
290
|
|
|
break; |
291
|
|
|
case 'rating': |
292
|
|
|
$sort[] = $this->filter->sort( $direction, 'review.rating' ); |
293
|
|
|
break; |
294
|
|
|
default: |
295
|
|
|
$sort[] = $this->filter->sort( $direction, $sortkey ); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$this->filter->setSortations( $sort ); |
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|