diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/controller/event_config_controller.php | 64 | ||||
-rw-r--r-- | includes/includes.php | 1 | ||||
-rw-r--r-- | includes/model/EventConfig_model.php | 78 | ||||
-rw-r--r-- | includes/model/UserWorkLog_model.php | 10 | ||||
-rw-r--r-- | includes/model/User_model.php | 46 | ||||
-rw-r--r-- | includes/pages/guest_login.php | 26 | ||||
-rw-r--r-- | includes/pages/user_settings.php | 19 | ||||
-rw-r--r-- | includes/sys_form.php | 2 | ||||
-rw-r--r-- | includes/sys_page.php | 12 | ||||
-rw-r--r-- | includes/view/EventConfig_view.php | 59 | ||||
-rw-r--r-- | includes/view/User_view.php | 9 |
11 files changed, 143 insertions, 183 deletions
diff --git a/includes/controller/event_config_controller.php b/includes/controller/event_config_controller.php index c227b785..79c276e4 100644 --- a/includes/controller/event_config_controller.php +++ b/includes/controller/event_config_controller.php @@ -1,5 +1,8 @@ <?php +use Carbon\Carbon; +use Engelsystem\Models\EventConfig; + /** * @return string */ @@ -20,22 +23,17 @@ function event_config_edit_controller() } $request = request(); - $event_name = null; - $event_welcome_msg = null; - $buildup_start_date = null; - $event_start_date = null; - $event_end_date = null; - $teardown_end_date = null; - - $event_config = EventConfig(); - if (!empty($event_config)) { - $event_name = $event_config['event_name']; - $buildup_start_date = $event_config['buildup_start_date']; - $event_start_date = $event_config['event_start_date']; - $event_end_date = $event_config['event_end_date']; - $teardown_end_date = $event_config['teardown_end_date']; - $event_welcome_msg = $event_config['event_welcome_msg']; - } + $config = config(); + $event_name = $config->get('name'); + $event_welcome_msg = $config->get('welcome_msg'); + /** @var Carbon $buildup_start_date */ + $buildup_start_date = $config->get('buildup_start'); + /** @var Carbon $event_start_date */ + $event_start_date = $config->get('event_start'); + /** @var Carbon $event_end_date */ + $event_end_date = $config->get('event_end'); + /** @var Carbon $teardown_end_date */ + $teardown_end_date = $config->get('teardown_end'); if ($request->has('submit')) { $valid = true; @@ -91,24 +89,34 @@ function event_config_edit_controller() } if ($valid) { - EventConfig_update( - $event_name, - $buildup_start_date, - $event_start_date, - $event_end_date, - $teardown_end_date, - $event_welcome_msg - ); + $eventConfig = new EventConfig(); + + foreach ( + [ + 'name' => $event_name, + 'welcome_msg' => $event_welcome_msg, + 'buildup_start' => $buildup_start_date, + 'event_start' => $event_start_date, + 'event_end' => $event_end_date, + 'teardown_end' => $teardown_end_date, + ] as $key => $value + ) { + $eventConfig + ->findOrNew($key) + ->setAttribute('name', $key) + ->setAttribute('value', $value) + ->save(); + } engelsystem_log( sprintf( 'Changed event config: %s, %s, %s, %s, %s, %s', $event_name, $event_welcome_msg, - date('Y-m-d', $buildup_start_date), - date('Y-m-d', $event_start_date), - date('Y-m-d', $event_end_date), - date('Y-m-d', $teardown_end_date) + $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') : '' ) ); success(__('Settings saved.')); diff --git a/includes/includes.php b/includes/includes.php index 628cf88e..855ff359 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -12,7 +12,6 @@ $includeFiles = [ __DIR__ . '/../includes/sys_template.php', __DIR__ . '/../includes/model/AngelType_model.php', - __DIR__ . '/../includes/model/EventConfig_model.php', __DIR__ . '/../includes/model/Message_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/Room_model.php', diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php deleted file mode 100644 index 766a5849..00000000 --- a/includes/model/EventConfig_model.php +++ /dev/null @@ -1,78 +0,0 @@ -<?php - -use Engelsystem\Database\DB; - -/** - * Get event config. - * - * @return array|null - */ -function EventConfig() -{ - $config = DB::selectOne('SELECT * FROM `EventConfig` LIMIT 1'); - - return empty($config) ? null : $config; -} - -/** - * Update event config. - * - * @param string $event_name - * @param int $buildup_start_date - * @param int $event_start_date - * @param int $event_end_date - * @param int $teardown_end_date - * @param string $event_welcome_msg - * @return bool - */ -function EventConfig_update( - $event_name, - $buildup_start_date, - $event_start_date, - $event_end_date, - $teardown_end_date, - $event_welcome_msg -) { - $eventConfig = EventConfig(); - if (empty($eventConfig)) { - return DB::insert(' - INSERT INTO `EventConfig` ( - `event_name`, - `buildup_start_date`, - `event_start_date`, - `event_end_date`, - `teardown_end_date`, - `event_welcome_msg` - ) - VALUES (?, ?, ?, ?, ?, ?) - ', - [ - $event_name, - $buildup_start_date, - $event_start_date, - $event_end_date, - $teardown_end_date, - $event_welcome_msg - ] - ); - } - - return (bool)DB::update(' - UPDATE `EventConfig` SET - `event_name` = ?, - `buildup_start_date` = ?, - `event_start_date` = ?, - `event_end_date` = ?, - `teardown_end_date` = ?, - `event_welcome_msg` = ? - ', - [ - $event_name, - $buildup_start_date, - $event_start_date, - $event_end_date, - $teardown_end_date, - $event_welcome_msg, - ] - ); -} diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php index ff39ba8f..dd4b2574 100644 --- a/includes/model/UserWorkLog_model.php +++ b/includes/model/UserWorkLog_model.php @@ -1,5 +1,6 @@ <?php +use Carbon\Carbon; use Engelsystem\Database\Db; /** @@ -128,10 +129,13 @@ function UserWorkLog_create($userWorkLog) function UserWorkLog_new($user) { $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time())); - $event_config = EventConfig(); - if (!empty($event_config['buildup_start_date'])) { - $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', $event_config['buildup_start_date'])); + + /** @var Carbon $buildup */ + $buildup = $buildup = config('buildup_start'); + if (!empty($buildup)) { + $work_date = $buildup->format('Y-m-d H:i'); } + return [ 'user_id' => $user['UID'], 'work_timestamp' => $work_date, diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 092ddf27..051f8ff6 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -1,5 +1,6 @@ <?php +use Carbon\Carbon; use Engelsystem\Database\DB; use Engelsystem\ValidationResult; @@ -359,19 +360,23 @@ function User_validate_planned_arrival_date($planned_arrival_date) // null is not okay return new ValidationResult(false, time()); } - $event_config = EventConfig(); - if (empty($event_config)) { - // Nothing to validate against - return new ValidationResult(true, $planned_arrival_date); - } - if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) { + + $config = config(); + $buildup = $config->get('buildup_start'); + $teardown = $config->get('teardown_end'); + + /** @var Carbon $buildup */ + if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_arrival_date))) { // Planned arrival can not be before buildup start date - return new ValidationResult(false, $event_config['buildup_start_date']); + return new ValidationResult(false, $buildup->getTimestamp()); } - if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) { + + /** @var Carbon $teardown */ + if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_arrival_date))) { // Planned arrival can not be after teardown end date - return new ValidationResult(false, $event_config['teardown_end_date']); + return new ValidationResult(false, $teardown->getTimestamp()); } + return new ValidationResult(true, $planned_arrival_date); } @@ -388,23 +393,28 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de // null is okay return new ValidationResult(true, null); } + if ($planned_arrival_date > $planned_departure_date) { // departure cannot be before arrival return new ValidationResult(false, $planned_arrival_date); } - $event_config = EventConfig(); - if (empty($event_config)) { - // Nothing to validate against - return new ValidationResult(true, $planned_departure_date); - } - if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) { + + $config = config(); + $buildup = $config->get('buildup_start'); + $teardown = $config->get('teardown_end'); + + /** @var Carbon $buildup */ + if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_departure_date))) { // Planned arrival can not be before buildup start date - return new ValidationResult(false, $event_config['buildup_start_date']); + return new ValidationResult(false, $buildup->getTimestamp()); } - if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) { + + /** @var Carbon $teardown */ + if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_departure_date))) { // Planned arrival can not be after teardown end date - return new ValidationResult(false, $event_config['teardown_end_date']); + return new ValidationResult(false, $teardown->getTimestamp()); } + return new ValidationResult(true, $planned_departure_date); } diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index fbad8ff6..797aaea7 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -1,5 +1,6 @@ <?php +use Carbon\Carbon; use Engelsystem\Database\DB; /** @@ -37,7 +38,7 @@ function guest_register() $tshirt_sizes = config('tshirt_sizes'); $enable_tshirt_size = config('enable_tshirt_size'); $min_password_length = config('min_password_length'); - $event_config = EventConfig(); + $config = config(); $request = request(); $session = session(); @@ -273,8 +274,8 @@ function guest_register() } // If a welcome message is present, display registration success page. - if (!empty($event_config) && !empty($event_config['event_welcome_msg'])) { - return User_registration_success_view($event_config['event_welcome_msg']); + if ($message = $config->get('welcome_msg')) { + return User_registration_success_view($message); } redirect(page_link_to('/')); @@ -283,13 +284,14 @@ function guest_register() $buildup_start_date = time(); $teardown_end_date = null; - if (!empty($event_config)) { - if (isset($event_config['buildup_start_date'])) { - $buildup_start_date = $event_config['buildup_start_date']; - } - if (isset($event_config['teardown_end_date'])) { - $teardown_end_date = $event_config['teardown_end_date']; - } + if ($buildup = $config->get('buildup_start')) { + /** @var Carbon $buildup */ + $buildup_start_date = $buildup->getTimestamp(); + } + + if ($teardown = $config->get('teardown_end')) { + /** @var Carbon $teardown */ + $teardown_end_date = $teardown->getTimestamp(); } return page_with_title(register_title(), [ @@ -452,12 +454,10 @@ function guest_login() } } - $event_config = EventConfig(); - return page([ div('col-md-12', [ div('row', [ - EventConfig_countdown_page($event_config) + EventConfig_countdown_page() ]), div('row', [ div('col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4', [ diff --git a/includes/pages/user_settings.php b/includes/pages/user_settings.php index d8f6e44b..cf8d2f0b 100644 --- a/includes/pages/user_settings.php +++ b/includes/pages/user_settings.php @@ -1,5 +1,6 @@ <?php +use Carbon\Carbon; use Engelsystem\Database\DB; /** @@ -206,6 +207,7 @@ function user_settings() { global $user; $request = request(); + $config = config(); $themes = config('available_themes'); $enable_tshirt_size = config('enable_tshirt_size'); @@ -214,14 +216,15 @@ function user_settings() $buildup_start_date = null; $teardown_end_date = null; - $event_config = EventConfig(); - if (!empty($event_config)) { - if (isset($event_config['buildup_start_date'])) { - $buildup_start_date = $event_config['buildup_start_date']; - } - if (isset($event_config['teardown_end_date'])) { - $teardown_end_date = $event_config['teardown_end_date']; - } + + if ($buildup = $config->get('buildup_start')) { + /** @var Carbon $buildup */ + $buildup_start_date = $buildup->getTimestamp(); + } + + if ($teardown = $config->get('teardown_end')) { + /** @var Carbon $teardown */ + $teardown_end_date = $teardown->getTimestamp(); } $user_source = $user; diff --git a/includes/sys_form.php b/includes/sys_form.php index edf9542a..a1b78b70 100644 --- a/includes/sys_form.php +++ b/includes/sys_form.php @@ -1,5 +1,6 @@ <?php // Methods to build a html form. +use Carbon\Carbon; /** * Renders a hidden input @@ -63,6 +64,7 @@ function form_spinner($name, $label, $value) function form_date($name, $label, $value, $start_date = '', $end_date = '') { $dom_id = $name . '-date'; + $value = ($value instanceof Carbon) ? $value->getTimestamp() : $value; $value = is_numeric($value) ? date('Y-m-d', $value) : ''; $start_date = is_numeric($start_date) ? date('Y-m-d', $start_date) : ''; $end_date = is_numeric($end_date) ? date('Y-m-d', $end_date) : ''; diff --git a/includes/sys_page.php b/includes/sys_page.php index 55fb3b38..a560c3ba 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -1,5 +1,6 @@ <?php +use Carbon\Carbon; use Engelsystem\ValidationResult; /** @@ -134,9 +135,16 @@ function check_request_date($name, $error_message = null, $null_allowed = false) */ function check_date($input, $error_message = null, $null_allowed = false) { - if ($tmp = parse_date('Y-m-d H:i', trim($input) . ' 00:00')) { - return new ValidationResult(true, $tmp); + try { + $time = Carbon::createFromFormat('Y-m-d', trim($input)); + } catch (InvalidArgumentException $e) { + $time = null; } + + if ($time) { + return new ValidationResult(true, $time); + } + if ($null_allowed) { return new ValidationResult(true, null); } diff --git a/includes/view/EventConfig_view.php b/includes/view/EventConfig_view.php index 4290ab0a..cd657c67 100644 --- a/includes/view/EventConfig_view.php +++ b/includes/view/EventConfig_view.php @@ -1,59 +1,62 @@ <?php +use Carbon\Carbon; + /** * Shows basic event infos and countdowns. * - * @param array $event_config The event configuration * @return string */ -function EventConfig_countdown_page($event_config) +function EventConfig_countdown_page() { - if (empty($event_config)) { - return div('col-md-12 text-center', [ - heading(sprintf(__('Welcome to the %s!'), '<span class="icon-icon_angel"></span> ENGELSYSTEM'), 2) - ]); - } - + $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 = []; - if (!is_null($event_config['event_name'])) { - $elements[] = div('col-sm-12 text-center', [ - heading(sprintf( - __('Welcome to the %s!'), - $event_config['event_name'] . ' <span class="icon-icon_angel"></span> ENGELSYSTEM' - ), 2) - ]); - } + $elements[] = div('col-sm-12 text-center', [ + heading(sprintf( + __('Welcome to the %s!'), + $name . ' <span class="icon-icon_angel"></span> ENGELSYSTEM' + ), 2) + ]); - if (!is_null($event_config['buildup_start_date']) && time() < $event_config['buildup_start_date']) { + 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="' . $event_config['buildup_start_date'] . '">%c</span>', - '<small>' . date(__('Y-m-d'), $event_config['buildup_start_date']) . '</small>' + '<span class="moment-countdown text-big" data-timestamp="' . $buildup->getTimestamp() . '">%c</span>', + '<small>' . $buildup->format(__('Y-m-d')) . '</small>' ]); } - if (!is_null($event_config['event_start_date']) && time() < $event_config['event_start_date']) { + 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="' . $event_config['event_start_date'] . '">%c</span>', - '<small>' . date(__('Y-m-d'), $event_config['event_start_date']) . '</small>' + '<span class="moment-countdown text-big" data-timestamp="' . $start->getTimestamp() . '">%c</span>', + '<small>' . $start->format(__('Y-m-d')) . '</small>' ]); } - if (!is_null($event_config['event_end_date']) && time() < $event_config['event_end_date']) { + 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="' . $event_config['event_end_date'] . '">%c</span>', - '<small>' . date(__('Y-m-d'), $event_config['event_end_date']) . '</small>' + '<span class="moment-countdown text-big" data-timestamp="' . $end->getTimestamp() . '">%c</span>', + '<small>' . $end->format(__('Y-m-d')) . '</small>' ]); } - if (!is_null($event_config['teardown_end_date']) && time() < $event_config['teardown_end_date']) { + 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="' . $event_config['teardown_end_date'] . '">%c</span>', - '<small>' . date(__('Y-m-d'), $event_config['teardown_end_date']) . '</small>' + '<span class="moment-countdown text-big" data-timestamp="' . $teardown->getTimestamp() . '">%c</span>', + '<small>' . $teardown->format(__('Y-m-d')) . '</small>' ]); } diff --git a/includes/view/User_view.php b/includes/view/User_view.php index e092855a..ddf49885 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -1,5 +1,7 @@ <?php +use Carbon\Carbon; + /** * Renders user settings page * @@ -887,10 +889,9 @@ function render_user_arrived_hint() global $user; if ($user['Gekommen'] == 0) { - $event_config = EventConfig(); - if (!empty($event_config) - && !is_null($event_config['buildup_start_date']) - && time() > $event_config['buildup_start_date']) { + /** @var Carbon $buildup */ + $buildup = config('buildup_start'); + if (!empty($buildup) && $buildup->lessThan(new Carbon())) { return __('You are not marked as arrived. Please go to heaven\'s desk, get your angel badge and/or tell them that you arrived already.'); } } |