7c7277dcc54db60a4a891ae75add06577833aa07
[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
8 /**
9 * Installs, updates and deletes user notification events.
10 *
11 * @author Marcel Werk
12 * @copyright 2001-2017 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\System\Package\Plugin
15 */
16 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin {
17 /**
18 * @inheritDoc
19 */
20 public $className = UserNotificationEventEditor::class;
21
22 /**
23 * @inheritDoc
24 */
25 public $tableName = 'user_notification_event';
26
27 /**
28 * @inheritDoc
29 */
30 public $tagName = 'event';
31
32 /**
33 * preset event ids
34 * @var integer[]
35 */
36 protected $presetEventIDs = [];
37
38 /**
39 * @inheritDoc
40 */
41 protected function handleDelete(array $items) {
42 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
43 WHERE packageID = ?
44 AND objectTypeID = ?
45 AND eventName = ?";
46 $statement = WCF::getDB()->prepareStatement($sql);
47 foreach ($items as $item) {
48 $statement->execute([
49 $this->installation->getPackageID(),
50 $this->getObjectTypeID($item['elements']['objecttype']),
51 $item['elements']['name']
52 ]);
53 }
54 }
55
56 /**
57 * @inheritDoc
58 */
59 protected function prepareImport(array $data) {
60 $presetMailNotificationType = 'none';
61 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
62 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
63 }
64
65 return [
66 'eventName' => $data['elements']['name'],
67 'className' => $data['elements']['classname'],
68 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
69 'permissions' => isset($data['elements']['permissions']) ? $data['elements']['permissions'] : '',
70 'options' => isset($data['elements']['options']) ? $data['elements']['options'] : '',
71 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
72 'presetMailNotificationType' => $presetMailNotificationType
73 ];
74 }
75
76 /**
77 * @inheritDoc
78 */
79 protected function import(array $row, array $data) {
80 /** @var UserNotificationEvent $event */
81 $event = parent::import($row, $data);
82
83 if (empty($row) && $data['preset']) {
84 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
85 }
86
87 return $event;
88 }
89
90 /**
91 * @inheritDoc
92 */
93 protected function cleanup() {
94 if (empty($this->presetEventIDs)) return;
95
96 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
97 (userID, eventID, mailNotificationType)
98 SELECT userID, ?, ?
99 FROM wcf".WCF_N."_user";
100 $statement = WCF::getDB()->prepareStatement($sql);
101 WCF::getDB()->beginTransaction();
102 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
103 $statement->execute([$eventID, $mailNotificationType]);
104 }
105 WCF::getDB()->commitTransaction();
106 }
107
108 /**
109 * @inheritDoc
110 */
111 protected function findExistingItem(array $data) {
112 $sql = "SELECT *
113 FROM wcf".WCF_N."_".$this->tableName."
114 WHERE objectTypeID = ?
115 AND eventName = ?";
116 $parameters = [
117 $data['objectTypeID'],
118 $data['eventName']
119 ];
120
121 return [
122 'sql' => $sql,
123 'parameters' => $parameters
124 ];
125 }
126
127 /**
128 * Gets the id of given object type id.
129 *
130 * @param string $objectType
131 * @return integer
132 */
133 protected function getObjectTypeID($objectType) {
134 // get object type id
135 $sql = "SELECT object_type.objectTypeID
136 FROM wcf".WCF_N."_object_type object_type
137 WHERE object_type.objectType = ?
138 AND object_type.definitionID IN (
139 SELECT definitionID
140 FROM wcf".WCF_N."_object_type_definition
141 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
142 )";
143 $statement = WCF::getDB()->prepareStatement($sql, 1);
144 $statement->execute([$objectType]);
145 $row = $statement->fetchArray();
146 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
147 return $row['objectTypeID'];
148 }
149 }