Commit | Line | Data |
---|---|---|
320f4a6d MW |
1 | <?php |
2 | namespace wcf\form; | |
3 | use wcf\data\object\type\ObjectTypeCache; | |
4 | use wcf\system\exception\UserInputException; | |
5 | use wcf\system\menu\user\UserMenu; | |
7a23a706 | 6 | use wcf\system\user\notification\event\IUserNotificationEvent; |
320f4a6d MW |
7 | use wcf\system\user\notification\UserNotificationHandler; |
8 | use wcf\system\WCF; | |
9 | ||
10 | /** | |
11 | * Shows the notification settings form. | |
12 | * | |
13 | * @author Alexander Ebert | |
c839bd49 | 14 | * @copyright 2001-2018 WoltLab GmbH |
320f4a6d | 15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 16 | * @package WoltLabSuite\Core\Form |
320f4a6d MW |
17 | */ |
18 | class NotificationSettingsForm extends AbstractForm { | |
19 | /** | |
0fcfe5f6 | 20 | * @inheritDoc |
320f4a6d MW |
21 | */ |
22 | public $loginRequired = true; | |
23 | ||
24 | /** | |
25 | * list of notification events | |
e4499881 | 26 | * @var IUserNotificationEvent[][] |
320f4a6d MW |
27 | */ |
28 | public $events = null; | |
29 | ||
30 | /** | |
31 | * list of settings by event | |
7a23a706 | 32 | * @var mixed[][] |
320f4a6d | 33 | */ |
058cbd6a | 34 | public $settings = []; |
320f4a6d MW |
35 | |
36 | /** | |
37 | * list of valid options for the mail notification type. | |
7a23a706 | 38 | * @var string[] |
320f4a6d | 39 | */ |
058cbd6a | 40 | protected static $validMailNotificationTypes = ['none', 'instant', 'daily']; |
320f4a6d MW |
41 | |
42 | /** | |
0fcfe5f6 | 43 | * @inheritDoc |
320f4a6d MW |
44 | */ |
45 | public function readParameters() { | |
46 | parent::readParameters(); | |
47 | ||
48 | $this->events = UserNotificationHandler::getInstance()->getAvailableEvents(); | |
49 | ||
50 | // filter events | |
51 | foreach ($this->events as $objectTypeID => $events) { | |
52 | foreach ($events as $eventName => $event) { | |
53 | if (!$event->isVisible()) { | |
54 | unset($this->events[$objectTypeID][$eventName]); | |
55 | } | |
56 | } | |
57 | ||
58 | if (empty($this->events[$objectTypeID])) { | |
59 | unset($this->events[$objectTypeID]); | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | /** | |
0fcfe5f6 | 65 | * @inheritDoc |
320f4a6d MW |
66 | */ |
67 | public function readFormParameters() { | |
68 | parent::readFormParameters(); | |
69 | ||
70 | if (isset($_POST['settings'])) $this->settings = $_POST['settings']; | |
71 | } | |
72 | ||
73 | /** | |
0fcfe5f6 | 74 | * @inheritDoc |
320f4a6d MW |
75 | */ |
76 | public function validate() { | |
77 | parent::validate(); | |
78 | ||
79 | // valid event ids | |
058cbd6a | 80 | $validEventIDs = []; |
320f4a6d MW |
81 | foreach ($this->events as $events) { |
82 | foreach ($events as $event) { | |
83 | $validEventIDs[] = $event->eventID; | |
0ceb9e95 MW |
84 | |
85 | if (!isset($this->settings[$event->eventID]['enabled'])) { | |
86 | $this->settings[$event->eventID]['enabled'] = 0; | |
87 | } | |
320f4a6d MW |
88 | } |
89 | } | |
90 | ||
91 | foreach ($this->settings as $eventID => &$settings) { | |
92 | // validate event id | |
93 | if (!in_array($eventID, $validEventIDs)) { | |
94 | throw new UserInputException(); | |
95 | } | |
96 | ||
97 | // ensure 'enabled' exists | |
98 | if (!isset($settings['enabled'])) { | |
99 | $settings['enabled'] = 0; | |
100 | } | |
101 | ||
102 | // ensure 'mailNotificationType' exists | |
103 | if (!isset($settings['mailNotificationType']) || !in_array($settings['mailNotificationType'], self::$validMailNotificationTypes)) { | |
104 | $settings['mailNotificationType'] = 'none'; | |
105 | } | |
106 | } | |
107 | unset($settings); | |
108 | } | |
109 | ||
110 | /** | |
0fcfe5f6 | 111 | * @inheritDoc |
320f4a6d MW |
112 | */ |
113 | public function readData() { | |
114 | parent::readData(); | |
115 | ||
116 | // default values | |
117 | if (empty($_POST)) { | |
118 | // get user settings | |
320f4a6d MW |
119 | foreach ($this->events as $events) { |
120 | foreach ($events as $event) { | |
058cbd6a | 121 | $this->settings[$event->eventID] = [ |
320f4a6d MW |
122 | 'enabled' => false, |
123 | 'mailNotificationType' => 'none' | |
058cbd6a | 124 | ]; |
320f4a6d MW |
125 | } |
126 | } | |
127 | ||
128 | // get activation state | |
129 | $sql = "SELECT eventID, mailNotificationType | |
130 | FROM wcf".WCF_N."_user_notification_event_to_user | |
131 | WHERE userID = ?"; | |
132 | $statement = WCF::getDB()->prepareStatement($sql); | |
058cbd6a | 133 | $statement->execute([WCF::getUser()->userID]); |
320f4a6d MW |
134 | while ($row = $statement->fetchArray()) { |
135 | $this->settings[$row['eventID']]['enabled'] = true; | |
136 | $this->settings[$row['eventID']]['mailNotificationType'] = $row['mailNotificationType']; | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | /** | |
0fcfe5f6 | 142 | * @inheritDoc |
320f4a6d MW |
143 | */ |
144 | public function assignVariables() { | |
145 | parent::assignVariables(); | |
146 | ||
058cbd6a | 147 | $groupedEvents = []; |
320f4a6d MW |
148 | foreach ($this->events as $objectType => $events) { |
149 | $objectTypeObj = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.notification.objectType', $objectType); | |
150 | $category = ($objectTypeObj->category ?: $objectType); | |
151 | ||
152 | if (!isset($groupedEvents[$category])) { | |
058cbd6a | 153 | $groupedEvents[$category] = []; |
320f4a6d MW |
154 | } |
155 | ||
156 | foreach ($events as $event) $groupedEvents[$category][] = $event; | |
157 | } | |
158 | ||
159 | ksort($groupedEvents); | |
160 | ||
058cbd6a | 161 | WCF::getTPL()->assign([ |
320f4a6d MW |
162 | 'events' => $groupedEvents, |
163 | 'settings' => $this->settings | |
058cbd6a | 164 | ]); |
320f4a6d MW |
165 | } |
166 | ||
167 | /** | |
0fcfe5f6 | 168 | * @inheritDoc |
320f4a6d MW |
169 | */ |
170 | public function show() { | |
171 | // set active tab | |
172 | UserMenu::getInstance()->setActiveMenuItem('wcf.user.menu.settings.notification'); | |
173 | ||
174 | parent::show(); | |
175 | } | |
176 | ||
177 | /** | |
0fcfe5f6 | 178 | * @inheritDoc |
320f4a6d MW |
179 | */ |
180 | public function save() { | |
181 | parent::save(); | |
182 | ||
183 | $this->updateActivationStates(); | |
184 | $this->saved(); | |
185 | ||
186 | // show success message | |
187 | WCF::getTPL()->assign('success', true); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Updates preferences for notification events. | |
192 | */ | |
193 | protected function updateActivationStates() { | |
194 | $sql = "DELETE FROM wcf".WCF_N."_user_notification_event_to_user | |
195 | WHERE eventID = ? | |
196 | AND userID = ?"; | |
197 | $statement = WCF::getDB()->prepareStatement($sql); | |
198 | WCF::getDB()->beginTransaction(); | |
058cbd6a | 199 | $newSettings = []; |
320f4a6d | 200 | foreach ($this->settings as $eventID => $setting) { |
058cbd6a | 201 | $statement->execute([ |
320f4a6d MW |
202 | $eventID, |
203 | WCF::getUser()->userID | |
058cbd6a | 204 | ]); |
320f4a6d MW |
205 | |
206 | if ($setting['enabled']) { | |
058cbd6a | 207 | $newSettings[] = [ |
320f4a6d MW |
208 | 'eventID' => $eventID, |
209 | 'mailNotificationType' => $setting['mailNotificationType'] | |
058cbd6a | 210 | ]; |
320f4a6d MW |
211 | } |
212 | } | |
213 | ||
214 | if (!empty($newSettings)) { | |
215 | $sql = "INSERT INTO wcf".WCF_N."_user_notification_event_to_user | |
216 | (eventID, userID, mailNotificationType) | |
217 | VALUES (?, ?, ?)"; | |
218 | $statement = WCF::getDB()->prepareStatement($sql); | |
219 | foreach ($newSettings as $newSetting) { | |
058cbd6a | 220 | $statement->execute([ |
320f4a6d MW |
221 | $newSetting['eventID'], |
222 | WCF::getUser()->userID, | |
223 | $newSetting['mailNotificationType'] | |
058cbd6a | 224 | ]); |
320f4a6d MW |
225 | } |
226 | } | |
227 | WCF::getDB()->commitTransaction(); | |
228 | } | |
229 | } |