Commit | Line | Data |
---|---|---|
87d3a054 | 1 | <?php |
a9229942 | 2 | |
87d3a054 | 3 | namespace wcf\system\condition; |
a9229942 | 4 | |
87d3a054 | 5 | use wcf\data\condition\Condition; |
a9229942 | 6 | use wcf\data\DatabaseObjectList; |
87d3a054 | 7 | use wcf\data\user\User; |
e62dacea | 8 | use wcf\data\user\UserList; |
e81d91cd | 9 | use wcf\system\exception\InvalidObjectArgument; |
87d3a054 MS |
10 | use wcf\system\exception\UserInputException; |
11 | use wcf\system\language\LanguageFactory; | |
12 | use wcf\system\WCF; | |
13 | use wcf\util\ArrayUtil; | |
14 | ||
15 | /** | |
16 | * Condition implementation for the languages of a user. | |
a9229942 TD |
17 | * |
18 | * @author Matthias Schmidt | |
19 | * @copyright 2001-2019 WoltLab GmbH | |
20 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
21 | * @package WoltLabSuite\Core\System\Condition | |
87d3a054 | 22 | */ |
a9229942 TD |
23 | class UserLanguageCondition extends AbstractSingleFieldCondition implements |
24 | IContentCondition, | |
25 | IObjectListCondition, | |
26 | IUserCondition | |
27 | { | |
28 | use TObjectListUserCondition; | |
29 | ||
30 | /** | |
31 | * @inheritDoc | |
32 | */ | |
33 | protected $label = 'wcf.user.condition.languages'; | |
34 | ||
35 | /** | |
36 | * ids of the selected languages | |
37 | * @var int[] | |
38 | */ | |
39 | protected $languageIDs = []; | |
40 | ||
41 | /** | |
42 | * @inheritDoc | |
43 | */ | |
44 | public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) | |
45 | { | |
46 | if (!($objectList instanceof UserList)) { | |
47 | throw new InvalidObjectArgument($objectList, UserList::class, 'Object list'); | |
48 | } | |
49 | ||
50 | $objectList->getConditionBuilder()->add('user_table.languageID IN (?)', [$conditionData['languageIDs']]); | |
51 | } | |
52 | ||
53 | /** | |
54 | * @inheritDoc | |
55 | */ | |
56 | public function checkUser(Condition $condition, User $user) | |
57 | { | |
58 | if ( | |
59 | !empty($condition->conditionData['languageIDs']) | |
60 | && !\in_array($user->languageID, $condition->languageIDs) | |
61 | ) { | |
62 | return false; | |
63 | } | |
64 | ||
65 | return true; | |
66 | } | |
67 | ||
68 | /** | |
69 | * @inheritDoc | |
70 | */ | |
71 | public function getData() | |
72 | { | |
73 | if (!empty($this->languageIDs)) { | |
74 | return [ | |
75 | 'languageIDs' => $this->languageIDs, | |
76 | ]; | |
77 | } | |
78 | } | |
79 | ||
80 | /** | |
81 | * @inheritDoc | |
82 | */ | |
83 | protected function getFieldElement() | |
84 | { | |
85 | $returnValue = ""; | |
86 | foreach (LanguageFactory::getInstance()->getLanguages() as $language) { | |
87 | $returnValue .= "<label><input type=\"checkbox\" name=\"languageIDs[]\" value=\"" . $language->languageID . "\"" . (\in_array( | |
88 | $language->languageID, | |
89 | $this->languageIDs | |
90 | ) ? ' checked' : "") . "> " . $language->languageName . "</label>"; | |
91 | } | |
92 | ||
93 | return $returnValue; | |
94 | } | |
95 | ||
96 | /** | |
97 | * @inheritDoc | |
98 | */ | |
99 | public function readFormParameters() | |
100 | { | |
101 | if (isset($_POST['languageIDs']) && \is_array($_POST['languageIDs'])) { | |
102 | $this->languageIDs = ArrayUtil::toIntegerArray($_POST['languageIDs']); | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * @inheritDoc | |
108 | */ | |
109 | public function reset() | |
110 | { | |
111 | $this->languageIDs = []; | |
112 | } | |
113 | ||
114 | /** | |
115 | * @inheritDoc | |
116 | */ | |
117 | public function setData(Condition $condition) | |
118 | { | |
119 | if (!empty($condition->conditionData['languageIDs'])) { | |
120 | $this->languageIDs = $condition->conditionData['languageIDs']; | |
121 | } | |
122 | } | |
123 | ||
124 | /** | |
125 | * @inheritDoc | |
126 | */ | |
127 | public function validate() | |
128 | { | |
129 | foreach ($this->languageIDs as $languageID) { | |
130 | if (LanguageFactory::getInstance()->getLanguage($languageID) === null) { | |
131 | $this->errorMessage = 'wcf.global.form.error.noValidSelection'; | |
132 | ||
133 | throw new UserInputException('languageIDs', 'noValidSelection'); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * @inheritDoc | |
140 | */ | |
141 | public function showContent(Condition $condition) | |
142 | { | |
143 | if (WCF::getUser()->userID) { | |
144 | return $this->checkUser($condition, WCF::getUser()); | |
145 | } | |
146 | ||
147 | if ( | |
148 | !empty($condition->conditionData['languageIDs']) | |
149 | && !\in_array(WCF::getLanguage()->languageID, $condition->languageIDs) | |
150 | ) { | |
151 | return false; | |
152 | } | |
153 | ||
154 | return true; | |
155 | } | |
87d3a054 | 156 | } |