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