/**
  * Edit task
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_task()
 {
     $this->setTemplate('add_task');
     $task = ProjectTasks::findById(get_id());
     if (!$task instanceof ProjectTask) {
         flash_error(lang('task dnx'));
         $this->redirectTo('task');
     }
     // if
     $task_list = $task->getTaskList();
     if (!$task_list instanceof ProjectTaskList) {
         flash_error('task list dnx');
         $this->redirectTo('task');
     }
     // if
     if (!$task->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectTo('task');
     }
     // if
     $task_data = array_var($_POST, 'task');
     if (!is_array($task_data)) {
         $task_data = array('text' => $task->getText(), 'start_date' => $task->getStartDate(), 'due_date' => $task->getDueDate(), 'task_list_id' => $task->getTaskListId(), 'assigned_to' => $task->getAssignedToCompanyId() . ':' . $task->getAssignedToUserId(), 'send_notification' => config_option('send_notification_default', '0'));
         // array
     }
     // if
     tpl_assign('task', $task);
     tpl_assign('task_list', $task_list);
     tpl_assign('task_data', $task_data);
     if (is_array(array_var($_POST, 'task'))) {
         $old_owner = $task->getAssignedTo();
         //$task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
         if (isset($_POST['task_start_date'])) {
             $task_data['start_date'] = DateTimeValueLib::makeFromString($_POST['task_start_date']);
         } else {
             $task_data['start_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_start_date_month', 1), array_var($_POST, 'task_start_date_day', 1), array_var($_POST, 'task_start_date_year', 1970));
         }
         if (isset($_POST['task_due_date'])) {
             $task_data['due_date'] = DateTimeValueLib::makeFromString($_POST['task_due_date']);
         } else {
             $task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
         }
         $task->setFromAttributes($task_data);
         $task->setTaskListId($task_list->getId());
         // keep old task list id
         $assigned_to = explode(':', array_var($task_data, 'assigned_to', ''));
         $task->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
         $task->setAssignedToUserId(array_var($assigned_to, 1, 0));
         try {
             DB::beginWork();
             $task->save();
             // Move?
             $new_task_list_id = (int) array_var($task_data, 'task_list_id');
             if ($new_task_list_id && $task->getTaskListId() != $new_task_list_id) {
                 // Move!
                 $new_task_list = ProjectTaskLists::findById($new_task_list_id);
                 if ($new_task_list instanceof ProjectTaskList) {
                     $task_list->detachTask($task, $new_task_list);
                     // detach from old and attach to new list
                 }
                 // if
             }
             // if
             ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             trace(__FILE__, 'edit_task: notify user');
             // notify user
             if (array_var($task_data, 'send_notification') == 'checked') {
                 try {
                     if (Notifier::notifyNeeded($task->getAssignedTo(), $old_owner)) {
                         Notifier::taskAssigned($task);
                     }
                 } catch (Exception $e) {
                     Logger::log("Error: Notification failed, " . $e->getMessage(), Logger::ERROR);
                 }
                 // try
             }
             // if
             flash_success(lang('success edit task'));
             // Redirect to task list. Check if we have updated task list ID first
             if (isset($new_task_list) && $new_task_list instanceof ProjectTaskList) {
                 $this->redirectToUrl($new_task_list->getViewUrl());
             } else {
                 $this->redirectToUrl($task_list->getViewUrl());
             }
             // if
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }