1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2018-2022 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Frontend |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Frontend\Subscription; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default implementation of the subscription 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 $domains = []; |
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, 'subscription' ); |
39
|
|
|
$this->filter = $this->manager->filter(); |
40
|
|
|
$this->addExpression( $this->filter->compare( '==', 'order.base.customerid', $context->user() ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Clones objects in controller and resets values |
46
|
|
|
*/ |
47
|
|
|
public function __clone() |
48
|
|
|
{ |
49
|
|
|
$this->filter = clone $this->filter; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Cancels an active subscription |
55
|
|
|
* |
56
|
|
|
* @param string $id Unique subscription ID |
57
|
|
|
* @return \Aimeos\MShop\Subscription\Item\Iface Canceled subscription item |
58
|
|
|
*/ |
59
|
|
|
public function cancel( string $id ) : \Aimeos\MShop\Subscription\Item\Iface |
60
|
|
|
{ |
61
|
|
|
$item = $this->manager->get( $id ); |
62
|
|
|
$item = $item->setDateEnd( $item->getDateNext() ?: date( 'Y-m-d' ) ) |
63
|
|
|
->setReason( \Aimeos\MShop\Subscription\Item\Iface::REASON_CANCEL ); |
64
|
|
|
|
65
|
|
|
return $this->manager->save( $item ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Adds generic condition for filtering |
71
|
|
|
* |
72
|
|
|
* @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
73
|
|
|
* @param string $key Search key defined by the subscription manager, e.g. "subscription.status" |
74
|
|
|
* @param array|string $value Value or list of values to compare to |
75
|
|
|
* @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface |
76
|
|
|
* @since 2019.04 |
77
|
|
|
*/ |
78
|
|
|
public function compare( string $operator, string $key, $value ) : Iface |
79
|
|
|
{ |
80
|
|
|
$this->addExpression( $this->filter->compare( $operator, $key, $value ) ); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the subscription item for the given ID |
87
|
|
|
* |
88
|
|
|
* @param string $id Unique subscription ID |
89
|
|
|
* @return \Aimeos\MShop\Subscription\Item\Iface Subscription object |
90
|
|
|
*/ |
91
|
|
|
public function get( string $id ) : \Aimeos\MShop\Subscription\Item\Iface |
92
|
|
|
{ |
93
|
|
|
$userId = $this->context()->user(); |
94
|
|
|
|
95
|
|
|
$filter = $this->manager->filter( true )->add( [ |
96
|
|
|
'order.base.customerid' => $userId, |
97
|
|
|
'subscription.id' => $id |
98
|
|
|
] ); |
99
|
|
|
|
100
|
|
|
return $this->manager->search( $filter, $this->domains )->first( function() use ( $id, $userId ) { |
101
|
|
|
$msg = 'Invalid subscription ID "%1$s" for customer ID "%2$s"'; |
102
|
|
|
throw new \Aimeos\Controller\Frontend\Subscription\Exception( sprintf( $msg, $id, $userId ) ); |
103
|
|
|
} ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the available interval attribute items |
109
|
|
|
* |
110
|
|
|
* @return \Aimeos\Map Associative list of intervals as keys and items implementing \Aimeos\MShop\Attribute\Item\Iface |
111
|
|
|
*/ |
112
|
|
|
public function getIntervals() : \Aimeos\Map |
113
|
|
|
{ |
114
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'attribute' ); |
115
|
|
|
|
116
|
|
|
$search = $manager->filter( true )->add( [ |
117
|
|
|
'attribute.domain' => 'product', |
118
|
|
|
'attribute.type' => 'interval' |
119
|
|
|
] )->slice( 0, 10000 ); |
120
|
|
|
|
121
|
|
|
return $manager->search( $search, ['text'] )->col( null, 'attribute.code' ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Parses the given array and adds the conditions to the list of conditions |
127
|
|
|
* |
128
|
|
|
* @param array $conditions List of conditions, e.g. ['>' => ['subscription.interval' => 'P0Y1M0W0D']] |
129
|
|
|
* @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface |
130
|
|
|
* @since 2019.04 |
131
|
|
|
*/ |
132
|
|
|
public function parse( array $conditions ) : Iface |
133
|
|
|
{ |
134
|
|
|
if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) { |
135
|
|
|
$this->addExpression( $cond ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Saves the modified subscription item |
144
|
|
|
* |
145
|
|
|
* @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription object |
146
|
|
|
* @return \Aimeos\MShop\Subscription\Item\Iface Saved subscription item |
147
|
|
|
*/ |
148
|
|
|
public function save( \Aimeos\MShop\Subscription\Item\Iface $item ) : \Aimeos\MShop\Subscription\Item\Iface |
149
|
|
|
{ |
150
|
|
|
return $this->manager->save( $item ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the subscriptions filtered by the previously assigned conditions |
155
|
|
|
* |
156
|
|
|
* @param int &$total Parameter where the total number of found subscriptions will be stored in |
157
|
|
|
* @return \Aimeos\Map Ordered list of subscription items implementing \Aimeos\MShop\Subscription\Item\Iface |
158
|
|
|
* @since 2019.04 |
159
|
|
|
*/ |
160
|
|
|
public function search( int &$total = null ) : \Aimeos\Map |
161
|
|
|
{ |
162
|
|
|
$this->filter->setSortations( $this->getSortations() ); |
163
|
|
|
$this->filter->setConditions( $this->filter->and( $this->getConditions() ) ); |
164
|
|
|
|
165
|
|
|
return $this->manager->search( $this->filter, $this->domains, $total ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sets the start value and the number of returned subscription items for slicing the list of found subscription items |
171
|
|
|
* |
172
|
|
|
* @param int $start Start value of the first subscription item in the list |
173
|
|
|
* @param int $limit Number of returned subscription items |
174
|
|
|
* @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface |
175
|
|
|
* @since 2019.04 |
176
|
|
|
*/ |
177
|
|
|
public function slice( int $start, int $limit ) : Iface |
178
|
|
|
{ |
179
|
|
|
$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 ); |
180
|
|
|
$this->filter->slice( $start, min( $limit, $maxsize ) ); |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets the sorting of the result list |
187
|
|
|
* |
188
|
|
|
* @param string|null $key Sorting key of the result list like "interval", null for no sorting |
189
|
|
|
* @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface |
190
|
|
|
* @since 2019.04 |
191
|
|
|
*/ |
192
|
|
|
public function sort( string $key = null ) : Iface |
193
|
|
|
{ |
194
|
|
|
$list = $this->splitKeys( $key ); |
195
|
|
|
|
196
|
|
|
foreach( $list as $sortkey ) |
197
|
|
|
{ |
198
|
|
|
$direction = ( $sortkey[0] === '-' ? '-' : '+' ); |
199
|
|
|
$sortkey = ltrim( $sortkey, '+-' ); |
200
|
|
|
|
201
|
|
|
switch( $sortkey ) |
202
|
|
|
{ |
203
|
|
|
case 'interval': |
204
|
|
|
$this->addExpression( $this->filter->sort( $direction, 'subscription.interval' ) ); break; |
205
|
|
|
default: |
206
|
|
|
$this->addExpression( $this->filter->sort( $direction, $sortkey ) ); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Sets the referenced domains that will be fetched too when retrieving items |
216
|
|
|
* |
217
|
|
|
* @param array $domains Domain names of the referenced items that should be fetched too |
218
|
|
|
* @return \Aimeos\Controller\Frontend\Subscription\Iface Subscription controller for fluent interface |
219
|
|
|
* @since 2022.04 |
220
|
|
|
*/ |
221
|
|
|
public function uses( array $domains ) : Iface |
222
|
|
|
{ |
223
|
|
|
$this->domains = $domains; |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the manager used by the controller |
230
|
|
|
* |
231
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object |
232
|
|
|
*/ |
233
|
|
|
protected function getManager() : \Aimeos\MShop\Common\Manager\Iface |
234
|
|
|
{ |
235
|
|
|
return $this->manager; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|