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