NoteController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Pro;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Http;
  7. use App\Models\Note;
  8. use App\Models\Client;
  9. use App\Models\Section;
  10. use App\Models\SectionTemplate;
  11. class NoteController extends Controller
  12. {
  13. public function dashboard(Request $request, Client $patient, Note $note )
  14. {
  15. $pros = Pro::all();
  16. $noteSections = $note->sections;
  17. $noteSectionTemplateIDs = [];
  18. foreach ($noteSections as $noteSection) {
  19. $noteSectionTemplateIDs[] = $noteSection->sectionTemplate->id;
  20. }
  21. $sections = SectionTemplate::whereNotIn('id', $noteSectionTemplateIDs)->get();
  22. return view('app.patient.note.dashboard', compact('patient', 'note', 'pros', 'sections'));
  23. }
  24. public function renderNote($noteUid, Request $request)
  25. {
  26. $note = Note::where('uid', $noteUid)->first();
  27. $client = Client::where('id', $note->client_id)->first();
  28. return view('client/note', compact('note', 'client'));
  29. }
  30. public function sectionCreateForm($note_uid, $section_template_uid, Request $request)
  31. {
  32. $note = Note::where('uid', $note_uid)->first();
  33. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  34. $section = null; // convenience
  35. include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
  36. }
  37. public function sectionUpdateForm($section_uid, Request $request)
  38. {
  39. $section = Section::where('uid', $section_uid)->first();
  40. $note = Note::where('id', $section->note_id)->first();
  41. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  42. include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
  43. }
  44. public function processFormSubmit(Request $request)
  45. {
  46. // for CREATE
  47. $note_uid = $request->note_uid;
  48. $section_template_uid = $request->section_template_uid;
  49. // for UPDATE
  50. $section_uid = $request->section_uid;
  51. $section = $section_uid ? Section::where('uid', $section_uid)->first() : null;
  52. $note = null;
  53. $sectionTemplate = null;
  54. if($section == null){
  55. $note = Note::where('uid', $note_uid)->first();
  56. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  57. } else {
  58. $note = Note::where('id', $section->note_id)->first();
  59. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  60. }
  61. $newContentData = [];
  62. // we wish to pass THESE arguments into this include:
  63. // if CREATE, $note and $sectionTemplate, and $request
  64. // if UPDATE, $section, and $request
  65. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  66. // now, create summaryHtml appropriate
  67. ob_start();
  68. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  69. $newSummaryHtml = ob_get_contents();
  70. ob_end_clean();
  71. if($section){
  72. // call Java to update section
  73. $data = [
  74. 'uid' => $section->uid,
  75. 'contentData' => json_encode($newContentData),
  76. 'summaryHtml' => $newSummaryHtml
  77. ];
  78. $response = $this->calljava($request, '/section/update', $data);
  79. //TODO: handle if response->success == false
  80. }else{
  81. // call Java to create section
  82. $data = [
  83. 'noteUid' => $note->uid,
  84. 'sectionTemplateUid' => $sectionTemplate->uid,
  85. 'contentData' => json_encode($newContentData),
  86. 'summaryHtml' => $newSummaryHtml
  87. ];
  88. $response = $this->callJava($request, '/section/create', $data);
  89. //TODO: handle if response->success == false
  90. }
  91. return redirect(route('patients.view.notes',$note->client->uid));
  92. }
  93. private function callJava($request, $endPoint, $data){
  94. $url = env('BACKEND_URL', 'http://localhost:8080') . $endPoint;
  95. $response = Http::asForm()
  96. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  97. ->post($url, $data)
  98. ->json();
  99. return $response;
  100. }
  101. }