PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/ProjectsController.php

https://gitlab.com/mlnkv/ribbbon
PHP | 134 lines | 67 code | 23 blank | 44 comment | 11 complexity | f7eb1d48b2165b72e8161437562f5cbc MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\User;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Support\Facades\View;
  6. use Illuminate\Support\Facades\Validator;
  7. use Illuminate\Support\Facades\Input;
  8. use Illuminate\Support\Facades\Redirect;
  9. use App\Project;
  10. use App\Task;
  11. use App\Credential;
  12. class ProjectsController extends BaseController {
  13. // Returns the given project view
  14. public function show($id)
  15. {
  16. $project = Project::find($id);
  17. // Must be refactored as a filter
  18. if ( $project->isOwner() == false && $project->isMember() == false ) {
  19. return Redirect::to('/hud');
  20. }
  21. return View::make('ins/projects/show')->with('pTitle', $project->name);
  22. }
  23. // Get all user projects
  24. public function getAllUserProjects(){
  25. $projects = Project::where('user_id',Auth::id())->get();
  26. if($projects) {
  27. foreach ($projects as $project) {
  28. $completedWeight = Project::find($project->id)->tasks()->where('state','=','complete')->sum('weight');
  29. $totalWeight = Project::find($project->id)->tasks()->sum('weight');
  30. $project["completedWeight"] = $completedWeight;
  31. $project["totalWeight"] = $totalWeight;
  32. }
  33. }
  34. return $this->setStatusCode(200)->makeResponse('Projects retrieved successfully',$projects->toArray());
  35. }
  36. // Return the given project
  37. public function getProject($id){
  38. if (!Project::find($id)) {
  39. return $this->setStatusCode(404)->makeResponse('The project was not found');
  40. }
  41. $project = Project::find($id);
  42. $project->tasks = Task::where('project_id', $id)->get();
  43. $project->credentials = Credential::where('project_id', $id)->get();
  44. return $this->setStatusCode(200)->makeResponse('Project was successfully found', $project);
  45. }
  46. // Insert the given project into the database
  47. public function storeProject(){
  48. if (!Input::all() || strlen(trim(Input::get('name'))) == 0) {
  49. return $this->setStatusCode(406)->makeResponse('No information provided to create project');
  50. }
  51. Input::merge(array('user_id' => Auth::id()));
  52. Project::create(Input::all());
  53. $id = \DB::getPdo()->lastInsertId();
  54. return $this->setStatusCode(200)->makeResponse('Project created successfully', Project::find($id));
  55. }
  56. // Update the given project
  57. public function updateProject($id){
  58. if ( Input::get('name') === "") {
  59. return $this->setStatusCode(406)->makeResponse('The project needs a name');
  60. }
  61. if (!Project::find($id)) {
  62. return $this->setStatusCode(404)->makeResponse('Project not found');
  63. }
  64. $input = Input::all();
  65. unset($input['_method']);
  66. Project::find($id)->update($input);
  67. return $this->setStatusCode(200)->makeResponse('The project has been updated');
  68. }
  69. // Invites a user to the given project.
  70. public function invite($id){
  71. // // Validation
  72. // $rules = ['email' => 'required|email|exists:users,email'];
  73. // $messages = [ 'exists' => 'That email is not currently associated with a user.',];
  74. //
  75. // $validator = Validator::make(Input::all(), $rules, $messages);
  76. //
  77. // if ($validator->fails())
  78. // {
  79. // return Redirect::back()->withErrors($validator)->withInput();
  80. // }
  81. //
  82. // // Get the id of the user being sent the invite
  83. // $user_id = DB::table('users')->whereEmail(Input::get('email'))->pluck('id');
  84. //
  85. // if( count(Projectuser::whereUserId($user_id)->whereProjectId($id)->get()) != 0 )
  86. // {
  87. // $validator->getMessageBag()->add('email', 'A user with that email has already been invited.');
  88. // return Redirect::back()->withErrors($validator)->withInput();
  89. // }
  90. //
  91. // // Save the relationship between user and project.
  92. // $pu = new Projectuser;
  93. // $pu->project_id = $id;
  94. // $pu->user_id = $user_id;
  95. // $pu->save();
  96. //
  97. // // Prepare email invitation & send it
  98. // $project_name = Project::find($id)->pluck('name');
  99. // $project_url = url() . '/projects/'.$id;
  100. // sendProjectInviteMail(Input::get('email'), $project_name, $project_url);
  101. //
  102. // return Redirect::back()->with('success', "A new member has been added to this project.");
  103. }
  104. // Removes a member from a given project
  105. public function remove($id){
  106. // $project = Project::find($id);
  107. // $project->members()->detach(Input::get('member_id'));
  108. //
  109. // return Redirect::back();
  110. }
  111. }