Add @noinspection PhpMultipleClassesDeclarationsInOneFile tags
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / condition / UserStateCondition.class.php
CommitLineData
87d3a054
MS
1<?php
2namespace wcf\system\condition;
3use wcf\data\condition\Condition;
4use wcf\data\user\User;
e62dacea 5use wcf\data\user\UserList;
23d57595 6use wcf\data\DatabaseObjectList;
87d3a054
MS
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
f2a4a429 14 * @copyright 2001-2016 WoltLab GmbH
87d3a054
MS
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 */
23d57595
MS
20class UserStateCondition extends AbstractSingleFieldCondition implements IContentCondition, IObjectListCondition, IUserCondition {
21 use TObjectListUserCondition;
22
87d3a054 23 /**
f2a4a429 24 * @inheritDoc
87d3a054
MS
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 /**
f2a4a429 53 * @inheritDoc
87d3a054 54 */
23d57595 55 public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) {
f2a4a429 56 if (!($objectList instanceof UserList)) {
a92f7b96 57 throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given.");
f2a4a429 58 }
23d57595
MS
59
60 if (isset($conditionData['userIsBanned'])) {
f2a4a429 61 $objectList->getConditionBuilder()->add('user_table.banned = ?', [$conditionData['userIsBanned']]);
87d3a054
MS
62 }
63
23d57595
MS
64 if ($conditionData['userIsEnabled']) {
65 if ($conditionData['userIsEnabled']) {
f2a4a429 66 $objectList->getConditionBuilder()->add('user_table.activationCode = ?', [0]);
87d3a054
MS
67 }
68 else {
f2a4a429 69 $objectList->getConditionBuilder()->add('user_table.activationCode <> ?', [0]);
87d3a054
MS
70 }
71 }
72 }
73
74 /**
f2a4a429 75 * @inheritDoc
87d3a054
MS
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 /**
f2a4a429 95 * @inheritDoc
87d3a054
MS
96 */
97 public function getData() {
f2a4a429 98 $data = [];
87d3a054
MS
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 if ($this->$propertyName) {
128 return ' checked="checked"';
129 }
130
131 return '';
132 }
133
134 /**
f2a4a429 135 * @inheritDoc
87d3a054
MS
136 */
137 protected function getFieldElement() {
138 $userIsNotBanned = WCF::getLanguage()->get('wcf.user.condition.state.isNotBanned');
139 $userIsBanned = WCF::getLanguage()->get('wcf.user.condition.state.isBanned');
140 $userIsDisabled = WCF::getLanguage()->get('wcf.user.condition.state.isDisabled');
141 $userIsEnabled = WCF::getLanguage()->get('wcf.user.condition.state.isEnabled');
142
143 return <<<HTML
144<label><input type="checkbox" name="userIsBanned" value="1"{$this->getCheckedAttribute('userIsBanned')} /> {$userIsBanned}</label>
145<label><input type="checkbox" name="userIsNotBanned" value="1"{$this->getCheckedAttribute('userIsNotBanned')} /> {$userIsNotBanned}</label>
146<label><input type="checkbox" name="userIsEnabled" value="1"{$this->getCheckedAttribute('userIsEnabled')} /> {$userIsEnabled}</label>
147<label><input type="checkbox" name="userIsDisabled" value="1"{$this->getCheckedAttribute('userIsDisabled')} /> {$userIsDisabled}</label>
148HTML;
149 }
150
151 /**
f2a4a429 152 * @inheritDoc
87d3a054
MS
153 */
154 public function readFormParameters() {
155 if (isset($_POST['userIsBanned'])) $this->userIsBanned = 1;
156 if (isset($_POST['userIsDisabled'])) $this->userIsDisabled = 1;
157 if (isset($_POST['userIsEnabled'])) $this->userIsEnabled = 1;
158 if (isset($_POST['userIsNotBanned'])) $this->userIsNotBanned = 1;
159 }
160
161 /**
f2a4a429 162 * @inheritDoc
87d3a054
MS
163 */
164 public function reset() {
165 $this->userIsBanned = 0;
166 $this->userIsDisabled = 0;
167 $this->userIsEnabled = 0;
168 $this->userIsNotBanned = 0;
169 }
170
171 /**
f2a4a429 172 * @inheritDoc
87d3a054
MS
173 */
174 public function setData(Condition $condition) {
175 if ($condition->userIsBanned !== null) {
176 $this->userIsBanned = $condition->userIsBanned;
177 $this->userIsNotBanned = !$condition->userIsBanned;
178 }
179 if ($condition->userIsEnabled !== null) {
180 $this->userIsEnabled = $condition->userIsEnabled;
181 $this->userIsDisabled = !$condition->userIsEnabled;
182 }
183 }
184
185 /**
f2a4a429 186 * @inheritDoc
87d3a054
MS
187 */
188 public function validate() {
189 if ($this->userIsBanned && $this->userIsNotBanned) {
190 $this->errorMessage = 'wcf.user.condition.state.isBanned.error.conflict';
191
192 throw new UserInputException('userIsBanned', 'conflict');
193 }
194
195 if ($this->userIsDisabled && $this->userIsEnabled) {
196 $this->errorMessage = 'wcf.user.condition.state.isEnabled.error.conflict';
197
2b755412 198 throw new UserInputException('userIsEnabled', 'conflict');
87d3a054
MS
199 }
200 }
20933e61
MS
201
202 /**
f2a4a429 203 * @inheritDoc
20933e61 204 */
1bf25f6b 205 public function showContent(Condition $condition) {
64571a65 206 if (!WCF::getUser()->userID) return false;
20933e61
MS
207
208 return $this->checkUser($condition, WCF::getUser());
209 }
87d3a054 210}