NoteController.php 5.8 KB

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