engelsystem/includes/controller/user_worklog_controller.php

218 lines
4.7 KiB
PHP
Raw Permalink 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;
2020-09-12 19:45:25 +02:00
use Engelsystem\Models\Worklog;
2018-10-09 21:47:31 +02:00
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();
$worklog = Worklog::find($request->input('user_worklog_id'));
if (empty($worklog)) {
2019-09-08 02:25:49 +02:00
throw_redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
$user = $worklog->user;
2018-01-14 17:47:26 +01:00
if ($request->hasPostData('submit')) {
UserWorkLog_delete($worklog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry deleted.'));
throw_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
return [
UserWorkLog_delete_title(),
UserWorkLog_delete_view($user)
2017-12-29 17:19:27 +01:00
];
}
/**
* 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();
$worklog = Worklog::find($request->input('user_worklog_id'));
if (empty($worklog)) {
2019-09-08 02:25:49 +02:00
throw_redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
$user = $worklog->user;
2018-01-14 17:47:26 +01:00
if ($request->hasPostData('submit')) {
list ($valid, $worklog) = user_worklog_from_request($worklog);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($valid) {
$worklog->save();
2020-09-12 19:45:25 +02:00
engelsystem_log(sprintf(
'Updated work log for %s, %s hours, %s',
User_Nick_render($worklog->user, true),
$worklog->hours,
$worklog->comment
2020-09-12 19:45:25 +02:00
));
2018-01-14 17:47:26 +01:00
success(__('Work log entry updated.'));
throw_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
return [
UserWorkLog_edit_title(),
UserWorkLog_edit_view($user, $worklog)
2017-12-29 17:19:27 +01:00
];
}
/**
2018-01-14 18:09:34 +01:00
* Handle form
2017-12-29 17:19:27 +01:00
*
2020-09-12 19:45:25 +02:00
* @param Worklog $worklog
* @return bool[]|Worklog[] [bool $valid, Worklog $worklog]
2017-12-29 17:19:27 +01:00
*/
2020-09-12 19:45:25 +02:00
function user_worklog_from_request(Worklog $worklog)
2017-12-29 17:19:27 +01:00
{
$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
2020-09-12 19:45:25 +02:00
$worklog->worked_at = DateTime::createFromFormat('Y-m-d H:i', $request->input('work_timestamp') . ' 00:00');
if (!$worklog->worked_at) {
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
2020-09-12 19:45:25 +02:00
$worklog->hours = $request->input('work_hours');
if (!preg_match("/^\d+(\.\d{0,2})?$/", $request->input('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
2020-09-12 19:45:25 +02:00
$worklog->comment = $request->input('comment');
if (empty($worklog->comment)) {
2017-12-29 17:19:27 +01:00
$valid = false;
error(__('Please enter a comment.'));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
2020-09-12 19:45:25 +02:00
if (mb_strlen($worklog->comment) > 200) {
$valid = false;
error(__('Comment too long.'));
}
2017-12-29 17:19:27 +01:00
return [
$valid,
2020-09-12 19:45:25 +02:00
$worklog
2017-12-29 17:19:27 +01:00
];
}
/**
* 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();
$user = User::find($request->input('user_id'));
if (!$user) {
2019-09-08 02:25:49 +02:00
throw_redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
2018-01-14 17:47:26 +01:00
$worklog = UserWorkLog_new($user->id);
2018-01-14 17:47:26 +01:00
if ($request->hasPostData('submit')) {
list ($valid, $worklog) = user_worklog_from_request($worklog);
2018-01-14 17:47:26 +01:00
2017-12-29 17:19:27 +01:00
if ($valid) {
UserWorkLog_create($worklog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry created.'));
throw_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
return [
UserWorkLog_add_title(),
UserWorkLog_add_view($user, $worklog)
2017-12-29 17:19:27 +01:00
];
}
/**
* 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
*/
2020-09-12 19:45:25 +02:00
function user_worklog_add_link(User $user)
2017-12-29 17:19:27 +01:00
{
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.
*
* @param Worklog $worklog
2018-01-14 18:09:34 +01:00
* @return string
2017-12-29 17:19:27 +01:00
*/
function user_worklog_edit_link(Worklog $worklog)
2017-12-29 17:19:27 +01:00
{
return page_link_to('user_worklog', [
2018-01-14 18:09:34 +01:00
'action' => 'edit',
'user_worklog_id' => $worklog->id
2017-12-29 17:19:27 +01:00
]);
}
/**
* Link to work log entry delete.
*
* @param Worklog $worklog
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(Worklog $worklog, $parameters = [])
2017-12-29 17:19:27 +01:00
{
return page_link_to('user_worklog', array_merge([
2018-01-14 18:09:34 +01:00
'action' => 'delete',
'user_worklog_id' => $worklog->id
2017-12-29 17:19:27 +01:00
], $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
$user = auth()->user();
2018-01-14 17:47:26 +01:00
if (!auth()->can('admin_user_worklog')) {
2019-09-08 02:25:49 +02:00
throw_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')) {
2019-09-08 02:25:49 +02:00
throw_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();
}
2020-09-12 19:45:25 +02:00
return ['', ''];
2017-12-29 17:19:27 +01:00
}