2 declare(strict_types=1);
3 namespace wcf\system\package\plugin;
4 use wcf\data\object\type\definition\ObjectTypeDefinitionEditor;
5 use wcf\data\object\type\ObjectTypeCache;
6 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
7 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
8 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
9 use wcf\system\form\builder\field\validation\FormFieldValidationError;
10 use wcf\system\form\builder\field\validation\FormFieldValidator;
11 use wcf\system\form\builder\field\TextFormField;
12 use wcf\system\form\builder\IFormDocument;
16 * Installs, updates and deletes object type definitions.
18 * @author Alexander Ebert
19 * @copyright 2001-2018 WoltLab GmbH
20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 * @package WoltLabSuite\Core\Acp\Package\Plugin
23 class ObjectTypeDefinitionPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
24 use TXmlGuiPackageInstallationPlugin;
29 public $className = ObjectTypeDefinitionEditor::class;
34 public $tagName = 'definition';
39 protected function handleDelete(array $items) {
40 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
41 WHERE definitionName = ?
43 $statement = WCF::getDB()->prepareStatement($sql);
44 foreach ($items as $item) {
46 $item['attributes']['name'],
47 $this->installation->getPackageID()
55 protected function prepareImport(array $data) {
57 'interfaceName' => isset($data['elements']['interfacename']) ? $data['elements']['interfacename'] : '',
58 'definitionName' => $data['elements']['name'],
59 'categoryName' => isset($data['elements']['categoryname']) ? $data['elements']['categoryname'] : ''
66 protected function findExistingItem(array $data) {
68 FROM wcf".WCF_N."_".$this->tableName."
69 WHERE definitionName = ?";
70 $parameters = [$data['definitionName']];
74 'parameters' => $parameters
82 public static function getSyncDependencies() {
90 public function addFormFields(IFormDocument $form) {
91 $form->getNodeById('data')->appendChildren([
92 TextFormField::create('name')
93 ->label('wcf.acp.pip.objectTypeDefinition.definitionName')
94 ->description('wcf.acp.pip.objectTypeDefinition.definitionName.description', ['project' => $this->installation->getProject()])
96 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
97 if ($formField->getValue()) {
98 $segments = explode('.', $formField->getValue());
99 if (count($segments) < 4) {
100 $formField->addValidationError(
101 new FormFieldValidationError(
103 'wcf.acp.pip.objectTypeDefinition.definitionName.error.tooFewSegments',
104 ['segmentCount' => count($segments)]
109 $invalidSegments = [];
110 foreach ($segments as $key => $segment) {
111 if (!preg_match('~^[A-z0-9\-\_]+$~', $segment)) {
112 $invalidSegments[$key] = $segment;
116 if (!empty($invalidSegments)) {
117 $formField->addValidationError(
118 new FormFieldValidationError(
120 'wcf.acp.pip.objectTypeDefinition.definitionName.error.invalidSegments',
121 ['invalidSegments' => $invalidSegments]
128 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
129 if ($formField->getValue()) {
130 $objectTypeDefinition = ObjectTypeCache::getInstance()->getDefinitionByName($formField->getValue());
132 // the definition name is not unique if such an object type definition
133 // already exists and (a) a new definition is added or (b) an existing
134 // definition is edited but the new definition name is not the old definition
135 // name so that the existing definition is not the definition currently edited
136 if ($objectTypeDefinition !== null && (
137 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
138 $this->editedEntry->getElementsByTagName('name')->item(0)->nodeValue !== $formField->getValue()
140 $formField->addValidationError(
141 new FormFieldValidationError(
143 'wcf.acp.pip.objectTypeDefinition.definitionName.error.notUnique'
150 TextFormField::create('interfaceName')
151 ->objectProperty('interfacename')
152 ->label('wcf.acp.pip.objectTypeDefinition.interfaceName')
153 ->description('wcf.acp.pip.objectTypeDefinition.interfaceName.description')
154 ->addValidator(new FormFieldValidator('interfaceExists', function(TextFormField $formField) {
155 if ($formField->getValue() && !interface_exists($formField->getValue())) {
156 $formField->addValidationError(
157 new FormFieldValidationError(
159 'wcf.acp.pip.objectTypeDefinition.interfaceName.error.nonExistent'
171 protected function getElementData(\DOMElement $element, bool $saveData = false): array {
173 'definitionName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
174 'packageID' => $this->installation->getPackage()->packageID
177 $interfaceName = $element->getElementsByTagName('interfacename')->item(0);
178 if ($interfaceName) {
179 $data['interfaceName'] = $interfaceName->nodeValue;
189 public function getElementIdentifier(\DOMElement $element): string {
190 return $element->getElementsByTagName('name')->item(0)->nodeValue;
197 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
198 $entryList->setKeys([
199 'definitionName' => 'wcf.acp.pip.objectTypeDefinition.definitionName',
200 'interfaceName' => 'wcf.acp.pip.objectTypeDefinition.interfaceName'
208 protected function sortDocument(\DOMDocument $document) {
209 $this->sortImportDelete($document);
211 $this->sortChildNodes($document->getElementsByTagName('import'), function(\DOMElement $element1, \DOMElement $element2) {
213 $element1->getElementsByTagName('name')->item(0)->nodeValue,
214 $element2->getElementsByTagName('name')->item(0)->nodeValue
218 $this->sortChildNodes($document->getElementsByTagName('delete'), function(\DOMElement $element1, \DOMElement $element2) {
220 $element1->getAttribute('name'),
221 $element2->getAttribute('name')
230 protected function writeEntry(\DOMDocument $document, IFormDocument $form): \DOMElement {
231 $definition = $document->createElement('definition');
232 $definition->appendChild($document->createElement('name', $form->getNodeById('name')->getSaveValue()));
234 /** @var TextFormField $interfaceName */
235 $interfaceName = $form->getNodeById('interfaceName');
236 if ($interfaceName->getSaveValue()) {
237 $definition->appendChild($document->createElement('interfacename', $interfaceName->getSaveValue()));
240 $import = $document->getElementsByTagName('import')->item(0);
241 $import->appendChild($definition);