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