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