NoteController.php 6.6 KB

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