engelsystem/includes/controller/user_worklog_controller.php

207 lines
4.5 KiB
PHP
Raw Normal View History

2017-12-29 17:19:27 +01:00
<?php
2018-10-09 21:47:31 +02:00
use Engelsystem\Models\User\User;
2017-12-29 17:19:27 +01:00
/**
* 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()
{
$request = request();
$userWorkLog = UserWorkLog($request->input('user_worklog_id'));
if (empty($userWorkLog)) {
2018-10-17 01:30:10 +02:00
redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
2018-10-09 21:47:31 +02:00
$user_source = User::find($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.'));
2018-10-09 21:47:31 +02:00
redirect(user_link($user_source->id));
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()
{
$request = request();
$userWorkLog = UserWorkLog($request->input('user_worklog_id'));
if (empty($userWorkLog)) {
2018-10-17 01:30:10 +02:00
redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
2018-10-10 03:10:28 +02:00
$user_source = User::find($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.'));
2018-10-10 03:10:28 +02:00
redirect(user_link($user_source->id));
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()
{
$request = request();
2018-10-10 03:10:28 +02:00
$user_source = User::find($request->input('user_id'));
if (!$user_source) {
2018-10-17 01:30:10 +02:00
redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2018-10-10 03:10:28 +02:00
$userWorkLog = UserWorkLog_new($user_source->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_create($userWorkLog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry created.'));
2018-10-10 03:10:28 +02:00
redirect(user_link($user_source->id));
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-10-10 03:10:28 +02:00
* @param User $user
2018-01-14 18:09:34 +01:00
*
* @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',
2018-10-10 03:10:28 +02:00
'user_id' => $user->id,
2017-12-29 17:19:27 +01:00
]);
}
/**
* 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
{
2018-10-09 21:47:31 +02:00
global $privileges;
$user = auth()->user();
2018-01-14 17:47:26 +01:00
2018-01-14 18:09:34 +01:00
if (!in_array('admin_user_worklog', $privileges)) {
2018-10-09 21:47:31 +02:00
redirect(user_link($user->id));
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')) {
2018-10-09 21:47:31 +02:00
redirect(user_link($user->id));
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();
}
}