Example #1
0
 function save($process_task = true)
 {
     $current_user = current_user();
     //we need to import parameters before we set the value, otherwise the manipulated values will be overridden
     $this->import_parameters();
     if (!isset($this->project_id)) {
         $this->set_error('project_id', Language::get('errors.task_invalid_project'));
         return false;
     }
     //todo:should we check that the user is the owner of this project or is the task owner function enough
     $project = new Project($this->project_id);
     //let's set the client id even though it's sometimes passed
     $this->set('client_id', $project->client_id);
     //there are some cases when we don't want to do all of this additional processing (i.e. when we're just
     //updating the status (overdue, on schedule, etc)
     if ($process_task == false) {
         //unset params that aren't saved to the db
         $this->unset_param('assigned_to_name');
         return parent::save();
     }
     $update_tasks_manager = false;
     //unset params that aren't saved to the db
     $this->unset_param('assigned_to_name');
     $this->prep_fields_for_db();
     if (!isset($this->assigned_to)) {
         $this->set('assigned_to', $current_user->id);
     }
     if ($this->is_new()) {
         $is_new = true;
         $update_tasks_manager = true;
         $this->set('created_by', $current_user->id);
     } else {
         $is_new = false;
         //we need to compare the existing value to the new values
         //todo:we can eliminate the need for this extra set of queries if we create a 'previous values' array on the model that's set each time we call clear params. Too many queries when saving tasks.
         $existing_values = new Task($this->id);
         //are we marking the task complete? (i.e. it was previously set as incomplete, but we're not setting it as
         //complete
         if ((bool) $existing_values->is_complete == false && (bool) $this->is_complete == true) {
             $this->set('completed_date', time());
             $this->set('completed_by', $current_user->id);
             ActivityManager::task_completed($this);
         } else {
             if ((bool) $existing_values->is_complete == true && (bool) $this->is_complete == false) {
                 //the task was previously complete, but we're setting it back to active
                 $this->set('completed_date', NULL);
             }
         }
         //are we assigning this task to someone new?
         if (isset($this->assigned_to) && $this->assigned_to != $existing_values->assigned_to) {
             $assigned_to_user = new User($this->assigned_to);
             ActivityManager::task_reassigned($this, $assigned_to_user);
         }
     }
     //save the invoice in the database
     $result = parent::save();
     //add the task to the activity stream if it's new
     if ($is_new) {
         ActivityManager::task_created($this);
     }
     //update teh task order, if this is a new task (existing task reorders generate a call directly to the taskmanger
     //class so they don't need to be handled here
     if ($update_tasks_manager == true) {
         //newOrder, and oldOrder will only be set if the task is created from task list, not from the modal
         if (isset($_POST['newOrder']) && isset($_POST['oldOrder'])) {
             $tasks_manager = new TasksManager();
             //normalize names using order/original order, newOrder/oldOrder, and order/newOrder or something
             $tasks_manager->order = $_POST['newOrder'];
             $tasks_manager->original_order = $_POST['oldOrder'];
             $tasks_manager->save_new($this->id);
         }
     }
     //todo:it would be nice if this only ran if the weight or complete status changed
     $project->update_progress();
     return $result;
     //todo: (all this goes in the controller - the functionality described by these words goes in the controller) this needs to 1) send back an error if validation has failed on the model 2)send an error if the sql has failed 3) only send success if neither of the first two cases happened. There should be one central function for this that we can use for all functions that call Model::save()
     //Response($result);
 }