8021f5bbdfb4cd94f5c4163bc5e2b07c62e42206
[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-2014 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 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::$className
19 */
20 public $className = 'wcf\data\user\notification\event\UserNotificationEventEditor';
21
22 /**
23 * @see \wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName
24 */
25 public $tableName = 'user_notification_event';
26
27 /**
28 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::$tagName
29 */
30 public $tagName = 'event';
31
32 /**
33 * preset event ids
34 * @var array<integer>
35 */
36 protected $presetEventIDs = array();
37
38 /**
39 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::handleDelete()
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(array(
48 $this->installation->getPackageID(),
49 $item['elements']['name']
50 ));
51 }
52 }
53
54 /**
55 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::prepareImport()
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(array($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 return array(
74 'eventName' => $data['elements']['name'],
75 'className' => $data['elements']['classname'],
76 'objectTypeID' => $objectTypeID,
77 'permissions' => (isset($data['elements']['permissions']) ? $data['elements']['permissions'] : ''),
78 'options' => (isset($data['elements']['options']) ? $data['elements']['options'] : ''),
79 'preset' => (!empty($data['elements']['preset']) ? 1 : 0)
80 );
81 }
82
83 /**
84 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::import()
85 */
86 protected function import(array $row, array $data) {
87 $result = parent::import($row, $data);
88
89 if (empty($row)) {
90 $this->presetEventIDs[] = $result->eventID;
91 }
92
93 return $result;
94 }
95
96 /**
97 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::cleanup()
98 */
99 protected function cleanup() {
100 if (empty($this->presetEventIDs)) return;
101
102 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
103 (userID, eventID)
104 SELECT userID, ?
105 FROM wcf".WCF_N."_user";
106 $statement = WCF::getDB()->prepareStatement($sql);
107 WCF::getDB()->beginTransaction();
108 foreach ($this->presetEventIDs as $eventID) {
109 $statement->execute(array($eventID));
110 }
111 WCF::getDB()->commitTransaction();
112 }
113
114 /**
115 * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::findExistingItem()
116 */
117 protected function findExistingItem(array $data) {
118 $sql = "SELECT *
119 FROM wcf".WCF_N."_".$this->tableName."
120 WHERE objectTypeID = ?
121 AND eventName = ?";
122 $parameters = array(
123 $data['objectTypeID'],
124 $data['eventName']
125 );
126
127 return array(
128 'sql' => $sql,
129 'parameters' => $parameters
130 );
131 }
132 }