5 use wcf\data\
object\type\ObjectTypeCache
;
6 use wcf\system\exception\UserInputException
;
7 use wcf\system\menu\user\UserMenu
;
8 use wcf\system\user\notification\event\IUserNotificationEvent
;
9 use wcf\system\user\notification\UserNotificationHandler
;
13 * Shows the notification settings form.
15 * @author Alexander Ebert
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 class NotificationSettingsForm
extends AbstractForm
24 public $loginRequired = true;
27 * list of notification events
28 * @var IUserNotificationEvent[][]
33 * list of settings by event
36 public $settings = [];
39 * list of valid options for the mail notification type.
42 protected static $validMailNotificationTypes = ['none', 'instant', 'daily'];
47 public function readParameters()
49 parent
::readParameters();
51 $this->events
= UserNotificationHandler
::getInstance()->getAvailableEvents();
54 foreach ($this->events
as $objectTypeID => $events) {
55 foreach ($events as $eventName => $event) {
56 if (!$event->isVisible()) {
57 unset($this->events
[$objectTypeID][$eventName]);
61 if (empty($this->events
[$objectTypeID])) {
62 unset($this->events
[$objectTypeID]);
70 public function readFormParameters()
72 parent
::readFormParameters();
74 if (isset($_POST['settings'])) {
75 $this->settings
= $_POST['settings'];
82 public function validate()
88 foreach ($this->events
as $events) {
89 foreach ($events as $event) {
90 $validEventIDs[] = $event->eventID
;
92 if (!isset($this->settings
[$event->eventID
]['enabled'])) {
93 $this->settings
[$event->eventID
]['enabled'] = 0;
98 foreach ($this->settings
as $eventID => &$settings) {
100 if (!\
in_array($eventID, $validEventIDs)) {
101 throw new UserInputException();
104 // ensure 'enabled' exists
105 if (!isset($settings['enabled'])) {
106 $settings['enabled'] = 0;
109 // ensure 'mailNotificationType' exists
111 !isset($settings['mailNotificationType'])
112 ||
!\
in_array($settings['mailNotificationType'], self
::$validMailNotificationTypes)
114 $settings['mailNotificationType'] = 'none';
123 public function readData()
130 foreach ($this->events
as $events) {
131 foreach ($events as $event) {
132 $this->settings
[$event->eventID
] = [
134 'mailNotificationType' => 'none',
139 // get activation state
140 $sql = "SELECT eventID, mailNotificationType
141 FROM wcf" . WCF_N
. "_user_notification_event_to_user
143 $statement = WCF
::getDB()->prepareStatement($sql);
144 $statement->execute([WCF
::getUser()->userID
]);
145 while ($row = $statement->fetchArray()) {
146 $this->settings
[$row['eventID']]['enabled'] = true;
147 $this->settings
[$row['eventID']]['mailNotificationType'] = $row['mailNotificationType'];
155 public function assignVariables()
157 parent
::assignVariables();
160 foreach ($this->events
as $objectType => $events) {
161 $objectTypeObj = ObjectTypeCache
::getInstance()
162 ->getObjectTypeByName('com.woltlab.wcf.notification.objectType', $objectType);
163 $category = ($objectTypeObj->category ?
: $objectType);
165 if (!isset($groupedEvents[$category])) {
166 $groupedEvents[$category] = [];
169 foreach ($events as $event) {
170 $groupedEvents[$category][] = $event;
174 \
ksort($groupedEvents);
176 WCF
::getTPL()->assign([
177 'events' => $groupedEvents,
178 'settings' => $this->settings
,
185 public function show()
188 UserMenu
::getInstance()->setActiveMenuItem('wcf.user.menu.settings.notification');
196 public function save()
200 $this->updateActivationStates();
203 // show success message
204 WCF
::getTPL()->assign('success', true);
208 * Updates preferences for notification events.
210 protected function updateActivationStates()
212 $sql = "DELETE FROM wcf" . WCF_N
. "_user_notification_event_to_user
215 $statement = WCF
::getDB()->prepareStatement($sql);
216 WCF
::getDB()->beginTransaction();
218 foreach ($this->settings
as $eventID => $setting) {
219 $statement->execute([
221 WCF
::getUser()->userID
,
224 if ($setting['enabled']) {
226 'eventID' => $eventID,
227 'mailNotificationType' => $setting['mailNotificationType'],
232 if (!empty($newSettings)) {
233 $sql = "INSERT INTO wcf" . WCF_N
. "_user_notification_event_to_user
234 (eventID, userID, mailNotificationType)
236 $statement = WCF
::getDB()->prepareStatement($sql);
237 foreach ($newSettings as $newSetting) {
238 $statement->execute([
239 $newSetting['eventID'],
240 WCF
::getUser()->userID
,
241 $newSetting['mailNotificationType'],
245 WCF
::getDB()->commitTransaction();