4e0a9538a4501801854eff91d3eb46e47c131b2e
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\form\builder\field\user\group\option;
3 use wcf\system\database\util\PreparedStatementConditionBuilder;
4 use wcf\system\form\builder\field\IPackagesFormField;
5 use wcf\system\form\builder\field\ItemListFormField;
6 use wcf\system\form\builder\field\TDefaultIdFormField;
7 use wcf\system\form\builder\field\TPackagesFormField;
8 use wcf\system\form\builder\field\validation\FormFieldValidationError;
9 use wcf\system\WCF;
10
11 /**
12 * Implementation of a form field for user group options/permissions.
13 *
14 * This field uses the `wcf.form.field.userGroupOption` language item as the default
15 * form field label and uses `permissions` as the default node id.
16 *
17 * @author Matthias Schmidt
18 * @copyright 2001-2018 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\System\Form\Builder\Field\User\Group\Option
21 * @since 3.2
22 */
23 class UserGroupOptionFormField extends ItemListFormField implements IPackagesFormField {
24 use TDefaultIdFormField;
25 use TPackagesFormField;
26
27 /**
28 * Creates a new instance of `OptionsFormField`.
29 */
30 public function __construct() {
31 $this->label('wcf.form.field.userGroupOption');
32 }
33
34 /**
35 * @inheritDoc
36 */
37 public function validate() {
38 parent::validate();
39
40 if (empty($this->getValidationErrors()) && is_array($this->getValue()) && !empty($this->getValue())) {
41 $conditionBuilder = new PreparedStatementConditionBuilder();
42 $conditionBuilder->add('optionName IN (?)', [$this->getValue()]);
43 if (!empty($this->getPackageIDs())) {
44 $conditionBuilder->add('packageID IN (?)', [$this->getPackageIDs()]);
45 }
46
47 $sql = "SELECT optionName
48 FROM wcf" . WCF_N . "_user_group_option
49 " . $conditionBuilder;
50 $statement = WCF::getDB()->prepareStatement($sql);
51 $statement->execute($conditionBuilder->getParameters());
52 $availableOptions = $statement->fetchAll(\PDO::FETCH_COLUMN);
53
54 $unknownOptions = array_diff($this->getValue(), $availableOptions);
55
56 if (!empty($unknownOptions)) {
57 $this->addValidationError(
58 new FormFieldValidationError(
59 'nonExistent',
60 'wcf.form.field.userGroupOption.error.nonExistent',
61 ['options' => $unknownOptions]
62 )
63 );
64 }
65 }
66 }
67
68 /**
69 * @inheritDoc
70 */
71 protected static function getDefaultId() {
72 return 'permissions';
73 }
74 }