engelsystem/includes/controller/user_worklog_controller.php

218 lines
4.9 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;
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();
2020-09-12 19:45:25 +02:00
$userWorkLog = Worklog::find($request->input('user_worklog_id'));
2017-12-29 17:19:27 +01:00
if (empty($userWorkLog)) {
2019-09-08 02:25:49 +02:00
throw_redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
2020-09-12 19:45:25 +02:00
$user_source = $userWorkLog->user;
2018-01-14 17:47:26 +01:00
if ($request->hasPostData('submit')) {
2017-12-29 17:19:27 +01:00
UserWorkLog_delete($userWorkLog);
2018-01-14 17:47:26 +01:00
success(__('Work log entry deleted.'));
2019-09-08 02:25:49 +02:00
throw_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)
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();
2020-09-12 19:45:25 +02:00
$userWorkLog = Worklog::find($request->input('user_worklog_id'));
2017-12-29 17:19:27 +01:00
if (empty($userWorkLog)) {
2019-09-08 02:25:49 +02:00
throw_redirect(user_link(auth()->user()->id));
2017-12-29 17:19:27 +01:00
}
2020-09-12 19:45:25 +02:00
$user_source = $userWorkLog->user;
2018-01-14 17:47:26 +01:00
if ($request->hasPostData('submit')) {
2017-12-29 17:19:27 +01:00
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) {
2020-09-12 19:45:25 +02:00
$userWorkLog->save();
engelsystem_log(sprintf(
'Updated work log for %s, %s hours, %s',
User_Nick_render($userWorkLog->user, true),
$userWorkLog->hours,
$userWorkLog->comment
));
2018-01-14 17:47:26 +01:00
success(__('Work log entry updated.'));
2019-09-08 02:25:49 +02:00
throw_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
*
2020-09-12 19:45:25 +02:00
* @param Worklog $worklog
* @return bool[]|Worklog[] [bool $valid, Worklog $userWorkLog]
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("/^[0-9]+(\.[0-9]{0,2})?$/", $worklog->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();
2018-10-10 03:10:28 +02:00
$user_source = User::find($request->input('user_id'));
if (!$user_source) {
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
2018-10-10 03:10:28 +02:00
$userWorkLog = UserWorkLog_new($user_source->id);
2018-01-14 17:47:26 +01:00
if ($request->hasPostData('submit')) {
2017-12-29 17:19:27 +01:00
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.'));
2019-09-08 02:25:49 +02:00
throw_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
*/
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.
*
2020-09-12 19:45:25 +02:00
* @param Worklog $userWorkLog
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_edit_link(Worklog $userWorkLog)
2017-12-29 17:19:27 +01:00
{
return page_link_to('user_worklog', [
2018-01-14 18:09:34 +01:00
'action' => 'edit',
2020-09-12 19:45:25 +02:00
'user_worklog_id' => $userWorkLog->id
2017-12-29 17:19:27 +01:00
]);
}
/**
* Link to work log entry delete.
*
2020-09-12 19:45:25 +02:00
* @param Worklog $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
*/
2020-09-12 19:45:25 +02:00
function user_worklog_delete_link(Worklog $userWorkLog, $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',
2020-09-12 19:45:25 +02:00
'user_worklog_id' => $userWorkLog->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
}