bab26968738896cd49dd7c0ad17d0a7e0b903274
[GitHub/WoltLab/WCF.git] /
1 <?php
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;
7 use wcf\system\WCF;
8 use wcf\util\StringUtil;
9
10 /**
11 * Installs, updates and deletes user notification events.
12 *
13 * @author Marcel Werk
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
17 */
18 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IIdempotentPackageInstallationPlugin {
19 /**
20 * @inheritDoc
21 */
22 public $className = UserNotificationEventEditor::class;
23
24 /**
25 * @inheritDoc
26 */
27 public $tableName = 'user_notification_event';
28
29 /**
30 * @inheritDoc
31 */
32 public $tagName = 'event';
33
34 /**
35 * preset event ids
36 * @var integer[]
37 */
38 protected $presetEventIDs = [];
39
40 /**
41 * @inheritDoc
42 */
43 protected function handleDelete(array $items) {
44 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
45 WHERE packageID = ?
46 AND objectTypeID = ?
47 AND eventName = ?";
48 $statement = WCF::getDB()->prepareStatement($sql);
49 foreach ($items as $item) {
50 $statement->execute([
51 $this->installation->getPackageID(),
52 $this->getObjectTypeID($item['elements']['objecttype']),
53 $item['elements']['name']
54 ]);
55 }
56 }
57
58 /**
59 * @inheritDoc
60 */
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'];
65 }
66
67 return [
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
75 ];
76 }
77
78 /**
79 * @inheritDoc
80 */
81 protected function import(array $row, array $data) {
82 /** @var UserNotificationEvent $event */
83 $event = parent::import($row, $data);
84
85 if (empty($row) && $data['preset']) {
86 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
87 }
88
89 return $event;
90 }
91
92 /**
93 * @inheritDoc
94 */
95 protected function cleanup() {
96 if (empty($this->presetEventIDs)) return;
97
98 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
99 (userID, eventID, mailNotificationType)
100 SELECT userID, ?, ?
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]);
106 }
107 WCF::getDB()->commitTransaction();
108 }
109
110 /**
111 * @inheritDoc
112 */
113 protected function findExistingItem(array $data) {
114 $sql = "SELECT *
115 FROM wcf".WCF_N."_".$this->tableName."
116 WHERE objectTypeID = ?
117 AND eventName = ?";
118 $parameters = [
119 $data['objectTypeID'],
120 $data['eventName']
121 ];
122
123 return [
124 'sql' => $sql,
125 'parameters' => $parameters
126 ];
127 }
128
129 /**
130 * Gets the id of given object type id.
131 *
132 * @param string $objectType
133 * @return integer
134 * @throws SystemException
135 */
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 (
142 SELECT definitionID
143 FROM wcf".WCF_N."_object_type_definition
144 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
145 )";
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'];
151 }
152
153 /**
154 * @inheritDoc
155 */
156 public static function getSyncDependencies() {
157 return ['objectType'];
158 }
159 }