NoteController.php 7.3 KB

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