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-13 14:31:21 +02:00
|
|
|
$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
|
|
|
}
|
2020-09-13 14:31:21 +02:00
|
|
|
$user = $worklog->user;
|
2018-01-14 17:47:26 +01:00
|
|
|
|
2018-11-20 16:02:03 +01:00
|
|
|
if ($request->hasPostData('submit')) {
|
2020-09-13 14:31:21 +02:00
|
|
|
UserWorkLog_delete($worklog);
|
2018-01-14 17:47:26 +01:00
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Work log entry deleted.'));
|
2020-09-13 14:31:21 +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
|
|
|
return [
|
|
|
|
UserWorkLog_delete_title(),
|
2020-09-13 14:31:21 +02:00
|
|
|
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();
|
2020-09-13 14:31:21 +02:00
|
|
|
$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
|
|
|
}
|
2020-09-13 14:31:21 +02:00
|
|
|
$user = $worklog->user;
|
2018-01-14 17:47:26 +01:00
|
|
|
|
2018-11-20 16:02:03 +01:00
|
|
|
if ($request->hasPostData('submit')) {
|
2020-09-13 14:31:21 +02:00
|
|
|
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) {
|
2020-09-13 14:31:21 +02:00
|
|
|
$worklog->save();
|
2020-09-12 19:45:25 +02:00
|
|
|
|
|
|
|
engelsystem_log(sprintf(
|
|
|
|
'Updated work log for %s, %s hours, %s',
|
2020-09-13 14:31:21 +02:00
|
|
|
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
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Work log entry updated.'));
|
2020-09-13 14:31:21 +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
|
|
|
return [
|
|
|
|
UserWorkLog_edit_title(),
|
2020-09-13 14:31:21 +02:00
|
|
|
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
|
2020-09-13 14:31:21 +02:00
|
|
|
* @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;
|
2018-08-29 21:55:32 +02:00
|
|
|
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');
|
2022-06-16 23:00:56 +02:00
|
|
|
if (!preg_match("/^\d+(\.\d{0,2})?$/", $worklog->hours)) {
|
2017-12-29 17:19:27 +01:00
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
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;
|
2018-08-29 21:55:32 +02:00
|
|
|
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) {
|
2020-01-09 13:43:10 +01:00
|
|
|
$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();
|
2020-09-13 14:31:21 +02:00
|
|
|
$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
|
|
|
|
2020-09-13 14:31:21 +02:00
|
|
|
$worklog = UserWorkLog_new($user->id);
|
2018-01-14 17:47:26 +01:00
|
|
|
|
2018-11-20 16:02:03 +01:00
|
|
|
if ($request->hasPostData('submit')) {
|
2020-09-13 14:31:21 +02:00
|
|
|
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) {
|
2020-09-13 14:31:21 +02:00
|
|
|
UserWorkLog_create($worklog);
|
2018-01-14 17:47:26 +01:00
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Work log entry created.'));
|
2020-09-13 14:31:21 +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
|
|
|
return [
|
|
|
|
UserWorkLog_add_title(),
|
2020-09-13 14:31:21 +02:00
|
|
|
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.
|
|
|
|
*
|
2020-09-13 14:31:21 +02:00
|
|
|
* @param Worklog $worklog
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return string
|
2017-12-29 17:19:27 +01:00
|
|
|
*/
|
2020-09-13 14:31:21 +02: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',
|
2020-09-13 14:31:21 +02:00
|
|
|
'user_worklog_id' => $worklog->id
|
2017-12-29 17:19:27 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link to work log entry delete.
|
|
|
|
*
|
2020-09-13 14:31:21 +02:00
|
|
|
* @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
|
|
|
*/
|
2020-09-13 14:31:21 +02: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',
|
2020-09-13 14:31:21 +02:00
|
|
|
'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
|
|
|
|
2018-11-12 14:41:23 +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
|
|
|
}
|