f9eab984463c502ceb86c51520fea24adc7ed2f4
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\object\type\definition\ObjectTypeDefinitionEditor;
4 use wcf\data\object\type\ObjectTypeCache;
5 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
6 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
7 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
8 use wcf\system\form\builder\container\FormContainer;
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\field\validation\FormFieldValidatorUtil;
13 use wcf\system\form\builder\IFormDocument;
14 use wcf\system\WCF;
15
16 /**
17 * Installs, updates and deletes object type definitions.
18 *
19 * @author Alexander Ebert
20 * @copyright 2001-2018 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\Acp\Package\Plugin
23 */
24 class ObjectTypeDefinitionPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
25 use TXmlGuiPackageInstallationPlugin;
26
27 /**
28 * @inheritDoc
29 */
30 public $className = ObjectTypeDefinitionEditor::class;
31
32 /**
33 * @inheritDoc
34 */
35 public $tagName = 'definition';
36
37 /**
38 * @inheritDoc
39 */
40 protected function handleDelete(array $items) {
41 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
42 WHERE definitionName = ?
43 AND packageID = ?";
44 $statement = WCF::getDB()->prepareStatement($sql);
45 foreach ($items as $item) {
46 $statement->execute([
47 $item['attributes']['name'],
48 $this->installation->getPackageID()
49 ]);
50 }
51 }
52
53 /**
54 * @inheritDoc
55 */
56 protected function prepareImport(array $data) {
57 return [
58 'interfaceName' => isset($data['elements']['interfacename']) ? $data['elements']['interfacename'] : '',
59 'definitionName' => $data['elements']['name'],
60 'categoryName' => isset($data['elements']['categoryname']) ? $data['elements']['categoryname'] : ''
61 ];
62 }
63
64 /**
65 * @inheritDoc
66 */
67 protected function findExistingItem(array $data) {
68 $sql = "SELECT *
69 FROM wcf".WCF_N."_".$this->tableName."
70 WHERE definitionName = ?";
71 $parameters = [$data['definitionName']];
72
73 return [
74 'sql' => $sql,
75 'parameters' => $parameters
76 ];
77 }
78
79 /**
80 * @inheritDoc
81 * @since 3.1
82 */
83 public static function getSyncDependencies() {
84 return [];
85 }
86
87 /**
88 * @inheritDoc
89 * @since 3.2
90 */
91 protected function addFormFields(IFormDocument $form) {
92 /** @var FormContainer $dataContainer */
93 $dataContainer = $form->getNodeById('data');
94
95 $dataContainer->appendChildren([
96 TextFormField::create('definitionName')
97 ->objectProperty('name')
98 ->label('wcf.acp.pip.objectTypeDefinition.definitionName')
99 ->description('wcf.acp.pip.objectTypeDefinition.definitionName.description', ['project' => $this->installation->getProject()])
100 ->required()
101 ->addValidator(FormFieldValidatorUtil::getDotSeparatedStringValidator(
102 'wcf.acp.pip.objectTypeDefinition.definitionName',
103 4
104 ))
105 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
106 if ($formField->getValue()) {
107 $objectTypeDefinition = ObjectTypeCache::getInstance()->getDefinitionByName($formField->getValue());
108
109 // the definition name is not unique if such an object type definition
110 // already exists and (a) a new definition is added or (b) an existing
111 // definition is edited but the new definition name is not the old definition
112 // name so that the existing definition is not the definition currently edited
113 if ($objectTypeDefinition !== null && (
114 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
115 $this->editedEntry->getElementsByTagName('name')->item(0)->nodeValue !== $formField->getValue()
116 )) {
117 $formField->addValidationError(
118 new FormFieldValidationError(
119 'notUnique',
120 'wcf.acp.pip.objectTypeDefinition.definitionName.error.notUnique'
121 )
122 );
123 }
124 }
125 })),
126
127 TextFormField::create('interfaceName')
128 ->objectProperty('interfacename')
129 ->label('wcf.acp.pip.objectTypeDefinition.interfaceName')
130 ->description('wcf.acp.pip.objectTypeDefinition.interfaceName.description')
131 ->addValidator(new FormFieldValidator('interfaceExists', function(TextFormField $formField) {
132 if ($formField->getValue() && !interface_exists($formField->getValue())) {
133 $formField->addValidationError(
134 new FormFieldValidationError(
135 'nonExistent',
136 'wcf.acp.pip.objectTypeDefinition.interfaceName.error.nonExistent'
137 )
138 );
139 }
140 }))
141 ]);
142 }
143
144 /**
145 * @inheritDoc
146 * @since 3.2
147 */
148 protected function doGetElementData(\DOMElement $element, $saveData) {
149 $data = [
150 'definitionName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
151 'packageID' => $this->installation->getPackage()->packageID
152 ];
153
154 $interfaceName = $element->getElementsByTagName('interfacename')->item(0);
155 if ($interfaceName) {
156 $data['interfaceName'] = $interfaceName->nodeValue;
157 }
158
159 return $data;
160 }
161
162 /**
163 * @inheritDoc
164 * @since 3.2
165 */
166 public function getElementIdentifier(\DOMElement $element) {
167 return $element->getElementsByTagName('name')->item(0)->nodeValue;
168 }
169
170 /**
171 * @inheritDoc
172 * @since 3.2
173 */
174 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
175 $entryList->setKeys([
176 'definitionName' => 'wcf.acp.pip.objectTypeDefinition.definitionName',
177 'interfaceName' => 'wcf.acp.pip.objectTypeDefinition.interfaceName'
178 ]);
179 }
180
181 /**
182 * @inheritDoc
183 * @since 3.2
184 */
185 protected function doCreateXmlElement(\DOMDocument $document, IFormDocument $form) {
186 $definition = $document->createElement($this->tagName);
187
188 $this->appendElementChildren(
189 $definition,
190 [
191 'name',
192 'interfacename' => ''
193 ],
194 $form
195 );
196
197 return $definition;
198 }
199 }