3491295bd0d66f902f9708ad134a5bc70c9a0b7a
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\object\type\ObjectTypeCache;
4 use wcf\data\user\notification\event\UserNotificationEvent;
5 use wcf\data\user\notification\event\UserNotificationEventEditor;
6 use wcf\data\user\notification\event\UserNotificationEventList;
7 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
8 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
9 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
10 use wcf\system\exception\SystemException;
11 use wcf\system\form\builder\container\FormContainer;
12 use wcf\system\form\builder\field\OptionFormField;
13 use wcf\system\form\builder\field\UserGroupOptionFormField;
14 use wcf\system\form\builder\field\validation\FormFieldValidationError;
15 use wcf\system\form\builder\field\validation\FormFieldValidator;
16 use wcf\system\form\builder\field\BooleanFormField;
17 use wcf\system\form\builder\field\ClassNameFormField;
18 use wcf\system\form\builder\field\ItemListFormField;
19 use wcf\system\form\builder\field\SingleSelectionFormField;
20 use wcf\system\form\builder\field\TextFormField;
21 use wcf\system\form\builder\IFormDocument;
22 use wcf\system\user\notification\event\IUserNotificationEvent;
23 use wcf\system\WCF;
24 use wcf\util\StringUtil;
25
26 /**
27 * Installs, updates and deletes user notification events.
28 *
29 * @author Matthias Schmidt, Marcel Werk
30 * @copyright 2001-2018 WoltLab GmbH
31 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
32 * @package WoltLabSuite\Core\System\Package\Plugin
33 */
34 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
35 use TXmlGuiPackageInstallationPlugin;
36
37 /**
38 * @inheritDoc
39 */
40 public $className = UserNotificationEventEditor::class;
41
42 /**
43 * @inheritDoc
44 */
45 public $tableName = 'user_notification_event';
46
47 /**
48 * @inheritDoc
49 */
50 public $tagName = 'event';
51
52 /**
53 * preset event ids
54 * @var integer[]
55 */
56 protected $presetEventIDs = [];
57
58 /**
59 * @inheritDoc
60 */
61 protected function handleDelete(array $items) {
62 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
63 WHERE packageID = ?
64 AND objectTypeID = ?
65 AND eventName = ?";
66 $statement = WCF::getDB()->prepareStatement($sql);
67 foreach ($items as $item) {
68 $statement->execute([
69 $this->installation->getPackageID(),
70 $this->getObjectTypeID($item['elements']['objecttype']),
71 $item['elements']['name']
72 ]);
73 }
74 }
75
76 /**
77 * @inheritDoc
78 */
79 protected function prepareImport(array $data) {
80 $presetMailNotificationType = 'none';
81 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
82 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
83 }
84
85 return [
86 'eventName' => $data['elements']['name'],
87 'className' => $data['elements']['classname'],
88 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
89 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
90 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
91 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
92 'presetMailNotificationType' => $presetMailNotificationType
93 ];
94 }
95
96 /**
97 * @inheritDoc
98 */
99 protected function import(array $row, array $data) {
100 /** @var UserNotificationEvent $event */
101 $event = parent::import($row, $data);
102
103 if (empty($row) && $data['preset']) {
104 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
105 }
106
107 return $event;
108 }
109
110 /**
111 * @inheritDoc
112 */
113 protected function cleanup() {
114 if (empty($this->presetEventIDs)) return;
115
116 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
117 (userID, eventID, mailNotificationType)
118 SELECT userID, ?, ?
119 FROM wcf".WCF_N."_user";
120 $statement = WCF::getDB()->prepareStatement($sql);
121 WCF::getDB()->beginTransaction();
122 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
123 $statement->execute([$eventID, $mailNotificationType]);
124 }
125 WCF::getDB()->commitTransaction();
126 }
127
128 /**
129 * @inheritDoc
130 */
131 protected function findExistingItem(array $data) {
132 $sql = "SELECT *
133 FROM wcf".WCF_N."_".$this->tableName."
134 WHERE objectTypeID = ?
135 AND eventName = ?";
136 $parameters = [
137 $data['objectTypeID'],
138 $data['eventName']
139 ];
140
141 return [
142 'sql' => $sql,
143 'parameters' => $parameters
144 ];
145 }
146
147 /**
148 * Gets the id of given object type id.
149 *
150 * @param string $objectType
151 * @return integer
152 * @throws SystemException
153 */
154 protected function getObjectTypeID($objectType) {
155 // get object type id
156 $sql = "SELECT object_type.objectTypeID
157 FROM wcf".WCF_N."_object_type object_type
158 WHERE object_type.objectType = ?
159 AND object_type.definitionID IN (
160 SELECT definitionID
161 FROM wcf".WCF_N."_object_type_definition
162 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
163 )";
164 $statement = WCF::getDB()->prepareStatement($sql, 1);
165 $statement->execute([$objectType]);
166 $row = $statement->fetchArray();
167 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
168 return $row['objectTypeID'];
169 }
170
171 /**
172 * @inheritDoc
173 * @since 3.1
174 */
175 public static function getSyncDependencies() {
176 return ['objectType'];
177 }
178
179 /**
180 * @inheritDoc
181 * @since 3.2
182 */
183 public function addFormFields(IFormDocument $form) {
184 /** @var FormContainer $dataContainer */
185 $dataContainer = $form->getNodeById('data');
186
187 $dataContainer->appendChildren([
188 TextFormField::create('name')
189 ->label('wcf.acp.pip.userNotificationEvent.name')
190 ->description('wcf.acp.pip.userNotificationEvent.name.description')
191 ->required()
192 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
193 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
194 $formField->addValidationError(
195 new FormFieldValidationError(
196 'format',
197 'wcf.acp.pip.userNotificationEvent.name.error.format'
198 )
199 );
200 }
201 })),
202
203 SingleSelectionFormField::create('objectType')
204 ->objectProperty('objecttype')
205 ->label('wcf.acp.pip.userNotificationEvent.objectType')
206 ->description('wcf.acp.pip.userNotificationEvent.objectType.description')
207 ->required()
208 ->options(function() {
209 $options = [];
210 foreach (ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.notification.objectType') as $objectType) {
211 $options[$objectType->objectType] = $objectType->objectType;
212 }
213
214 asort($options);
215
216 return $options;
217 })
218 // validate the uniqueness of the `name` field after knowing that the selected object type is valid
219 ->addValidator(new FormFieldValidator('nameUniqueness', function(SingleSelectionFormField $formField) {
220 /** @var TextFormField $nameField */
221 $nameField = $formField->getDocument()->getNodeById('name');
222
223 if ($formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE || $this->editedEntry->getAttribute('name') !== $nameField->getSaveValue()) {
224 $eventList = new UserNotificationEventList();
225 $eventList->getConditionBuilder()->add('user_notification_event.eventName = ?', [$nameField->getSaveValue()]);
226 $eventList->getConditionBuilder()->add(
227 'user_notification_event.objectTypeID = ?',
228 [ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.notification.objectType', $formField->getSaveValue())->objectTypeID]
229 );
230
231 if ($eventList->countObjects() > 0) {
232 $nameField->addValidationError(
233 new FormFieldValidationError(
234 'notUnique',
235 'wcf.acp.pip.userNotificationEvent.name.error.notUnique'
236 )
237 );
238 }
239 }
240 })),
241
242 ClassNameFormField::create()
243 ->objectProperty('classname')
244 ->required()
245 ->implementedInterface(IUserNotificationEvent::class),
246
247 BooleanFormField::create('preset')
248 ->label('wcf.acp.pip.userNotificationEvent.preset')
249 ->description('wcf.acp.pip.userNotificationEvent.preset.description'),
250
251 OptionFormField::create()
252 ->description('wcf.acp.pip.userNotificationEvent.options.description')
253 ->packageIDs(array_merge(
254 [$this->installation->getPackage()->packageID],
255 array_keys($this->installation->getPackage()->getAllRequiredPackages())
256 )),
257
258 UserGroupOptionFormField::create()
259 ->description('wcf.acp.pip.userNotificationEvent.permissions.description')
260 ->packageIDs(array_merge(
261 [$this->installation->getPackage()->packageID],
262 array_keys($this->installation->getPackage()->getAllRequiredPackages())
263 )),
264
265 SingleSelectionFormField::create('presetMailNotificationType')
266 ->objectProperty('presetmailnotificationtype')
267 ->label('wcf.acp.pip.userNotificationEvent.presetMailNotificationType')
268 ->description('wcf.acp.pip.userNotificationEvent.presetMailNotificationType.description')
269 ->nullable()
270 ->options([
271 '' => 'wcf.user.notification.mailNotificationType.none',
272 'daily' => 'wcf.user.notification.mailNotificationType.daily',
273 'instant' => 'wcf.user.notification.mailNotificationType.instant'
274 ])
275 ]);
276 }
277
278 /**
279 * @inheritDoc
280 * @since 3.2
281 */
282 protected function getElementData(\DOMElement $element, $saveData = false) {
283 $data = [
284 'className' => $element->getElementsByTagName('classname')->item(0)->nodeValue,
285 'objectTypeID' => $this->getObjectTypeID($element->getElementsByTagName('objecttype')->item(0)->nodeValue),
286 'eventName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
287 'packageID' => $this->installation->getPackage()->packageID,
288 'preset' => 0
289 ];
290
291 $options = $element->getElementsByTagName('options')->item(0);
292 if ($options) {
293 $data['options'] = StringUtil::normalizeCsv($options->nodeValue);
294 }
295
296 $permissions = $element->getElementsByTagName('permissions')->item(0);
297 if ($permissions) {
298 $data['permissions'] = StringUtil::normalizeCsv($permissions->nodeValue);
299 }
300
301 // the presence of a `preset` element is treated as `<preset>1</preset>
302 if ($element->getElementsByTagName('preset')->length === 1) {
303 $data['preset'] = 1;
304 }
305
306 $presetMailNotificationType = $element->getElementsByTagName('presetmailnotificationtype')->item(0);
307 if ($presetMailNotificationType && in_array($presetMailNotificationType->nodeValue, ['instant', 'daily'])) {
308 $data['presetMailNotificationType'] = $presetMailNotificationType->nodeValue;
309 }
310
311 return $data;
312 }
313
314 /**
315 * @inheritDoc
316 * @since 3.2
317 */
318 public function getElementIdentifier(\DOMElement $element) {
319 return sha1(
320 $element->getElementsByTagName('name')->item(0)->nodeValue . '/' .
321 $element->getElementsByTagName('objecttype')->item(0)->nodeValue
322 );
323 }
324
325 /**
326 * @inheritDoc
327 * @since 3.2
328 */
329 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
330 $entryList->setKeys([
331 'name' => 'wcf.acp.pip.userNotificationEvent.name',
332 'className' => 'wcf.acp.pip.userNotificationEvent.className'
333 ]);
334 }
335
336 /**
337 * @inheritDoc
338 * @since 3.2
339 */
340 protected function sortDocument(\DOMDocument $document) {
341 $this->sortImportDelete($document);
342
343 $compareFunction = function(\DOMElement $element1, \DOMElement $element2) {
344 $objectType1 = $element1->getElementsByTagName('objecttype')->item(0)->nodeValue;
345 $objectType2 = $element2->getElementsByTagName('objecttype')->item(0)->nodeValue;
346
347 if ($objectType1 !== $objectType2) {
348 return strcmp($objectType1, $objectType2);
349 }
350
351 return strcmp(
352 $element1->getElementsByTagName('name')->item(0)->nodeValue,
353 $element2->getElementsByTagName('name')->item(0)->nodeValue
354 );
355 };
356
357 $this->sortChildNodes($document->getElementsByTagName('import'), $compareFunction);
358 $this->sortChildNodes($document->getElementsByTagName('delete'), $compareFunction);
359 }
360
361 /**
362 * @inheritDoc
363 * @since 3.2
364 */
365 protected function writeEntry(\DOMDocument $document, IFormDocument $form) {
366 $data = $form->getData()['data'];
367
368 $event = $document->createElement($this->tagName);
369
370 foreach (['name', 'objecttype', 'classname'] as $element) {
371 $event->appendChild(
372 $document->createElement(
373 $element,
374 (string)$data[$element]
375 )
376 );
377 }
378
379 foreach (['options', 'permissions', 'preset', 'presetmailnotificationtype'] as $optionalElement) {
380 if (!empty($data[$optionalElement])) {
381 $event->appendChild(
382 $document->createElement(
383 $optionalElement,
384 (string)$data[$optionalElement]
385 )
386 );
387 }
388 }
389
390 $document->getElementsByTagName('import')->item(0)->appendChild($event);
391
392 return $event;
393 }
394 }