engelsystem/includes/controller/user_worklog_controller.php

210 lines
4.5 KiB
PHP
Raw Normal View History

2017-12-29 17:19:27 +01:00
<?php
/**
* Delete a work log entry.
2018-01-14 18:09:34 +01:00
*
* @return array
2017-12-29 17:19:27 +01:00
*/
function user_worklog_delete_controller()
{
global $user;
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$request = request();
$userWorkLog = UserWorkLog($request->input('user_worklog_id'));
if (empty($userWorkLog)) {
redirect(user_link($user['UID']));
2017-12-29 17:19:27 +01:00
}
$user_source = User($userWorkLog['user_id']);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($request->has('confirmed')) {
UserWorkLog_delete($userWorkLog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry deleted.'));
redirect(user_link($user_source['UID']));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
return [
UserWorkLog_delete_title(),
UserWorkLog_delete_view($user_source, $userWorkLog)
];
}
/**
* Edit work log for user.
2018-01-14 18:09:34 +01:00
*
* @return array
2017-12-29 17:19:27 +01:00
*/
function user_worklog_edit_controller()
{
global $user;
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$request = request();
$userWorkLog = UserWorkLog($request->input('user_worklog_id'));
if (empty($userWorkLog)) {
redirect(user_link($user['UID']));
2017-12-29 17:19:27 +01:00
}
$user_source = User($userWorkLog['user_id']);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($request->has('submit')) {
list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($valid) {
UserWorkLog_update($userWorkLog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry updated.'));
redirect(user_link($user_source['UID']));
2017-12-29 17:19:27 +01:00
}
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
return [
UserWorkLog_edit_title(),
UserWorkLog_edit_view($user_source, $userWorkLog)
];
}
/**
2018-01-14 18:09:34 +01:00
* Handle form
2017-12-29 17:19:27 +01:00
*
2018-01-14 18:09:34 +01:00
* @param array $userWorkLog
* @return array [bool $valid, UserWorkLog $userWorkLog]
2017-12-29 17:19:27 +01:00
*/
function user_worklog_from_request($userWorkLog)
{
$request = request();
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$valid = true;
2018-01-14 17:47:26 +01:00
2018-01-14 18:09:34 +01:00
$userWorkLog['work_timestamp'] = parse_date(
'Y-m-d H:i',
$request->input('work_timestamp') . ' 00:00'
);
2018-01-14 17:47:26 +01:00
if (is_null($userWorkLog['work_timestamp'])) {
2017-12-29 18:57:11 +01:00
$valid = false;
error(__('Please enter work date.'));
2017-12-29 18:57:11 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$userWorkLog['work_hours'] = $request->input('work_hours');
2018-01-14 18:09:34 +01:00
if (!preg_match("/[0-9]+(\.[0-9]+)?/", $userWorkLog['work_hours'])) {
2017-12-29 17:19:27 +01:00
$valid = false;
error(__('Please enter work hours in format ##[.##].'));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$userWorkLog['comment'] = $request->input('comment');
if (empty($userWorkLog['comment'])) {
$valid = false;
error(__('Please enter a comment.'));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
return [
$valid,
$userWorkLog
];
}
/**
* Add work log entry to user.
2018-01-14 18:09:34 +01:00
*
* @return array
2017-12-29 17:19:27 +01:00
*/
function user_worklog_add_controller()
{
global $user;
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$request = request();
$user_source = User($request->input('user_id'));
if (empty($user_source)) {
redirect(user_link($user['UID']));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$userWorkLog = UserWorkLog_new($user_source);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($request->has('submit')) {
list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($valid) {
UserWorkLog_create($userWorkLog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry created.'));
redirect(user_link($user_source['UID']));
2017-12-29 17:19:27 +01:00
}
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
return [
UserWorkLog_add_title(),
UserWorkLog_add_view($user_source, $userWorkLog)
];
}
/**
* Link to work log entry add for given user.
*
2018-01-14 18:09:34 +01:00
* @param array $user
*
* @return string
2017-12-29 17:19:27 +01:00
*/
function user_worklog_add_link($user)
{
return page_link_to('user_worklog', [
2018-01-14 18:09:34 +01:00
'action' => 'add',
2017-12-29 17:19:27 +01:00
'user_id' => $user['UID']
]);
}
/**
* Link to work log entry edit.
*
2018-01-14 18:09:34 +01:00
* @param array $userWorkLog
* @return string
2017-12-29 17:19:27 +01:00
*/
function user_worklog_edit_link($userWorkLog)
{
return page_link_to('user_worklog', [
2018-01-14 18:09:34 +01:00
'action' => 'edit',
2017-12-29 17:19:27 +01:00
'user_worklog_id' => $userWorkLog['id']
]);
}
/**
* Link to work log entry delete.
*
2018-01-14 18:09:34 +01:00
* @param array $userWorkLog
2018-01-14 17:47:26 +01:00
* @param array[] $parameters
2018-01-14 18:09:34 +01:00
* @return string
2017-12-29 17:19:27 +01:00
*/
function user_worklog_delete_link($userWorkLog, $parameters = [])
{
return page_link_to('user_worklog', array_merge([
2018-01-14 18:09:34 +01:00
'action' => 'delete',
2017-12-29 17:19:27 +01:00
'user_worklog_id' => $userWorkLog['id']
], $parameters));
}
/**
* Work log entry actions
2018-01-14 18:09:34 +01:00
*
* @return array
2017-12-29 17:19:27 +01:00
*/
2018-01-14 18:09:34 +01:00
function user_worklog_controller()
2017-12-29 17:19:27 +01:00
{
global $user, $privileges;
2018-01-14 17:47:26 +01:00
2018-01-14 18:09:34 +01:00
if (!in_array('admin_user_worklog', $privileges)) {
redirect(user_link($user['UID']));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
$request = request();
$action = $request->input('action');
2018-01-14 18:09:34 +01:00
if (!$request->has('action')) {
redirect(user_link($user['UID']));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
switch ($action) {
case 'add':
return user_worklog_add_controller();
case 'edit':
return user_worklog_edit_controller();
case 'delete':
return user_worklog_delete_controller();
}
}