1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
7
|
|
|
* @package Controller |
8
|
|
|
* @subpackage Frontend |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\Controller\Frontend\Order; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default implementation of the order frontend controller. |
17
|
|
|
* |
18
|
|
|
* @package Controller |
19
|
|
|
* @subpackage Frontend |
20
|
|
|
*/ |
21
|
|
|
class Standard |
22
|
|
|
extends \Aimeos\Controller\Frontend\Base |
23
|
|
|
implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
24
|
|
|
{ |
25
|
|
|
private $conditions = []; |
26
|
|
|
private $manager; |
27
|
|
|
private $filter; |
28
|
|
|
private $item; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes the controller |
33
|
|
|
* |
34
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
35
|
|
|
*/ |
36
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
37
|
|
|
{ |
38
|
|
|
parent::__construct( $context ); |
39
|
|
|
|
40
|
|
|
$this->manager = \Aimeos\MShop::create( $context, 'order' ); |
41
|
|
|
$this->item = $this->manager->createItem(); |
42
|
|
|
|
43
|
|
|
$this->filter = $this->manager->createSearch( true ); |
44
|
|
|
$this->conditions[] = $this->filter->compare( '==', 'order.base.customerid', $context->getUserId() ); |
45
|
|
|
$this->conditions[] = $this->filter->getConditions(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Clones objects in controller and resets values |
51
|
|
|
*/ |
52
|
|
|
public function __clone() |
53
|
|
|
{ |
54
|
|
|
$this->item = clone $this->item; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Adds the values to the order object (not yet stored) |
60
|
|
|
* |
61
|
|
|
* @param string $baseId ID of the stored basket |
62
|
|
|
* @param array $values Values added to the order item (new or existing) like "order.type" |
63
|
|
|
* @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface |
64
|
|
|
* @since 2019.04 |
65
|
|
|
*/ |
66
|
|
|
public function add( $baseId, array $values = [] ) |
67
|
|
|
{ |
68
|
|
|
$this->item = $this->item->fromArray( $values )->setBaseId( $baseId ); |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Adds generic condition for filtering orders |
75
|
|
|
* |
76
|
|
|
* @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
77
|
|
|
* @param string $key Search key defined by the order manager, e.g. "order.type" |
78
|
|
|
* @param array|string $value Value or list of values to compare to |
79
|
|
|
* @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface |
80
|
|
|
* @since 2019.04 |
81
|
|
|
*/ |
82
|
|
|
public function compare( $operator, $key, $value ) |
83
|
|
|
{ |
84
|
|
|
$this->conditions[] = $this->filter->compare( $operator, $key, $value ); |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the order for the given order ID |
91
|
|
|
* |
92
|
|
|
* @param string $id Unique order ID |
93
|
|
|
* @param boolean $default Use default criteria to limit orders |
94
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Order item object |
95
|
|
|
* @since 2019.04 |
96
|
|
|
*/ |
97
|
|
|
public function get( $id, $default = true ) |
98
|
|
|
{ |
99
|
|
|
return $this->manager->getItem( $id, [], $default ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Parses the given array and adds the conditions to the list of conditions |
105
|
|
|
* |
106
|
|
|
* @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['order.statuspayment' => 0]], ['==' => ['order.type' => 'web']]]] |
107
|
|
|
* @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface |
108
|
|
|
* @since 2019.04 |
109
|
|
|
*/ |
110
|
|
|
public function parse( array $conditions ) |
111
|
|
|
{ |
112
|
|
|
$this->conditions[] = $this->filter->toConditions( $conditions ); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Updates the given order item in the storage |
119
|
|
|
* |
120
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
121
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface $orderItem Saved order item object |
122
|
|
|
* @since 2019.04 |
123
|
|
|
*/ |
124
|
|
|
public function save( \Aimeos\MShop\Order\Item\Iface $orderItem ) |
125
|
|
|
{ |
126
|
|
|
return $this->manager->saveItem( $orderItem ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the orders filtered by the previously assigned conditions |
131
|
|
|
* |
132
|
|
|
* @param string[] $domains Domain names of items that are associated with the orders and that should be fetched too |
133
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface[] Ordered list of order items |
134
|
|
|
* @since 2019.04 |
135
|
|
|
*/ |
136
|
|
|
public function search( &$total = null ) |
137
|
|
|
{ |
138
|
|
|
$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) ); |
139
|
|
|
return $this->manager->searchItems( $this->filter, [], $total ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sets the start value and the number of returned orders for slicing the list of found orders |
145
|
|
|
* |
146
|
|
|
* @param integer $start Start value of the first order in the list |
147
|
|
|
* @param integer $limit Number of returned orders |
148
|
|
|
* @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface |
149
|
|
|
* @since 2019.04 |
150
|
|
|
*/ |
151
|
|
|
public function slice( $start, $limit ) |
152
|
|
|
{ |
153
|
|
|
$this->filter->setSlice( $start, $limit ); |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Sets the sorting of the result list |
160
|
|
|
* |
161
|
|
|
* @param string|null $key Sorting of the result list like "-order.id", null for no sorting |
162
|
|
|
* @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface |
163
|
|
|
* @since 2019.04 |
164
|
|
|
*/ |
165
|
|
|
public function sort( $key = null ) |
166
|
|
|
{ |
167
|
|
|
$direction = '+'; |
168
|
|
|
|
169
|
|
|
if( $key != null && $key[0] === '-' ) |
170
|
|
|
{ |
171
|
|
|
$key = substr( $key, 1 ); |
172
|
|
|
$direction = '-'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
switch( $key ) |
176
|
|
|
{ |
177
|
|
|
case null: |
178
|
|
|
$this->sort = null; |
|
|
|
|
179
|
|
|
break; |
180
|
|
|
|
181
|
|
|
default: |
182
|
|
|
$this->sort = $this->filter->sort( $direction, $key ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if( $this->sort ) { |
186
|
|
|
$this->filter->setSortations( [$this->sort] ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Saves the modified order item in the storage and blocks the stock and coupon codes |
195
|
|
|
* |
196
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface New or updated order item object |
197
|
|
|
* @since 2019.04 |
198
|
|
|
*/ |
199
|
|
|
public function store() |
200
|
|
|
{ |
201
|
|
|
$this->checkLimit( $this->item->getBaseId() ); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$cntl = \Aimeos\Controller\Common\Order\Factory::create( $this->getContext() ); |
204
|
|
|
$this->item = $this->manager->saveItem( $this->item ); |
205
|
|
|
|
206
|
|
|
return $cntl->block( $this->item ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Checks if more orders than allowed have been created by the user |
212
|
|
|
* |
213
|
|
|
* @param string $baseId Unique ID of the order base item (basket) |
214
|
|
|
* @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface |
215
|
|
|
* @throws \Aimeos\Controller\Frontend\Order\Exception If limit is exceeded |
216
|
|
|
*/ |
217
|
|
|
protected function checkLimit( $baseId ) |
218
|
|
|
{ |
219
|
|
|
/** controller/frontend/order/limit-seconds |
220
|
|
|
* Order limitation time frame in seconds |
221
|
|
|
* |
222
|
|
|
* Creating new orders is limited to avoid abuse and mitigate denial of |
223
|
|
|
* service attacks. Within the configured time frame, only one order |
224
|
|
|
* item can be created per order base item. All orders for the order |
225
|
|
|
* base item within the last X seconds are counted. If there's already |
226
|
|
|
* one available, an error message will be shown to the user instead of |
227
|
|
|
* creating the new order item. |
228
|
|
|
* |
229
|
|
|
* @param integer Number of seconds to check order items within |
230
|
|
|
* @since 2017.05 |
231
|
|
|
* @category Developer |
232
|
|
|
* @see controller/frontend/basket/limit-count |
233
|
|
|
* @see controller/frontend/basket/limit-seconds |
234
|
|
|
*/ |
235
|
|
|
$seconds = $this->getContext()->getConfig()->get( 'controller/frontend/order/limit-seconds', 300 ); |
236
|
|
|
|
237
|
|
|
$search = $this->manager->createSearch()->setSlice( 0, 0 ); |
238
|
|
|
$search->setConditions( $search->combine( '&&', [ |
239
|
|
|
$search->compare( '==', 'order.baseid', $baseId ), |
240
|
|
|
$search->compare( '>=', 'order.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ), |
241
|
|
|
] ) ); |
242
|
|
|
|
243
|
|
|
$total = 0; |
244
|
|
|
$this->manager->searchItems( $search, [], $total ); |
245
|
|
|
|
246
|
|
|
if( $total > 0 ) { |
247
|
|
|
throw new \Aimeos\Controller\Frontend\Order\Exception( sprintf( 'The order has already been created' ) ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|