EventConfig: Set time on dates

This commit is contained in:
Igor Scheller 2021-01-02 01:22:05 +01:00 committed by msquare
parent bb2a13f605
commit 30e1d41c0a
7 changed files with 94 additions and 127 deletions

View File

@ -50,19 +50,19 @@ function event_config_edit_controller()
$event_welcome_msg = null;
}
$result = check_request_date('buildup_start_date', __('Please enter buildup start date.'), true);
$result = check_request_date('buildup_start_date', __('Please enter buildup start date.'), true, true);
$buildup_start_date = $result->getValue();
$valid &= $result->isValid();
$result = check_request_date('event_start_date', __('Please enter event start date.'), true);
$result = check_request_date('event_start_date', __('Please enter event start date.'), true, true);
$event_start_date = $result->getValue();
$valid &= $result->isValid();
$result = check_request_date('event_end_date', __('Please enter event end date.'), true);
$result = check_request_date('event_end_date', __('Please enter event end date.'), true, true);
$event_end_date = $result->getValue();
$valid &= $result->isValid();
$result = check_request_date('teardown_end_date', __('Please enter teardown end date.'), true);
$result = check_request_date('teardown_end_date', __('Please enter teardown end date.'), true, true);
$teardown_end_date = $result->getValue();
$valid &= $result->isValid();
@ -111,10 +111,10 @@ function event_config_edit_controller()
'Changed event config: %s, %s, %s, %s, %s, %s',
$event_name,
$event_welcome_msg,
$buildup_start_date ? $buildup_start_date->format('Y-m-d') : '',
$event_start_date ? $event_start_date->format('Y-m-d') : '',
$event_end_date ? $event_end_date->format('Y-m-d') : '',
$teardown_end_date ? $teardown_end_date->format('Y-m-d') : ''
$buildup_start_date ? $buildup_start_date->format('Y-m-d H:i') : '',
$event_start_date ? $event_start_date->format('Y-m-d H:i') : '',
$event_end_date ? $event_end_date->format('Y-m-d H:i') : '',
$teardown_end_date ? $teardown_end_date->format('Y-m-d H:i') : ''
)
);
success(__('Settings saved.'));

View File

