MessageController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Client;
  4. use App\Models\Pro;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\File;
  8. use App\Models\InternalMessage;
  9. use Ramsey\Collection\CollectionInterface;
  10. use OpenTok\MediaMode;
  11. use OpenTok\OpenTok;
  12. use Jenssegers\Agent\Agent;
  13. use Illuminate\Support\Facades\Storage;
  14. use Illuminate\Support\Str;
  15. use OpenTok\ArchiveMode;
  16. use OpenTok\OutputMode;
  17. class MessageController extends Controller
  18. {
  19. public function index(Request $request)
  20. {
  21. if($request->input('m')) {
  22. $im = InternalMessage::where('uid', $request->input('m'))->first();
  23. if($im) {
  24. return redirect(route('messages') . '?r=' . $im->regarding_client_id);
  25. }
  26. }
  27. $currentPro = $this->performer()->pro;
  28. $myProID = $currentPro->id;
  29. // SELECT * FROM internal_message WHERE regarding_client_id IN (SELECT shadow_client_id FROM pro WHERE hr_rep_pro_id = $myProID);
  30. $conversations = DB::select("
  31. SELECT * FROM (
  32. select
  33. distinct on (im.regarding_client_id) regarding_client_id, im.content_text, im.id,
  34. (c.name_first || ' ' || c.name_last) as regarding_client_name,
  35. (p.name_first) as from_pro_short_name,
  36. im.created_at,
  37. (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments,
  38. (select count(id) from internal_message imc
  39. where imc.regarding_client_id = im.regarding_client_id
  40. and imc.is_from_shadow_client is true
  41. and imc.is_removed = false
  42. and imc.is_read = false) as num_unread
  43. from internal_message im
  44. join client c on im.regarding_client_id = c.id
  45. join pro sp on c.shadow_pro_id = sp.id
  46. join pro p on im.from_pro_id= p.id
  47. where im.is_removed = false AND im.regarding_client_id IN (SELECT shadow_client_id FROM pro WHERE hr_rep_pro_id = ?)
  48. order by im.regarding_client_id, im.created_at desc
  49. ) as x ORDER BY x.created_at DESC", [$myProID]);
  50. $messages = [];
  51. $flattenedAttachments = [];
  52. $regardingClient = null;
  53. if($request->input('r')) {
  54. $regardingClient = Client::where('id', $request->input('r'))->first();
  55. }
  56. if($regardingClient) {
  57. $messages = DB::select("
  58. select
  59. im.uid, im.regarding_client_id, im.is_read, im.message_video_file_id, sf.uid as message_video_file_system_file_uid,
  60. im.content_text, im.id, im.from_pro_id, im.created_at,
  61. (p.name_first || ' ' || p.name_last) as from_name,
  62. im.is_from_shadow_client, im.is_to_shadow_client,
  63. im.is_removed, im.is_cleared, im.is_edited,
  64. im.original_content_text, im.cleared_content_text,
  65. (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments
  66. from internal_message im
  67. join pro p on im.from_pro_id = p.id
  68. left join system_file sf ON sf.id = im.message_video_file_id
  69. where im.regarding_client_id = ?
  70. order by im.created_at asc
  71. ",
  72. [$regardingClient->id]);
  73. }
  74. else {
  75. $regardingClient = null;
  76. }
  77. $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
  78. $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
  79. $otSessionId = $otSession->getSessionId();
  80. $otToken = $opentok->generateToken($otSessionId);
  81. $step = 'webcam-test';
  82. $agent = new Agent();
  83. $allow = !$agent->isPhone() && !$agent->isTablet();
  84. return view('app.messages.index', compact('conversations', 'regardingClient', 'messages', 'flattenedAttachments','otSessionId', 'otToken', 'step', 'allow'));
  85. }
  86. public function thread(Request $request)
  87. {
  88. $messages = [];
  89. $flattenedAttachments = [];
  90. $regardingClient = null;
  91. if($request->input('r')) {
  92. $regardingClient = Client::where('id', $request->input('r'))->first();
  93. }
  94. if($regardingClient) {
  95. $messages = DB::select("
  96. select
  97. im.uid, im.regarding_client_id, im.is_read, im.message_video_file_id, sf.uid as message_video_file_system_file_uid,
  98. im.content_text, im.id, im.from_pro_id, im.created_at,
  99. (p.name_first || ' ' || p.name_last) as from_name,
  100. im.is_from_shadow_client, im.is_to_shadow_client,
  101. im.is_removed, im.is_cleared, im.is_edited,
  102. im.original_content_text, im.cleared_content_text,
  103. (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments
  104. from internal_message im
  105. join pro p on im.from_pro_id = p.id
  106. left join system_file sf ON sf.id = im.message_video_file_id
  107. where im.regarding_client_id = ?
  108. order by im.created_at asc
  109. ",
  110. [$regardingClient->id]);
  111. }
  112. else {
  113. $regardingClient = null;
  114. }
  115. $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
  116. $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
  117. $otSessionId = $otSession->getSessionId();
  118. $otToken = $opentok->generateToken($otSessionId);
  119. return view('app.messages.thread', compact('regardingClient', 'messages', 'flattenedAttachments', 'otSessionId'));
  120. }
  121. public function proofread(Request $request)
  122. {
  123. $messages = [];
  124. $flattenedAttachments = [];
  125. $regardingClient = null;
  126. if($request->input('r')) {
  127. $regardingClient = Client::where('id', $request->input('r'))->first();
  128. }
  129. if($regardingClient) {
  130. $messages = DB::select("
  131. select
  132. im.uid, im.regarding_client_id, im.is_read, im.message_video_file_id, sf.uid as message_video_file_system_file_uid,
  133. im.content_text, im.id, im.from_pro_id, im.created_at,
  134. (p.name_first || ' ' || p.name_last) as from_name,
  135. im.is_from_shadow_client, im.is_to_shadow_client,
  136. im.is_removed, im.is_cleared, im.is_edited,
  137. im.original_content_text, im.cleared_content_text, im.proofreader_memo,
  138. (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments
  139. from internal_message im
  140. join pro p on im.from_pro_id = p.id
  141. left join system_file sf ON sf.id = im.message_video_file_id
  142. where im.regarding_client_id = ?
  143. order by im.created_at asc
  144. ",
  145. [$regardingClient->id]);
  146. }
  147. else {
  148. $regardingClient = null;
  149. }
  150. $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
  151. $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
  152. $otSessionId = $otSession->getSessionId();
  153. $otToken = $opentok->generateToken($otSessionId);
  154. return view('app.messages.proofread', compact('regardingClient', 'messages', 'flattenedAttachments', 'otSessionId'));
  155. }
  156. public function attachments(Request $request, InternalMessage $message) {
  157. if(!$message) return '';
  158. $output = [];
  159. foreach ($message->attachments as $attachment) {
  160. $output[] = '<a native target="_blank" ' .
  161. 'href="/api/internalMessageAttachment/download/' . $attachment->uid . '" ' .
  162. 'class="attachment text-sm my-1">' .
  163. '<i class="fa fa-paperclip"></i>&nbsp;' .
  164. $attachment->systemFile->file_name .
  165. '</a>';
  166. }
  167. return implode("", $output);
  168. }
  169. public function clients(Request $request)
  170. {
  171. $term = $request->input('term') ? trim($request->input('term')) : '';
  172. if (empty($term)) return '';
  173. $term = strtolower($term);
  174. $clients = $this->performer->pro->getAccessibleClientsQuery()
  175. ->where(function ($q) use ($term) {
  176. $q->orWhereRaw('LOWER(name_first::text) LIKE ?', ['%' . $term . '%'])
  177. ->orWhereRaw('LOWER(name_last::text) LIKE ?', ['%' . $term . '%']);
  178. })
  179. ->orderBy('name_last', 'asc')
  180. ->orderBy('name_first', 'asc')
  181. ->get();
  182. $clients = $clients->map(function($_client) {
  183. return [
  184. "uid" => $_client->uid,
  185. "id" => $_client->uid,
  186. "text" => $_client->displayName()
  187. ];
  188. });
  189. return json_encode([
  190. "results" => $clients
  191. ]);
  192. }
  193. public function sendFromPros(Request $request)
  194. {
  195. $term = $request->input('term') ? trim($request->input('term')) : '';
  196. if (empty($term)) return '';
  197. $term = strtolower($term);
  198. $results = Pro::where(function ($q) use ($term) {
  199. $q->orWhereRaw('LOWER(name_first::text) LIKE ?', ['%' . $term . '%'])
  200. ->orWhereRaw('LOWER(name_last::text) LIKE ?', ['%' . $term . '%']);
  201. })
  202. ->orderBy('name_last', 'asc')
  203. ->orderBy('name_first', 'asc')
  204. ->get();
  205. $pros = $results->map(function($_pro) {
  206. return [
  207. "uid" => $_pro->uid,
  208. "id" => $_pro->uid,
  209. "text" => $_pro->name_first.' '.$_pro->name_last
  210. ];
  211. });
  212. return json_encode([
  213. "results" => $pros
  214. ]);
  215. }
  216. }