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