Refactor task management and add attachment functionality
- Updated task editing template to handle default status for new tasks and corrected variable names. - Enhanced work time reporting by rounding time to the nearest quarter hour and adjusting amount formatting. - Introduced TasksController to manage task-related operations, including status resolution and email notifications. - Added TaskAttachmentRepository for handling task attachments, including upload, rename, and delete functionalities. - Implemented WorkTimeRepository to fetch clients with unsettled tasks and calculate total work time. - Created unit tests for TasksController and TaskAttachmentRepository to ensure functionality and correctness.
This commit is contained in:
@@ -328,29 +328,12 @@ class Tasks
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use \Controllers\TasksController::taskChangeStatus() instead.
|
||||
*/
|
||||
static public function task_change_status()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( $mdb -> update( 'tasks', [ 'status' => \S::get( 'status' ) ], [ 'id' => \S::get( 'task_id' ) ] ) )
|
||||
{
|
||||
if ( \S::get( 'status' ) == 3 or \S::get( 'status' ) == 1 )
|
||||
{
|
||||
self::send_email_task_change_status( \S::get( 'task_id' ) );
|
||||
}
|
||||
|
||||
if ( \S::get( 'status' ) == 2 )
|
||||
{
|
||||
$mdb -> update( 'tasks', [ 'date_complete' => date( 'Y-m-d H:i:s' ) ], [ 'id' => \S::get( 'task_id' ) ] );
|
||||
}
|
||||
|
||||
echo json_encode( [ 'status' => 'success' ] );
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode( [ 'status' => 'error' ] );
|
||||
}
|
||||
exit;
|
||||
return \Controllers\TasksController::taskChangeStatus();
|
||||
}
|
||||
|
||||
static public function task_end()
|
||||
@@ -385,17 +368,19 @@ class Tasks
|
||||
exit;
|
||||
}
|
||||
|
||||
$task = \factory\Tasks::task_details( \S::get( 'task_id' ) );
|
||||
$task['status'] = \Controllers\TasksController::resolveTaskStatusForForm( $task );
|
||||
|
||||
return \Tpl::view( 'tasks/task_edit', [
|
||||
'projects' => \factory\Projects::user_projects( $user['id'] ),
|
||||
'priorities' => \factory\Tasks::$priorities,
|
||||
'task' => \factory\Tasks::task_details( \S::get( 'task_id' ) ),
|
||||
'task' => $task,
|
||||
'parent_tasks' => \factory\Tasks::parent_tasks( $user['id'] ),
|
||||
'users' => \factory\Users::users_list(),
|
||||
'clients' => \factory\Crm::get_client_list(),
|
||||
'user' => $user
|
||||
] );
|
||||
}
|
||||
|
||||
static public function task_save()
|
||||
{
|
||||
global $user;
|
||||
@@ -408,9 +393,10 @@ class Tasks
|
||||
|
||||
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania zadania wystąpił błąd. Proszę spróbować ponownie.' ];
|
||||
$values = \S::json_to_array( \S::get( 'values' ) );
|
||||
$status = \Controllers\TasksController::resolveTaskStatusForSave( $values );
|
||||
|
||||
if ( $id = \factory\Tasks::task_save(
|
||||
$values['id'], null, $user['id'], $values['name'], $values['text'], $values['date_start'], $values['date_end'], $values['project_id'], $values['client_id'], $values['pay_rate'], $values['reminders_interval'], $values['recursively'], $values['frequency'], $values['period'], $values['users'], null, null, $values['send_email_notification'], $values['status_change_mail'], false, $values['status'], $values['show_in_calendar'], $values['priority']
|
||||
$values['id'], null, $user['id'], $values['name'], $values['text'], $values['date_start'], $values['date_end'], $values['project_id'], $values['client_id'], $values['pay_rate'], $values['reminders_interval'], $values['recursively'], $values['frequency'], $values['period'], $values['users'], null, null, $values['send_email_notification'], $values['status_change_mail'], false, $status, $values['show_in_calendar'], $values['priority']
|
||||
) )
|
||||
{
|
||||
\factory\Tasks::clear_task_opened( $id );
|
||||
@@ -420,7 +406,6 @@ class Tasks
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function task_popup()
|
||||
{
|
||||
global $user;
|
||||
@@ -431,11 +416,14 @@ class Tasks
|
||||
exit;
|
||||
}
|
||||
|
||||
$attachments_repository = new \Domain\Tasks\TaskAttachmentRepository();
|
||||
|
||||
\factory\Tasks::set_task_opened_by_user( \S::get( 'task_id' ), $user['id'] );
|
||||
|
||||
echo \Tpl::view( 'tasks/task_popup', [
|
||||
'task' => \factory\Tasks::task_details( \S::get( 'task_id' ), $user['id'] ),
|
||||
'task_works' => \factory\Tasks::task_works( \S::get( 'task_id' ) ),
|
||||
'task_attachments' => $attachments_repository -> listByTaskId( \S::get( 'task_id' ) ),
|
||||
'user' => $user,
|
||||
'statuses' => \factory\Tasks::get_statuses(),
|
||||
'projects' => \factory\Projects::user_projects( $user['id'] )
|
||||
@@ -443,6 +431,116 @@ class Tasks
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function task_attachment_upload()
|
||||
{
|
||||
global $user;
|
||||
|
||||
if ( !$user )
|
||||
{
|
||||
header( 'Location: /logowanie' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$files_input = isset( $_FILES['attachments'] ) ? $_FILES['attachments'] : ( isset( $_FILES['attachment'] ) ? $_FILES['attachment'] : null );
|
||||
if ( !$files_input )
|
||||
{
|
||||
echo json_encode( [ 'status' => 'error', 'msg' => 'Nie przesłano plików.' ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
$repository = new \Domain\Tasks\TaskAttachmentRepository();
|
||||
$files = self::normalize_uploads_array( $files_input );
|
||||
|
||||
if ( !count( $files ) )
|
||||
{
|
||||
echo json_encode( [ 'status' => 'error', 'msg' => 'Brak poprawnych plików do wysłania.' ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
$failed = [];
|
||||
$success = 0;
|
||||
foreach ( $files as $file )
|
||||
{
|
||||
$result = $repository -> upload( \S::get( 'task_id' ), $user['id'], $file );
|
||||
if ( $result['status'] == 'success' )
|
||||
$success++;
|
||||
else
|
||||
$failed[] = isset( $file['name'] ) ? $file['name'] : 'plik';
|
||||
}
|
||||
|
||||
if ( $success and !count( $failed ) )
|
||||
echo json_encode( [ 'status' => 'success', 'msg' => 'Dodano załączniki.' ] );
|
||||
elseif ( $success and count( $failed ) )
|
||||
echo json_encode( [ 'status' => 'partial', 'msg' => 'Część plików dodano, część się nie powiodła.' ] );
|
||||
else
|
||||
echo json_encode( [ 'status' => 'error', 'msg' => 'Nie udało się dodać załączników.' ] );
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
static private function normalize_uploads_array( $files_input )
|
||||
{
|
||||
if ( !is_array( $files_input ) or !isset( $files_input['name'] ) )
|
||||
return [];
|
||||
|
||||
if ( !is_array( $files_input['name'] ) )
|
||||
return [ $files_input ];
|
||||
|
||||
$normalized = [];
|
||||
foreach ( $files_input['name'] as $i => $name )
|
||||
{
|
||||
$normalized[] = [
|
||||
'name' => $name,
|
||||
'type' => isset( $files_input['type'][$i] ) ? $files_input['type'][$i] : null,
|
||||
'tmp_name' => isset( $files_input['tmp_name'][$i] ) ? $files_input['tmp_name'][$i] : null,
|
||||
'error' => isset( $files_input['error'][$i] ) ? $files_input['error'][$i] : UPLOAD_ERR_NO_FILE,
|
||||
'size' => isset( $files_input['size'][$i] ) ? $files_input['size'][$i] : 0
|
||||
];
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
static public function task_attachment_delete()
|
||||
{
|
||||
global $user;
|
||||
|
||||
if ( !$user )
|
||||
{
|
||||
header( 'Location: /logowanie' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$repository = new \Domain\Tasks\TaskAttachmentRepository();
|
||||
$result = $repository -> delete( \S::get( 'attachment_id' ), $user['id'] );
|
||||
|
||||
if ( $result )
|
||||
echo json_encode( [ 'status' => 'success' ] );
|
||||
else
|
||||
echo json_encode( [ 'status' => 'error' ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function task_attachment_rename()
|
||||
{
|
||||
global $user;
|
||||
|
||||
if ( !$user )
|
||||
{
|
||||
header( 'Location: /logowanie' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$repository = new \Domain\Tasks\TaskAttachmentRepository();
|
||||
$result = $repository -> rename( \S::get( 'attachment_id' ), \S::get( 'title' ) );
|
||||
|
||||
if ( $result )
|
||||
echo json_encode( [ 'status' => 'success' ] );
|
||||
else
|
||||
echo json_encode( [ 'status' => 'error' ] );
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function filtr_save_form() {
|
||||
echo json_encode( [
|
||||
'status' => 'success',
|
||||
@@ -545,3 +643,4 @@ class Tasks
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user