<?
namespace controls;
class Tasks
{
  static public function task_change_dates() {
    global $mdb;

    $response = [ 'status' => 'error', 'msg' => 'Podczas zmiany daty wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::task_change_dates( \S::get( 'task_id' ), \S::get( 'date_start' ), \S::get( 'date_end' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Data została zmieniona.' ];

    echo json_encode( $response );
    exit;
  }

  static public function task_delete() {
    global $user;

    if ( !$user ) {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas usuwania zadania wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::task_delete( \S::get( 'task_id' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Zadanie zostało usunięte.' ];

    echo json_encode( $response );
    exit;
  }

  static public function main_view_by_ajax()
  {
    global $user;

    $show_tasks_closed = \S::get_session( 'tasks_closed' );
    $show_tasks_bulk = \S::get_session( 'tasks_bulk' );
    $show_tasks_to_review = \S::get_session( 'tasks_to_review' );

    $open_task_id = \factory\Tasks::get_open_task_id( $user['id'] );
    $projects = \S::get( 'projects' );
    $users = \S::get( 'users' );

    if ( $projects )
      \S::set_session( 'selected_projects', $projects );
    else
      \S::set_session( 'selected_projects', null );

    if ( $users )
      \S::set_session( 'selected_users', $users );
    else
      \S::set_session( 'selected_users', null );

    $show_tasks_closed = \S::get_session( 'tasks_closed' ) ? \S::get_session( 'tasks_closed' ) : 'hide';
    $show_tasks_bulk = \S::get_session( 'tasks_bulk' );
    $show_tasks_to_review = \S::get_session( 'tasks_to_review' );

    echo json_encode( [
      'tasks_new' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => \factory\Tasks::get_tasks( 0, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_to_review' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => $show_tasks_to_review == 'hide' ? null : \factory\Tasks::get_tasks( 1, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_closed' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => $show_tasks_closed == 'hide' ? null : \factory\Tasks::get_tasks( 2, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_bulk' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => $show_tasks_bulk == 'hide' ? null : \factory\Tasks::get_tasks( 3, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_to_do' => \Tpl::view( 'tasks/main_view_by_ajax', [
        'tasks' => \factory\Tasks::get_tasks( 5, $user['id'], $projects, $users ),
        'user' => $user,
        'statuses' => \factory\Tasks::get_statuses(),
        'open_task_id' => $open_task_id
      ] ),
      'tasks_gantt' => \factory\Tasks::get_tasks_gantt( $user['id'], $projects, $users ),
    ] );
    exit;
  }

  static public function main_view()
  {
    global $user;

    if ( \S::get( 'tasks_closed' ) == 'hide' )
    {
      \S::set_session( 'tasks_closed', 'hide' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_closed' ) == 'show' )
    {
      \S::set_session( 'tasks_closed', 'show' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_bulk' ) == 'hide' )
    {
      \S::set_session( 'tasks_bulk', 'hide' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_bulk' ) == 'show' )
    {
      \S::set_session( 'tasks_bulk', 'show' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    // tasks_to_review
    if ( \S::get( 'tasks_to_review' ) == 'hide' )
    {
      \S::set_session( 'tasks_to_review', 'hide' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    if ( \S::get( 'tasks_to_review' ) == 'show' )
    {
      \S::set_session( 'tasks_to_review', 'show' );
      header( 'Location: /tasks/main_view/' );
      exit;
    }

    $show_tasks_closed = \S::get_session( 'tasks_closed' ) ? \S::get_session( 'tasks_closed' ) : 'hide';
    $show_tasks_bulk = \S::get_session( 'tasks_bulk' );
    $show_tasks_to_review = \S::get_session( 'tasks_to_review' );

    // get default filtr
    if ( !\S::get_session( 'fresh_load' ) ) {
      $default_filtr = \factory\Tasks::get_default_filtr( $user['id'] );
      if ( $default_filtr )
      {
        if ( !\S::get_session( 'selected_projects' ) and $default_filtr['projects'] )
          \S::set_session( 'selected_projects', explode( ',', $default_filtr['projects'] ) );

        if ( !\S::get_session( 'selected_users' ) and $default_filtr['users'] )
          \S::set_session( 'selected_users', explode( ',', $default_filtr['users'] ) );
      }
      \S::set_session( 'fresh_load', true );
    }

    return \Tpl::view( 'tasks/main_view', [
      'tasks_new' => \factory\Tasks::get_tasks( 0, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ),
      'tasks_to_review' => $show_tasks_to_review == 'hide' ? null : \factory\Tasks::get_tasks( 1, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ),
      'tasks_closed' => $show_tasks_closed == 'hide' ? null : \factory\Tasks::get_tasks( 2, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ), // zakończone
      'tasks_bulk' => $show_tasks_bulk == 'hide' ? null : \factory\Tasks::get_tasks( 3, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' )  ), // opłaty
      'tasks_to_do' => \factory\Tasks::get_tasks( 5, $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ), // do zrobienia
      'user' => $user,
      'statuses' => \factory\Tasks::get_statuses(),
      'open_task_id' => \factory\Tasks::get_open_task_id( $user['id'] ),
      'projects' => \factory\Projects::user_projects( $user['id'] ),
      'selected_projects' => \S::get_session( 'selected_projects' ),
      'selected_users' => \S::get_session( 'selected_users' ),
      'tasks_filtrs' => \factory\Tasks::get_filtrs( $user['id'] ),
      'show_tasks_closed' => $show_tasks_closed,
      'show_tasks_bulk' => $show_tasks_bulk,
      'show_tasks_to_review' => $show_tasks_to_review,
      'users' => \factory\Users::users_list(),
      'tasks_gantt' => \factory\Tasks::get_tasks_gantt( $user['id'], \S::get_session( 'selected_projects' ), \S::get_session( 'selected_users' ) ),
      'selected_gantt_users' => \S::get_session( 'selected_gantt_users' )
    ] );
  }

  static public function action_change_status() {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas zmiany statusu czynności wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::action_change_status( \S::get( 'action_id' ), \S::get( 'status' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Status czynności został zmieniony.' ];

    echo json_encode( $response );
    exit;
  }

  static public function comment_delete() {
    global $user;

    if ( !$user ) {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas usuwania komentarza wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::comment_delete( \S::get( 'comment_id' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Komentarz został usunięty.' ];

    echo json_encode( $response );
    exit;
  }

  static public function comment_save()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania komentarza wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( $id = \factory\Tasks::comment_save( \S::get( 'task_id' ), $user['id'], \S::get( 'text' ) ) )
    {
      \factory\Tasks::clear_task_opened( \S::get( 'task_id' ) );
      $response = [ 'status' => 'ok', 'msg' => 'Komentarz został zapisany.', 'id' => $id ];
    }

    echo json_encode( $response );
    exit;
  }

  static public function action_delete()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas usuwania czynności wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::action_delete( \S::get( 'action_id' ) ) )
      $response = [ 'status' => 'ok', 'msg' => 'Czynność została usunięta.' ];

    echo json_encode( $response );
    exit;
  }

  static public function action_save()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania czynności wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( $id = \factory\Tasks::action_save( \S::get( 'task_id' ), \S::get( 'text' ) ) )
    {
      \factory\Tasks::clear_task_opened( \S::get( 'task_id' ) );
      $response = [ 'status' => 'ok', 'msg' => 'Czynność została zapisana.', 'id' => $id ];
    }

    echo json_encode( $response );
    exit;
  }

  // Zapisanie kolejności zadań
  static public function tasks_order_save()
  {
    global $mdb;

    foreach ( \S::get( 'tasks_order' ) as $key => $val ) {
      $mdb -> update( 'tasks', [ 'o' => $key ], [ 'id' => $val ] );
    }
    exit;
  }

  static public function send_email_task_change_status( $task_id )
  {
    $task = \factory\Tasks::task_details( $task_id );

    $statuses = \factory\Tasks::get_statuses();

    if ( $task['status_change_mail'] ) {
      \S::send_email(
        'biuro@project-pro.pl',
        'crmPRO - zmieniono status zadania',
        '<p>Witaj<br/>zmieniono status zadania <b>' . $task['name'] . ' - ' . \factory\Crm::get_client_name( (int)$task['client_id'] ) . '</b> na <b>' . $statuses[ $task['status'] ] . '</b>.</p>' .
        '<p>' . html_entity_decode( $task['text'] ) . '</p>' .
        '<p>Pozdrawiamy<br/>Zespół crmPRO.pl</p>'
      );
    }
  }

  static public function task_change_project() {
    global $mdb;

    if ( $mdb -> update( 'tasks', [ 'project_id' => \S::get( 'project_id' ) ], [ 'id' => \S::get( 'task_id' ) ] ) ) {
      echo json_encode( [ 'status' => 'success' ] );
    } else {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_change_client() {
    global $mdb;

    $client_id = (int)\S::get( 'client_id' );
    $client_value = $client_id > 0 ? $client_id : null;

    if ( $mdb -> update( 'tasks', [ 'client_id' => $client_value ], [ 'id' => \S::get( 'task_id' ) ] ) ) {
      echo json_encode( [ 'status' => 'success' ] );
    } else {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_change_parent() {
    global $mdb;

    $task_id = (int)\S::get( 'task_id' );
    $parent_id = (int)\S::get( 'parent_id' );

    if ( $task_id and $task_id === $parent_id )
      $parent_id = 0;

    $parent_value = $parent_id > 0 ? $parent_id : null;

    if ( $mdb -> update( 'tasks', [ 'parent_id' => $parent_value ], [ 'id' => $task_id ] ) ) {
      echo json_encode( [ 'status' => 'success' ] );
    } else {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_change_priority() {
    global $mdb;

    if ( $mdb -> update( 'tasks', [ 'priority' => \S::get( 'priority' ) ], [ 'id' => \S::get( 'task_id' ) ] ) ) {
      echo json_encode( [ 'status' => 'success' ] );
    } else {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_change_title() {
    global $mdb;

    if ( $mdb -> update( 'tasks', [ 'name' => \S::get( 'title' ) ], [ 'id' => \S::get( 'task_id' ) ] ) ) {
      echo json_encode( [ 'status' => 'success' ] );
    } else {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_change_text() {
    global $mdb;

    if ( $mdb -> update( 'tasks', [ 'text' => \S::get( 'text' ) ], [ 'id' => \S::get( 'task_id' ) ] ) ) {
      echo json_encode( [ 'status' => 'success' ] );
    } else {
      echo json_encode( [ 'status' => 'error' ] );
    }
    exit;
  }

  static public function task_change_users() {
    global $mdb, $user;

    if ( !$user or $user['id'] != 1 ) {
      echo json_encode( [ 'status' => 'error' ] );
      exit;
    }

    $task_id = (int)\S::get( 'task_id' );
    if ( !$task_id )
      $task_id = (int)\S::get( 'id' );
    $users = \S::get( 'users' );
    $status_change_mail_value = \S::get( 'status_change_mail' );
    $status_change_mail = 0;

    if ( is_bool( $status_change_mail_value ) )
      $status_change_mail = $status_change_mail_value ? 1 : 0;
    elseif ( is_numeric( $status_change_mail_value ) )
      $status_change_mail = (int)$status_change_mail_value === 1 ? 1 : 0;
    elseif ( is_string( $status_change_mail_value ) )
      $status_change_mail = in_array( strtolower( trim( $status_change_mail_value ) ), [ '1', 'on', 'true', 'yes' ], true ) ? 1 : 0;

    if ( !$task_id ) {
      echo json_encode( [ 'status' => 'error' ] );
      exit;
    }

    $mdb -> delete( 'task_user', [ 'task_id' => $task_id ] );

    if ( is_array( $users ) and !empty( $users ) ) {
      foreach ( $users as $uid ) {
        $mdb -> insert( 'task_user', [ 'task_id' => $task_id, 'user_id' => (int)$uid ] );
      }
    }

    $mdb -> update( 'tasks', [ 'status_change_mail' => $status_change_mail ], [ 'id' => $task_id ] );

    echo json_encode( [ 'status' => 'success' ] );
    exit;
  }

  /**
   * @deprecated Use \Controllers\TasksController::taskChangeStatus() instead.
   */
  static public function task_change_status()
  {
    return \Controllers\TasksController::taskChangeStatus();
  }

  static public function task_end()
  {
    global $user;

    if ( !$user )
      return json_encode( [ 'status' => 'error' ] );

    echo json_encode( \factory\Tasks::task_end( \S::get( 'task_id' ), $user['id'] ) );
    exit;
  }

  static public function task_start()
  {
    global $user;

    if ( !$user )
      return json_encode( [ 'status' => 'error' ] );

    echo json_encode( \factory\Tasks::task_start( \S::get( 'task_id' ), $user['id'] ) );
    exit;
  }

  static public function task_edit()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $task_id = (int)\S::get( 'task_id' );
    if ( !$task_id )
      $task_id = (int)\S::get( 'id' );

    $attachments_repository = new \Domain\Tasks\TaskAttachmentRepository();
    $task = \factory\Tasks::task_details( $task_id );
    if ( !is_array( $task ) )
      $task = [];

    if ( !$task_id )
    {
      $prefill_project_id = \S::get( 'project_id' );
      $prefill_client_id = \S::get( 'client_id' );
      $prefill_parent_id = \S::get( 'parent_id' );

      if ( $prefill_project_id !== '' and $prefill_project_id !== null )
        $task['project_id'] = (int)$prefill_project_id;

      if ( $prefill_client_id !== '' and $prefill_client_id !== null )
        $task['client_id'] = (int)$prefill_client_id;

      if ( $prefill_parent_id !== '' and $prefill_parent_id !== null )
        $task['parent_id'] = (int)$prefill_parent_id;
    }

    $task['status'] = \Controllers\TasksController::resolveTaskStatusForForm( $task );
    $task['id'] = isset( $task['id'] ) ? (int)$task['id'] : $task_id;
    $task_id_for_attachments = (int)$task['id'];

    $wiki_categories = \factory\Wiki::get_categories_for_user( (int)$user['id'] );
    if ( !is_array( $wiki_categories ) or !count( $wiki_categories ) )
      $wiki_categories = \factory\Wiki::get_all_categories();

    return \Tpl::view( 'tasks/task_edit', [
      'projects' => \factory\Projects::user_projects( $user['id'] ),
      'priorities' => \factory\Tasks::$priorities,
      'task' => $task,
      'wiki_categories' => $wiki_categories,
      'task_attachments' => $task_id_for_attachments ? $attachments_repository -> listByTaskId( $task_id_for_attachments ) : [],
      '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;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania zadania wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );
    $values['recursively'] = isset( $values['recursively'] ) ? $values['recursively'] : 'off';
    $values['frequency'] = isset( $values['frequency'] ) && $values['frequency'] !== '' ? $values['frequency'] : 1;
    $values['period'] = isset( $values['period'] ) && $values['period'] !== '' ? $values['period'] : 1;
    $values['show_in_calendar'] = isset( $values['show_in_calendar'] ) ? $values['show_in_calendar'] : 'off';
    $values['status_change_mail'] = isset( $values['status_change_mail'] ) ? $values['status_change_mail'] : 'off';
    $values['send_email_notification'] = isset( $values['send_email_notification'] ) ? $values['send_email_notification'] : 'off';
    $values['users'] = isset( $values['users'] ) ? $values['users'] : [];
    $values['parent_id'] = isset( $values['parent_id'] ) && $values['parent_id'] !== '' ? (int)$values['parent_id'] : null;
    if ( !empty( $values['id'] ) and (int)$values['id'] === (int)$values['parent_id'] )
      $values['parent_id'] = null;
    $values['priority'] = isset( $values['priority'] ) && $values['priority'] !== '' ? $values['priority'] : ( empty( $values['id'] ) ? 1 : 0 );
    $values['recursive_last_date'] = isset( $values['recursive_last_date'] ) ? $values['recursive_last_date'] : null;
    if ( isset( $values['wiki_ids'] ) )
      $values['wiki_ids'] = $values['wiki_ids'];
    else if ( isset( $values['wiki_ids[]'] ) )
      $values['wiki_ids'] = $values['wiki_ids[]'];
    else
      $values['wiki_ids'] = [];
    $status   = \Controllers\TasksController::resolveTaskStatusForSave( $values );
    $recursive_parent_id = null;

    if ( !empty( $values['id'] ) )
    {
      $current_task = \factory\Tasks::task_details( (int)$values['id'] );
      if ( is_array( $current_task ) and isset( $current_task['recursive_parent_id'] ) and $current_task['recursive_parent_id'] !== '' )
        $recursive_parent_id = (int)$current_task['recursive_parent_id'];
    }

    if ( $id = \factory\Tasks::task_save(
            $values['id'], $values['parent_id'], $recursive_parent_id, $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'], $values['recursive_last_date'], $values['wiki_ids']
          ) )
    {
      \factory\Tasks::clear_task_opened( $id );
      $response = [ 'status' => 'ok', 'msg' => 'Zadanie zostało zapisane.', 'id' => $id ];
    }

    echo json_encode( $response );
    exit;
  }
  static public function task_popup()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $attachments_repository = new \Domain\Tasks\TaskAttachmentRepository();

    \factory\Tasks::set_task_opened_by_user( \S::get( 'task_id' ), $user['id'] );
    $wiki_data = \factory\Tasks::task_wiki_entries_for_user( (int)\S::get( 'task_id' ), (int)$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' ) ),
      'task_wiki_entries' => $wiki_data['entries'],
      'task_wiki_all_count' => $wiki_data['all_count'],
      'task_wiki_visible_count' => $wiki_data['visible_count'],
      'task_wiki_hidden_count' => $wiki_data['hidden_count'],
      'clients' => \factory\Crm::get_client_list(),
      'user' => $user,
      'statuses' => \factory\Tasks::get_statuses(),
      'projects' => \factory\Projects::user_projects( $user['id'] ),
      'parent_tasks' => \factory\Tasks::parent_tasks( $user['id'] ),
      'all_users' => \factory\Users::users_list()
    ] );
    exit;
  }

  static public function task_work_logs_popup()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $task_id = (int)\S::get( 'task_id' );
    $task = \factory\Tasks::task_details( $task_id, $user['id'] );

    if ( !is_array( $task ) )
      $task = [ 'id' => $task_id, 'name' => '' ];

    echo \Tpl::view( 'tasks/task_work_logs_popup', [
      'task' => $task,
      'task_works' => \factory\Tasks::task_works( $task_id )
    ] );
    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 task_attachments_list()
  {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $task_id = (int)\S::get( 'task_id' );

    if ( !$task_id )
    {
      echo json_encode( [ 'status' => 'error', 'msg' => 'Brak identyfikatora zadania.' ] );
      exit;
    }

    $repository = new \Domain\Tasks\TaskAttachmentRepository();
    $attachments = $repository -> listByTaskId( $task_id );

    echo json_encode( [
      'status' => 'success',
      'attachments' => is_array( $attachments ) ? $attachments : [],
      'count' => is_array( $attachments ) ? count( $attachments ) : 0
    ] );
    exit;
  }

  static public function filtr_save_form() {
    echo json_encode( [
      'status' => 'success',
      'title' => 'Zapisz filtr',
      'popup_content' => \Tpl::view( 'tasks/filtr_save_form', [
        'projects' => \S::get( 'projects' ),
        'users' => \S::get( 'users' )
      ] )
    ] );
    exit;
  }

  static public function filtr_save() {
    global $user;

    if ( !$user )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania filtru wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::filtr_save( $user['id'], \S::get( 'name' ), \S::get( 'projects' ), \S::get( 'users' ), \S::get( 'is_default' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Filtr został zapisany.' ];

    echo json_encode( $response );
    exit;
  }

  static public function filtr_update() {
    global $user;

    if ( !$user ) {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas aktualizacji filtru wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::filtr_update( \S::get( 'filtr_id' ), \S::get( 'projects' ), \S::get( 'users' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Filtr został zaktualizowany.' ];

    echo json_encode( $response );
    exit;
  }

  /**
   * @deprecated Use \Controllers\TasksController::workTime() instead.
   */
  static public function work_time()
  {
    return \Controllers\TasksController::workTime();
  }

  static public function change_task_work_date_start() {
    $result = \factory\Tasks::change_task_work_date_start( \S::get( 'task_work_id' ), \S::get( 'date_start' ) );
    exit;
  }

  static public function change_task_work_date_end() {
    $result = \factory\Tasks::change_task_work_date_end( \S::get( 'task_work_id' ), \S::get( 'date_end' ) );
    exit;
  }

  static public function work_delete() {

    if ( \factory\Tasks::work_delete( \S::get( 'work_id' ) ) )
      echo json_encode( [ 'status' => 'success' ] );
    else
      echo json_encode( [ 'status' => 'error' ] );
    exit;
  }

  static public function filtr_set_default() {
    global $user;

    if ( !$user ) {
      header( 'Location: /logowanie' );
      exit;
    }

    $response = [ 'status' => 'error', 'msg' => 'Podczas ustawiania domyślnego filtru wystąpił błąd. Proszę spróbować ponownie.' ];

    if ( \factory\Tasks::filtr_set_default( $user['id'], \S::get( 'filtr_id' ) ) )
      $response = [ 'status' => 'success', 'msg' => 'Filtr został ustawiony jako domyślny.' ];

    echo json_encode( $response );
    exit;
  }

  static function filtr_get() {
    $filtr = \factory\Tasks::filtr_details( \S::get( 'filtr_id' ) );

    echo json_encode( [
      'status' => 'success',
      'projects' => $filtr['projects'],
      'users' => $filtr['users']
    ] );
    exit;
  }
}

