EventConfig: Set time on dates

main
Igor Scheller 4 years ago committed by msquare
parent bb2a13f605
commit 30e1d41c0a

@ -50,19 +50,19 @@ function event_config_edit_controller()
$event_welcome_msg = null; $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(); $buildup_start_date = $result->getValue();
$valid &= $result->isValid(); $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(); $event_start_date = $result->getValue();
$valid &= $result->isValid(); $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(); $event_end_date = $result->getValue();
$valid &= $result->isValid(); $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(); $teardown_end_date = $result->getValue();
$valid &= $result->isValid(); $valid &= $result->isValid();
@ -111,10 +111,10 @@ function event_config_edit_controller()
'Changed event config: %s, %s, %s, %s, %s, %s', 'Changed event config: %s, %s, %s, %s, %s, %s',
$event_name, $event_name,
$event_welcome_msg, $event_welcome_msg,
$buildup_start_date ? $buildup_start_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') : '', $event_start_date ? $event_start_date->format('Y-m-d H:i') : '',
$event_end_date ? $event_end_date->format('Y-m-d') : '', $event_end_date ? $event_end_date->format('Y-m-d H:i') : '',
$teardown_end_date ? $teardown_end_date->format('Y-m-d') : '' $teardown_end_date ? $teardown_end_date->format('Y-m-d H:i') : ''
) )
); );
success(__('Settings saved.')); success(__('Settings saved.'));

@ -77,6 +77,31 @@ function form_date($name, $label, $value, $start_date = '', $end_date = '')
', $dom_id); ', $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 * Rendert eine Liste von Checkboxen für ein Formular
* *

@ -118,33 +118,36 @@ function check_request_int_array($name, $default = [])
* Checks if given request item (name) can be parsed to a date. * Checks if given request item (name) can be parsed to a date.
* If not parsable, given error message is put into msg() and null is returned. * If not parsable, given error message is put into msg() and null is returned.
* *
* @param string $name to be parsed into a date. * @param string $name to be parsed into a date.
* @param string $error_message the error message displayed if $input is not parsable * @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 * @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(); $request = request();
if (!$request->has($name)) { if (!$request->has($name)) {
return new ValidationResult($null_allowed, null); 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);
} }
/** /**
* Checks if given string can be parsed to a date. * Checks if given string can be parsed to a date.
* If not parsable, given error message is put into msg() and null is returned. * If not parsable, given error message is put into msg() and null is returned.
* *
* @param string $input String to be parsed into a date. * @param string $input String to be parsed into a date.
* @param string $error_message the error message displayed if $input is not parsable * @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 * @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 { 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) { } catch (InvalidArgumentException $e) {
$time = null; $time = null;
} }

@ -1,78 +1,15 @@
<?php <?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. * Render edit page for event config.
* *
* @param string $event_name The event name * @param string $event_name The event name
* @param string $event_welcome_msg The welcome message * @param string $event_welcome_msg The welcome message
* @param int $buildup_start_date unix time stamp * @param string $buildup_start_date Date (Y-M-D H:i)
* @param int $event_start_date unix time stamp * @param string $event_start_date Date (Y-M-D H:i)
* @param int $event_end_date unix time stamp * @param string $event_end_date Date (Y-M-D H:i)
* @param int $teardown_end_date unix time stamp * @param string $teardown_end_date Date (Y-M-D H:i)
*
* @return string * @return string
*/ */
function EventConfig_edit_view( function EventConfig_edit_view(
@ -97,12 +34,10 @@ function EventConfig_edit_view(
) )
]), ]),
div('col-md-3 col-xs-6', [ div('col-md-3 col-xs-6', [
form_date('buildup_start_date', __('Buildup date'), $buildup_start_date), form_datetime('buildup_start_date', __('Buildup date'), $buildup_start_date),
form_date('event_start_date', __('Event start date'), $event_start_date) form_datetime('event_start_date', __('Event start date'), $event_start_date),
]), form_datetime('event_end_date', __('Event end date'), $event_end_date),
div('col-md-3 col-xs-6', [ form_datetime('teardown_end_date', __('Teardown end date'), $teardown_end_date),
form_date('teardown_end_date', __('Teardown end date'), $teardown_end_date),
form_date('event_end_date', __('Event end date'), $event_end_date)
]) ])
]), ]),
div('row', [ div('row', [

@ -97,31 +97,36 @@ $(function () {
* Add a datepicker to all date input fields. * Add a datepicker to all date input fields.
*/ */
$(function () { $(function () {
$('.input-group.date').each(function () { $([
var elem = $(this); {'select': $('.input-group.date'), 'format': 'YYYY-MM-DD'},
var opts = { {'select': $('.input-group.datetime'), 'format': 'YYYY-MM-DD HH:mm'},
minDate: '', ]).each(function (_, element) {
maxDate: '', element.select.each(function () {
locale: $('html').attr('lang'), var elem = $(this);
format: 'YYYY-MM-DD', var opts = {
widgetPositioning: {horizontal: 'auto', vertical: 'bottom'} minDate: '',
}; maxDate: '',
$.extend(opts, elem.data()); locale: $('html').attr('lang'),
if (opts.minDate.length === 0) { format: element.format,
delete opts.minDate; widgetPositioning: {horizontal: 'auto', vertical: 'bottom'}
} };
if (opts.maxDate.length === 0) { $.extend(opts, elem.data());
delete opts.maxDate; if (opts.minDate.length === 0) {
} delete opts.minDate;
elem.children('input').attr('type', 'text'); }
elem.children().on('click', function (ev) { if (opts.maxDate.length === 0) {
ev.stopImmediatePropagation(); delete opts.maxDate;
if (typeof elem.data('DateTimePicker') === 'undefined') {
elem.datetimepicker(opts);
elem.data('DateTimePicker').show();
} else {
elem.data('DateTimePicker').toggle();
} }
elem.children('input').attr('type', 'text');
elem.children().on('click', function (ev) {
ev.stopImmediatePropagation();
if (typeof elem.data('DateTimePicker') === 'undefined') {
elem.datetimepicker(opts);
elem.data('DateTimePicker').show();
} else {
elem.data('DateTimePicker').toggle();
}
});
}); });
}); });
}); });

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

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

Loading…
Cancel
Save