Standard::slice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
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-2025
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
	/** controller/frontend/order/name
26
	 * Class name of the used order frontend controller implementation
27
	 *
28
	 * Each default frontend controller can be replace by an alternative imlementation.
29
	 * To use this implementation, you have to set the last part of the class
30
	 * name as configuration value so the controller factory knows which class it
31
	 * has to instantiate.
32
	 *
33
	 * For example, if the name of the default class is
34
	 *
35
	 *  \Aimeos\Controller\Frontend\Order\Standard
36
	 *
37
	 * and you want to replace it with your own version named
38
	 *
39
	 *  \Aimeos\Controller\Frontend\Order\Myorder
40
	 *
41
	 * then you have to set the this configuration option:
42
	 *
43
	 *  controller/frontend/order/name = Myorder
44
	 *
45
	 * The value is the last part of your own class name and it's case sensitive,
46
	 * so take care that the configuration value is exactly named like the last
47
	 * part of the class name.
48
	 *
49
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
50
	 * characters are possible! You should always start the last part of the class
51
	 * name with an upper case character and continue only with lower case characters
52
	 * or numbers. Avoid chamel case names like "MyOrder"!
53
	 *
54
	 * @param string Last part of the class name
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
59
	/** controller/frontend/order/decorators/excludes
60
	 * Excludes decorators added by the "common" option from the order frontend controllers
61
	 *
62
	 * Decorators extend the functionality of a class by adding new aspects
63
	 * (e.g. log what is currently done), executing the methods of the underlying
64
	 * class only in certain conditions (e.g. only for logged in users) or
65
	 * modify what is returned to the caller.
66
	 *
67
	 * This option allows you to remove a decorator added via
68
	 * "controller/frontend/common/decorators/default" before they are wrapped
69
	 * around the frontend controller.
70
	 *
71
	 *  controller/frontend/order/decorators/excludes = array( 'decorator1' )
72
	 *
73
	 * This would remove the decorator named "decorator1" from the list of
74
	 * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via
75
	 * "controller/frontend/common/decorators/default" for the order frontend controller.
76
	 *
77
	 * @param array List of decorator names
78
	 * @since 2014.03
79
	 * @category Developer
80
	 * @see controller/frontend/common/decorators/default
81
	 * @see controller/frontend/order/decorators/global
82
	 * @see controller/frontend/order/decorators/local
83
	 */
84
85
	/** controller/frontend/order/decorators/global
86
	 * Adds a list of globally available decorators only to the order frontend controllers
87
	 *
88
	 * Decorators extend the functionality of a class by adding new aspects
89
	 * (e.g. log what is currently done), executing the methods of the underlying
90
	 * class only in certain conditions (e.g. only for logged in users) or
91
	 * modify what is returned to the caller.
92
	 *
93
	 * This option allows you to wrap global decorators
94
	 * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller.
95
	 *
96
	 *  controller/frontend/order/decorators/global = array( 'decorator1' )
97
	 *
98
	 * This would add the decorator named "decorator1" defined by
99
	 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller.
100
	 *
101
	 * @param array List of decorator names
102
	 * @since 2014.03
103
	 * @category Developer
104
	 * @see controller/frontend/common/decorators/default
105
	 * @see controller/frontend/order/decorators/excludes
106
	 * @see controller/frontend/order/decorators/local
107
	 */
108
109
	/** controller/frontend/order/decorators/local
110
	 * Adds a list of local decorators only to the order frontend controllers
111
	 *
112
	 * Decorators extend the functionality of a class by adding new aspects
113
	 * (e.g. log what is currently done), executing the methods of the underlying
114
	 * class only in certain conditions (e.g. only for logged in users) or
115
	 * modify what is returned to the caller.
116
	 *
117
	 * This option allows you to wrap local decorators
118
	 * ("\Aimeos\Controller\Frontend\Order\Decorator\*") around the frontend controller.
119
	 *
120
	 *  controller/frontend/order/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Controller\Frontend\Catalog\Decorator\Decorator2" only to the frontend
124
	 * controller.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2014.03
128
	 * @category Developer
129
	 * @see controller/frontend/common/decorators/default
130
	 * @see controller/frontend/order/decorators/excludes
131
	 * @see controller/frontend/order/decorators/global
132
	 */
133
134
135
	private array $domains = [];
136
	private \Aimeos\Base\Criteria\Iface $filter;
137
	private \Aimeos\MShop\Common\Manager\Iface $manager;
138
139
140
	/**
141
	 * Initializes the controller
142
	 *
143
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
144
	 */
145
	public function __construct( \Aimeos\MShop\ContextIface $context )
146
	{
147
		parent::__construct( $context );
148
149
		$this->manager = \Aimeos\MShop::create( $context, 'order' );
150
		$this->filter = $this->manager->filter( true );
151
		$this->addExpression( $this->filter->compare( '==', 'order.customerid', $context->user() ) );
152
	}
