NoteController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Http;
  5. use App\HttpModels\ClientLobbyModel;
  6. use App\HttpModels\MeetingModel;
  7. use App\Models\AppSession;
  8. use Cookie;
  9. use App\Models\Note;
  10. use App\Models\Client;
  11. use App\Models\Section;
  12. use App\Models\SectionTemplate;
  13. class NoteController extends Controller
  14. {
  15. public function renderNote($noteUid, Request $request)
  16. {
  17. $note = Note::where('uid', $noteUid)->first();
  18. $client = Client::where('id', $note->client_id)->first();
  19. return view('client/note', compact('note', 'client'));
  20. }
  21. public function selectSectionTemplateForm($note_uid, Request $request)
  22. {
  23. $sectionTemplates = SectionTemplate::all();
  24. $note = Note::where('uid', $note_uid)->first();
  25. return view('client/select_section_template_form', compact('note', 'sectionTemplates'));
  26. }
  27. public function sectionCreateForm($note_uid, $section_template_uid, Request $request)
  28. {
  29. $note = Note::where('uid', $note_uid)->first();
  30. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  31. $section = null; // convenience
  32. include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
  33. }
  34. public function sectionUpdateForm($section_uid, Request $request)
  35. {
  36. $section = Section::where('uid', $section_uid)->first();
  37. $note = Note::where('id', $section->note_id)->first();
  38. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  39. include(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'));
  40. }
  41. public function processFormSubmit(Request $request)
  42. {
  43. // for CREATE
  44. $note_uid = $request->note_uid;
  45. $section_template_uid = $request->section_template_uid;
  46. // for UPDATE
  47. $section_uid = $request->section_uid;
  48. $section = $section_uid ? Section::where('uid', $section_uid)->first() : null;
  49. $note = null;
  50. $sectionTemplate = null;
  51. if($section == null){
  52. $note = Note::where('uid', $note_uid)->first();
  53. $sectionTemplate = SectionTemplate::where('uid', $section_template_uid)->first();
  54. } else {
  55. $note = Note::where('id', $section->note_id)->first();
  56. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  57. }
  58. $newContentData = [];
  59. // we wish to pass THESE arguments into this include:
  60. // if CREATE, $note and $sectionTemplate, and $request
  61. // if UPDATE, $section, and $request
  62. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  63. // now, create summaryHtml appropriate
  64. ob_start();
  65. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  66. $newSummaryHtml = ob_get_contents();
  67. ob_end_clean();
  68. if($section){
  69. // call Java to update section
  70. $data = [
  71. 'uid' => $section->uid,
  72. 'contentData' => json_encode($newContentData),
  73. 'summaryHtml' => $newSummaryHtml
  74. ];
  75. $response = $this->calljava($request, '/api/section/update', $data);
  76. //TODO: handle if response->success == false
  77. }else{
  78. // call Java to create section
  79. $data = [
  80. 'noteUid' => $note->uid,
  81. 'sectionTemplateUid' => $sectionTemplate->uid,
  82. 'contentData' => json_encode($newContentData),
  83. 'summaryHtml' => $newSummaryHtml
  84. ];
  85. $response = $this->callJava($request, '/api/section/create', $data);
  86. //TODO: handle if response->success == false
  87. }
  88. return redirect(route('render-note',$note->uid));
  89. }
  90. private function callJava($request, $endPoint, $data){
  91. $url = env('BACKEND_URL', 'http://localhost:8080') . $endPoint;
  92. $response = Http::asForm()
  93. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  94. ->post($url, $data)
  95. ->json();
  96. return $response;
  97. }
  98. }