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;
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 /** @var FormContainer $dataContainer */
92 $dataContainer = $form->getNodeById('data');
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()])
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(
107 'wcf.acp.pip.objectTypeDefinition.definitionName.error.tooFewSegments',
108 ['segmentCount' => count($segments)]
113 $invalidSegments = [];
114 foreach ($segments as $key => $segment) {
115 if (!preg_match('~^[A-z0-9\-\_]+$~', $segment)) {
116 $invalidSegments[$key] = $segment;
120 if (!empty($invalidSegments)) {
121 $formField->addValidationError(
122 new FormFieldValidationError(
124 'wcf.acp.pip.objectTypeDefinition.definitionName.error.invalidSegments',
125 ['invalidSegments' => $invalidSegments]
132 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
133 if ($formField->getValue()) {
134 $objectTypeDefinition = ObjectTypeCache::getInstance()->getDefinitionByName($formField->getValue());
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()
144 $formField->addValidationError(
145 new FormFieldValidationError(
147 'wcf.acp.pip.objectTypeDefinition.definitionName.error.notUnique'
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(
163 'wcf.acp.pip.objectTypeDefinition.interfaceName.error.nonExistent'
175 protected function getElementData(\DOMElement $element, $saveData = false) {
177 'definitionName' => $element->getElementsByTagName('name')->item(0)->nodeValue,
178 'packageID' => $this->installation->getPackage()->packageID
181 $interfaceName = $element->getElementsByTagName('interfacename')->item(0);
182 if ($interfaceName) {
183 $data['interfaceName'] = $interfaceName->nodeValue;
193 public function getElementIdentifier(\DOMElement $element) {
194 return $element->getElementsByTagName('name')->item(0)->nodeValue;
201 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
202 $entryList->setKeys([
203 'definitionName' => 'wcf.acp.pip.objectTypeDefinition.definitionName',
204 'interfaceName' => 'wcf.acp.pip.objectTypeDefinition.interfaceName'
212 protected function sortDocument(\DOMDocument $document) {
213 $this->sortImportDelete($document);
215 $this->sortChildNodes($document->getElementsByTagName('import'), static::getSortFunction(['name']));
216 $this->sortChildNodes($document->getElementsByTagName('delete'), static::getSortFunction([
228 protected function writeEntry(\DOMDocument $document, IFormDocument $form) {
229 $data = $form->getData()['data'];
231 $definition = $document->createElement('definition');
232 $definition->appendChild($document->createElement('name', $data['name']));
234 if (!empty($data['interfacename'])) {
235 $definition->appendChild($document->createElement('interfacename', $data['interfacename']));
238 $import = $document->getElementsByTagName('import')->item(0);
239 $import->appendChild($definition);