@ -77,6 +77,31 @@ function form_date($name, $label, $value, $start_date = '', $end_date = '')
', $dom_id);
}
/**
* Render a bootstrap datepicker
*
* @param string $name Name of the parameter
* @param string $label
* @param mixed $value
*
* @return string HTML
*/
function form_datetime(string $name, string $label, $value)
{
$dom_id = $name . '-datetime';
if ($value) {
$value = ($value instanceof Carbon) ? $value : Carbon::createFromTimestamp($value);
}
return form_element($label, sprintf('
<div class="input-group datetime" id="%s">
<input type="datetime-local" placeholder="YYYY-MM-DD HH:MM" name="%s"
class="form-control" value="%s" autocomplete="off">'
. '<span class="input-group-addon">' . glyph('th') . '</span>
</div>
', $dom_id, $name, htmlspecialchars($value ? $value->format('Y-m-d H:i') : '')), $dom_id);
}
/**
* Rendert eine Liste von Checkboxen für ein Formular
*

View File

@ -120,16 +120,17 @@ function check_request_int_array($name, $default = [])
*
* @param string $name to be parsed into a date.
* @param string $error_message the error message displayed if $input is not parsable
* @param boolean $null_allowed is a null value allowed?
* @param bool $null_allowed is a null value allowed?
* @param bool $time_allowed is time allowed?
* @return ValidationResult containing the parsed date
*/
function check_request_date($name, $error_message = null, $null_allowed = false)
function check_request_date($name, $error_message = null, $null_allowed = false, $time_allowed = false)
{
$request = request();
if (!$request->has($name)) {
return new ValidationResult($null_allowed, null);
}
return check_date($request->input($name), $error_message, $null_allowed);
return check_date($request->input($name), $error_message, $null_allowed, $time_allowed);
}
/**
@ -138,13 +139,15 @@ function check_request_date($name, $error_message = null, $null_allowed = false)
*
* @param string $input String to be parsed into a date.
* @param string $error_message the error message displayed if $input is not parsable
* @param boolean $null_allowed is a null value allowed?
* @param bool $null_allowed is a null value allowed?
* @param bool $time_allowed is time allowed?
* @return ValidationResult containing the parsed date
*/
function check_date($input, $error_message = null, $null_allowed = false)
function check_date($input, $error_message = null, $null_allowed = false, $time_allowed = false)
{
try {
$time = Carbon::createFromFormat('Y-m-d', trim($input));
$format = $time_allowed ? 'Y-m-d H:i' : 'Y-m-d';
$time = Carbon::createFromFormat($format, trim($input));
} catch (InvalidArgumentException $e) {
$time = null;
}

View File

@ -1,78 +1,15 @@
<?php
use Carbon\Carbon;
use Illuminate\Support\Str;
/**
* Shows basic event infos and countdowns.
*
* @return string
*/
function EventConfig_countdown_page()
{
$config = config();
$name = $config->get('name', '');
/** @var Carbon $buildup */
$buildup = $config->get('buildup_start');
/** @var Carbon $start */
$start = $config->get('event_start');
/** @var Carbon $end */
$end = $config->get('event_end');
/** @var Carbon $teardown */
$teardown = $config->get('teardown_end');
$elements = [];
$elements[] = div('col-sm-12 text-center', [
heading(sprintf(
__('Welcome to the %s!'),
$name . ' <span class="icon-icon_angel"></span> ' . Str::upper(config('app_name'))
), 2)
]);
if (!empty($buildup) && $buildup->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Buildup starts'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $buildup->getTimestamp() . '">%c</span>',
'<small>' . $buildup->format(__('Y-m-d')) . '</small>'
]);
}
if (!empty($start) && $start->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Event starts'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $start->getTimestamp() . '">%c</span>',
'<small>' . $start->format(__('Y-m-d')) . '</small>'
]);
}
if (!empty($end) && $end->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Event ends'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $end->getTimestamp() . '">%c</span>',
'<small>' . $end->format(__('Y-m-d')) . '</small>'
]);
}
if (!empty($teardown) && $teardown->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Teardown ends'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $teardown->getTimestamp() . '">%c</span>',
'<small>' . $teardown->format(__('Y-m-d')) . '</small>'
]);
}
return join('', $elements);
}
/**
* Render edit page for event config.
*
* @param string $event_name The event name
* @param string $event_welcome_msg The welcome message
* @param int $buildup_start_date unix time stamp
* @param int $event_start_date unix time stamp
* @param int $event_end_date unix time stamp
* @param int $teardown_end_date unix time stamp
* @param string $buildup_start_date Date (Y-M-D H:i)
* @param string $event_start_date Date (Y-M-D H:i)
* @param string $event_end_date Date (Y-M-D H:i)
* @param string $teardown_end_date Date (Y-M-D H:i)
*
* @return string
*/
function EventConfig_edit_view(
@ -97,12 +34,10 @@ function EventConfig_edit_view(
)
]),
div('col-md-3 col-xs-6', [
form_date('buildup_start_date', __('Buildup date'), $buildup_start_date),
form_date('event_start_date', __('Event start date'), $event_start_date)
]),
div('col-md-3 col-xs-6', [
form_date('teardown_end_date', __('Teardown end date'), $teardown_end_date),
form_date('event_end_date', __('Event end date'), $event_end_date)
form_datetime('buildup_start_date', __('Buildup date'), $buildup_start_date),
form_datetime('event_start_date', __('Event start date'), $event_start_date),
form_datetime('event_end_date', __('Event end date'), $event_end_date),
form_datetime('teardown_end_date', __('Teardown end date'), $teardown_end_date),
])
]),
div('row', [

View File

@ -97,13 +97,17 @@ $(function () {
* Add a datepicker to all date input fields.
*/
$(function () {
$('.input-group.date').each(function () {
$([
{'select': $('.input-group.date'), 'format': 'YYYY-MM-DD'},
{'select': $('.input-group.datetime'), 'format': 'YYYY-MM-DD HH:mm'},
]).each(function (_, element) {
element.select.each(function () {
var elem = $(this);
var opts = {
minDate: '',
maxDate: '',
locale: $('html').attr('lang'),
format: 'YYYY-MM-DD',
format: element.format,
widgetPositioning: {horizontal: 'auto', vertical: 'bottom'}
};
$.extend(opts, elem.data());
@ -125,6 +129,7 @@ $(function () {
});
});
});
});
/*
* Add a timepicker to all time input fields.

View File

@ -32,10 +32,10 @@ class EventConfig extends BaseModel
/** @var array The configuration values that should be cast to native types */
protected $valueCasts = [
'buildup_start' => 'date',
'event_start' => 'date',
'event_end' => 'date',
'teardown_end' => 'date',
'buildup_start' => 'datetime_human',
'event_start' => 'datetime_human',
'event_end' => 'datetime_human',
'teardown_end' => 'datetime_human',
'last_metrics' => 'datetime',
];
@ -55,9 +55,8 @@ class EventConfig extends BaseModel
/** @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute */
if (!empty($value)) {
switch ($this->getValueCast($this->name)) {
case 'date':
return Carbon::createFromFormat('Y-m-d', $value)
->setTime(0, 0);
case 'datetime_human':
return Carbon::make($value);
case 'datetime':
return Carbon::createFromFormat(Carbon::ISO8601, $value);
}
@ -76,9 +75,9 @@ class EventConfig extends BaseModel
{
if (!empty($value)) {
switch ($this->getValueCast($this->name)) {
case 'date':
case 'datetime_human':
/** @var Carbon $value */
$value = $value->toDateString();
$value = $value->toDateTimeString('minute');
break;
case 'datetime':
/** @var Carbon $value */

View File

@ -28,7 +28,7 @@ class EventConfigTest extends ModelTest
->setAttribute('value', new Carbon('2000-01-01 10:20'))
->save();
$this->assertEquals(
'"2000-01-01"',
'"2000-01-01 10:20"',
$this->database
->selectOne("SELECT `value` FROM event_config WHERE name='buildup_start'")
->value
@ -60,7 +60,7 @@ class EventConfigTest extends ModelTest
->setAttribute('value', new Carbon('2001-02-03 11:12'))
->save();
$this->assertEquals(
'2001-02-03 00:00',
'2001-02-03 11:12',
(new EventConfig())->find('buildup_start')
->value
->format('Y-m-d H:i')