aa28ff0ffdce27a3af75e680cfd7c642939cd5e8
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\user\notification\event\UserNotificationEventEditor;
4 use wcf\system\exception\SystemException;
5 use wcf\system\WCF;
6
7 /**
8 * Installs, updates and deletes user notification events.
9 *
10 * @author Marcel Werk
11 * @copyright 2001-2016 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package com.woltlab.wcf
14 * @subpackage system.package.plugin
15 * @category Community Framework
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 eventName = ?";
46 $statement = WCF::getDB()->prepareStatement($sql);
47 foreach ($items as $item) {
48 $statement->execute([
49 $this->installation->getPackageID(),
50 $item['elements']['name']
51 ]);
52 }
53 }
54
55 /**
56 * @inheritDoc
57 */
58 protected function prepareImport(array $data) {
59 // get object type id
60 $sql = "SELECT object_type.objectTypeID
61 FROM wcf".WCF_N."_object_type object_type
62 WHERE object_type.objectType = ?
63 AND object_type.definitionID IN (
64 SELECT definitionID
65 FROM wcf".WCF_N."_object_type_definition
66 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
67 )";
68 $statement = WCF::getDB()->prepareStatement($sql, 1);
69 $statement->execute([$data['elements']['objecttype']]);
70 $row = $statement->fetchArray();
71 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$data['elements']['objecttype']."' given");
72 $objectTypeID = $row['objectTypeID'];
73
74 $presetMailNotificationType = 'none';
75 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
76 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
77 }
78
79 return [
80 'eventName' => $data['elements']['name'],
81 'className' => $data['elements']['classname'],
82 'objectTypeID' => $objectTypeID,
83 'permissions' => (isset($data['elements']['permissions']) ? $data['elements']['permissions'] : ''),
84 'options' => (isset($data['elements']['options']) ? $data['elements']['options'] : ''),
85 'preset' => (!empty($data['elements']['preset']) ? 1 : 0),
86 'presetMailNotificationType' => $presetMailNotificationType
87 ];
88 }
89
90 /**
91 * @inheritDoc
92 */
93 protected function import(array $row, array $data) {
94 $result = parent::import($row, $data);
95
96 if (empty($row) && $data['preset']) {
97 $this->presetEventIDs[$result->eventID] = $data['presetMailNotificationType'];
98 }
99
100 return $result;
101 }
102
103 /**
104 * @inheritDoc
105 */
106 protected function cleanup() {
107 if (empty($this->presetEventIDs)) return;
108
109 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
110 (userID, eventID, mailNotificationType)
111 SELECT userID, ?, ?
112 FROM wcf".WCF_N."_user";
113 $statement = WCF::getDB()->prepareStatement($sql);
114 WCF::getDB()->beginTransaction();
115 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
116 $statement->execute([$eventID, $mailNotificationType]);
117 }
118 WCF::getDB()->commitTransaction();
119 }
120
121 /**
122 * @inheritDoc
123 */
124 protected function findExistingItem(array $data) {
125 $sql = "SELECT *
126 FROM wcf".WCF_N."_".$this->tableName."
127 WHERE objectTypeID = ?
128 AND eventName = ?";
129 $parameters = [
130 $data['objectTypeID'],
131 $data['eventName']
132 ];
133
134 return [
135 'sql' => $sql,
136 'parameters' => $parameters
137 ];
138 }
139 }