2 declare(strict_types=1);
3 namespace wcf\system\package\plugin;
4 use wcf\data\object\type\ObjectTypeCache;
5 use wcf\data\user\notification\event\UserNotificationEvent;
6 use wcf\data\user\notification\event\UserNotificationEventEditor;
7 use wcf\data\user\notification\event\UserNotificationEventList;
8 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
9 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
10 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
11 use wcf\system\exception\SystemException;
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;
24 use wcf\util\StringUtil;
27 * Installs, updates and deletes user notification events.
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
34 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
35 use TXmlGuiPackageInstallationPlugin;
40 public $className = UserNotificationEventEditor::class;
45 public $tableName = 'user_notification_event';
50 public $tagName = 'event';
56 protected $presetEventIDs = [];
61 protected function handleDelete(array $items) {
62 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
66 $statement = WCF::getDB()->prepareStatement($sql);
67 foreach ($items as $item) {
69 $this->installation->getPackageID(),
70 $this->getObjectTypeID($item['elements']['objecttype']),
71 $item['elements']['name']
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'];
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
99 protected function import(array $row, array $data) {
100 /** @var UserNotificationEvent $event */
101 $event = parent::import($row, $data);
103 if (empty($row) && $data['preset']) {
104 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
113 protected function cleanup() {
114 if (empty($this->presetEventIDs)) return;
116 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
117 (userID, eventID, mailNotificationType)
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]);
125 WCF::getDB()->commitTransaction();
131 protected function findExistingItem(array $data) {
133 FROM wcf".WCF_N."_".$this->tableName."
134 WHERE objectTypeID = ?
137 $data['objectTypeID'],
143 'parameters' => $parameters
148 * Gets the id of given object type id.
150 * @param string $objectType
152 * @throws SystemException
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 (
161 FROM wcf".WCF_N."_object_type_definition
162 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
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'];
175 public static function getSyncDependencies() {
176 return ['objectType'];
183 public function addFormFields(IFormDocument $form) {
184 $form->getNodeById('data')->appendChildren([
185 TextFormField::create('name')
186 ->label('wcf.acp.pip.userNotificationEvent.name')
187 ->description('wcf.acp.pip.userNotificationEvent.name.description')
189 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
190 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
191 $formField->addValidationError(
192 new FormFieldValidationError(
194 'wcf.acp.pip.userNotificationEvent.name.error.format'
200 SingleSelectionFormField::create('objectType')
201 ->objectProperty('objecttype')
202 ->label('wcf.acp.pip.userNotificationEvent.objectType')
203 ->description('wcf.acp.pip.userNotificationEvent.objectType.description')
205 ->options(function(): array {
207 foreach (ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.notification.objectType') as $objectType) {
208 $options[$objectType->objectType] = $objectType->objectType;
215 // validate the uniqueness of the `name` field after knowing that the selected object type is valid
216 ->addValidator(new FormFieldValidator('nameUniqueness', function(SingleSelectionFormField $formField) {
217 /** @var TextFormField $nameField */
218 $nameField = $formField->getDocument()->getNodeById('name');
220 if ($formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE || $this->editedEntry->getAttribute('name') !== $nameField->getSaveValue()) {
221 $eventList = new UserNotificationEventList();
222 $eventList->getConditionBuilder()->add('user_notification_event.eventName = ?', [$nameField->getSaveValue()]);
223 $eventList->getConditionBuilder()->add(
224 'user_notification_event.objectTypeID = ?',
225 [ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.notification.objectType', $formField->getSaveValue())->objectTypeID]
228 if ($eventList->countObjects() > 0) {
229 $nameField->addValidationError(
230 new FormFieldValidationError(
232 'wcf.acp.pip.userNotificationEvent.name.error.notUnique'
239 ClassNameFormField::create()
240 ->objectProperty('classname')
242 ->implementedInterface(IUserNotificationEvent::class),
244 BooleanFormField::create('preset')
245 ->label('wcf.acp.pip.userNotificationEvent.preset')
246 ->description('wcf.acp.pip.userNotificationEvent.preset.description'),
248 OptionFormField::create()
249 ->description('wcf.acp.pip.userNotificationEvent.options.description')
250 ->packageIDs(array_merge(
251 [$this->installation->getPackage()->packageID],
252 array_keys($this->installation->getPackage()->getAllRequiredPackages())
255 UserGroupOptionFormField::create()
256 ->description('wcf.acp.pip.userNotificationEvent.permissions.description')
257 ->packageIDs(array_merge(
258 [$this->installation->getPackage()->packageID],
259 array_keys($this->installation->getPackage()->getAllRequiredPackages())
262 SingleSelectionFormField::create('presetMailNotificationType')
263 ->objectProperty('presetmailnotificationtype')
264 ->label('wcf.acp.pip.userNotificationEvent.presetMailNotificationType')
265 ->description('wcf.acp.pip.userNotificationEvent.presetMailNotificationType.description')
268 '' => 'wcf.user.notification.mailNotificationType.none',
269 'daily' => 'wcf.user.notification.mailNotificationType.daily',
270 'instant' => 'wcf.user.notification.mailNotificationType.instant'
279 protected function getElementData(\DOMElement $element, bool $saveData = false): array {
281 'className' => $element->getElementsByTagName('classname')->item(0)->nodeValue,
282 'objectTypeID' => $this->getObjectTypeID($element->getElementsByTagName('objecttype')->item(0)->nodeValue),
283 'eventName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
284 'packageID' => $this->installation->getPackage()->packageID,
288 $options = $element->getElementsByTagName('options')->item(0);
290 $data['options'] = StringUtil::normalizeCsv($options->nodeValue);
293 $permissions = $element->getElementsByTagName('permissions')->item(0);
295 $data['permissions'] = StringUtil::normalizeCsv($permissions->nodeValue);
298 // the presence of a `preset` element is treated as `<preset>1</preset>
299 if ($element->getElementsByTagName('preset')->length === 1) {
303 $presetMailNotificationType = $element->getElementsByTagName('presetmailnotificationtype')->item(0);
304 if ($presetMailNotificationType && in_array($presetMailNotificationType->nodeValue, ['instant', 'daily'])) {
305 $data['presetMailNotificationType'] = $presetMailNotificationType->nodeValue;
315 public function getElementIdentifier(\DOMElement $element): string {
317 $element->getElementsByTagName('name')->item(0)->nodeValue . '/' .
318 $element->getElementsByTagName('objecttype')->item(0)->nodeValue
326 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
327 $entryList->setKeys([
328 'name' => 'wcf.acp.pip.userNotificationEvent.name',
329 'className' => 'wcf.acp.pip.userNotificationEvent.className'
337 protected function sortDocument(\DOMDocument $document) {
338 $this->sortImportDelete($document);
340 $compareFunction = function(\DOMElement $element1, \DOMElement $element2) {
341 $objectType1 = $element1->getElementsByTagName('objecttype')->item(0)->nodeValue;
342 $objectType2 = $element2->getElementsByTagName('objecttype')->item(0)->nodeValue;
344 if ($objectType1 !== $objectType2) {
345 return strcmp($objectType1, $objectType2);
349 $element1->getElementsByTagName('name')->item(0)->nodeValue,
350 $element2->getElementsByTagName('name')->item(0)->nodeValue
354 $this->sortChildNodes($document->getElementsByTagName('import'), $compareFunction);
355 $this->sortChildNodes($document->getElementsByTagName('delete'), $compareFunction);
362 protected function writeEntry(\DOMDocument $document, IFormDocument $form): \DOMElement {
363 $event = $document->createElement($this->tagName);
365 $event->appendChild($document->createElement('name', $form->getNodeById('name')->getSaveValue()));
366 $event->appendChild($document->createElement('objecttype', $form->getNodeById('objectType')->getSaveValue()));
367 $event->appendChild($document->createElement('classname', $form->getNodeById('className')->getSaveValue()));
369 /** @var ItemListFormField $options */
370 $options = $form->getNodeById('options');
371 if ($options->getSaveValue()) {
372 $event->appendChild($document->createElement('options', $options->getSaveValue()));
375 /** @var ItemListFormField $permissions */
376 $permissions = $form->getNodeById('permissions');
377 if ($permissions->getSaveValue()) {
378 $event->appendChild($document->createElement('permissions', $permissions->getSaveValue()));
381 /** @var BooleanFormField $permissions */
382 $preset = $form->getNodeById('preset');
383 if ($preset->getSaveValue()) {
384 $event->appendChild($document->createElement('preset', '1'));
387 /** @var BooleanFormField $permissions */
388 $presetMailNotificationType = $form->getNodeById('presetMailNotificationType');
389 if ($presetMailNotificationType->getSaveValue()) {
390 $event->appendChild($document->createElement('presetmailnotificationtype', $presetMailNotificationType->getSaveValue()));
393 $document->getElementsByTagName('import')->item(0)->appendChild($event);