2 declare(strict_types=1);
3 namespace wcf\system\package\plugin;
4 use wcf\data\acp\search\provider\ACPSearchProviderEditor;
5 use wcf\data\acp\search\provider\ACPSearchProviderList;
6 use wcf\system\cache\builder\ACPSearchProviderCacheBuilder;
7 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
8 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
9 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
10 use wcf\system\form\builder\container\FormContainer;
11 use wcf\system\form\builder\field\validation\FormFieldValidationError;
12 use wcf\system\form\builder\field\validation\FormFieldValidator;
13 use wcf\system\form\builder\field\ClassNameFormField;
14 use wcf\system\form\builder\field\IntegerFormField;
15 use wcf\system\form\builder\field\TextFormField;
16 use wcf\system\form\builder\IFormDocument;
17 use wcf\system\search\acp\IACPSearchResultProvider;
21 * Installs, updates and deletes ACP search providers.
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
28 class ACPSearchProviderPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
29 use TXmlGuiPackageInstallationPlugin;
34 public $className = ACPSearchProviderEditor::class;
39 protected function handleDelete(array $items) {
40 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
41 WHERE providerName = ?
43 $statement = WCF::getDB()->prepareStatement($sql);
45 WCF::getDB()->beginTransaction();
46 foreach ($items as $item) {
48 $item['attributes']['name'],
49 $this->installation->getPackageID()
52 WCF::getDB()->commitTransaction();
58 protected function prepareImport(array $data) {
60 $showOrder = isset($data['elements']['showorder']) ? $data['elements']['showorder'] : null;
61 $showOrder = $this->getShowOrder($showOrder);
64 'className' => $data['elements']['classname'],
65 'providerName' => $data['attributes']['name'],
66 'showOrder' => $showOrder
73 protected function findExistingItem(array $data) {
75 FROM wcf".WCF_N."_".$this->tableName."
76 WHERE providerName = ?
79 $data['providerName'],
80 $this->installation->getPackageID()
85 'parameters' => $parameters
92 protected function cleanup() {
93 ACPSearchProviderCacheBuilder::getInstance()->reset();
97 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::getDefaultFilename()
100 public static function getDefaultFilename() {
101 return 'acpSearchProvider.xml';
108 public static function getSyncDependencies() {
116 protected function getElementData(\DOMElement $element, bool $saveData = false): array {
118 'className' => $element->getElementsByTagName('classname')->item(0)->nodeValue,
119 'packageID' => $this->installation->getPackage()->packageID,
120 'providerName' => $element->getAttribute('name')
123 $showOrder = $element->getElementsByTagName('showorder')->item(0);
125 $data['showOrder'] = $showOrder->nodeValue;
135 public function getElementIdentifier(\DOMElement $element): string {
136 return $element->getAttribute('name');
143 public function addFormFields(IFormDocument $form) {
144 /** @var FormContainer $dataContainer */
145 $dataContainer = $form->getNodeById('data');
147 $dataContainer->appendChildren([
148 TextFormField::create('providerName')
149 ->objectProperty('name')
150 ->label('wcf.acp.pip.acpSearchProvider.providerName')
151 ->description('wcf.acp.pip.acpSearchProvider.providerName.description', ['project' => $this->installation->getProject()])
153 ->addValidator(ObjectTypePackageInstallationPlugin::getObjectTypeAlikeValueValidator('wcf.acp.pip.acpSearchProvider.providerName'))
154 ->addValidator(new FormFieldValidator('uniqueness', function(TextFormField $formField) {
156 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
157 $this->editedEntry->getAttribute('name') !== $formField->getValue()
159 $providerList = new ACPSearchProviderList();
160 $providerList->getConditionBuilder()->add('providerName <> ?', [$formField->getValue()]);
162 if ($providerList->countObjects() > 0) {
163 $formField->addValidationError(
164 new FormFieldValidationError(
166 'wcf.acp.pip.acpSearchProvider.providerName.error.notUnique'
173 ClassNameFormField::create('className')
174 ->objectProperty('classname')
176 ->implementedInterface(IACPSearchResultProvider::class),
178 IntegerFormField::create('showOrder')
179 ->objectProperty('showorder')
180 ->label('wcf.acp.pip.acpSearchProvider.showOrder')
181 ->description('wcf.acp.pip.acpSearchProvider.showOrder.description')
191 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
192 $entryList->setKeys([
193 'providerName' => 'wcf.acp.pip.acpSearchProvider.providerName',
194 'className' => 'wcf.form.field.className'
202 protected function sortDocument(\DOMDocument $document) {
203 $this->sortImportDelete($document);
205 $this->sortChildNodes($document->getElementsByTagName('import'), function(\DOMElement $element1, \DOMElement $element2) {
206 $showOrder1 = PHP_INT_MAX;
207 if ($element1->getElementsByTagName('showorder')->length === 1) {
208 $showOrder1 = $element1->getElementsByTagName('showorder')->item(0)->nodeValue;
211 $showOrder2 = PHP_INT_MAX;
212 if ($element2->getElementsByTagName('showorder')->length === 1) {
213 $showOrder2 = $element2->getElementsByTagName('showorder')->item(0)->nodeValue;
216 if ($showOrder1 !== $showOrder2) {
217 return $showOrder1 > $showOrder2;
221 $element1->getAttribute('name'),
222 $element2->getAttribute('name')
225 $this->sortChildNodes($document->getElementsByTagName('delete'), function(\DOMElement $element1, \DOMElement $element2) {
227 $element1->getAttribute('name'),
228 $element2->getAttribute('name')
237 protected function writeEntry(\DOMDocument $document, IFormDocument $form): \DOMElement {
238 $acpSearchProvider = $document->createElement('acpsearchprovider');
239 $acpSearchProvider->setAttribute('name', $form->getNodeById('providerName')->getSaveValue());
240 $acpSearchProvider->appendChild($document->createElement('classname', $form->getNodeById('className')->getSaveValue()));
242 /** @var IntegerFormField $showOrder */
243 $showOrder = $form->getNodeById('showOrder');
244 if ($showOrder->getSaveValue()) {
245 $acpSearchProvider->appendChild($document->createElement('showorder', (string) $showOrder->getSaveValue()));
248 $document->getElementsByTagName('import')->item(0)->appendChild($acpSearchProvider);
250 return $acpSearchProvider;