f0417e7f9ae02e22e162f9f2e8a237d25eea43de
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\condition\user;
4
5 use wcf\data\condition\Condition;
6 use wcf\data\DatabaseObject;
7 use wcf\data\DatabaseObjectList;
8 use wcf\data\user\User;
9 use wcf\data\user\UserList;
10 use wcf\system\condition\AbstractSingleFieldCondition;
11 use wcf\system\condition\IObjectCondition;
12 use wcf\system\condition\IObjectListCondition;
13 use wcf\system\exception\InvalidObjectArgument;
14 use wcf\system\exception\UserInputException;
15 use wcf\system\WCF;
16 use wcf\util\ClassUtil;
17
18 /**
19 * User condition for the interval (in days) of their last activity.
20 *
21 * @author Matthias Schmidt
22 * @copyright 2001-2020 WoltLab GmbH
23 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
24 * @package WoltLabSuite\Core\System\Condition\User
25 * @since 5.3
26 */
27 class UserLastActivityTimeIntervalDaysCondition extends AbstractSingleFieldCondition implements
28 IObjectCondition,
29 IObjectListCondition
30 {
31 /**
32 * @inheritDoc
33 */
34 protected $label = 'wcf.user.condition.lastActivityTimeIntervalDays';
35
36 /**
37 * end value of the days interval
38 * @var int|string
39 */
40 protected $endDays = '';
41
42 /**
43 * start value of the days interval
44 * @var int|string
45 */
46 protected $startDays = '';
47
48 /**
49 * @inheritDoc
50 */
51 public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData)
52 {
53 if (!($objectList instanceof UserList)) {
54 throw new InvalidObjectArgument($objectList, UserList::class, 'Object list');
55 }
56
57 /** @noinspection PhpUndefinedFieldInspection */
58 if ($this->object->ignoreZeroTime) {
59 $objectList->getConditionBuilder()->add('user_table.lastActivityTime <> ?', [0]);
60 }
61 if (isset($conditionData['startDays'])) {
62 $objectList->getConditionBuilder()->add(
63 'user_table.lastActivityTime <= ?',
64 [TIME_NOW - $conditionData['startDays'] * 24 * 3600]
65 );
66 }
67 if (isset($conditionData['endDays'])) {
68 $objectList->getConditionBuilder()->add(
69 'user_table.lastActivityTime >= ?',
70 [TIME_NOW - $conditionData['endDays'] * 24 * 3600]
71 );
72 }
73 }
74
75 /**
76 * @inheritDoc
77 */
78 public function checkObject(DatabaseObject $object, array $conditionData)
79 {
80 if (!($object instanceof User) && !ClassUtil::isDecoratedInstanceOf($object, User::class)) {
81 throw new InvalidObjectArgument($object, User::class);
82 }
83
84 if (isset($conditionData['startDays'])) {
85 if ($object->lastActivityTime > TIME_NOW - $conditionData['startDays'] * 24 * 3600) {
86 return false;
87 }
88
89 if (isset($conditionData['endDays'])) {
90 if ($object->lastActivityTime < TIME_NOW - $conditionData['endDays'] * 24 * 3600) {
91 return false;
92 }
93 }
94
95 return true;
96 } elseif (
97 isset($conditionData['endDays'])
98 && $object->lastActivityTime < TIME_NOW - $conditionData['endDays'] * 24 * 3600
99 ) {
100 return false;
101 }
102
103 return true;
104 }
105
106 /**
107 * @inheritDoc
108 */
109 public function getData()
110 {
111 $data = [];
112
113 if (\strlen($this->startDays)) {
114 $data['startDays'] = $this->startDays;
115 }
116 if (\strlen($this->endDays)) {
117 $data['endDays'] = $this->endDays;
118 }
119
120 if (!empty($data)) {
121 return $data;
122 }
123 }
124
125 /**
126 * @inheritDoc
127 */
128 protected function getFieldElement()
129 {
130 $start = WCF::getLanguage()->get('wcf.date.period.start');
131 $end = WCF::getLanguage()->get('wcf.date.period.end');
132 $days = WCF::getLanguage()->get('wcf.acp.option.suffix.days');
133
134 return <<<HTML
135 <div class="inputAddon">
136 <input type="number" id="userLastActivityTimeIntervalStartDays" name="userLastActivityTimeIntervalStartDays" class="short" min="1" value="{$this->startDays}" placeholder="{$start}">
137 <span class="inputSuffix">{$days}</span>
138 </div>
139 <div class="inputAddon">
140 <input type="number" id="userLastActivityTimeIntervalEndDays" name="userLastActivityTimeIntervalEndDays" class="short" min="1" value="{$this->endDays}" placeholder="{$end}">
141 <span class="inputSuffix">{$days}</span>
142 </div>
143 HTML;
144 }
145
146 /**
147 * @inheritDoc
148 */
149 public function readFormParameters()
150 {
151 if (isset($_POST['userLastActivityTimeIntervalEndDays'])) {
152 $this->endDays = $_POST['userLastActivityTimeIntervalEndDays'];
153 }
154 if (isset($_POST['userLastActivityTimeIntervalStartDays'])) {
155 $this->startDays = $_POST['userLastActivityTimeIntervalStartDays'];
156 }
157 }
158
159 /**
160 * @inheritDoc
161 */
162 public function reset()
163 {
164 $this->endDays = '';
165 $this->startDays = '';
166 }
167
168 /**
169 * @inheritDoc
170 */
171 public function setData(Condition $condition)
172 {
173 /** @noinspection PhpUndefinedFieldInspection */
174 $endDays = $condition->endDays;
175 if ($endDays) {
176 $this->endDays = $endDays;
177 }
178
179 /** @noinspection PhpUndefinedFieldInspection */
180 $startDays = $condition->startDays;
181 if ($startDays) {
182 $this->startDays = $startDays;
183 }
184 }
185
186 /**
187 * @inheritDoc
188 */
189 public function validate()
190 {
191 $endDays = $startDays = null;
192 if (\strlen($this->startDays)) {
193 $startDays = \intval($this->startDays);
194 if ($startDays <= 0) {
195 $this->errorMessage = 'wcf.user.condition.lastActivityTimeIntervalDays.error.invalidStart';
196
197 throw new UserInputException('userLastActivityTimeIntervalDays', 'invalidStart');
198 }
199 }
200 if (\strlen($this->endDays)) {
201 $endDays = \intval($this->endDays);
202 if ($endDays <= 0) {
203 $this->errorMessage = 'wcf.user.condition.lastActivityTimeIntervalDays.error.invalidEnd';
204
205 throw new UserInputException('userLastActivityTimeIntervalDays', 'invalidEnd');
206 }
207 }
208
209 if ($endDays !== null && $startDays !== null && $endDays <= $startDays) {
210 $this->errorMessage = 'wcf.user.condition.lastActivityTimeIntervalDays.error.endBeforeStart';
211
212 throw new UserInputException('userLastActivityTimeIntervalDays', 'endBeforeStart');
213 }
214 }
215 }