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\devtools\pip\IIdempotentPackageInstallationPlugin;
6 use wcf\system\exception\SystemException;
8 use wcf\util\StringUtil;
11 * Installs, updates and deletes user notification events.
14 * @copyright 2001-2017 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Package\Plugin
18 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IIdempotentPackageInstallationPlugin {
22 public $className = UserNotificationEventEditor::class;
27 public $tableName = 'user_notification_event';
32 public $tagName = 'event';
38 protected $presetEventIDs = [];
43 protected function handleDelete(array $items) {
44 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
48 $statement = WCF::getDB()->prepareStatement($sql);
49 foreach ($items as $item) {
51 $this->installation->getPackageID(),
52 $this->getObjectTypeID($item['elements']['objecttype']),
53 $item['elements']['name']
61 protected function prepareImport(array $data) {
62 $presetMailNotificationType = 'none';
63 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
64 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
68 'eventName' => $data['elements']['name'],
69 'className' => $data['elements']['classname'],
70 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
71 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
72 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
73 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
74 'presetMailNotificationType' => $presetMailNotificationType
81 protected function import(array $row, array $data) {
82 /** @var UserNotificationEvent $event */
83 $event = parent::import($row, $data);
85 if (empty($row) && $data['preset']) {
86 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
95 protected function cleanup() {
96 if (empty($this->presetEventIDs)) return;
98 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
99 (userID, eventID, mailNotificationType)
101 FROM wcf".WCF_N."_user";
102 $statement = WCF::getDB()->prepareStatement($sql);
103 WCF::getDB()->beginTransaction();
104 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
105 $statement->execute([$eventID, $mailNotificationType]);
107 WCF::getDB()->commitTransaction();
113 protected function findExistingItem(array $data) {
115 FROM wcf".WCF_N."_".$this->tableName."
116 WHERE objectTypeID = ?
119 $data['objectTypeID'],
125 'parameters' => $parameters
130 * Gets the id of given object type id.
132 * @param string $objectType
134 * @throws SystemException
136 protected function getObjectTypeID($objectType) {
137 // get object type id
138 $sql = "SELECT object_type.objectTypeID
139 FROM wcf".WCF_N."_object_type object_type
140 WHERE object_type.objectType = ?
141 AND object_type.definitionID IN (
143 FROM wcf".WCF_N."_object_type_definition
144 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
146 $statement = WCF::getDB()->prepareStatement($sql, 1);
147 $statement->execute([$objectType]);
148 $row = $statement->fetchArray();
149 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
150 return $row['objectTypeID'];
156 public static function getSyncDependencies() {
157 return ['objectType'];