|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wcf\data; |
| 4 | + |
| 5 | +use wcf\system\attachment\AttachmentHandler; |
| 6 | +use wcf\system\html\input\HtmlInputProcessor; |
| 7 | +use wcf\system\language\I18nHandler; |
| 8 | +use wcf\system\message\embedded\object\MessageEmbeddedObjectManager; |
| 9 | +use wcf\util\StringUtil; |
| 10 | + |
| 11 | +/** |
| 12 | + * Trait providing simple usage functions for handling i18n values in DatabaseObjectActions |
| 13 | + * |
| 14 | + * @notice meant to be used in extensions of `AbstractDatabaseObjectAction` only |
| 15 | + */ |
| 16 | +trait TI18nDatabaseObjectAction |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param string $propertyName |
| 20 | + * @param string|null $value |
| 21 | + */ |
| 22 | + protected function enforceValue(string $propertyName, ?string $value = null): void |
| 23 | + { |
| 24 | + if (isset($this->parameters['data'][$propertyName])) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if ( |
| 29 | + !isset($this->parameter[$propertyName . '_htmlInputProcessors']) |
| 30 | + && !isset($this->parameters[$propertyName . '_i18n']) |
| 31 | + ) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + if ($value === null) { |
| 36 | + $value = StringUtil::getRandomID(); |
| 37 | + } |
| 38 | + |
| 39 | + $this->parameter['data'][$propertyName] = $value; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param string $propertyName |
| 44 | + * @param string $languageItemPattern |
| 45 | + * @param string $languageItemCategory |
| 46 | + * @param DatabaseObject $object |
| 47 | + * @param int $packageID |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + protected function saveI18nValues( |
| 51 | + string $propertyName, |
| 52 | + string $languageItemPattern, |
| 53 | + string $languageItemCategory, |
| 54 | + DatabaseObject $object, |
| 55 | + int $packageID = PACKAGE_ID |
| 56 | + ): array { |
| 57 | + $values = null; |
| 58 | + $hasEmbeddedObjects = false; |
| 59 | + $attachmentCount = 0; |
| 60 | + $languageItem = \str_replace('\\d+', $object->getObjectID(), $languageItemPattern); |
| 61 | + |
| 62 | + if (isset($this->parameter[$propertyName . '_htmlInputProcessors'])) { |
| 63 | + $values = []; |
| 64 | + /** @var HtmlInputProcessor $htmlInputProcessor */ |
| 65 | + foreach ($this->parameter[$propertyName . '_htmlInputProcessors'] as $languageID => $htmlInputProcessor) { |
| 66 | + $htmlInputProcessor->setObjectID($object->getObjectID()); |
| 67 | + $hasEmbeddedObjects = MessageEmbeddedObjectManager::getInstance()->registerObjects( |
| 68 | + $htmlInputProcessor, |
| 69 | + true |
| 70 | + ) || $hasEmbeddedObjects; |
| 71 | + $values[$languageID] = $htmlInputProcessor->getHtml(); |
| 72 | + } |
| 73 | + |
| 74 | + if (isset($this->parameter[$propertyName . '_attachmentHandler'])) { |
| 75 | + $attachmentHandler = $this->parameter[$propertyName . '_attachmentHandler']; |
| 76 | + \assert($attachmentHandler instanceof AttachmentHandler); |
| 77 | + $attachmentHandler->updateObjectID($object->getObjectID()); |
| 78 | + $attachmentCount = $attachmentHandler->count(); |
| 79 | + } |
| 80 | + } elseif (isset($this->parameters[$propertyName . '_i18n'])) { |
| 81 | + $values = $this->parameter[$propertyName . '_i18n']; |
| 82 | + } |
| 83 | + |
| 84 | + if (!empty($values)) { |
| 85 | + I18nHandler::getInstance()->save($values, $languageItem, $languageItemCategory, $packageID); |
| 86 | + if ($object->$propertyName !== $languageItem) { |
| 87 | + $className = $this->getClassName(); |
| 88 | + (new $className($object))->update([ |
| 89 | + $propertyName => $languageItem, |
| 90 | + ]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return [ |
| 95 | + 'languageItem' => $languageItem, |
| 96 | + 'hasEmbeddedObjects' => $hasEmbeddedObjects, |
| 97 | + 'hasAttachments' => $attachmentCount > 0, |
| 98 | + 'attachmentCount' => $attachmentCount, |
| 99 | + ]; |
| 100 | + } |
| 101 | +} |
0 commit comments