summaryrefslogtreecommitdiff
path: root/includes/controller/event_config_controller.php
blob: e9b27cba520246d6418ccd59128ac4daf47685d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

use Carbon\Carbon;
use Engelsystem\Models\EventConfig;

/**
 * @return string
 */
function event_config_title()
{
    return __('Event config');
}

/**
 * @return array
 */
function event_config_edit_controller()
{
    global $privileges;

    if (!in_array('admin_event_config', $privileges)) {
        redirect(page_link_to('/'));
    }

    $request = request();
    $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->hasPostData('submit')) {
        $valid = true;

        if ($request->has('event_name')) {
            $event_name = strip_request_item('event_name');
        }
        if ($event_name == '') {
            $event_name = null;
        }

        if ($request->has('event_welcome_msg')) {
            $event_welcome_msg = strip_request_item_nl('event_welcome_msg');
        }
        if ($event_welcome_msg == '') {
            $event_welcome_msg = null;
        }

        $result = check_request_date('buildup_start_date', __('Please enter buildup start date.'), true);
        $buildup_start_date = $result->getValue();
        $valid &= $result->isValid();

        $result = check_request_date('event_start_date', __('Please enter event start date.'), true);
        $event_start_date = $result->getValue();
        $valid &= $result->isValid();

        $result = check_request_date('event_end_date', __('Please enter event end date.'), true);
        $event_end_date = $result->getValue();
        $valid &= $result->isValid();

        $result = check_request_date('teardown_end_date', __('Please enter teardown end date.'), true);
        $teardown_end_date = $result->getValue();
        $valid &= $result->isValid();

        if (!is_null($buildup_start_date) && !is_null($event_start_date) && $buildup_start_date > $event_start_date) {
            $valid = false;
            error(__('The buildup start date has to be before the event start date.'));
        }

        if (!is_null($event_start_date) && !is_null($event_end_date) && $event_start_date > $event_end_date) {
            $valid = false;
            error(__('The event start date has to be before the event end date.'));
        }

        if (!is_null($event_end_date) && !is_null($teardown_end_date) && $event_end_date > $teardown_end_date) {
            $valid = false;
            error(__('The event end date has to be before the teardown end date.'));
        }

        if (!is_null($buildup_start_date) && !is_null($teardown_end_date) && $buildup_start_date > $teardown_end_date) {
            $valid = false;
            error(__('The buildup start date has to be before the teardown end date.'));
        }

        if ($valid) {
            $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,
                    $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.'));
            redirect(page_link_to('admin_event_config'));
        }
    }

    return [
        event_config_title(),
        EventConfig_edit_view(
            $event_name,
            $event_welcome_msg,
            $buildup_start_date,
            $event_start_date,
            $event_end_date,
            $teardown_end_date
        )
    ];
}