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;
7 use wcf\util\StringUtil;
10 * Installs, updates and deletes user notification events.
13 * @copyright 2001-2017 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\Package\Plugin
17 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin {
21 public $className = UserNotificationEventEditor::class;
26 public $tableName = 'user_notification_event';
31 public $tagName = 'event';
37 protected $presetEventIDs = [];
42 protected function handleDelete(array $items) {
43 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
47 $statement = WCF::getDB()->prepareStatement($sql);
48 foreach ($items as $item) {
50 $this->installation->getPackageID(),
51 $this->getObjectTypeID($item['elements']['objecttype']),
52 $item['elements']['name']
60 protected function prepareImport(array $data) {
61 $presetMailNotificationType = 'none';
62 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
63 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
67 'eventName' => $data['elements']['name'],
68 'className' => $data['elements']['classname'],
69 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
70 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
71 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
72 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
73 'presetMailNotificationType' => $presetMailNotificationType
80 protected function import(array $row, array $data) {
81 /** @var UserNotificationEvent $event */
82 $event = parent::import($row, $data);
84 if (empty($row) && $data['preset']) {
85 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
94 protected function cleanup() {
95 if (empty($this->presetEventIDs)) return;
97 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
98 (userID, eventID, mailNotificationType)
100 FROM wcf".WCF_N."_user";
101 $statement = WCF::getDB()->prepareStatement($sql);
102 WCF::getDB()->beginTransaction();
103 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
104 $statement->execute([$eventID, $mailNotificationType]);
106 WCF::getDB()->commitTransaction();
112 protected function findExistingItem(array $data) {
114 FROM wcf".WCF_N."_".$this->tableName."
115 WHERE objectTypeID = ?
118 $data['objectTypeID'],
124 'parameters' => $parameters
129 * Gets the id of given object type id.
131 * @param string $objectType
134 protected function getObjectTypeID($objectType) {
135 // get object type id
136 $sql = "SELECT object_type.objectTypeID
137 FROM wcf".WCF_N."_object_type object_type
138 WHERE object_type.objectType = ?
139 AND object_type.definitionID IN (
141 FROM wcf".WCF_N."_object_type_definition
142 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
144 $statement = WCF::getDB()->prepareStatement($sql, 1);
145 $statement->execute([$objectType]);
146 $row = $statement->fetchArray();
147 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
148 return $row['objectTypeID'];