2 namespace wcf\system\condition
;
3 use wcf\data\condition\Condition
;
4 use wcf\system\exception\UserInputException
;
7 use wcf\util\StringUtil
;
10 * Condition implementation for the current time.
12 * @author Matthias Schmidt
13 * @copyright 2001-2018 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\Condition
17 class TimeCondition
extends AbstractMultipleFieldsCondition
implements IContentCondition
{
22 protected $endTime = '00:00';
28 'time' => 'wcf.date.time',
29 'timezone' => 'wcf.date.timezone'
36 protected $startTime = '00:00';
39 * timezone used to evaluate the start/end time
42 protected $timezone = 0;
47 public function getData() {
50 if ($this->startTime
) {
51 $data['startTime'] = $this->startTime
;
54 $data['endTime'] = $this->endTime
;
57 if (!empty($data) && $this->timezone
) {
58 $data['timezone'] = $this->timezone
;
71 public function getHTML() {
72 $start = WCF
::getLanguage()->get('wcf.date.period.start');
73 $end = WCF
::getLanguage()->get('wcf.date.period.end');
77 <dt>{$this->getLabel('time')}</dt>
79 <input type="datetime" data-ignore-timezone="1" data-time-only="1" id="startTime" name="startTime" value="{$this->startTime}" placeholder="{$start}">
80 <input type="datetime" data-ignore-timezone="1" data-time-only="1" id="endTime" name="endTime" value="{$this->endTime}" placeholder="{$end}">
81 {$this->getDescriptionElement('time')}
82 {$this->getErrorMessageElement('time')}
86 <dt>{$this->getLabel('timezone')}</dt>
88 {$this->getTimezoneFieldElement()}
89 {$this->getDescriptionElement('timezone')}
90 {$this->getErrorMessageElement('timezone')}
97 * Returns the select element with all available timezones.
101 protected function getTimezoneFieldElement() {
102 $fieldElement = '<select name="timezone" id="timezone"><option value="0"'.($this->timezone ?
' selected' : '').'>'.WCF
::getLanguage()->get('wcf.date.timezone.user').'</option>';
103 foreach (DateUtil
::getAvailableTimezones() as $timezone) {
104 $fieldElement .= '<option value="'.$timezone.'"'.($this->timezone
=== $timezone ?
' selected' : '').'>'.WCF
::getLanguage()->get('wcf.date.timezone.'.str_replace('/', '.', strtolower($timezone))).'</option>';
106 $fieldElement .= '</select>';
108 return $fieldElement;
114 public function readFormParameters() {
115 if (isset($_POST['endTime'])) $this->endTime
= StringUtil
::trim($_POST['endTime']);
116 if (isset($_POST['startTime'])) $this->startTime
= StringUtil
::trim($_POST['startTime']);
117 if (isset($_POST['timezone'])) $this->timezone
= StringUtil
::trim($_POST['timezone']);
123 public function reset() {
124 $this->endTime
= '00:00';
125 $this->startTime
= '00:00';
132 public function setData(Condition
$condition) {
133 /** @noinspection PhpUndefinedFieldInspection */
134 $endTime = $condition->endTime
;
136 $this->endTime
= $endTime;
139 /** @noinspection PhpUndefinedFieldInspection */
140 $startTime = $condition->startTime
;
142 $this->startTime
= $startTime;
145 /** @noinspection PhpUndefinedFieldInspection */
146 $timezone = $condition->timezone
;
148 $this->timezone
= $timezone;
155 public function validate() {
156 if ($this->startTime
== '00:00' && $this->endTime
== '00:00') {
157 $this->startTime
= $this->endTime
= '';
161 $startDateTime = $endDateTime = null;
162 if ($this->startTime
) {
163 $startDateTime = \DateTime
::createFromFormat('H:i', $this->startTime
);
164 if ($startDateTime === false) {
165 $this->errorMessages
['time'] = 'wcf.date.startTime.error.invalid';
167 throw new UserInputException('startTime', 'invalid');
170 if ($this->endTime
) {
171 $endDateTime = \DateTime
::createFromFormat('H:i', $this->endTime
);
172 if ($endDateTime === false) {
173 $this->errorMessages
['time'] = 'wcf.date.endTime.error.invalid';
175 throw new UserInputException('endTime', 'invalid');
179 if ($startDateTime !== null && $endDateTime !== null) {
180 if ($startDateTime->getTimestamp() >= $endDateTime->getTimestamp()) {
181 $this->errorMessages
['time'] = 'wcf.date.endTime.error.beforeStartTime';
183 throw new UserInputException('endTime', 'beforeStartTime');
187 if ($this->timezone
&& !in_array($this->timezone
, DateUtil
::getAvailableTimezones())) {
188 $this->errorMessages
['timezone'] = 'wcf.global.form.error.noValidSelection';
190 throw new UserInputException('timezone', 'noValidSelection');
197 public function showContent(Condition
$condition) {
198 $timezone = WCF
::getUser()->getTimeZone();
199 /** @noinspection PhpUndefinedFieldInspection */
200 $conditionTimezone = $condition->timezone
;
201 if ($conditionTimezone) {
202 $timezone = new \
DateTimeZone($conditionTimezone);
205 /** @noinspection PhpUndefinedFieldInspection */
206 $startTime = $condition->startTime
;
208 $dateTime = \DateTime
::createFromFormat('H:i', $startTime, $timezone);
209 if ($dateTime->getTimestamp() > TIME_NOW
) {
214 /** @noinspection PhpUndefinedFieldInspection */
215 $endTime = $condition->endTime
;
217 $dateTime = \DateTime
::createFromFormat('H:i', $endTime, $timezone);
218 if ($dateTime->getTimestamp() < TIME_NOW
) {