PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Http/Controllers/PatientController.php

https://gitlab.com/omazon/SINRAIM
PHP | 202 lines | 99 code | 37 blank | 66 comment | 1 complexity | 652ea6701e5164228b97e322680df349 MD5 | raw file
  1. <?php namespace SINRAIM\Http\Controllers;
  2. use SINRAIM\Http\Requests;
  3. use SINRAIM\Http\Requests\PatientCreateRequest;
  4. use SINRAIM\Http\Requests\PatientUpdateRequest;
  5. use SINRAIM\Http\Controllers\Controller;
  6. use Illuminate\Http\Request;
  7. use SINRAIM\Patient;
  8. use SINRAIM\User;
  9. use SINRAIM\History;
  10. use SINRAIM\MedicalDiccionary;
  11. use Illuminate\Routing\Route;
  12. use Illuminate\Database\Eloquent\Collection;
  13. use Redirect;
  14. use DB;
  15. use Auth;
  16. use Input;
  17. class PatientController extends Controller
  18. {
  19. /**
  20. * Busqueda desde modelo para Metodos de Actualizar, editar y eliminar.
  21. *
  22. *
  23. */
  24. public function __construct(){
  25. $this->middleware('auth');
  26. $this->beforeFilter('@find',['only' => ['edit','update','destroy']]);
  27. }
  28. public function find(Route $route){
  29. $this->patients = Patient::find($route->getParameter('paciente'));
  30. }
  31. /**
  32. * Display a listing of the resource.
  33. *
  34. * @return \Illuminate\Http\Response
  35. */
  36. public function index()
  37. {
  38. //Recibir del modelo paciente a todos los que existen y retornarlo a la vista principal.
  39. $idnotificador = Auth::user()->id;
  40. $patients = Patient::verpacientenoti($idnotificador);
  41. return view('Patient.index',compact('patients','idnotificador'));
  42. }
  43. /**
  44. * Show the form for creating a new resource.
  45. *
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function create()
  49. {
  50. //retornar a la vista donde esta el formulario de crear, en este caso es un modal ubicado en el index.
  51. return view('Patient.index');
  52. }
  53. /**
  54. * Store a newly created resource in storage.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function store(Request $request)
  60. {
  61. $v = \Validator::make($request->all(), [
  62. 'name' => 'required',
  63. 'last_name' => 'required',
  64. 'fechanacimiento' => 'required',
  65. 'genero' => 'required'
  66. ]);
  67. if ($v->fails())
  68. {
  69. return redirect('/paciente')->with('message-error','Error! El paciente no se ha guardado por que falta informacion, revisa el formulario de nuevo paciente!!!.')->withInput()->withErrors($v->errors());
  70. //return redirect()->back()->withInput()->withErrors($v->errors());
  71. }
  72. //Guardar los valores recibidos de la vista en los objetos que representan al modelo, despues de un proceso de validacion
  73. //mediante el Request
  74. $patients=Patient::create([
  75. 'name' => $request['name'],
  76. 'last_name' => $request['last_name'],
  77. 'fechanacimiento' => $request['fechanacimiento'],
  78. 'edad' => $request['edad'],
  79. 'genero' => $request['genero'],
  80. ]);
  81. $idnotificador = Input::get('idnotificador');
  82. $patients->save();
  83. $patients->users()->attach($idnotificador);
  84. //retornar a la vista index con el mensaje.
  85. return redirect('/paciente')->with('message','Felicidades!, Tu nuevo paciente ha sido guardado con exito!!!.');
  86. }
  87. /**
  88. * Display the specified resource.
  89. *
  90. * @param int $id
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function show($id)
  94. {
  95. //Busqueda del paciente segun el ID
  96. $patients = Patient::find($id);
  97. //Guardar los objetos ID y nombre en variables que seran utilizadas en la vista de forma independiente.
  98. $idpaciente = $patients->id;
  99. $nombrepaciente = $patients->name;
  100. //Traer del modelo la consulta que indica los antecedentes por usuario seleccionado
  101. $histories = History::Antecedentes($id);
  102. //$descripcion_id = MedicalDiccionary::lists('descripcion','iddescripcion');
  103. //retornar a la vista el arreglo recibido mas las variables independientes.
  104. return view('Patient.show',compact('histories'))->with('nombrepaciente',$nombrepaciente)->with('idpaciente',$idpaciente);
  105. }
  106. /**
  107. * Show the form for editing the specified resource.
  108. *
  109. * @param int $id
  110. * @return \Illuminate\Http\Response
  111. */
  112. public function edit($id)
  113. {
  114. //Recibe el ID del paciente a editar y luego envia el objeto al ajax.
  115. return response()->json([
  116. 'name' => $this->patients->name,
  117. 'last_name' => $this->patients->last_name,
  118. 'fechanacimiento' => $this->patients->fechanacimiento,
  119. 'edad' => $this->patients->edad,
  120. 'genero' => $this->patients->genero,
  121. 'id' => $this->patients->id
  122. ]);
  123. }
  124. /**
  125. * Update the specified resource in storage.
  126. *
  127. * @param \Illuminate\Http\Request $request
  128. * @param int $id
  129. * @return \Illuminate\Http\Response
  130. */
  131. public function update(Request $request, $id)
  132. {
  133. //Recibir el ID y la validacion para luego actualizar en la BD
  134. $this->patients->fill($request->all());
  135. $this->patients->save();
  136. return response()->json([
  137. "mensaje"=> "Listo"
  138. ]);
  139. }
  140. /**
  141. * Remove the specified resource from storage.
  142. *
  143. * @param int $id
  144. * @return \Illuminate\Http\Response
  145. */
  146. public function destroy($id)
  147. {
  148. //recibir del modelo con el ID del paciente que debe ser eliminado y borrarlo
  149. //si este proceso se ejecuta mediante ajax enviar el mensaje.
  150. $this->patients->delete();
  151. /* $patients = Patient::find($id);
  152. $patients->users()->detach($patient_id);
  153. $patients->histories()->detach($patient_id);
  154. $this->patients->delete();*/
  155. //$patients->users()->delete();
  156. return response()->json([
  157. "mensaje"=> "Borrado"
  158. ]);
  159. }
  160. public function autocomplete(Request $request)
  161. {
  162. $data = MedicalDiccionary::select("descripcion as name","iddescripcion as id")->where("descripcion","LIKE", "%{$request->input('query')}%")->take(10)->get();
  163. return response()->json($data);
  164. }
  165. }