summaryrefslogtreecommitdiff
path: root/includes/controller/user_worklog_controller.php
blob: 4eaa5e91beee2c8cb077f70d52363c235599c7da (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
<?php

use Engelsystem\Models\User\User;

/**
 * Delete a work log entry.
 *
 * @return array
 */
function user_worklog_delete_controller()
{
    $request = request();
    $userWorkLog = UserWorkLog($request->input('user_worklog_id'));
    if (empty($userWorkLog)) {
        redirect(user_link(auth()->user()->id));
    }
    $user_source = User::find($userWorkLog['user_id']);

    if ($request->hasPostData('submit')) {
        UserWorkLog_delete($userWorkLog);

        success(__('Work log entry deleted.'));
        redirect(user_link($user_source->id));
    }

    return [
        UserWorkLog_delete_title(),
        UserWorkLog_delete_view($user_source)
    ];
}

/**
 * Edit work log for user.
 *
 * @return array
 */
function user_worklog_edit_controller()
{
    $request = request();
    $userWorkLog = UserWorkLog($request->input('user_worklog_id'));
    if (empty($userWorkLog)) {
        redirect(user_link(auth()->user()->id));
    }
    $user_source = User::find($userWorkLog['user_id']);

    if ($request->hasPostData('submit')) {
        list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);

        if ($valid) {
            UserWorkLog_update($userWorkLog);

            success(__('Work log entry updated.'));
            redirect(user_link($user_source->id));
        }
    }

    return [
        UserWorkLog_edit_title(),
        UserWorkLog_edit_view($user_source, $userWorkLog)
    ];
}

/**
 * Handle form
 *
 * @param array $userWorkLog
 * @return array [bool $valid, UserWorkLog $userWorkLog]
 */
function user_worklog_from_request($userWorkLog)
{
    $request = request();

    $valid = true;

    $userWorkLog['work_timestamp'] = parse_date(
        'Y-m-d H:i',
        $request->input('work_timestamp') . ' 00:00'
    );
    if (is_null($userWorkLog['work_timestamp'])) {
        $valid = false;
        error(__('Please enter work date.'));
    }

    $userWorkLog['work_hours'] = $request->input('work_hours');
    if (!preg_match("/[0-9]+(\.[0-9]+)?/", $userWorkLog['work_hours'])) {
        $valid = false;
        error(__('Please enter work hours in format ##[.##].'));
    }

    $userWorkLog['comment'] = $request->input('comment');
    if (empty($userWorkLog['comment'])) {
        $valid = false;
        error(__('Please enter a comment.'));
    }

    return [
        $valid,
        $userWorkLog
    ];
}

/**
 * Add work log entry to user.
 *
 * @return array
 */
function user_worklog_add_controller()
{
    $request = request();
    $user_source = User::find($request->input('user_id'));
    if (!$user_source) {
        redirect(user_link(auth()->user()->id));
    }

    $userWorkLog = UserWorkLog_new($user_source->id);

    if ($request->hasPostData('submit')) {
        list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);

        if ($valid) {
            UserWorkLog_create($userWorkLog);

            success(__('Work log entry created.'));
            redirect(user_link($user_source->id));
        }
    }

    return [
        UserWorkLog_add_title(),
        UserWorkLog_add_view($user_source, $userWorkLog)
    ];
}

/**
 * Link to work log entry add for given user.
 *
 * @param User $user
 *
 * @return string
 */
function user_worklog_add_link($user)
{
    return page_link_to('user_worklog', [
        'action'  => 'add',
        'user_id' => $user->id,
    ]);
}

/**
 * Link to work log entry edit.
 *
 * @param array $userWorkLog
 * @return string
 */
function user_worklog_edit_link($userWorkLog)
{
    return page_link_to('user_worklog', [
        'action'          => 'edit',
        'user_worklog_id' => $userWorkLog['id']
    ]);
}

/**
 * Link to work log entry delete.
 *
 * @param array   $userWorkLog
 * @param array[] $parameters
 * @return string
 */
function user_worklog_delete_link($userWorkLog, $parameters = [])
{
    return page_link_to('user_worklog', array_merge([
        'action'          => 'delete',
        'user_worklog_id' => $userWorkLog['id']
    ], $parameters));
}

/**
 * Work log entry actions
 *
 * @return array
 */
function user_worklog_controller()
{
    global $privileges;
    $user = auth()->user();

    if (!in_array('admin_user_worklog', $privileges)) {
        redirect(user_link($user->id));
    }

    $request = request();
    $action = $request->input('action');
    if (!$request->has('action')) {
        redirect(user_link($user->id));
    }

    switch ($action) {
        case 'add':
            return user_worklog_add_controller();
        case 'edit':
            return user_worklog_edit_controller();
        case 'delete':
            return user_worklog_delete_controller();
    }
}