8098bee288e948a72fc2d646b6150faf337f23ae
[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\IFormDocument;
13 use wcf\system\WCF;
14
15 /**
16 * Installs, updates and deletes object type definitions.
17 *
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
22 */
23 class ObjectTypeDefinitionPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
24 use TXmlGuiPackageInstallationPlugin;
25
26 /**
27 * @inheritDoc
28 */
29 public $className = ObjectTypeDefinitionEditor::class;
30
31 /**
32 * @inheritDoc
33 */
34 public $tagName = 'definition';
35
36 /**
37 * @inheritDoc
38 */
39 protected function handleDelete(array $items) {
40 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
41 WHERE definitionName = ?
42 AND packageID = ?";
43 $statement = WCF::getDB()->prepareStatement($sql);
44 foreach ($items as $item) {
45 $statement->execute([
46 $item['attributes']['name'],
47 $this->installation->getPackageID()
48 ]);
49 }
50 }
51
52 /**
53 * @inheritDoc
54 */
55 protected function prepareImport(array $data) {
56 return [
57 'interfaceName' => isset($data['elements']['interfacename']) ? $data['elements']['interfacename'] : '',
58 'definitionName' => $data['elements']['name'],
59 'categoryName' => isset($data['elements']['categoryname']) ? $data['elements']['categoryname'] : ''
60 ];
61 }
62
63 /**
64 * @inheritDoc
65 */
66 protected function findExistingItem(array $data) {
67 $sql = "SELECT *
68 FROM wcf".WCF_N."_".$this->tableName."
69 WHERE definitionName = ?";
70 $parameters = [$data['definitionName']];
71
72 return [
73 'sql' => $sql,
74 'parameters' => $parameters
75 ];
76 }
77
78 /**
79 * @inheritDoc
80 * @since 3.1
81 */
82 public static function getSyncDependencies() {
83 return [];
84 }
85
86 /**
87 * @inheritDoc
88 * @since 3.2
89 */
90 public function addFormFields(IFormDocument $form) {
91 /** @var FormContainer $dataContainer */
92 $dataContainer = $form->getNodeById('data');
93
94 $dataContainer->appendChildren([
95 TextFormField::create('definitionName')
96 ->objectProperty('name')
97 ->label('wcf.acp.pip.objectTypeDefinition.definitionName')
98 ->description('wcf.acp.pip.objectTypeDefinition.definitionName.description', ['project' => $this->installation->getProject()])
99 ->required()
100 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
101 if ($formField->getValue()) {
102 $segments = explode('.', $formField->getValue());
103 if (count($segments) < 4) {
104 $formField->addValidationError(
105 new FormFieldValidationError(
106 'tooFewSegments',
107 'wcf.acp.pip.objectTypeDefinition.definitionName.error.tooFewSegments',
108 ['segmentCount' => count($segments)]
109 )
110 );
111 }
112 else {
113 $invalidSegments = [];
114 foreach ($segments as $key => $segment) {
115 if (!preg_match('~^[A-z0-9\-\_]+$~', $segment)) {
116 $invalidSegments[$key] = $segment;
117 }
118 }
119
120 if (!empty($invalidSegments)) {
121 $formField->addValidationError(
122 new FormFieldValidationError(
123 'invalidSegments',
124 'wcf.acp.pip.objectTypeDefinition.definitionName.error.invalidSegments',
125 ['invalidSegments' => $invalidSegments]
126 )
127 );
128 }
129 }
130 }
131 }))
132 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
133 if ($formField->getValue()) {
134 $objectTypeDefinition = ObjectTypeCache::getInstance()->getDefinitionByName($formField->getValue());
135
136 // the definition name is not unique if such an object type definition
137 // already exists and (a) a new definition is added or (b) an existing
138 // definition is edited but the new definition name is not the old definition
139 // name so that the existing definition is not the definition currently edited
140 if ($objectTypeDefinition !== null && (
141 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
142 $this->editedEntry->getElementsByTagName('name')->item(0)->nodeValue !== $formField->getValue()
143 )) {
144 $formField->addValidationError(
145 new FormFieldValidationError(
146 'notUnique',
147 'wcf.acp.pip.objectTypeDefinition.definitionName.error.notUnique'
148 )
149 );
150 }
151 }
152 })),
153
154 TextFormField::create('interfaceName')
155 ->objectProperty('interfacename')
156 ->label('wcf.acp.pip.objectTypeDefinition.interfaceName')
157 ->description('wcf.acp.pip.objectTypeDefinition.interfaceName.description')
158 ->addValidator(new FormFieldValidator('interfaceExists', function(TextFormField $formField) {
159 if ($formField->getValue() && !interface_exists($formField->getValue())) {
160 $formField->addValidationError(
161 new FormFieldValidationError(
162 'nonExistent',
163 'wcf.acp.pip.objectTypeDefinition.interfaceName.error.nonExistent'
164 )
165 );
166 }
167 }))
168 ]);
169 }
170
171 /**
172 * @inheritDoc
173 * @since 3.2
174 */
175 protected function getElementData(\DOMElement $element, $saveData = false) {
176 $data = [
177 'definitionName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
178 'packageID' => $this->installation->getPackage()->packageID
179 ];
180
181 $interfaceName = $element->getElementsByTagName('interfacename')->item(0);
182 if ($interfaceName) {
183 $data['interfaceName'] = $interfaceName->nodeValue;
184 }
185
186 return $data;
187 }
188
189 /**
190 * @inheritDoc
191 * @since 3.2
192 */
193 public function getElementIdentifier(\DOMElement $element) {
194 return $element->getElementsByTagName('name')->item(0)->nodeValue;
195 }
196
197 /**
198 * @inheritDoc
199 * @since 3.2
200 */
201 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
202 $entryList->setKeys([
203 'definitionName' => 'wcf.acp.pip.objectTypeDefinition.definitionName',
204 'interfaceName' => 'wcf.acp.pip.objectTypeDefinition.interfaceName'
205 ]);
206 }
207
208 /**
209 * @inheritDoc
210 * @since 3.2
211 */
212 protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
213 $data = $form->getData()['data'];
214
215 $definition = $document->createElement($this->tagName);
216 $definition->appendChild($document->createElement('name', $data['name']));
217
218 if (!empty($data['interfacename'])) {
219 $definition->appendChild($document->createElement('interfacename', $data['interfacename']));
220 }
221
222 return $definition;
223 }
224 }