2 declare(strict_types=1);
3 namespace wcf\system\package\plugin;
4 use wcf\data\user\notification\event\UserNotificationEvent;
5 use wcf\data\user\notification\event\UserNotificationEventEditor;
6 use wcf\system\devtools\pip\IIdempotentPackageInstallationPlugin;
7 use wcf\system\exception\SystemException;
9 use wcf\util\StringUtil;
12 * Installs, updates and deletes user notification events.
15 * @copyright 2001-2018 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Package\Plugin
19 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IIdempotentPackageInstallationPlugin {
23 public $className = UserNotificationEventEditor::class;
28 public $tableName = 'user_notification_event';
33 public $tagName = 'event';
39 protected $presetEventIDs = [];
44 protected function handleDelete(array $items) {
45 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
49 $statement = WCF::getDB()->prepareStatement($sql);
50 foreach ($items as $item) {
52 $this->installation->getPackageID(),
53 $this->getObjectTypeID($item['elements']['objecttype']),
54 $item['elements']['name']
62 protected function prepareImport(array $data) {
63 $presetMailNotificationType = 'none';
64 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
65 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
69 'eventName' => $data['elements']['name'],
70 'className' => $data['elements']['classname'],
71 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
72 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
73 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
74 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
75 'presetMailNotificationType' => $presetMailNotificationType
82 protected function import(array $row, array $data) {
83 /** @var UserNotificationEvent $event */
84 $event = parent::import($row, $data);
86 if (empty($row) && $data['preset']) {
87 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
96 protected function cleanup() {
97 if (empty($this->presetEventIDs)) return;
99 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
100 (userID, eventID, mailNotificationType)
102 FROM wcf".WCF_N."_user";
103 $statement = WCF::getDB()->prepareStatement($sql);
104 WCF::getDB()->beginTransaction();
105 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
106 $statement->execute([$eventID, $mailNotificationType]);
108 WCF::getDB()->commitTransaction();
114 protected function findExistingItem(array $data) {
116 FROM wcf".WCF_N."_".$this->tableName."
117 WHERE objectTypeID = ?
120 $data['objectTypeID'],
126 'parameters' => $parameters
131 * Gets the id of given object type id.
133 * @param string $objectType
135 * @throws SystemException
137 protected function getObjectTypeID($objectType) {
138 // get object type id
139 $sql = "SELECT object_type.objectTypeID
140 FROM wcf".WCF_N."_object_type object_type
141 WHERE object_type.objectType = ?
142 AND object_type.definitionID IN (
144 FROM wcf".WCF_N."_object_type_definition
145 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
147 $statement = WCF::getDB()->prepareStatement($sql, 1);
148 $statement->execute([$objectType]);
149 $row = $statement->fetchArray();
150 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
151 return $row['objectTypeID'];
157 public static function getSyncDependencies() {
158 return ['objectType'];