Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / form / builder / field / option / OptionFormField.class.php
1 <?php
2
3 namespace wcf\system\form\builder\field\option;
4
5 use wcf\system\database\util\PreparedStatementConditionBuilder;
6 use wcf\system\form\builder\field\IPackagesFormField;
7 use wcf\system\form\builder\field\ItemListFormField;
8 use wcf\system\form\builder\field\TDefaultIdFormField;
9 use wcf\system\form\builder\field\TPackagesFormField;
10 use wcf\system\form\builder\field\validation\FormFieldValidationError;
11 use wcf\system\WCF;
12
13 /**
14 * Implementation of a form field for options.
15 *
16 * This field uses the `wcf.form.field.option` language item as the default
17 * form field label and uses `options` as the default node id.
18 *
19 * @author Matthias Schmidt
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\System\Form\Builder\Field\Option
23 * @since 5.2
24 */
25 class OptionFormField extends ItemListFormField implements IPackagesFormField
26 {
27 use TDefaultIdFormField;
28 use TPackagesFormField;
29
30 /**
31 * Creates a new instance of `OptionsFormField`.
32 */
33 public function __construct()
34 {
35 parent::__construct();
36
37 $this->label('wcf.form.field.option');
38 }
39
40 /**
41 * @inheritDoc
42 */
43 public function validate()
44 {
45 parent::validate();
46
47 if (empty($this->getValidationErrors()) && \is_array($this->getValue()) && !empty($this->getValue())) {
48 // ignore `module_attachment`, see https://github.com/WoltLab/WCF/issues/2531
49 $options = $this->getValue();
50 if (($index = \array_search('module_attachment', $options)) !== false) {
51 unset($options[$index]);
52 }
53
54 if (empty($options)) {
55 return;
56 }
57
58 $conditionBuilder = new PreparedStatementConditionBuilder();
59 $conditionBuilder->add('optionName IN (?)', [$options]);
60 if (!empty($this->getPackageIDs())) {
61 $conditionBuilder->add('packageID IN (?)', [$this->getPackageIDs()]);
62 }
63
64 $sql = "SELECT optionName
65 FROM wcf" . WCF_N . "_option
66 " . $conditionBuilder;
67 $statement = WCF::getDB()->prepareStatement($sql);
68 $statement->execute($conditionBuilder->getParameters());
69 $availableOptions = $statement->fetchAll(\PDO::FETCH_COLUMN);
70
71 $unknownOptions = \array_diff($options, $availableOptions);
72
73 if (!empty($unknownOptions)) {
74 $this->addValidationError(
75 new FormFieldValidationError(
76 'nonExistent',
77 'wcf.form.field.option.error.nonExistent',
78 ['options' => $unknownOptions]
79 )
80 );
81 }
82 }
83 }
84
85 /**
86 * @inheritDoc
87 */
88 protected static function getDefaultId()
89 {
90 return 'options';
91 }
92 }