2 namespace wcf\system\package\plugin;
3 use wcf\data\user\notification\event\UserNotificationEvent;
4 use wcf\data\user\notification\event\UserNotificationEventEditor;
5 use wcf\system\exception\SystemException;
9 * Installs, updates and deletes user notification events.
12 * @copyright 2001-2017 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\System\Package\Plugin
16 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin {
20 public $className = UserNotificationEventEditor::class;
25 public $tableName = 'user_notification_event';
30 public $tagName = 'event';
36 protected $presetEventIDs = [];
41 protected function handleDelete(array $items) {
42 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
46 $statement = WCF::getDB()->prepareStatement($sql);
47 foreach ($items as $item) {
49 $this->installation->getPackageID(),
50 $this->getObjectTypeID($item['elements']['objecttype']),
51 $item['elements']['name']
59 protected function prepareImport(array $data) {
60 $presetMailNotificationType = 'none';
61 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
62 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
66 'eventName' => $data['elements']['name'],
67 'className' => $data['elements']['classname'],
68 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
69 'permissions' => isset($data['elements']['permissions']) ? $data['elements']['permissions'] : '',
70 'options' => isset($data['elements']['options']) ? $data['elements']['options'] : '',
71 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
72 'presetMailNotificationType' => $presetMailNotificationType
79 protected function import(array $row, array $data) {
80 /** @var UserNotificationEvent $event */
81 $event = parent::import($row, $data);
83 if (empty($row) && $data['preset']) {
84 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
93 protected function cleanup() {
94 if (empty($this->presetEventIDs)) return;
96 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
97 (userID, eventID, mailNotificationType)
99 FROM wcf".WCF_N."_user";
100 $statement = WCF::getDB()->prepareStatement($sql);
101 WCF::getDB()->beginTransaction();
102 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
103 $statement->execute([$eventID, $mailNotificationType]);
105 WCF::getDB()->commitTransaction();
111 protected function findExistingItem(array $data) {
113 FROM wcf".WCF_N."_".$this->tableName."
114 WHERE objectTypeID = ?
117 $data['objectTypeID'],
123 'parameters' => $parameters
128 * Gets the id of given object type id.
130 * @param string $objectType
133 protected function getObjectTypeID($objectType) {
134 // get object type id
135 $sql = "SELECT object_type.objectTypeID
136 FROM wcf".WCF_N."_object_type object_type
137 WHERE object_type.objectType = ?
138 AND object_type.definitionID IN (
140 FROM wcf".WCF_N."_object_type_definition
141 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
143 $statement = WCF::getDB()->prepareStatement($sql, 1);
144 $statement->execute([$objectType]);
145 $row = $statement->fetchArray();
146 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
147 return $row['objectTypeID'];