NoteController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Pro;
  4. use App\Models\Ticket;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Http;
  8. use App\Models\Note;
  9. use App\Models\Client;
  10. use App\Models\Section;
  11. use App\Models\SectionTemplate;
  12. class NoteController extends Controller
  13. {
  14. public function dashboard(Request $request, Client $patient, Note $note)
  15. {
  16. $pros = $this->pros;
  17. $noteSections = $note->sections;
  18. $allSections = SectionTemplate::where('is_active', true)->get();
  19. foreach ($allSections as $section) {
  20. $section->used = false;
  21. foreach ($noteSections as $noteSection) {
  22. if ($noteSection->sectionTemplate->id === $section->id) {
  23. $section->used = true;
  24. $section->section_uid = $noteSection->uid;
  25. break;
  26. }
  27. }
  28. }
  29. $allyPros = Pro::all(); //TODO: paginate, use select2
  30. // load today's tickets for patient
  31. $ticketsToday = Ticket::where('client_id', $patient->id)
  32. ->where('is_entry_error', false)
  33. ->whereRaw('DATE(created_at) = ?', [date('Y-m-d')])
  34. ->get();
  35. return view('app.patient.note.dashboard', compact('patient', 'note',
  36. 'allyPros', 'allSections', 'ticketsToday'));
  37. }
  38. public function renderNote($noteUid, Request $request)
  39. {
  40. $note = Note::where('uid', $noteUid)->first();
  41. $client = Client::where('id', $note->client_id)->first();
  42. return view('client/note', compact('note', 'client'));
  43. }
  44. // JAVA ONLY
  45. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  46. {
  47. $contentData = [];
  48. $summaryHtml = '';
  49. $patient = Client::where('id', $patientID)->first();
  50. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  51. if ($sectionTemplate->is_canvas) {
  52. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  53. // default should simply assign to $contentData
  54. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  55. ob_start();
  56. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  57. $summaryHtml = ob_get_contents();
  58. ob_end_clean();
  59. }
  60. } else {
  61. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  62. // default should simply assign to $contentData and $summaryHtml as needed
  63. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  64. }
  65. }
  66. return [
  67. 'contentData' => $contentData,
  68. 'summaryHtml' => $summaryHtml
  69. ];
  70. }
  71. public function processFormSubmit(Request $request)
  72. {
  73. $guestAccessCode = $request->get('guest_access_code');
  74. if($guestAccessCode){
  75. //its from guest
  76. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  77. abort_if(!$sectionForToken, 401, 'Unauthorized');
  78. }else{
  79. //its not from guest so require performer
  80. abort_if(!$this->performer, 401, 'Unauthorized');
  81. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  82. }
  83. // TODO require
  84. $section_uid = $request->get('section_uid');
  85. $section = Section::where('uid', $section_uid)->first();
  86. $note = Note::where('id', $section->note_id)->first();
  87. $client = null;
  88. if($note){
  89. $client = Client::where('id', $note->client_id)->first();
  90. }else{
  91. $client = Client::where('id', $section->client_id)->first();
  92. }
  93. $patient = $client;
  94. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  95. $newContentData = [];
  96. $newSummaryHtml = "";
  97. $sectionInternalName = $sectionTemplate->internal_name;
  98. if ($sectionTemplate->is_canvas) {
  99. $response = null;
  100. $data = [
  101. 'uid' => $client->uid,
  102. 'key' => $sectionTemplate->internal_name,
  103. 'data' => $request->get('data')
  104. ];
  105. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  106. //TODO: handle $response->success == false
  107. if($note){
  108. $client = Client::where('id', $note->client_id)->first();
  109. }else{
  110. $client = Client::where('id', $section->client_id)->first();
  111. }
  112. $patient = $client;
  113. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  114. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  115. } else {
  116. $newContentData = json_decode($request->get('data'), true);
  117. }
  118. ob_start();
  119. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  120. $newSummaryHtml = ob_get_contents();
  121. ob_end_clean();
  122. // TODO call Java to update the canvas
  123. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  124. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  125. ob_start();
  126. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  127. $newSummaryHtml = ob_get_contents();
  128. ob_end_clean();
  129. } else {
  130. $newContentData = json_decode($request->get('data'), true);
  131. if (isset($newContentData['value'])) {
  132. $newSummaryHtml = $newContentData['value'];
  133. }
  134. }
  135. $response = null;
  136. $data = [
  137. 'uid' => $section->uid,
  138. 'contentData' => json_encode($newContentData),
  139. 'summaryHtml' => $newSummaryHtml
  140. ];
  141. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  142. return [
  143. 'success' => $response['success'],
  144. 'newSummaryHtml' => $newSummaryHtml
  145. ];
  146. }
  147. // TODO move to utility
  148. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  149. {
  150. $url = config('stag.backendUrl') . $endPoint;
  151. $response = Http::asForm()
  152. ->withHeaders([
  153. 'sessionKey' => $request->cookie('sessionKey'),
  154. 'guestAccessCode' => $guestAccessCode
  155. ])
  156. ->post($url, $data)
  157. ->json();
  158. return $response;
  159. }
  160. }