Example #1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  Project  $project
  * @return Response
  */
 public function update($project)
 {
     $input = Input::all();
     $project->title = $input['title'];
     $project->contact_firstname = $input['contact_firstname'];
     $project->contact_lastname = $input['contact_lastname'];
     $project->contact_email = $input['contact_email'];
     $project->contact_phone_number = $input['contact_phone_number'];
     $project->contact_phone_number_ext = $input['contact_phone_number_ext'];
     $project->description = $input['description'];
     $project->location = $input['location'];
     $project->expected_time = $input['expected_time'];
     $project->motivation = $input['motivation'];
     $project->resources = $input['resources'];
     $project->constraints = $input['constraints'];
     $project->state = $input['state'];
     $input['tags'] = isset($input['tags']) ? $input['tags'] : array();
     //verify that there are some goals and tags in the $input
     $input['goals'] = isset($input['goals']) ? $input['goals'] : array();
     if ($project->update()) {
         // Delete project goals and then re-create them
         $project->goals()->delete();
         $loop_index = 0;
         foreach ($input['goals'] as $goal) {
             //Ignore empty goals
             if ($goal == '') {
                 $loop_index++;
                 continue;
             } else {
                 $goalObj = new Goal();
                 $goalObj->goal = $goal;
                 // The completed variable posted is an array containing the indexes for the goals that are marked as complete
                 // This will swap the array's index  with the value (ie. [0] => a, [1] => b will become [a] => 0, [b] => 1)
                 // This then allows us to see if the current loop index has a goal that is complete without needing to use a for loop
                 $completed = isset($input['completed']) ? array_flip($input['completed']) : array();
                 if (isset($completed[$loop_index])) {
                     $goalObj->complete = 1;
                 } else {
                     $goalObj->complete = 0;
                 }
                 $project->goals()->save($goalObj);
             }
             $loop_index++;
         }
         // Delete project tags and then re-create them
         $project->tags()->detach();
         foreach ($input['tags'] as $tag) {
             if ($tag == '') {
                 continue;
             } else {
                 $tagObj = Tag::find($tag);
                 $project->tags()->attach($tagObj);
             }
         }
         return Redirect::to('/admin/project/' . $project->id . '/edit')->with('info', 'The project has been updated.');
     } else {
         return Redirect::to('/admin/project/' . $project->id . '/edit')->withErrors($project->errors());
     }
 }