Revert "Removed obsolete trailing slashes from void elements"
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / condition / UserStateCondition.class.php
... / ...
CommitLineData
1<?php
2namespace wcf\system\condition;
3use wcf\data\condition\Condition;
4use wcf\data\user\User;
5use wcf\data\user\UserList;
6use wcf\data\DatabaseObjectList;
7use wcf\system\exception\UserInputException;
8use wcf\system\WCF;
9
10/**
11 * Condition implementation for the state (banned, enabled) of a user.
12 *
13 * @author Matthias Schmidt
14 * @copyright 2001-2016 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package com.woltlab.wcf
17 * @subpackage system.condition
18 * @category Community Framework
19 */
20class UserStateCondition extends AbstractSingleFieldCondition implements IContentCondition, IObjectListCondition, IUserCondition {
21 use TObjectListUserCondition;
22
23 /**
24 * @inheritDoc
25 */
26 protected $label = 'wcf.user.condition.state';
27
28 /**
29 * true if the the user has to be banned
30 * @var integer
31 */
32 protected $userIsBanned = 0;
33
34 /**
35 * true if the user has to be disabled
36 * @var integer
37 */
38 protected $userIsDisabled = 0;
39
40 /**
41 * true if the user has to be enabled
42 * @var integer
43 */
44 protected $userIsEnabled = 0;
45
46 /**
47 * true if the the user may not be banned
48 * @var integer
49 */
50 protected $userIsNotBanned = 0;
51
52 /**
53 * @inheritDoc
54 */
55 public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) {
56 if (!($objectList instanceof UserList)) {
57 throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given.");
58 }
59
60 if (isset($conditionData['userIsBanned'])) {
61 $objectList->getConditionBuilder()->add('user_table.banned = ?', [$conditionData['userIsBanned']]);
62 }
63
64 if ($conditionData['userIsEnabled']) {
65 if ($conditionData['userIsEnabled']) {
66 $objectList->getConditionBuilder()->add('user_table.activationCode = ?', [0]);
67 }
68 else {
69 $objectList->getConditionBuilder()->add('user_table.activationCode <> ?', [0]);
70 }
71 }
72 }
73
74 /**
75 * @inheritDoc
76 */
77 public function checkUser(Condition $condition, User $user) {
78 if ($condition->userIsBanned !== null && $user->banned != $condition->userIsBanned) {
79 return false;
80 }
81
82 if ($condition->userIsEnabled !== null) {
83 if ($condition->userIsEnabled && $user->activationCode) {
84 return false;
85 }
86 else if (!$condition->userIsEnabled && !$user->activationCode) {
87 return false;
88 }
89 }
90
91 return true;
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function getData() {
98 $data = [];
99
100 if ($this->userIsBanned) {
101 $data['userIsBanned'] = 1;
102 }
103 else if ($this->userIsNotBanned) {
104 $data['userIsBanned'] = 0;
105 }
106 if ($this->userIsEnabled) {
107 $data['userIsEnabled'] = 1;
108 }
109 else if ($this->userIsDisabled) {
110 $data['userIsEnabled'] = 0;
111 }
112
113 if (!empty($data)) {
114 return $data;
115 }
116
117 return null;
118 }
119
120 /**
121 * Returns the "checked" attribute for an input element.
122 *
123 * @param string $propertyName
124 * @return string
125 */
126 protected function getCheckedAttribute($propertyName) {
127 /** @noinspection PhpVariableVariableInspection */
128 if ($this->$propertyName) {
129 return ' checked="checked"';
130 }
131
132 return '';
133 }
134
135 /**
136 * @inheritDoc
137 */
138 protected function getFieldElement() {
139 $userIsNotBanned = WCF::getLanguage()->get('wcf.user.condition.state.isNotBanned');
140 $userIsBanned = WCF::getLanguage()->get('wcf.user.condition.state.isBanned');
141 $userIsDisabled = WCF::getLanguage()->get('wcf.user.condition.state.isDisabled');
142 $userIsEnabled = WCF::getLanguage()->get('wcf.user.condition.state.isEnabled');
143
144 return <<<HTML
145<label><input type="checkbox" name="userIsBanned" value="1"{$this->getCheckedAttribute('userIsBanned')} /> {$userIsBanned}</label>
146<label><input type="checkbox" name="userIsNotBanned" value="1"{$this->getCheckedAttribute('userIsNotBanned')} /> {$userIsNotBanned}</label>
147<label><input type="checkbox" name="userIsEnabled" value="1"{$this->getCheckedAttribute('userIsEnabled')} /> {$userIsEnabled}</label>
148<label><input type="checkbox" name="userIsDisabled" value="1"{$this->getCheckedAttribute('userIsDisabled')} /> {$userIsDisabled}</label>
149HTML;
150 }
151
152 /**
153 * @inheritDoc
154 */
155 public function readFormParameters() {
156 if (isset($_POST['userIsBanned'])) $this->userIsBanned = 1;
157 if (isset($_POST['userIsDisabled'])) $this->userIsDisabled = 1;
158 if (isset($_POST['userIsEnabled'])) $this->userIsEnabled = 1;
159 if (isset($_POST['userIsNotBanned'])) $this->userIsNotBanned = 1;
160 }
161
162 /**
163 * @inheritDoc
164 */
165 public function reset() {
166 $this->userIsBanned = 0;
167 $this->userIsDisabled = 0;
168 $this->userIsEnabled = 0;
169 $this->userIsNotBanned = 0;
170 }
171
172 /**
173 * @inheritDoc
174 */
175 public function setData(Condition $condition) {
176 if ($condition->userIsBanned !== null) {
177 $this->userIsBanned = $condition->userIsBanned;
178 $this->userIsNotBanned = !$condition->userIsBanned;
179 }
180 if ($condition->userIsEnabled !== null) {
181 $this->userIsEnabled = $condition->userIsEnabled;
182 $this->userIsDisabled = !$condition->userIsEnabled;
183 }
184 }
185
186 /**
187 * @inheritDoc
188 */
189 public function validate() {
190 if ($this->userIsBanned && $this->userIsNotBanned) {
191 $this->errorMessage = 'wcf.user.condition.state.isBanned.error.conflict';
192
193 throw new UserInputException('userIsBanned', 'conflict');
194 }
195
196 if ($this->userIsDisabled && $this->userIsEnabled) {
197 $this->errorMessage = 'wcf.user.condition.state.isEnabled.error.conflict';
198
199 throw new UserInputException('userIsEnabled', 'conflict');
200 }
201 }
202
203 /**
204 * @inheritDoc
205 */
206 public function showContent(Condition $condition) {
207 if (!WCF::getUser()->userID) return false;
208
209 return $this->checkUser($condition, WCF::getUser());
210 }
211}