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\IFormDocument;
16 use wcf\system\search\acp\IACPSearchResultProvider;
20 * Installs, updates and deletes ACP search providers.
22 * @author Alexander Ebert
23 * @copyright 2001-2018 WoltLab GmbH
24 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
25 * @package WoltLabSuite\Core\System\Package\Plugin
27 class ACPSearchProviderPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
28 use TXmlGuiPackageInstallationPlugin;
33 public $className = ACPSearchProviderEditor::class;
38 protected function handleDelete(array $items) {
39 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
40 WHERE providerName = ?
42 $statement = WCF::getDB()->prepareStatement($sql);
44 WCF::getDB()->beginTransaction();
45 foreach ($items as $item) {
47 $item['attributes']['name'],
48 $this->installation->getPackageID()
51 WCF::getDB()->commitTransaction();
57 protected function prepareImport(array $data) {
59 $showOrder = isset($data['elements']['showorder']) ? $data['elements']['showorder'] : null;
60 $showOrder = $this->getShowOrder($showOrder);
63 'className' => $data['elements']['classname'],
64 'providerName' => $data['attributes']['name'],
65 'showOrder' => $showOrder
72 protected function findExistingItem(array $data) {
74 FROM wcf".WCF_N."_".$this->tableName."
75 WHERE providerName = ?
78 $data['providerName'],
79 $this->installation->getPackageID()
84 'parameters' => $parameters
91 protected function cleanup() {
92 ACPSearchProviderCacheBuilder::getInstance()->reset();
96 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::getDefaultFilename()
99 public static function getDefaultFilename() {
100 return 'acpSearchProvider.xml';
107 public static function getSyncDependencies() {
115 protected function getElementData(\DOMElement $element, $saveData = false) {
117 'className' => $element->getElementsByTagName('classname')->item(0)->nodeValue,
118 'packageID' => $this->installation->getPackage()->packageID,
119 'providerName' => $element->getAttribute('name')
122 $showOrder = $element->getElementsByTagName('showorder')->item(0);
124 $data['showOrder'] = $showOrder->nodeValue;
134 public function getElementIdentifier(\DOMElement $element) {
135 return $element->getAttribute('name');
142 public function addFormFields(IFormDocument $form) {
143 /** @var FormContainer $dataContainer */
144 $dataContainer = $form->getNodeById('data');
146 $dataContainer->appendChildren([
147 TextFormField::create('providerName')
148 ->objectProperty('name')
149 ->label('wcf.acp.pip.acpSearchProvider.providerName')
150 ->description('wcf.acp.pip.acpSearchProvider.providerName.description', ['project' => $this->installation->getProject()])
152 ->addValidator(ObjectTypePackageInstallationPlugin::getObjectTypeAlikeValueValidator('wcf.acp.pip.acpSearchProvider.providerName'))
153 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
155 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
156 $this->editedEntry->getAttribute('name') !== $formField->getValue()
158 $providerList = new ACPSearchProviderList();
159 $providerList->getConditionBuilder()->add('providerName <> ?', [$formField->getValue()]);
161 if ($providerList->countObjects() > 0) {
162 $formField->addValidationError(
163 new FormFieldValidationError(
165 'wcf.acp.pip.acpSearchProvider.providerName.error.notUnique'
172 ClassNameFormField::create('className')
173 ->objectProperty('classname')
175 ->implementedInterface(IACPSearchResultProvider::class),
177 IntegerFormField::create('showOrder')
178 ->objectProperty('showorder')
179 ->label('wcf.acp.pip.acpSearchProvider.showOrder')
180 ->description('wcf.acp.pip.acpSearchProvider.showOrder.description')
190 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
191 $entryList->setKeys([
192 'providerName' => 'wcf.acp.pip.acpSearchProvider.providerName',
193 'className' => 'wcf.form.field.className'
201 protected function sortDocument(\DOMDocument $document) {
202 $this->sortImportDelete($document);
204 $this->sortChildNodes($document->getElementsByTagName('import'), function(\DOMElement $element1, \DOMElement $element2) {
205 $showOrder1 = PHP_INT_MAX;
206 if ($element1->getElementsByTagName('showorder')->length === 1) {
207 $showOrder1 = $element1->getElementsByTagName('showorder')->item(0)->nodeValue;
210 $showOrder2 = PHP_INT_MAX;
211 if ($element2->getElementsByTagName('showorder')->length === 1) {
212 $showOrder2 = $element2->getElementsByTagName('showorder')->item(0)->nodeValue;
215 if ($showOrder1 !== $showOrder2) {
216 return $showOrder1 > $showOrder2;
220 $element1->getAttribute('name'),
221 $element2->getAttribute('name')
224 $this->sortChildNodes($document->getElementsByTagName('delete'), function(\DOMElement $element1, \DOMElement $element2) {
226 $element1->getAttribute('name'),
227 $element2->getAttribute('name')
236 protected function writeEntry(\DOMDocument $document, IFormDocument $form) {
237 $data = $form->getData()['data'];
239 $acpSearchProvider = $document->createElement('acpsearchprovider');
240 $acpSearchProvider->setAttribute('name', $data['name']);
241 $acpSearchProvider->appendChild($document->createElement('classname', $data['classname']));
243 /** @var IntegerFormField $showOrder */
244 if (!empty($data['showOrder'])) {
245 $acpSearchProvider->appendChild($document->createElement('showorder', (string) $data['showorder']));
248 $document->getElementsByTagName('import')->item(0)->appendChild($acpSearchProvider);
250 return $acpSearchProvider;