NoteController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 CREATE
  72. if($section == null){
  73. // TODO require valid note_uid & section_template_uid
  74. $note = Note::where('uid', $note_uid)->first();
  75. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  76. } else {
  77. $note = Note::where('id', $section->note_id)->first();
  78. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  79. }
  80. // our intention is to now process a submit...
  81. // ... the point of which is to have newContentData and newSummaryHtml
  82. $newContentData = [];
  83. $newSummaryHtml = "";
  84. // we wish to pass THESE arguments into this include:
  85. // if CREATE, $note and $sectionTemplate, and $request
  86. // if UPDATE, $section, and $request
  87. // remember: the existence of form.php overrides section_template.is_canvas == TRUE
  88. if($sectionTemplate->is_canvas){
  89. if(file_exists()){
  90. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  91. }else{
  92. $newContentData = json_decode($request->get('data'), true);
  93. }
  94. ob_start();
  95. // TODO include the resource_path
  96. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  97. $newSummaryHtml = ob_get_contents();
  98. ob_end_clean();
  99. }elseif(file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  100. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  101. // now, create summaryHtml appropriate
  102. ob_start();
  103. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  104. $newSummaryHtml = ob_get_contents();
  105. ob_end_clean();
  106. }else{
  107. $newContentData = json_decode($request->get('data'), true);
  108. if(isset($newContentData['value'])){
  109. $newSummaryHtml = $newContentData['value'];
  110. }
  111. }
  112. $response = null;
  113. if($section){
  114. // call Java to update section
  115. $data = [
  116. 'uid' => $section->uid,
  117. 'contentData' => json_encode($newContentData),
  118. 'summaryHtml' => $newSummaryHtml
  119. ];
  120. $response = $this->calljava($request, '/section/update', $data);
  121. //TODO: handle if response->success == false
  122. }else{
  123. // call Java to create section
  124. // if default.php, run it to hydrate $newContentData and $newSummaryHtml
  125. if(file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  126. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  127. }
  128. $data = [
  129. 'noteUid' => $note->uid,
  130. 'sectionTemplateUid' => $sectionTemplate->uid,
  131. 'contentData' => json_encode($newContentData),
  132. 'summaryHtml' => $newSummaryHtml
  133. ];
  134. $response = $this->callJava($request, '/section/create', $data);
  135. //TODO: handle if response->success == false
  136. }
  137. return redirect(route('patients.view.notes',$note->client->uid));
  138. return [
  139. 'success' => $response->success,
  140. 'newSummaryHtml' => $newSummaryHtml
  141. ];
  142. }
  143. private function callJava($request, $endPoint, $data){
  144. $url = config('stag.backendUrl') . $endPoint;
  145. $response = Http::asForm()
  146. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  147. ->post($url, $data)
  148. ->json();
  149. return $response;
  150. }
  151. }