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