NoteController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // TODO require
  55. $section_uid = $request->get('section_uid');
  56. $section = Section::where('uid', $section_uid)->first();
  57. $note = Note::where('id', $section->note_id)->first();
  58. $client = Client::where('id', $note->client_id)->first();
  59. $patient = $client;
  60. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  61. $newContentData = [];
  62. $newSummaryHtml = "";
  63. $sectionInternalName = $sectionTemplate->internal_name;
  64. if ($sectionTemplate->is_canvas) {
  65. $response = null;
  66. $data = [
  67. 'uid' => $client->uid,
  68. 'key' => $sectionTemplate->internal_name,
  69. 'data' => $request->get('data')
  70. ];
  71. $response = $this->calljava($request, '/client/updateCanvasData', $data);
  72. //TODO: handle $response->success == false
  73. $client = Client::where('id', $note->client_id)->first();
  74. $patient = $client;
  75. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  76. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  77. } else {
  78. $newContentData = json_decode($request->get('data'), true);
  79. }
  80. ob_start();
  81. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  82. $newSummaryHtml = ob_get_contents();
  83. ob_end_clean();
  84. // TODO call Java to update the canvas
  85. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  86. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  87. ob_start();
  88. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  89. $newSummaryHtml = ob_get_contents();
  90. ob_end_clean();
  91. } else {
  92. $newContentData = json_decode($request->get('data'), true);
  93. if (isset($newContentData['value'])) {
  94. $newSummaryHtml = $newContentData['value'];
  95. }
  96. }
  97. $response = null;
  98. $data = [
  99. 'uid' => $section->uid,
  100. 'contentData' => json_encode($newContentData),
  101. 'summaryHtml' => $newSummaryHtml
  102. ];
  103. $response = $this->calljava($request, '/section/update', $data);
  104. return [
  105. 'success' => $response['success'],
  106. 'newSummaryHtml' => $newSummaryHtml
  107. ];
  108. }
  109. // TODO move to utility
  110. private function callJava($request, $endPoint, $data)
  111. {
  112. $url = config('stag.backendUrl') . $endPoint;
  113. $response = Http::asForm()
  114. ->withHeaders(['sessionKey' => $request->cookie('sessionKey')])
  115. ->post($url, $data)
  116. ->json();
  117. return $response;
  118. }
  119. }