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