Commit | Line | Data |
---|---|---|
4d8036f5 MW |
1 | <?php |
2 | namespace wcf\acp\form; | |
5fdb5c9d AE |
3 | use wcf\data\paid\subscription\user\PaidSubscriptionUser; |
4 | use wcf\data\paid\subscription\user\PaidSubscriptionUserAction; | |
4d8036f5 | 5 | use wcf\data\paid\subscription\PaidSubscription; |
5fdb5c9d | 6 | use wcf\data\user\User; |
4d8036f5 | 7 | use wcf\form\AbstractForm; |
91b90209 | 8 | use wcf\system\exception\IllegalLinkException; |
4d8036f5 MW |
9 | use wcf\system\exception\UserInputException; |
10 | use wcf\system\WCF; | |
4d8036f5 | 11 | use wcf\util\DateUtil; |
5fdb5c9d | 12 | use wcf\util\StringUtil; |
4d8036f5 MW |
13 | |
14 | /** | |
15 | * Shows the user subscription add form. | |
16 | * | |
17 | * @author Marcel Werk | |
c839bd49 | 18 | * @copyright 2001-2018 WoltLab GmbH |
4d8036f5 | 19 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 20 | * @package WoltLabSuite\Core\Acp\Form |
4d8036f5 MW |
21 | */ |
22 | class PaidSubscriptionUserAddForm extends AbstractForm { | |
23 | /** | |
0fcfe5f6 | 24 | * @inheritDoc |
4d8036f5 MW |
25 | */ |
26 | public $activeMenuItem = 'wcf.acp.menu.link.paidSubscription'; | |
27 | ||
1641e9af | 28 | /** |
0fcfe5f6 | 29 | * @inheritDoc |
1641e9af | 30 | */ |
058cbd6a | 31 | public $neededModules = ['MODULE_PAID_SUBSCRIPTION']; |
1641e9af | 32 | |
4d8036f5 | 33 | /** |
0fcfe5f6 | 34 | * @inheritDoc |
4d8036f5 | 35 | */ |
058cbd6a | 36 | public $neededPermissions = ['admin.paidSubscription.canManageSubscription']; |
4d8036f5 MW |
37 | |
38 | /** | |
39 | * subscription id | |
40 | * @var integer | |
41 | */ | |
42 | public $subscriptionID = 0; | |
43 | ||
44 | /** | |
45 | * subscription object | |
4e25add7 | 46 | * @var PaidSubscription |
4d8036f5 MW |
47 | */ |
48 | public $subscription = null; | |
49 | ||
50 | /** | |
51 | * username | |
1a6e8c52 | 52 | * @var string |
4d8036f5 MW |
53 | */ |
54 | public $username = ''; | |
55 | ||
56 | /** | |
57 | * user object | |
4e25add7 | 58 | * @var User |
4d8036f5 MW |
59 | */ |
60 | public $user = null; | |
61 | ||
62 | /** | |
63 | * subscription end date | |
1a6e8c52 | 64 | * @var string |
4d8036f5 MW |
65 | */ |
66 | public $endDate = ''; | |
67 | ||
68 | /** | |
69 | * subscription end date | |
70 | * @var \DateTime | |
71 | */ | |
72 | public $endDateTime = null; | |
73 | ||
74 | /** | |
0fcfe5f6 | 75 | * @inheritDoc |
4d8036f5 MW |
76 | */ |
77 | public function readParameters() { | |
78 | parent::readParameters(); | |
79 | ||
80 | if (isset($_REQUEST['id'])) $this->subscriptionID = intval($_REQUEST['id']); | |
81 | $this->subscription = new PaidSubscription($this->subscriptionID); | |
82 | if (!$this->subscription->subscriptionID) { | |
91b90209 | 83 | throw new IllegalLinkException(); |
4d8036f5 MW |
84 | } |
85 | } | |
86 | ||
87 | /** | |
0fcfe5f6 | 88 | * @inheritDoc |
4d8036f5 MW |
89 | */ |
90 | public function readFormParameters() { | |
91 | parent::readFormParameters(); | |
92 | ||
93 | if (isset($_POST['username'])) $this->username = StringUtil::trim($_POST['username']); | |
94 | if (isset($_POST['endDate'])) $this->endDate = $_POST['endDate']; | |
95 | } | |
96 | ||
97 | /** | |
0fcfe5f6 | 98 | * @inheritDoc |
4d8036f5 MW |
99 | */ |
100 | public function validate() { | |
101 | parent::validate(); | |
102 | ||
91b90209 MW |
103 | $this->validateUsername(); |
104 | $this->validateEndDate(); | |
105 | } | |
106 | ||
107 | /** | |
108 | * Validates given username. | |
109 | * | |
110 | * @throws UserInputException | |
111 | */ | |
112 | protected function validateUsername() { | |
4d8036f5 MW |
113 | if (empty($this->username)) { |
114 | throw new UserInputException('username'); | |
115 | } | |
116 | $this->user = User::getUserByUsername($this->username); | |
117 | if (!$this->user->userID) { | |
118 | throw new UserInputException('username', 'notFound'); | |
119 | } | |
91b90209 MW |
120 | } |
121 | ||
122 | /** | |
123 | * Validates given end date. | |
124 | * | |
125 | * @throws UserInputException | |
126 | */ | |
127 | protected function validateEndDate() { | |
fca74c71 | 128 | if ($this->subscription->subscriptionLength) { |
4d8036f5 MW |
129 | $this->endDateTime = \DateTime::createFromFormat('Y-m-d', $this->endDate, new \DateTimeZone('UTC')); |
130 | if ($this->endDateTime === false || $this->endDateTime->getTimestamp() < TIME_NOW) { | |
131 | throw new UserInputException('endDate'); | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
0fcfe5f6 | 137 | * @inheritDoc |
4d8036f5 MW |
138 | */ |
139 | public function save() { | |
140 | parent::save(); | |
141 | ||
142 | $userSubscription = PaidSubscriptionUser::getSubscriptionUser($this->subscriptionID, $this->user->userID); | |
058cbd6a | 143 | $data = []; |
fca74c71 | 144 | if ($this->subscription->subscriptionLength) { |
4d8036f5 MW |
145 | $data['endDate'] = $this->endDateTime->getTimestamp(); |
146 | } | |
147 | if ($userSubscription === null) { | |
148 | // create new subscription | |
91b90209 | 149 | $this->objectAction = new PaidSubscriptionUserAction([], 'create', [ |
4d8036f5 MW |
150 | 'user' => $this->user, |
151 | 'subscription' => $this->subscription, | |
152 | 'data' => $data | |
058cbd6a | 153 | ]); |
91b90209 | 154 | $this->objectAction->executeAction(); |
4d8036f5 MW |
155 | } |
156 | else { | |
157 | // extend existing subscription | |
91b90209 MW |
158 | $this->objectAction = new PaidSubscriptionUserAction([$userSubscription], 'extend', ['data' => $data]); |
159 | $this->objectAction->executeAction(); | |
4d8036f5 MW |
160 | } |
161 | $this->saved(); | |
162 | ||
163 | // reset values | |
91b90209 MW |
164 | $this->username = ''; |
165 | $this->setDefaultEndDate(); | |
4d8036f5 | 166 | |
47b90344 MS |
167 | // show success message |
168 | WCF::getTPL()->assign('success', true); | |
4d8036f5 MW |
169 | } |
170 | ||
171 | /** | |
0fcfe5f6 | 172 | * @inheritDoc |
4d8036f5 MW |
173 | */ |
174 | public function readData() { | |
175 | parent::readData(); | |
1a6e8c52 | 176 | |
4d8036f5 | 177 | if (empty($_POST)) { |
91b90209 MW |
178 | $this->setDefaultEndDate(); |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
183 | * Sets the default value for the end date. | |
184 | */ | |
185 | protected function setDefaultEndDate() { | |
186 | if ($this->subscription->subscriptionLength) { | |
187 | $d = DateUtil::getDateTimeByTimestamp(TIME_NOW); | |
188 | $d->add($this->subscription->getDateInterval()); | |
189 | $this->endDate = $d->format('Y-m-d'); | |
4d8036f5 MW |
190 | } |
191 | } | |
192 | ||
193 | /** | |
0fcfe5f6 | 194 | * @inheritDoc |
4d8036f5 MW |
195 | */ |
196 | public function assignVariables() { | |
197 | parent::assignVariables(); | |
198 | ||
058cbd6a | 199 | WCF::getTPL()->assign([ |
4d8036f5 MW |
200 | 'subscriptionID' => $this->subscriptionID, |
201 | 'subscription' => $this->subscription, | |
202 | 'username' => $this->username, | |
91b90209 MW |
203 | 'endDate' => $this->endDate, |
204 | 'action' => 'add' | |
058cbd6a | 205 | ]); |
4d8036f5 MW |
206 | } |
207 | } |