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