153
154
155
	/**
156
	 * Clones objects in controller
157
	 */
158
	public function __clone()
159
	{
160
		$this->filter = clone $this->filter;
161
		parent::__clone();
162
	}
163
164
165
	/**
166
	 * Adds generic condition for filtering orders
167
	 *
168
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
169
	 * @param string $key Search key defined by the order manager, e.g. "order.type"
170
	 * @param array|string $value Value or list of values to compare to
171
	 * @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface
172
	 * @since 2019.04
173
	 */
174
	public function compare( string $operator, string $key, $value ) : Iface
175
	{
176
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
177
		return $this;
178
	}
179
180
181
	/**
182
	 * Returns the order for the given order ID
183
	 *
184
	 * @param string $id Unique order ID
185
	 * @param bool $default Use default criteria to limit orders
186
	 * @return \Aimeos\MShop\Order\Item\Iface Order item object
187
	 * @since 2019.04
188
	 */
189
	public function get( string $id, ?bool $default = null ) : \Aimeos\MShop\Order\Item\Iface
190
	{
191
		return $this->manager->get( $id, $this->domains, $default );
192
	}
193
194
195
	/**
196
	 * Parses the given array and adds the conditions to the list of conditions
197
	 *
198
	 * @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['order.statuspayment' => 0]], ['==' => ['order.type' => 'web']]]]
199
	 * @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface
200
	 * @since 2019.04
201
	 */
202
	public function parse( array $conditions ) : Iface
203
	{
204
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
205
			$this->addExpression( $cond );
206
		}
207
208
		return $this;
209
	}
210
211
212
	/**
213
	 * Updates the given order item in the storage
214
	 *
215
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object
216
	 * @return \Aimeos\MShop\Order\Item\Iface $orderItem Saved order item object
217
	 * @since 2019.04
218
	 */
219
	public function save( \Aimeos\MShop\Order\Item\Iface $orderItem ) : \Aimeos\MShop\Order\Item\Iface
220
	{
221
		return $this->manager->save( $orderItem );
222
	}
223
224
	/**
225
	 * Returns the orders filtered by the previously assigned conditions
226
	 *
227
	 * @param int &$total Parameter where the total number of found attributes will be stored in
228
	 * @return \Aimeos\Map Ordered list of order items implementing \Aimeos\MShop\Order\Item\Iface
229
	 * @since 2019.04
230
	 */
231
	public function search( ?int &$total = null ) : \Aimeos\Map
232
	{
233
		$filter = clone $this->filter;
234
235
		$this->addExpression( $filter->getConditions() );
236
237
		$filter->add( $filter->and( $this->getConditions() ) );
238
		$filter->setSortations( $this->getSortations() );
239
240
		return $this->manager->search( $filter, $this->domains, $total );
241
	}
242
243
244
	/**
245
	 * Sets the start value and the number of returned orders for slicing the list of found orders
246
	 *
247
	 * @param int $start Start value of the first order in the list
248
	 * @param int $limit Number of returned orders
249
	 * @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface
250
	 * @since 2019.04
251
	 */
252
	public function slice( int $start, int $limit ) : Iface
253
	{
254
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
255
		$this->filter->slice( $start, min( $limit, $maxsize ) );
256
		return $this;
257
	}
258
259
260
	/**
261
	 * Sets the sorting of the result list
262
	 *
263
	 * @param string|null $key Sorting of the result list like "-order.id", null for no sorting
264
	 * @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface
265
	 * @since 2019.04
266
	 */
267
	public function sort( ?string $key = null ) : Iface
268
	{
269
		$list = $this->splitKeys( $key );
270
271
		foreach( $list as $sortkey )
272
		{
273
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
274
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
275
		}
276
277
		return $this;
278
	}
279
280
281
	/**
282
	 * Updates stock levels and coupons counts
283
	 *
284
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object
285
	 * @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface
286
	 * @since 2024.01
287
	 */
288
	public function update( \Aimeos\MShop\Order\Item\Iface $orderItem ) : Iface
289
	{
290
		$this->manager->update( $orderItem );
291
		return $this;
292
	}
293
294
295
	/**
296
	 * Sets the referenced domains that will be fetched too when retrieving items
297
	 *
298
	 * @param array $domains Domain names of the referenced items that should be fetched too
299
	 * @return \Aimeos\Controller\Frontend\Order\Iface Order controller for fluent interface
300
	 * @since 2019.04
301
	 */
302
	public function uses( array $domains ) : Iface
303
	{
304
		$this->domains = $domains;
305
		return $this;
306
	}
307
308
309
	/**
310
	 * Returns the manager used by the controller
311
	 *
312
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
313
	 */
314
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
315
	{
316
		return $this->manager;
317
	}
318
}
319