cb769c02eaaf88e5d3f35627ce3bbdadb018fa9c
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\acp\search\provider\ACPSearchProviderEditor;
4 use wcf\data\acp\search\provider\ACPSearchProviderList;
5 use wcf\system\cache\builder\ACPSearchProviderCacheBuilder;
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\container\FormContainer;
10 use wcf\system\form\builder\field\validation\FormFieldValidationError;
11 use wcf\system\form\builder\field\validation\FormFieldValidator;
12 use wcf\system\form\builder\field\ClassNameFormField;
13 use wcf\system\form\builder\field\IntegerFormField;
14 use wcf\system\form\builder\field\TextFormField;
15 use wcf\system\form\builder\field\validation\FormFieldValidatorUtil;
16 use wcf\system\form\builder\IFormDocument;
17 use wcf\system\search\acp\IACPSearchResultProvider;
18 use wcf\system\WCF;
19
20 /**
21 * Installs, updates and deletes ACP search providers.
22 *
23 * @author Alexander Ebert
24 * @copyright 2001-2018 WoltLab GmbH
25 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
26 * @package WoltLabSuite\Core\System\Package\Plugin
27 */
28 class ACPSearchProviderPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
29 use TXmlGuiPackageInstallationPlugin;
30
31 /**
32 * @inheritDoc
33 */
34 public $className = ACPSearchProviderEditor::class;
35
36 /**
37 * @inheritDoc
38 */
39 protected function handleDelete(array $items) {
40 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
41 WHERE providerName = ?
42 AND packageID = ?";
43 $statement = WCF::getDB()->prepareStatement($sql);
44
45 WCF::getDB()->beginTransaction();
46 foreach ($items as $item) {
47 $statement->execute([
48 $item['attributes']['name'],
49 $this->installation->getPackageID()
50 ]);
51 }
52 WCF::getDB()->commitTransaction();
53 }
54
55 /**
56 * @inheritDoc
57 */
58 protected function prepareImport(array $data) {
59 // get show order
60 $showOrder = isset($data['elements']['showorder']) ? $data['elements']['showorder'] : null;
61 $showOrder = $this->getShowOrder($showOrder);
62
63 return [
64 'className' => $data['elements']['classname'],
65 'providerName' => $data['attributes']['name'],
66 'showOrder' => $showOrder
67 ];
68 }
69
70 /**
71 * @inheritDoc
72 */
73 protected function findExistingItem(array $data) {
74 $sql = "SELECT *
75 FROM wcf".WCF_N."_".$this->tableName."
76 WHERE providerName = ?
77 AND packageID = ?";
78 $parameters = [
79 $data['providerName'],
80 $this->installation->getPackageID()
81 ];
82
83 return [
84 'sql' => $sql,
85 'parameters' => $parameters
86 ];
87 }
88
89 /**
90 * @inheritDoc
91 */
92 protected function cleanup() {
93 ACPSearchProviderCacheBuilder::getInstance()->reset();
94 }
95
96 /**
97 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::getDefaultFilename()
98 * @since 3.0
99 */
100 public static function getDefaultFilename() {
101 return 'acpSearchProvider.xml';
102 }
103
104 /**
105 * @inheritDoc
106 * @since 3.1
107 */
108 public static function getSyncDependencies() {
109 return [];
110 }
111
112 /**
113 * @inheritDoc
114 * @since 3.2
115 */
116 protected function doGetElementData(\DOMElement $element, $saveData) {
117 $data = [
118 'className' => $element->getElementsByTagName('classname')->item(0)->nodeValue,
119 'packageID' => $this->installation->getPackage()->packageID,
120 'providerName' => $element->getAttribute('name')
121 ];
122
123 $showOrder = $element->getElementsByTagName('showorder')->item(0);
124 if ($showOrder) {
125 $data['showOrder'] = $showOrder->nodeValue;
126 }
127 else if ($saveData) {
128 $data['showOrder'] = $this->getShowOrder(null);
129 }
130
131 return $data;
132 }
133
134 /**
135 * @inheritDoc
136 * @since 3.2
137 */
138 public function getElementIdentifier(\DOMElement $element) {
139 return $element->getAttribute('name');
140 }
141
142 /**
143 * @inheritDoc
144 * @since 3.2
145 */
146 protected function addFormFields(IFormDocument $form) {
147 /** @var FormContainer $dataContainer */
148 $dataContainer = $form->getNodeById('data');
149
150 $dataContainer->appendChildren([
151 TextFormField::create('providerName')
152 ->objectProperty('name')
153 ->label('wcf.acp.pip.acpSearchProvider.providerName')
154 ->description('wcf.acp.pip.acpSearchProvider.providerName.description', ['project' => $this->installation->getProject()])
155 ->required()
156 ->addValidator(FormFieldValidatorUtil::getDotSeparatedStringValidator(
157 'wcf.acp.pip.acpSearchProvider.providerName',
158 4
159 ))
160 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
161 if (
162 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
163 $this->editedEntry->getAttribute('name') !== $formField->getValue()
164 ) {
165 $providerList = new ACPSearchProviderList();
166 $providerList->getConditionBuilder()->add('providerName = ?', [$formField->getValue()]);
167
168 if ($providerList->countObjects() > 0) {
169 $formField->addValidationError(
170 new FormFieldValidationError(
171 'notUnique',
172 'wcf.acp.pip.acpSearchProvider.providerName.error.notUnique'
173 )
174 );
175 }
176 }
177 })),
178
179 ClassNameFormField::create('className')
180 ->objectProperty('classname')
181 ->required()
182 ->implementedInterface(IACPSearchResultProvider::class),
183
184 IntegerFormField::create('showOrder')
185 ->objectProperty('showorder')
186 ->label('wcf.acp.pip.acpSearchProvider.showOrder')
187 ->description('wcf.acp.pip.acpSearchProvider.showOrder.description')
188 ->nullable()
189 ->minimum(1),
190 ]);
191 }
192
193 /**
194 * @inheritDoc
195 * @since 3.2
196 */
197 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
198 $entryList->setKeys([
199 'providerName' => 'wcf.acp.pip.acpSearchProvider.providerName',
200 'className' => 'wcf.form.field.className'
201 ]);
202 }
203
204 /**
205 * @inheritDoc
206 * @since 3.2
207 */
208 protected function doCreateXmlElement(\DOMDocument $document, IFormDocument $form) {
209 $data = $form->getData()['data'];
210
211 $acpSearchProvider = $document->createElement($this->tagName);
212 $acpSearchProvider->setAttribute('name', $data['name']);
213
214 $this->appendElementChildren(
215 $acpSearchProvider,
216 [
217 'classname',
218 'showorder' => null
219 ],
220 $form
221 );
222
223 return $acpSearchProvider;
224 }
225 }