Commit | Line | Data |
---|---|---|
8b55a6b6 MS |
1 | <?php |
2 | namespace wcf\system\form\builder\field; | |
3 | use wcf\data\package\PackageCache; | |
4 | use wcf\system\database\util\PreparedStatementConditionBuilder; | |
5 | use wcf\system\form\builder\field\validation\FormFieldValidationError; | |
6 | use wcf\system\form\builder\IFormNode; | |
7 | use wcf\system\WCF; | |
8 | ||
9 | /** | |
10 | * Implementation of a form field for options. | |
11 | * | |
1eaa9d03 | 12 | * This field uses the `wcf.form.field.option` language item as the default |
8b55a6b6 MS |
13 | * form field label and uses `options` as the default node id. |
14 | * | |
15 | * @author Matthias Schmidt | |
16 | * @copyright 2001-2018 WoltLab GmbH | |
17 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
18 | * @package WoltLabSuite\Core\System\Form\Builder\Field | |
19 | * @since 3.2 | |
20 | */ | |
21 | class OptionFormField extends ItemListFormField { | |
22 | /** | |
23 | * ids of the packages whose options will be considered | |
24 | * @var int[] | |
25 | */ | |
26 | protected $__packageIDs = []; | |
27 | ||
28 | /** | |
29 | * Creates a new instance of `OptionsFormField`. | |
30 | */ | |
31 | public function __construct() { | |
1eaa9d03 | 32 | $this->label('wcf.form.field.option'); |
8b55a6b6 MS |
33 | } |
34 | ||
35 | /** | |
36 | * Returns the ids of the packages whose options will be considered. An | |
37 | * empty array is returned if all packages are considered. | |
38 | * | |
39 | * @return int[] | |
40 | */ | |
41 | public function getPackageIDs(): array { | |
42 | return $this->__packageIDs; | |
43 | } | |
44 | ||
45 | /** | |
46 | * Sets the ids of the packages whose options will be considered. If an | |
47 | * empty array is given, all packages will be considered. | |
48 | * | |
49 | * @param int[] $packageIDs | |
1eaa9d03 | 50 | * @return static |
8b55a6b6 MS |
51 | * |
52 | * @throws \InvalidArgumentException if the given package ids are invalid | |
53 | */ | |
54 | public function packageIDs(array $packageIDs): OptionFormField { | |
55 | foreach ($packageIDs as $packageID) { | |
56 | if (PackageCache::getInstance()->getPackage($packageID) === null) { | |
57 | throw new \InvalidArgumentException("Unknown package with id '{$packageID}'."); | |
58 | } | |
59 | } | |
60 | ||
61 | $this->__packageIDs = $packageIDs; | |
62 | ||
63 | return $this; | |
64 | } | |
65 | ||
66 | /** | |
67 | * @inheritDoc | |
68 | */ | |
69 | public function validate() { | |
70 | parent::validate(); | |
71 | ||
72 | if (empty($this->getValidationErrors()) && is_array($this->getValue()) && !empty($this->getValue())) { | |
73 | $conditionBuilder = new PreparedStatementConditionBuilder(); | |
74 | $conditionBuilder->add('optionName IN (?)', [$this->getValue()]); | |
75 | if (!empty($this->getPackageIDs())) { | |
76 | $conditionBuilder->add('packageID IN (?)', [$this->getPackageIDs()]); | |
77 | } | |
78 | ||
79 | $sql = "SELECT optionName | |
80 | FROM wcf" . WCF_N . "_option | |
81 | " . $conditionBuilder; | |
82 | $statement = WCF::getDB()->prepareStatement($sql); | |
83 | $statement->execute($conditionBuilder->getParameters()); | |
84 | $availableOptions = $statement->fetchAll(\PDO::FETCH_COLUMN); | |
85 | ||
86 | $unknownOptions = array_diff($this->getValue(), $availableOptions); | |
87 | ||
88 | if (!empty($unknownOptions)) { | |
89 | $this->addValidationError( | |
90 | new FormFieldValidationError( | |
91 | 'nonExistent', | |
1eaa9d03 | 92 | 'wcf.form.field.option.error.nonExistent', |
8b55a6b6 MS |
93 | ['options' => $unknownOptions] |
94 | ) | |
95 | ); | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | /** | |
101 | * @inheritDoc | |
102 | */ | |
6b5dd794 | 103 | // @codingStandardsIgnoreStart |
8b55a6b6 MS |
104 | public static function create(string $id = 'options'): IFormNode { |
105 | return parent::create($id); | |
106 | } | |
6b5dd794 | 107 | // @codingStandardsIgnoreEnd |
8b55a6b6 | 108 | } |