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\option\OptionFormField;
13 use wcf\system\form\builder\field\user\group\option\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\SingleSelectionFormField;
19 use wcf\system\form\builder\field\TextFormField;
20 use wcf\system\form\builder\IFormDocument;
21 use wcf\system\user\notification\event\IUserNotificationEvent;
23 use wcf\util\StringUtil;
26 * Installs, updates and deletes user notification events.
28 * @author Matthias Schmidt, Marcel Werk
29 * @copyright 2001-2018 WoltLab GmbH
30 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
31 * @package WoltLabSuite\Core\System\Package\Plugin
33 class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
34 use TXmlGuiPackageInstallationPlugin;
39 public $className = UserNotificationEventEditor::class;
44 public $tableName = 'user_notification_event';
49 public $tagName = 'event';
55 protected $presetEventIDs = [];
60 protected function handleDelete(array $items) {
61 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
65 $statement = WCF::getDB()->prepareStatement($sql);
66 foreach ($items as $item) {
68 $this->installation->getPackageID(),
69 $this->getObjectTypeID($item['elements']['objecttype']),
70 $item['elements']['name']
78 protected function prepareImport(array $data) {
79 $presetMailNotificationType = 'none';
80 if (isset($data['elements']['presetmailnotificationtype']) && ($data['elements']['presetmailnotificationtype'] == 'instant' || $data['elements']['presetmailnotificationtype'] == 'daily')) {
81 $presetMailNotificationType = $data['elements']['presetmailnotificationtype'];
85 'eventName' => $data['elements']['name'],
86 'className' => $data['elements']['classname'],
87 'objectTypeID' => $this->getObjectTypeID($data['elements']['objecttype']),
88 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
89 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
90 'preset' => !empty($data['elements']['preset']) ? 1 : 0,
91 'presetMailNotificationType' => $presetMailNotificationType
98 protected function import(array $row, array $data) {
99 /** @var UserNotificationEvent $event */
100 $event = parent::import($row, $data);
102 if (empty($row) && $data['preset']) {
103 $this->presetEventIDs[$event->eventID] = $data['presetMailNotificationType'];
112 protected function cleanup() {
113 if (empty($this->presetEventIDs)) return;
115 $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_notification_event_to_user
116 (userID, eventID, mailNotificationType)
118 FROM wcf".WCF_N."_user";
119 $statement = WCF::getDB()->prepareStatement($sql);
120 WCF::getDB()->beginTransaction();
121 foreach ($this->presetEventIDs as $eventID => $mailNotificationType) {
122 $statement->execute([$eventID, $mailNotificationType]);
124 WCF::getDB()->commitTransaction();
130 protected function findExistingItem(array $data) {
132 FROM wcf".WCF_N."_".$this->tableName."
133 WHERE objectTypeID = ?
136 $data['objectTypeID'],
142 'parameters' => $parameters
147 * Gets the id of given object type id.
149 * @param string $objectType
151 * @throws SystemException
153 protected function getObjectTypeID($objectType) {
154 // get object type id
155 $sql = "SELECT object_type.objectTypeID
156 FROM wcf".WCF_N."_object_type object_type
157 WHERE object_type.objectType = ?
158 AND object_type.definitionID IN (
160 FROM wcf".WCF_N."_object_type_definition
161 WHERE definitionName = 'com.woltlab.wcf.notification.objectType'
163 $statement = WCF::getDB()->prepareStatement($sql, 1);
164 $statement->execute([$objectType]);
165 $row = $statement->fetchArray();
166 if (empty($row['objectTypeID'])) throw new SystemException("unknown notification object type '".$objectType."' given");
167 return $row['objectTypeID'];
174 public static function getSyncDependencies() {
175 return ['objectType'];
182 protected function addFormFields(IFormDocument $form) {
183 /** @var FormContainer $dataContainer */
184 $dataContainer = $form->getNodeById('data');
186 $dataContainer->appendChildren([
187 TextFormField::create('eventName')
188 ->objectProperty('name')
189 ->label('wcf.acp.pip.userNotificationEvent.eventName')
190 ->description('wcf.acp.pip.userNotificationEvent.eventName.description')
192 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
193 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
194 $formField->addValidationError(
195 new FormFieldValidationError(
197 'wcf.acp.pip.userNotificationEvent.eventName.error.format'
203 SingleSelectionFormField::create('objectType')
204 ->objectProperty('objecttype')
205 ->label('wcf.acp.pip.userNotificationEvent.objectType')
206 ->description('wcf.acp.pip.userNotificationEvent.objectType.description')
208 ->options(function() {
210 foreach (ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.notification.objectType') as $objectType) {
211 $options[$objectType->objectType] = $objectType->objectType;
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('eventName');
224 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
225 $this->editedEntry->getElementsByTagName('name')->item(0)->nodeValue !== $nameField->getSaveValue() ||
226 $this->editedEntry->getElementsByTagName('objecttype')->item(0)->nodeValue !== $formField->getSaveValue()
228 $eventList = new UserNotificationEventList();
229 $eventList->getConditionBuilder()->add('user_notification_event.eventName = ?', [$nameField->getSaveValue()]);
230 $eventList->getConditionBuilder()->add('user_notification_event.objectTypeID = ?', [
231 ObjectTypeCache::getInstance()->getObjectTypeIDByName('com.woltlab.wcf.notification.objectType', $formField->getSaveValue())
233 $eventList->getConditionBuilder()->add(
234 'user_notification_event.objectTypeID = ?',
235 [ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.notification.objectType', $formField->getSaveValue())->objectTypeID]
238 if ($eventList->countObjects() > 0) {
239 $nameField->addValidationError(
240 new FormFieldValidationError(
242 'wcf.acp.pip.userNotificationEvent.eventName.error.notUnique'
249 ClassNameFormField::create()
250 ->objectProperty('classname')
252 ->implementedInterface(IUserNotificationEvent::class),
254 BooleanFormField::create('preset')
255 ->label('wcf.acp.pip.userNotificationEvent.preset')
256 ->description('wcf.acp.pip.userNotificationEvent.preset.description'),
258 OptionFormField::create()
259 ->description('wcf.acp.pip.userNotificationEvent.options.description')
260 ->packageIDs(array_merge(
261 [$this->installation->getPackage()->packageID],
262 array_keys($this->installation->getPackage()->getAllRequiredPackages())
265 UserGroupOptionFormField::create()
266 ->description('wcf.acp.pip.userNotificationEvent.permissions.description')
267 ->packageIDs(array_merge(
268 [$this->installation->getPackage()->packageID],
269 array_keys($this->installation->getPackage()->getAllRequiredPackages())
272 SingleSelectionFormField::create('presetMailNotificationType')
273 ->objectProperty('presetmailnotificationtype')
274 ->label('wcf.acp.pip.userNotificationEvent.presetMailNotificationType')
275 ->description('wcf.acp.pip.userNotificationEvent.presetMailNotificationType.description')
278 '' => 'wcf.user.notification.mailNotificationType.none',
279 'daily' => 'wcf.user.notification.mailNotificationType.daily',
280 'instant' => 'wcf.user.notification.mailNotificationType.instant'
289 protected function fetchElementData(\DOMElement $element, $saveData) {
291 'className' => $element->getElementsByTagName('classname')->item(0)->nodeValue,
292 'eventName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
293 'packageID' => $this->installation->getPackage()->packageID,
297 $objectType = $element->getElementsByTagName('objecttype')->item(0)->nodeValue;
299 $data['objectTypeID'] = $this->getObjectTypeID($objectType);
302 $data['objectType'] = $objectType;
305 $options = $element->getElementsByTagName('options')->item(0);
307 $data['options'] = StringUtil::normalizeCsv($options->nodeValue);
309 else if ($saveData) {
310 $data['options'] = '';
313 $permissions = $element->getElementsByTagName('permissions')->item(0);
315 $data['permissions'] = StringUtil::normalizeCsv($permissions->nodeValue);
317 else if ($saveData) {
318 $data['permissions'] = '';
321 // the presence of a `preset` element is treated as `<preset>1</preset>
322 if ($element->getElementsByTagName('preset')->length === 1) {
325 else if ($saveData) {
329 $presetMailNotificationType = $element->getElementsByTagName('presetmailnotificationtype')->item(0);
330 if ($presetMailNotificationType && in_array($presetMailNotificationType->nodeValue, ['instant', 'daily'])) {
331 $data['presetMailNotificationType'] = $presetMailNotificationType->nodeValue;
333 else if ($saveData) {
334 $data['presetMailNotificationType'] = 'none';
344 public function getElementIdentifier(\DOMElement $element) {
346 $element->getElementsByTagName('name')->item(0)->nodeValue . '/' .
347 $element->getElementsByTagName('objecttype')->item(0)->nodeValue
355 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
356 $entryList->setKeys([
357 'eventName' => 'wcf.acp.pip.userNotificationEvent.eventName',
358 'className' => 'wcf.form.field.className'
366 protected function prepareXmlElement(\DOMDocument $document, IFormDocument $form) {
367 $event = $document->createElement($this->tagName);
369 $this->appendElementChildren(
378 'presetmailnotificationtype' => ''
390 protected function prepareDeleteXmlElement(\DOMElement $element) {
391 $userNotificationEvent = $element->ownerDocument->createElement($this->tagName);
393 foreach (['name', 'objecttype'] as $childElement) {
394 $userNotificationEvent->appendChild($element->ownerDocument->createElement(
396 $element->getElementsByTagName($childElement)->item(0)->nodeValue
400 return $userNotificationEvent;
407 protected function deleteObject(\DOMElement $element) {
409 foreach (['name', 'objecttype'] as $childElement) {
410 $elements[$childElement] = $element->getElementsByTagName($childElement)->item(0)->nodeValue;
413 $this->handleDelete([['elements' => $elements]]);