Merge branch 'master' into next
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / condition / UserRegistrationDateCondition.class.php
1 <?php
2 namespace wcf\system\condition;
3 use wcf\data\condition\Condition;
4 use wcf\data\user\User;
5 use wcf\data\user\UserList;
6 use wcf\data\DatabaseObjectList;
7 use wcf\system\exception\UserInputException;
8 use wcf\system\WCF;
9
10 /**
11 * Condition implementation for the registration date 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 */
20 class UserRegistrationDateCondition extends AbstractSingleFieldCondition implements IContentCondition, IObjectListCondition, IUserCondition {
21 use TObjectListUserCondition;
22
23 /**
24 * @inheritDoc
25 */
26 protected $label = 'wcf.user.condition.registrationDate';
27
28 /**
29 * registration start date
30 * @var string
31 */
32 protected $registrationDateEnd = '';
33
34 /**
35 * registration start date
36 * @var string
37 */
38 protected $registrationDateStart = '';
39
40 /**
41 * @inheritDoc
42 */
43 public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) {
44 if (!($objectList instanceof UserList)) {
45 throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given.");
46 }
47
48 if (isset($conditionData['registrationDateEnd'])) {
49 $objectList->getConditionBuilder()->add('user_table.registrationDate < ?', [strtotime($conditionData['registrationDateEnd']) + 86400]);
50 }
51 if (isset($conditionData['registrationDateStart'])) {
52 $objectList->getConditionBuilder()->add('user_table.registrationDate >= ?', [strtotime($conditionData['registrationDateStart'])]);
53 }
54 }
55
56 /**
57 * @inheritDoc
58 */
59 public function checkUser(Condition $condition, User $user) {
60 if ($condition->registrationDateStart !== null && $user->registrationDate < strtotime($condition->registrationDateStart)) {
61 return false;
62 }
63 if ($condition->registrationDateEnd !== null && $user->registrationDate >= strtotime($condition->registrationDateEnd) + 86400) {
64 return false;
65 }
66
67 return true;
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function getData() {
74 $data = [];
75
76 if (strlen($this->registrationDateStart)) {
77 $data['registrationDateStart'] = $this->registrationDateStart;
78 }
79 if (strlen($this->registrationDateEnd)) {
80 $data['registrationDateEnd'] = $this->registrationDateEnd;
81 }
82
83 if (!empty($data)) {
84 return $data;
85 }
86
87 return null;
88 }
89
90 /**
91 * @inheritDoc
92 */
93 protected function getFieldElement() {
94 $start = WCF::getLanguage()->get('wcf.date.period.start');
95 $end = WCF::getLanguage()->get('wcf.date.period.end');
96
97 return <<<HTML
98 <input type="date" id="registrationDateStart" name="registrationDateStart" value="{$this->registrationDateStart}" placeholder="{$start}" />
99 <input type="date" id="registrationDateEnd" name="registrationDateEnd" value="{$this->registrationDateEnd}" placeholder="{$end}" />
100 HTML;
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function readFormParameters() {
107 if (isset($_POST['registrationDateEnd'])) $this->registrationDateEnd = $_POST['registrationDateEnd'];
108 if (isset($_POST['registrationDateStart'])) $this->registrationDateStart = $_POST['registrationDateStart'];
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public function reset() {
115 $this->registrationDateEnd = '';
116 $this->registrationDateStart = '';
117 }
118
119 /**
120 * @inheritDoc
121 */
122 public function setData(Condition $condition) {
123 if ($condition->registrationDateEnd) {
124 $this->registrationDateEnd = $condition->registrationDateEnd;
125 }
126 if ($condition->registrationDateStart) {
127 $this->registrationDateStart = $condition->registrationDateStart;
128 }
129 }
130
131 /**
132 * @inheritDoc
133 */
134 public function validate() {
135 $registrationDateEnd = $registrationDateStart = null;
136 if (strlen($this->registrationDateStart)) {
137 $registrationDateStart = @strtotime($this->registrationDateStart);
138 if ($registrationDateStart === false) {
139 $this->errorMessage = 'wcf.condition.timestamp.error.startNotValid';
140
141 throw new UserInputException('registrationDate', 'startNotValid');
142 }
143 }
144 if (strlen($this->registrationDateEnd)) {
145 $registrationDateEnd = @strtotime($this->registrationDateEnd);
146 if ($registrationDateEnd === false) {
147 $this->errorMessage = 'wcf.condition.timestamp.error.endNotValid';
148
149 throw new UserInputException('registrationDate', 'endNotValid');
150 }
151 }
152
153 if ($registrationDateEnd !== null && $registrationDateStart !== null && $registrationDateEnd < $registrationDateStart) {
154 $this->errorMessage = 'wcf.condition.timestamp.error.endBeforeStart';
155
156 throw new UserInputException('registrationDate', 'endBeforeStart');
157 }
158 }
159
160 /**
161 * @inheritDoc
162 */
163 public function showContent(Condition $condition) {
164 if (!WCF::getUser()->userID) return false;
165
166 return $this->checkUser($condition, WCF::getUser());
167 }
168 }