-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathPaidSubscriptionAddForm.class.php
More file actions
383 lines (335 loc) · 11.9 KB
/
PaidSubscriptionAddForm.class.php
File metadata and controls
383 lines (335 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
namespace wcf\acp\form;
use wcf\data\paid\subscription\PaidSubscription;
use wcf\data\paid\subscription\PaidSubscriptionAction;
use wcf\data\paid\subscription\PaidSubscriptionEditor;
use wcf\data\paid\subscription\PaidSubscriptionList;
use wcf\data\user\group\UserGroup;
use wcf\form\AbstractForm;
use wcf\system\exception\NamedUserException;
use wcf\system\exception\UserInputException;
use wcf\system\language\I18nHandler;
use wcf\system\payment\method\PaymentMethodHandler;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\ArrayUtil;
use wcf\util\HtmlString;
/**
* Shows the paid subscription add form.
*
* @author Marcel Werk
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
class PaidSubscriptionAddForm extends AbstractForm
{
/**
* @inheritDoc
*/
public $activeMenuItem = 'wcf.acp.menu.link.paidSubscription.add';
/**
* @inheritDoc
*/
public $neededModules = ['MODULE_PAID_SUBSCRIPTION'];
/**
* @inheritDoc
*/
public $neededPermissions = ['admin.paidSubscription.canManageSubscription'];
/**
* @inheritDoc
*/
public $templateName = 'paidSubscriptionAdd';
/**
* subscription title
* @var string
*/
public $title = '';
/**
* subscription description
* @var string
*/
public $description = '';
/**
* indicates if the subscription is disabled
* @var int
*/
public $isDisabled = 0;
/**
* subscription show order
* @var int
*/
public $showOrder = 0;
/**
* subscription cost
* @var double
*/
public $cost = 0.0;
/**
* subscription currency
* @var string
*/
public $currency = 'USD';
/**
* indicates if the subscription is permanent
* @var int
*/
public $subscriptionLengthPermanent = 0;
/**
* subscription length
* @var int
*/
public $subscriptionLength = 0;
/**
* subscription length unit
* @var string
*/
public $subscriptionLengthUnit = '';
/**
* indicates if the subscription is a recurring payment
* @var int
*/
public $isRecurring = 0;
/**
* list of group ids
* @var int[]
*/
public $groupIDs = [];
/**
* list of excluded subscriptions
* @var int[]
*/
public $excludedSubscriptionIDs = [];
/**
* available user groups
* @var UserGroup[]
*/
public $availableUserGroups = [];
/**
* list of available currencies
* @var string[]
*/
public $availableCurrencies = [];
/**
* list of available subscriptions
* @var array<int, PaidSubscription>
*/
public $availableSubscriptions = [];
#[\Override]
public function readParameters()
{
parent::readParameters();
I18nHandler::getInstance()->register('description');
I18nHandler::getInstance()->register('title');
// get available user groups
$this->availableUserGroups = UserGroup::getSortedAccessibleGroups(
[],
[UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]
);
if (!\count(PaymentMethodHandler::getInstance()->getPaymentMethods())) {
throw new NamedUserException(HtmlString::fromSafeHtml(
WCF::getLanguage()->get('wcf.acp.paidSubscription.error.noPaymentMethods')
));
}
// get available currencies
foreach (PaymentMethodHandler::getInstance()->getPaymentMethods() as $paymentMethod) {
$this->availableCurrencies = \array_merge(
$this->availableCurrencies,
$paymentMethod->getSupportedCurrencies()
);
}
$this->availableCurrencies = \array_unique($this->availableCurrencies);
\sort($this->availableCurrencies);
// get available subscriptions
$this->getAvailableSubscriptions();
}
/**
* @return void
*/
protected function getAvailableSubscriptions()
{
$subscriptionList = new PaidSubscriptionList();
$subscriptionList->sqlOrderBy = 'title';
$subscriptionList->readObjects();
$this->availableSubscriptions = $subscriptionList->getObjects();
}
#[\Override]
public function readFormParameters()
{
parent::readFormParameters();
// read i18n values
I18nHandler::getInstance()->readValues();
// handle i18n plain input
if (I18nHandler::getInstance()->isPlainValue('description')) {
$this->description = I18nHandler::getInstance()->getValue('description');
}
if (I18nHandler::getInstance()->isPlainValue('title')) {
$this->title = I18nHandler::getInstance()->getValue('title');
}
if (!empty($_POST['isDisabled'])) {
$this->isDisabled = 1;
}
if (isset($_POST['showOrder'])) {
$this->showOrder = \intval($_POST['showOrder']);
}
if (isset($_POST['cost'])) {
$this->cost = \floatval($_POST['cost']);
}
if (isset($_POST['currency'])) {
$this->currency = $_POST['currency'];
}
if (!empty($_POST['subscriptionLengthPermanent'])) {
$this->subscriptionLengthPermanent = 1;
}
if (!$this->subscriptionLengthPermanent) {
if (isset($_POST['subscriptionLength'])) {
$this->subscriptionLength = \intval($_POST['subscriptionLength']);
}
if (isset($_POST['subscriptionLengthUnit'])) {
$this->subscriptionLengthUnit = $_POST['subscriptionLengthUnit'];
}
if (!empty($_POST['isRecurring'])) {
$this->isRecurring = 1;
}
}
if (isset($_POST['groupIDs']) && \is_array($_POST['groupIDs'])) {
$this->groupIDs = ArrayUtil::toIntegerArray($_POST['groupIDs']);
}
if (isset($_POST['excludedSubscriptionIDs']) && \is_array($_POST['excludedSubscriptionIDs'])) {
$this->excludedSubscriptionIDs = ArrayUtil::toIntegerArray($_POST['excludedSubscriptionIDs']);
}
}
#[\Override]
public function validate()
{
parent::validate();
// validate title
if (!I18nHandler::getInstance()->validateValue('title')) {
if (I18nHandler::getInstance()->isPlainValue('title')) {
throw new UserInputException('title');
} else {
throw new UserInputException('title', 'multilingual');
}
}
// validate description
if (!I18nHandler::getInstance()->validateValue('description', false, true)) {
throw new UserInputException('description');
}
// validate cost
if ($this->cost < 0.01) {
throw new UserInputException('cost');
}
// validate currency
if (!\in_array($this->currency, $this->availableCurrencies)) {
throw new UserInputException('cost');
}
if (!$this->subscriptionLengthPermanent) {
if ($this->subscriptionLength < 1) {
throw new UserInputException('subscriptionLength');
}
if ($this->subscriptionLengthUnit != 'D' && $this->subscriptionLengthUnit != 'M' && $this->subscriptionLengthUnit != 'Y') {
throw new UserInputException('subscriptionLength');
}
if (($this->subscriptionLengthUnit == 'D' && $this->subscriptionLength > 90) || ($this->subscriptionLengthUnit == 'M' && $this->subscriptionLength > 24) || ($this->subscriptionLengthUnit == 'Y' && $this->subscriptionLength > 5)) {
throw new UserInputException('subscriptionLength', 'invalid');
}
}
// validate group ids
if (empty($this->groupIDs)) {
throw new UserInputException('groupIDs');
}
foreach ($this->groupIDs as $groupID) {
if (!isset($this->availableUserGroups[$groupID])) {
throw new UserInputException('groupIDs');
}
}
// validate excluded subscriptions
foreach ($this->excludedSubscriptionIDs as $key => $subscriptionID) {
if (!isset($this->availableSubscriptions[$subscriptionID])) {
unset($this->excludedSubscriptionIDs[$key]);
}
}
}
#[\Override]
public function save()
{
parent::save();
// save subscription
$this->objectAction = new PaidSubscriptionAction([], 'create', [
'data' => \array_merge($this->additionalFields, [
'title' => $this->title,
'description' => $this->description,
'isDisabled' => $this->isDisabled,
'showOrder' => $this->showOrder,
'cost' => $this->cost,
'currency' => $this->currency,
'subscriptionLength' => $this->subscriptionLength,
'subscriptionLengthUnit' => $this->subscriptionLengthUnit,
'isRecurring' => $this->isRecurring,
'groupIDs' => \implode(',', $this->groupIDs),
'excludedSubscriptionIDs' => \implode(',', $this->excludedSubscriptionIDs),
]),
]);
$returnValues = $this->objectAction->executeAction();
// save i18n values
$this->saveI18nValue($returnValues['returnValues'], 'description');
$this->saveI18nValue($returnValues['returnValues'], 'title');
$this->saved();
// reset values
$this->title = $this->description = $this->subscriptionLengthUnit = '';
$this->isDisabled = $this->showOrder = $this->cost = $this->subscriptionLength = $this->isRecurring = 0;
$this->currency = 'EUR';
$this->groupIDs = [];
I18nHandler::getInstance()->reset();
// show success message
WCF::getTPL()->assign([
'success' => true,
'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
PaidSubscriptionEditForm::class,
['id' => $returnValues['returnValues']->subscriptionID]
),
]);
}
/**
* Saves i18n values.
*
* @return void
*/
public function saveI18nValue(PaidSubscription $subscription, string $columnName)
{
if (!I18nHandler::getInstance()->isPlainValue($columnName)) {
I18nHandler::getInstance()->save(
$columnName,
'wcf.paidSubscription.subscription' . $subscription->subscriptionID . ($columnName == 'description' ? '.description' : ''),
'wcf.paidSubscription',
1
);
// update database
$editor = new PaidSubscriptionEditor($subscription);
$editor->update([
$columnName => 'wcf.paidSubscription.subscription' . $subscription->subscriptionID . ($columnName == 'description' ? '.description' : ''),
]);
}
}
#[\Override]
public function assignVariables()
{
parent::assignVariables();
I18nHandler::getInstance()->assignVariables();
WCF::getTPL()->assign([
'action' => 'add',
'isDisabled' => $this->isDisabled,
'showOrder' => $this->showOrder,
'cost' => $this->cost,
'currency' => $this->currency,
'subscriptionLength' => $this->subscriptionLength,
'subscriptionLengthUnit' => $this->subscriptionLengthUnit,
'isRecurring' => $this->isRecurring,
'groupIDs' => $this->groupIDs,
'excludedSubscriptionIDs' => $this->excludedSubscriptionIDs,
'availableCurrencies' => $this->availableCurrencies,
'availableUserGroups' => $this->availableUserGroups,
'availableSubscriptions' => $this->availableSubscriptions,
'canChangePaymentOptions' => true,
]);
}
}