From 55b00c1c535c1c1da6fd6c713372314f32a7c3a4 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 1 Jun 2014 09:57:26 +0200 Subject: [PATCH] Add time-related content conditions --- com.woltlab.wcf/objectType.xml | 13 ++ .../install/files/acp/templates/noticeAdd.tpl | 11 + wcfsetup/install/files/js/WCF.js | 17 ++ .../condition/DaysOfWeekCondition.class.php | 51 +++++ .../system/condition/TimeCondition.class.php | 208 ++++++++++++++++++ wcfsetup/install/files/style/datePicker.less | 22 ++ wcfsetup/install/lang/de.xml | 9 + 7 files changed, 331 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/condition/DaysOfWeekCondition.class.php create mode 100644 wcfsetup/install/files/lib/system/condition/TimeCondition.class.php diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index 8597bdb9d2..16f6b23c85 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -603,6 +603,19 @@ com.woltlab.wcf.page + + com.woltlab.wcf.pointInTime.daysOfWeek + com.woltlab.wcf.condition.notice + + com.woltlab.wcf.pointInTime + + + com.woltlab.wcf.pointInTime.time + com.woltlab.wcf.condition.notice + + com.woltlab.wcf.pointInTime + + com.woltlab.wcf.user.username com.woltlab.wcf.condition.notice diff --git a/wcfsetup/install/files/acp/templates/noticeAdd.tpl b/wcfsetup/install/files/acp/templates/noticeAdd.tpl index 7b735be3f2..90840419bf 100644 --- a/wcfsetup/install/files/acp/templates/noticeAdd.tpl +++ b/wcfsetup/install/files/acp/templates/noticeAdd.tpl @@ -128,6 +128,17 @@ {@$pageConditionObjectType->getProcessor()->getHtml()} {/foreach} + +
+ {lang}wcf.acp.notice.conditions.pointInTime{/lang} + {lang}wcf.acp.notice.conditions.pointInTime.description{/lang} + + {foreach from=$groupedConditionObjectTypes['com.woltlab.wcf.pointInTime'] item='pointInTimeConditionObjectType'} + {@$pointInTimeConditionObjectType->getProcessor()->getHtml()} + {/foreach} +
+ + {event name='conditionTypeFieldsets'}
diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index 5209f95e42..1557b36581 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -2963,6 +2963,18 @@ WCF.Date.Picker = { } $inputValue = $inputValue.replace(' ', 'T'); + // Date objects require a date and a time, thus + // add the current date to a time only-value + if (false && $input.data('timeOnly')) { + $dateComponents = $inputValue.split(':'); + $date = new Date(); + $date.setHours($dateComponents[0]); + $date.setMinutes($dateComponents[1]); + $date.setSeconds(0); + + $inputValue = $date.toString(); + } + if ($input.data('ignoreTimezone')) { var $timezoneOffset = new Date($inputValue).getTimezoneOffset(); var $timezone = ($timezoneOffset > 0) ? '-' : '+'; // -120 equals GMT+0200 @@ -2984,6 +2996,7 @@ WCF.Date.Picker = { minuteText: WCF.Language.get('wcf.date.minute'), showTime: false, timeFormat: this._timeFormat, + timeOnly: $input.data('timeOnly') ? true : false, yearRange: ($input.hasClass('birthday') ? '-100:+0' : '1900:2038') }); } @@ -3008,6 +3021,10 @@ WCF.Date.Picker = { // bug workaround: setDate creates the widget but unfortunately doesn't hide it... $input.datepicker('widget').hide(); + + if ($input.data('timeOnly')) { + $input.datepicker('widget').addClass('timeOnlyPicker'); + } }, this)); } }; diff --git a/wcfsetup/install/files/lib/system/condition/DaysOfWeekCondition.class.php b/wcfsetup/install/files/lib/system/condition/DaysOfWeekCondition.class.php new file mode 100644 index 0000000000..a053dff0a1 --- /dev/null +++ b/wcfsetup/install/files/lib/system/condition/DaysOfWeekCondition.class.php @@ -0,0 +1,51 @@ + + * @package com.woltlab.wcf + * @subpackage system.condition + * @category Community Framework + */ +class DaysOfWeekCondition extends AbstractMultiSelectCondition implements IContentCondition { + /** + * @see wcf\system\condition\AbstractSelectCondition::$fieldName + */ + protected $fieldName = 'daysOfWeek'; + + /** + * @see \wcf\system\condition\AbstractSingleFieldCondition::$label + */ + protected $label = 'wcf.date.daysOfWeek'; + + /** + * @see \wcf\system\condition\AbstractSelectCondition::getOptions() + */ + protected function getOptions() { + $options = array(); + + $daysOfWeek = DateUtil::getWeekDays(); + foreach ($daysOfWeek as $index => $day) { + $options[$index] = WCF::getLanguage()->get('wcf.date.day.'.$day); + } + + return $options; + } + + /** + * @see \wcf\system\condition\IContentCondition::showContent() + */ + public function showContent(Condition $condition) { + $date = DateUtil::getDateTimeByTimestamp(TIME_NOW); + $date->setTimezone(WCF::getUser()->getTimeZone()); + + return in_array($date->format('w'), $condition->daysOfWeek); + } +} diff --git a/wcfsetup/install/files/lib/system/condition/TimeCondition.class.php b/wcfsetup/install/files/lib/system/condition/TimeCondition.class.php new file mode 100644 index 0000000000..321593f817 --- /dev/null +++ b/wcfsetup/install/files/lib/system/condition/TimeCondition.class.php @@ -0,0 +1,208 @@ + + * @package com.woltlab.wcf + * @subpackage system.condition + * @category Community Framework + */ +class TimeCondition extends AbstractMultipleFieldsCondition implements IContentCondition { + /** + * end time + * @var string + */ + protected $endTime = ''; + + /** + * @see \wcf\system\condition\AbstractMultipleFieldsCondition::$label + */ + protected $labels = array( + 'time' => 'wcf.date.time', + 'timezone' => 'wcf.date.timezone' + ); + + /** + * start time + * @var string + */ + protected $startTime = ''; + + /** + * timezone used to evaluate the start/end time + * @var string + */ + protected $timezone = 0; + + /** + * @see \wcf\system\condition\ICondition::getData() + */ + public function getData() { + $data = array(); + + if ($this->startTime) { + $data['startTime'] = $this->startTime; + } + if ($this->endTime) { + $data['endTime'] = $this->endTime; + } + + if (!empty($data) && $this->timezone) { + $data['timezone'] = $this->timezone; + } + + if (!empty($data)) { + return $data; + } + + return null; + } + + /** + * @see \wcf\system\condition\ICondition::getHTML() + */ + public function getHTML() { + $start = WCF::getLanguage()->get('wcf.date.period.start'); + $end = WCF::getLanguage()->get('wcf.date.period.end'); + + return << +
{$this->getLabel('time')}
+
+ + + {$this->getDescriptionElement('time')} + {$this->getErrorMessageElement('time')} +
+ +
+
{$this->getLabel('timezone')}
+
+ {$this->getTimezoneFieldElement()} + {$this->getDescriptionElement('timezone')} + {$this->getErrorMessageElement('timezone')} +
+
+HTML; + } + + protected function getTimezoneFieldElement() { + $fieldElement = ''; + + return $fieldElement; + } + + /** + * @see \wcf\system\condition\ICondition::readFormParameters() + */ + public function readFormParameters() { + if (isset($_POST['endTime'])) $this->endTime = StringUtil::trim($_POST['endTime']); + if (isset($_POST['startTime'])) $this->startTime = StringUtil::trim($_POST['startTime']); + if (isset($_POST['timezone'])) $this->timezone = StringUtil::trim($_POST['timezone']); + } + + /** + * @see \wcf\system\condition\ICondition::reset() + */ + public function reset() { + $this->endTime = ''; + $this->startTime = ''; + $this->timezone = 0; + } + + /** + * @see \wcf\system\condition\ICondition::setData() + */ + public function setData(Condition $condition) { + if ($condition->endTime) { + $this->endTime = $condition->endTime; + } + if ($condition->startTime) { + $this->startTime = $condition->startTime; + } + if ($condition->timezone) { + $this->timezone = $condition->timezone; + } + } + + /** + * @see \wcf\system\condition\ICondition::validate() + */ + public function validate() { + if ($this->startTime == '00:00' && $this->endTime == '00:00') { + $this->startTime = $this->endTime = ''; + return; + } + + $startDateTime = $endDateTime = null; + if ($this->startTime) { + $startDateTime = \DateTime::createFromFormat('H:i', $this->startTime); + if ($startDateTime === false) { + $this->errorMessages['time'] = 'wcf.date.startTime.error.notValid'; + + throw new UserInputException('startTime', 'notValid'); + } + } + if ($this->endTime) { + $endDateTime = \DateTime::createFromFormat('H:i', $this->endTime); + if ($endDateTime === false) { + $this->errorMessages['time'] = 'wcf.date.endTime.error.notValid'; + + throw new UserInputException('endTime', 'notValid'); + } + } + + if ($startDateTime !== null && $endDateTime !== null) { + if ($startDateTime->getTimestamp() >= $endDateTime->getTimeStamp()) { + $this->errorMessages['time'] = 'wcf.date.endTime.error.beforeStartTime'; + + throw new UserInputException('endTime', 'beforeStartTime'); + } + } + + if ($this->timezone && !in_array($this->timezone, DateUtil::getAvailableTimezones())) { + $this->errorMessages['timezone'] = 'wcf.global.form.error.notValidSelection'; + + throw new UserInputException('timezone', 'notValidSelection'); + } + } + + /** + * @see \wcf\system\condition\IContentCondition::showContent() + */ + public function showContent(Condition $condition) { + $timezone = WCF::getUser()->getTimeZone(); + if ($condition->timezone) { + $timezone = new \DateTimeZone($condition->timezone); + } + + if ($condition->startTime) { + $dateTime = \DateTime::createFromFormat('H:i', $condition->startTime, $timezone); + if ($dateTime->getTimestamp() > TIME_NOW) { + return false; + } + } + + if ($condition->endTime) { + $dateTime = \DateTime::createFromFormat('H:i', $condition->endTime, $timezone); + if ($dateTime->getTimestamp() < TIME_NOW) { + return false; + } + } + + return true; + } +} diff --git a/wcfsetup/install/files/style/datePicker.less b/wcfsetup/install/files/style/datePicker.less index 25e9e60d6c..ea7b957ee2 100644 --- a/wcfsetup/install/files/style/datePicker.less +++ b/wcfsetup/install/files/style/datePicker.less @@ -153,4 +153,26 @@ } } } + + &.timeOnlyPicker { + .ui-timepicker-div { + border-top: none; + + > .ui-widget-header { + display: none; + } + + > dl { + margin-right: @wcfGapTiny; + + > dt { + width: 70px; + } + + > dd { + margin-left: 90px; + } + } + } + } } diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index f98bcd1fed..530e4375e0 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -593,6 +593,8 @@ + + @@ -1825,6 +1827,13 @@ Fehler sind beispielsweise: + + + + + + + -- 2.20.1