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

/**
 * Delete a work log entry.
 */
function user_worklog_delete_controller()
{
    global $user;
    
    $request = request();
    $userWorkLog = UserWorkLog($request->input('user_worklog_id'));
    if (empty($userWorkLog)) {
        redirect(user_link($user));
    }
    $user_source = User($userWorkLog['user_id']);
    
    if ($request->has('confirmed')) {
        UserWorkLog_delete($userWorkLog);
        
        success(_('Work log entry deleted.'));
        redirect(user_link($user_source));
    }
    
    return [
        UserWorkLog_delete_title(),
        UserWorkLog_delete_view($user_source, $userWorkLog)
    ];
}

/**
 * Edit work log for user.
 */
function user_worklog_edit_controller()
{
    global $user;
    
    $request = request();
    $userWorkLog = UserWorkLog($request->input('user_worklog_id'));
    if (empty($userWorkLog)) {
        redirect(user_link($user));
    }
    $user_source = User($userWorkLog['user_id']);
    
    if ($request->has('submit')) {
        list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);
        
        if ($valid) {
            UserWorkLog_update($userWorkLog);
            
            success(_('Work log entry updated.'));
            redirect(user_link($user_source));
        }
    }
    
    return [
        UserWorkLog_edit_title(),
        UserWorkLog_edit_view($user_source, $userWorkLog)
    ];
}

/**
 *
 * @param UserWorkLog $userWorkLog            
 * @return [bool $valid, UserWorkLog $userWorkLog]
 */
function user_worklog_from_request($userWorkLog)
{
    $request = request();
    
    $valid = true;
    
    $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.
 */
function user_worklog_add_controller()
{
    global $user;
    
    $request = request();
    $user_source = User($request->input('user_id'));
    if (empty($user_source)) {
        redirect(user_link($user));
    }
    
    $userWorkLog = UserWorkLog_new($user_source);
    
    if ($request->has('submit')) {
        list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);
        
        if ($valid) {
            UserWorkLog_create($userWorkLog);
            
            success(_('Work log entry created.'));
            redirect(user_link($user_source));
        }
    }
    
    return [
        UserWorkLog_add_title(),
        UserWorkLog_add_view($user_source, $userWorkLog)
    ];
}

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

/**
 * Link to work log entry edit.
 *
 * @param UserWorkLog $userWorkLog            
 */
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 UserWorkLog $userWorkLog        
 * @param array[] $parameters    
 */
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
 */
function user_worklogs_controller()
{
    global $user, $privileges;
    
    if (! in_array('admin_user_worklog', $privileges)) {
        redirect(user_link($user));
    }
    
    $request = request();
    $action = $request->input('action');
    if (! $request->has('action')) {
        redirect(user_link($user));
    }
    
    switch ($action) {
        case 'add':
            return user_worklog_add_controller();
        case 'edit':
            return user_worklog_edit_controller();
        case 'delete':
            return user_worklog_delete_controller();
    }
}

?>