summaryrefslogtreecommitdiff
path: root/includes/view/Shifts_view.php
blob: cbd0208d888546ac17080ed51210e5e883faae15 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php

use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState;
use Illuminate\Support\Collection;

/**
 * Renders the basic shift view header.
 *
 * @param array $shift
 * @param array $room
 * @return string HTML
 */
function Shift_view_header($shift, $room)
{
    return div('row', [
        div('col-sm-3 col-xs-6', [
            '<h4>' . __('Title') . '</h4>',
            '<p class="lead">'
            . ($shift['URL'] != ''
                ? '<a href="' . $shift['URL'] . '">' . $shift['title'] . '</a>'
                : $shift['title'])
            . '</p>'
        ]),
        div('col-sm-3 col-xs-6', [
            '<h4>' . __('Start') . '</h4>',
            '<p class="lead' . (time() >= $shift['start'] ? ' text-success' : '') . '">',
            glyph('calendar') . date(__('Y-m-d'), $shift['start']),
            '<br />',
            glyph('time') . date('H:i', $shift['start']),
            '</p>'
        ]),
        div('col-sm-3 col-xs-6', [
            '<h4>' . __('End') . '</h4>',
            '<p class="lead' . (time() >= $shift['end'] ? ' text-success' : '') . '">',
            glyph('calendar') . date(__('Y-m-d'), $shift['end']),
            '<br />',
            glyph('time') . date('H:i', $shift['end']),
            '</p>'
        ]),
        div('col-sm-3 col-xs-6', [
            '<h4>' . __('Location') . '</h4>',
            '<p class="lead">' . Room_name_render($room) . '</p>'
        ])
    ]);
}

/**
 * @param array $shift
 * @return string
 */
function Shift_editor_info_render($shift)
{
    $info = [];
    if (!empty($shift['created_by_user_id'])) {
        $info[] = sprintf(
            glyph('plus') . __('created at %s by %s'),
            date('Y-m-d H:i', $shift['created_at_timestamp']),
            User_Nick_render(User::find($shift['created_by_user_id']))
        );
    }
    if (!empty($shift['edited_by_user_id'])) {
        $info[] = sprintf(
            glyph('pencil') . __('edited at %s by %s'),
            date('Y-m-d H:i', $shift['edited_at_timestamp']),
            User_Nick_render(User::find($shift['edited_by_user_id']))
        );
    }
    return join('<br />', $info);
}

/**
 * @param array $shift
 * @param array $angeltype
 * @param array $user_angeltype
 * @return string
 */
function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null)
{
    if (empty($user_angeltype)) {
        $user_angeltype = UserAngelType_by_User_and_AngelType(auth()->user()->id, $angeltype);
    }

    if (isset($angeltype['shift_signup_state']) && $angeltype['shift_signup_state']->isSignupAllowed()) {
        return button(shift_entry_create_link($shift, $angeltype), __('Sign up'));
    } elseif (empty($user_angeltype)) {
        return button(
            page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]),
            sprintf(__('Become %s'),
                $angeltype['name'])
        );
    }
    return '';
}

/**
 * @param array            $shift
 * @param array            $shifttype
 * @param array            $room
 * @param array[]          $angeltypes_source
 * @param ShiftSignupState $shift_signup_state
 * @return string
 */
function Shift_view($shift, $shifttype, $room, $angeltypes_source, ShiftSignupState $shift_signup_state)
{
    $shift_admin = auth()->can('admin_shifts');
    $user_shift_admin = auth()->can('user_shifts_admin');
    $admin_rooms = auth()->can('admin_rooms');
    $admin_shifttypes = auth()->can('shifttypes');

    $parsedown = new Parsedown();

    $angeltypes = [];
    foreach ($angeltypes_source as $angeltype) {
        $angeltypes[$angeltype['id']] = $angeltype;
    }

    $needed_angels = '';
    $neededAngels = new Collection($shift['NeedAngels']);
    foreach ($neededAngels as $needed_angeltype) {
        $needed_angels .= Shift_view_render_needed_angeltype($needed_angeltype, $angeltypes, $shift, $user_shift_admin);
    }

    foreach ($shift['ShiftEntry'] as $shiftEntry) {
        if (!$neededAngels->where('TID', $shiftEntry['TID'])->first()) {
            $needed_angels .= Shift_view_render_needed_angeltype([
                'TID'        => $shiftEntry['TID'],
                'count'      => 0,
                'restricted' => true,
                'taken'      => true,
            ], $angeltypes, $shift, $user_shift_admin);
        }
    }

    $content = [msg()];

    if ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) {
        $content[] = info(__('This shift collides with one of your shifts.'), true);
    }

    if ($shift_signup_state->getState() == ShiftSignupState::SIGNED_UP) {
        $content[] = info(__('You are signed up for this shift.'), true);
    }

    if (config('signup_advance_hours') && $shift['start'] > time() + config('signup_advance_hours') * 3600) {
        $content[] = info(sprintf(
            __('This shift is in the far future and becomes available for signup at %s.'),
            date(__('Y-m-d') . ' H:i', $shift['start'] - config('signup_advance_hours') * 3600)
        ), true);
    }

    $buttons = [];
    if ($shift_admin || $admin_shifttypes || $admin_rooms) {
        $buttons = [
            $shift_admin ? button(shift_edit_link($shift), glyph('pencil') . __('edit')) : '',
            $shift_admin ? button(shift_delete_link($shift), glyph('trash') . __('delete')) : '',
            $admin_shifttypes ? button(shifttype_link($shifttype), $shifttype['name']) : '',
            $admin_rooms ? button(room_link($room), glyph('map-marker') . $room['Name']) : '',
        ];
    }
    $buttons[] = button(user_link(auth()->user()->id), '<span class="icon-icon_angel"></span> ' . __('My shifts'));
    $content[] = buttons($buttons);

    $content[] = Shift_view_header($shift, $room);
    $content[] = div('row', [
        div('col-sm-6', [
            '<h2>' . __('Needed angels') . '</h2>',
            '<div class="list-group">' . $needed_angels . '</div>'
        ]),
        div('col-sm-6', [
            '<h2>' . __('Description') . '</h2>',
            $parsedown->parse($shifttype['description'])
        ])
    ]);

    if ($shift_admin) {
        $content[] = Shift_editor_info_render($shift);
    }

    return page_with_title(
        $shift['name'] . ' <small class="moment-countdown" data-timestamp="' . $shift['start'] . '">%c</small>',
        $content
    );
}

