NoteController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $allSections = SectionTemplate::where('is_active', true)->get();
  18. foreach ($allSections as $section) {
  19. $section->used = false;
  20. foreach ($noteSections as $noteSection) {
  21. if($noteSection->sectionTemplate->id === $section->id) {
  22. $section->used = true;
  23. $section->section_uid = $noteSection->uid;
  24. break;
  25. }
  26. }
  27. }
  28. return view('app.patient.note.dashboard', compact('patient', 'note', 'pros', 'allSections'));
  29. }
  30. public function renderNote($noteUid, Request $request)
  31. {
  32. $note = Note::where('uid', $noteUid)->first();
  33. $client = Client::where('id', $note->client_id)->first();
  34. return view('client/note', compact('note', 'client'));
  35. }
  36. public function sectionCreateForm($note_uid, $section_template_uid, Request $request)
  37. {
  38. $note = Note::where('uid', $note_uid)->first();
  39. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  40. $section = null; // convenience
  41. include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
  42. }
  43. public function sectionUpdateForm($section_uid, Request $request)
  44. {
  45. $section = Section::where('uid', $section_uid)->first();
  46. $note = Note::where('id', $section->note_id)->first();
  47. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  48. include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
  49. }
  50. public function getDefaultValueForSection($section, $patient_uid) {
  51. $defaultData = [
  52. "summary" => "",
  53. "value" => ""
  54. ];
  55. $patient = Client::where('uid', $patient_uid)->first();
  56. if(file_exists(storage_path('sections/' . $section . '/default.php'))) {
  57. include(storage_path('sections/' . $section . '/default.php'));
  58. }
  59. return json_encode($defaultData);
  60. }
  61. public function processFormSubmit(Request $request)
  62. {
  63. // for CREATE
  64. $note_uid = $request->note_uid;
  65. $section_template_uid = $request->section_template_uid;
  66. // for UPDATE
  67. $section_uid = $request->section_uid;
  68. $section = $section_uid ? Section::where('uid', $section_uid)->first() : null;
  69. $note = null;
  70. $sectionTemplate = null;
  71. if($section == null){
  72. $note = Note::where('uid', $note_uid)->first();
  73. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  74. } else {
  75. $note = Note::where('id', $section->note_id)->first();
  76. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  77. }
  78. $newContentData = [];
  79. // we wish to pass THESE arguments into this include:
  80. // if CREATE, $note and $sectionTemplate, and $request
  81. // if UPDATE, $section, and $request
  82. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  83. // now, create summaryHtml appropriate
  84. ob_start();
  85. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  86. $newSummaryHtml = ob_get_contents();
  87. ob_end_clean();
  88. if($section){
  89. // call Java to update section
  90. $data = [
  91. 'uid' => $section->uid,
  92. 'contentData' => json_encode($newContentData),
  93. 'summaryHtml' => $newSummaryHtml
  94. ];
  95. $response = $this->calljava($request, '/section/update', $data);
  96. //TODO: handle if response->success == false
  97. }else{
  98. // call Java to create section
  99. $data = [
  100. 'noteUid' => $note->uid,
  101. 'sectionTemplateUid' => $sectionTemplate->uid,
  102. 'contentData' => json_encode($newContentData),
  103. 'summaryHtml' => $newSummaryHtml
  104. ];
  105. $response = $this->callJava($request, '/section/create', $data);
  106. //TODO: handle if response->success == false
  107. }
  108. return redirect(route('patients.view.notes',$note->client->uid));
  109. }
  110. private function callJava($request, $endPoint, $data){
  111. $url = env('BACKEND_URL', 'http://localhost:8080/api') . $endPoint;
  112. $response = Http::asForm()
  113. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  114. ->post($url, $data)
  115. ->json();
  116. return $response;
  117. }
  118. }