NoteController.php 5.8 KB

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