/**
  * Validate the input and store the ProjectPhaseDocument in the database.
  *
  * @param \Illuminate\Http\Request $request
  * @param int $projectId
  * @param int $projectPhaseId
  *
  * @return Response
  */
 public function store(Request $request, $projectId, $projectPhaseId)
 {
     $projectPhase = ProjectPhase::where('id', '=', $projectPhaseId)->where('project_id', '=', $projectId)->with(['project'])->firstOrFail();
     $this->validate($request, $this->createProjectPhasePhaseDocumentRules);
     $projectPhaseDocument = ProjectPhaseDocument::create(['project_phase_id' => $projectPhase->id, 'title' => $request->input('title'), 'content' => $request->input('content'), 'type' => $request->input('type')]);
     if (!isset($projectPhaseDocument)) {
         abort(503);
     }
     return redirect()->route('project.phase.document.show', ['project' => $projectPhase->project->id, 'phase' => $projectPhase->id, 'document' => $projectPhaseDocument->id]);
 }
 /**
  * Validate the input and update the ProjectPhase in the database.
  *
  * @param \Illuminate\Http\Request $request
  * @param int $projectId
  * @param int $projectPhaseId
  *
  * @return Response
  */
 public function update(Request $request, $projectId, $projectPhaseId)
 {
     $projectPhase = ProjectPhase::where('project_id', '=', $projectId)->where('id', '=', $projectPhaseId)->firstOrFail();
     $this->validate($request, $this->createProjectPhasePhaseRules);
     $oldNumber = $projectPhase->number;
     $number = $request->input('number') + 1;
     $startedAt = $request->input('started_at') ? \Carbon\Carbon::createFromTimestamp(strtotime($request->input('started_at'))) : null;
     $completedAt = $request->input('completed_at') ? \Carbon\Carbon::createFromTimestamp(strtotime($request->input('completed_at'))) : null;
     $planedStartedAt = $request->input('planed_started_at') ? \Carbon\Carbon::createFromTimestamp(strtotime($request->input('planed_started_at'))) : null;
     $planedCompletedAt = $request->input('planed_completed_at') ? \Carbon\Carbon::createFromTimestamp(strtotime($request->input('planed_completed_at'))) : null;
     $projectPhase->fill(['title' => $request->input('title'), 'description' => $request->input('description'), 'number' => $number, 'started_at' => $startedAt, 'completed_at' => $completedAt, 'planed_started_at' => $planedStartedAt, 'planed_completed_at' => $planedCompletedAt]);
     if (!$projectPhase->save()) {
         abort(503);
     }
     // Rearange positions
     \DB::table('projects_phases')->where('project_id', '=', $projectId)->where('id', '!=', $projectPhase->id)->where('number', '>=', $oldNumber)->decrement('number');
     \DB::table('projects_phases')->where('project_id', '=', $projectId)->where('id', '!=', $projectPhase->id)->where('number', '>=', $number)->increment('number');
     $maxNumber = \DB::table('projects_phases')->where('project_id', '=', $projectId)->max('number');
     if ($maxNumber === $projectPhase->number) {
         $projectPhase->number = $projectPhase->number - 1;
         if (!$projectPhase->save()) {
             abort(500);
         }
     }
     return redirect()->route('project.phase.show', ['project' => $projectPhase->project_id, 'phase' => $projectPhase->id]);
 }