/**
 * @param array   $needed_angeltype
 * @param array   $angeltypes
 * @param array[] $shift
 * @param bool    $user_shift_admin
 * @return string
 */
function Shift_view_render_needed_angeltype($needed_angeltype, $angeltypes, $shift, $user_shift_admin)
{
    $angeltype = $angeltypes[$needed_angeltype['TID']];
    $angeltype_supporter = User_is_AngelType_supporter(auth()->user(), $angeltype);

    $needed_angels = '';

    $class = 'progress-bar-warning';
    if ($needed_angeltype['taken'] == 0) {
        $class = 'progress-bar-danger';
    }
    if ($needed_angeltype['taken'] >= $needed_angeltype['count']) {
        $class = 'progress-bar-success';
    }
    $needed_angels .= '<div class="list-group-item">';

    $needed_angels .= '<div class="pull-right">' . Shift_signup_button_render($shift, $angeltype) . '</div>';

    $needed_angels .= '<h3>' . AngelType_name_render($angeltype) . '</h3>';
    $bar_max = max($needed_angeltype['count'] * 10, $needed_angeltype['taken'] * 10, 10);
    $bar_value = max($bar_max / 10, $needed_angeltype['taken'] * 10);
    $needed_angels .= progress_bar(
        0,
        $bar_max,
        $bar_value,
        $class,
        $needed_angeltype['taken'] . ' / ' . $needed_angeltype['count']
    );

    $angels = [];
    foreach ($shift['ShiftEntry'] as $shift_entry) {
        if ($shift_entry['TID'] == $needed_angeltype['TID']) {
            $angels[] = Shift_view_render_shift_entry($shift_entry, $user_shift_admin, $angeltype_supporter);
        }
    }

    $needed_angels .= join(', ', $angels);
    $needed_angels .= '</div>';

    return $needed_angels;
}

/**
 * @param array $shift_entry
 * @param bool  $user_shift_admin
 * @param bool  $angeltype_supporter
 * @return string
 */
function Shift_view_render_shift_entry($shift_entry, $user_shift_admin, $angeltype_supporter)
{
    $entry = User_Nick_render(User::find($shift_entry['UID']));
    if ($shift_entry['freeloaded']) {
        $entry = '<del>' . $entry . '</del>';
    }
    $isUser = $shift_entry['UID'] == auth()->user()->id;
    if ($user_shift_admin || $angeltype_supporter || $isUser) {
        $entry .= ' <div class="btn-group">';
        if ($user_shift_admin || $isUser) {
            $entry .= button_glyph(
                page_link_to('user_myshifts', ['edit' => $shift_entry['id'], 'id' => $shift_entry['UID']]),
                'pencil',
                'btn-xs'
            );
        }
        $entry .= button_glyph(shift_entry_delete_link($shift_entry), 'trash', 'btn-xs');
        $entry .= '</div>';
    }
    return $entry;
}

/**
 * Calc shift length in format 12:23h.
 *
 * @param array $shift
 * @return string
 */
function shift_length($shift)
{
    $length = floor(($shift['end'] - $shift['start']) / (60 * 60)) . ':';
    $length .= str_pad(
            (($shift['end'] - $shift['start']) % (60 * 60)) / 60,
            2,
            '0',
            STR_PAD_LEFT
        ) . 'h';
    return $length;
}