NoteController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = $this->pros;
  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. // JAVA ONLY
  37. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  38. {
  39. $contentData = [];
  40. $summaryHtml = '';
  41. $patient = Client::where('id', $patientID)->first();
  42. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  43. if ($sectionTemplate->is_canvas) {
  44. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  45. // default should simply assign to $contentData
  46. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  47. ob_start();
  48. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  49. $summaryHtml = ob_get_contents();
  50. ob_end_clean();
  51. }
  52. } else {
  53. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  54. // default should simply assign to $contentData and $summaryHtml as needed
  55. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  56. }
  57. }
  58. return [
  59. 'contentData' => $contentData,
  60. 'summaryHtml' => $summaryHtml
  61. ];
  62. }
  63. public function processFormSubmit(Request $request)
  64. {
  65. $guestAccessCode = $request->get('guest_access_code');
  66. if($guestAccessCode){
  67. //its from guest
  68. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  69. abort_if(!$sectionForToken, 401, 'Unauthorized');
  70. }else{
  71. //its not from guest so require performer
  72. abort_if(!$this->performer, 401, 'Unauthorized');
  73. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  74. }
  75. // TODO require
  76. $section_uid = $request->get('section_uid');
  77. $section = Section::where('uid', $section_uid)->first();
  78. $note = Note::where('id', $section->note_id)->first();
  79. $client = null;
  80. if($note){
  81. $client = Client::where('id', $note->client_id)->first();
  82. }else{
  83. $client = Client::where('id', $section->client_id)->first();
  84. }
  85. $patient = $client;
  86. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  87. $newContentData = [];
  88. $newSummaryHtml = "";
  89. $sectionInternalName = $sectionTemplate->internal_name;
  90. if ($sectionTemplate->is_canvas) {
  91. $response = null;
  92. $data = [
  93. 'uid' => $client->uid,
  94. 'key' => $sectionTemplate->internal_name,
  95. 'data' => $request->get('data')
  96. ];
  97. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  98. //TODO: handle $response->success == false
  99. $patient = $client;
  100. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  101. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  102. } else {
  103. $newContentData = json_decode($request->get('data'), true);
  104. }
  105. ob_start();
  106. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  107. $newSummaryHtml = ob_get_contents();
  108. ob_end_clean();
  109. // TODO call Java to update the canvas
  110. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  111. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  112. ob_start();
  113. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  114. $newSummaryHtml = ob_get_contents();
  115. ob_end_clean();
  116. } else {
  117. $newContentData = json_decode($request->get('data'), true);
  118. if (isset($newContentData['value'])) {
  119. $newSummaryHtml = $newContentData['value'];
  120. }
  121. }
  122. $response = null;
  123. $data = [
  124. 'uid' => $section->uid,
  125. 'contentData' => json_encode($newContentData),
  126. 'summaryHtml' => $newSummaryHtml
  127. ];
  128. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  129. return [
  130. 'success' => $response['success'],
  131. 'newSummaryHtml' => $newSummaryHtml
  132. ];
  133. }
  134. // TODO move to utility
  135. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  136. {
  137. $url = config('stag.backendUrl') . $endPoint;
  138. $response = Http::asForm()
  139. ->withHeaders([
  140. 'sessionKey' => $request->cookie('sessionKey'),
  141. 'guestAccessCode' => $guestAccessCode
  142. ])
  143. ->post($url, $data)
  144. ->json();
  145. return $response;
  146. }
  147. }