From 7f61dc95be4ac543986c7df2459532fd8f81368d Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 25 Sep 2018 17:33:31 +0200 Subject: EventConfig: Merge event configuration from database to global config --- includes/controller/event_config_controller.php | 64 +++++++++++--------- includes/includes.php | 1 - includes/model/EventConfig_model.php | 78 ------------------------- includes/model/UserWorkLog_model.php | 10 +++- includes/model/User_model.php | 46 +++++++++------ includes/pages/guest_login.php | 26 ++++----- includes/pages/user_settings.php | 19 +++--- includes/sys_form.php | 2 + includes/sys_page.php | 12 +++- includes/view/EventConfig_view.php | 59 ++++++++++--------- includes/view/User_view.php | 9 +-- 11 files changed, 143 insertions(+), 183 deletions(-) delete mode 100644 includes/model/EventConfig_model.php (limited to 'includes') 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 @@ 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 @@ -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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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'] . ' ENGELSYSTEM' - ), 2) - ]); - } + $elements[] = div('col-sm-12 text-center', [ + heading(sprintf( + __('Welcome to the %s!'), + $name . ' 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), - '%c', - '' . date(__('Y-m-d'), $event_config['buildup_start_date']) . '' + '%c', + '' . $buildup->format(__('Y-m-d')) . '' ]); } - 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), - '%c', - '' . date(__('Y-m-d'), $event_config['event_start_date']) . '' + '%c', + '' . $start->format(__('Y-m-d')) . '' ]); } - 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), - '%c', - '' . date(__('Y-m-d'), $event_config['event_end_date']) . '' + '%c', + '' . $end->format(__('Y-m-d')) . '' ]); } - 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), - '%c', - '' . date(__('Y-m-d'), $event_config['teardown_end_date']) . '' + '%c', + '' . $teardown->format(__('Y-m-d')) . '' ]); } 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 @@ $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.'); } } -- cgit v1.2.3-54-g00ecf