123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Client;
- use App\Models\Pro;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\File;
- use App\Models\InternalMessage;
- use Ramsey\Collection\CollectionInterface;
- use OpenTok\MediaMode;
- use OpenTok\OpenTok;
- use Jenssegers\Agent\Agent;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use OpenTok\ArchiveMode;
- use OpenTok\OutputMode;
- class MessageController extends Controller
- {
- public function index(Request $request)
- {
- if($request->input('m')) {
- $im = InternalMessage::where('uid', $request->input('m'))->first();
- if($im) {
- return redirect(route('messages') . '?r=' . $im->regarding_client_id);
- }
- }
- $currentPro = $this->performer()->pro;
- $myProID = $currentPro->id;
-
- // SELECT * FROM internal_message WHERE regarding_client_id IN (SELECT shadow_client_id FROM pro WHERE hr_rep_pro_id = $myProID);
- $conversations = DB::select("
- SELECT * FROM (
- select
- distinct on (im.regarding_client_id) regarding_client_id, im.content_text, im.id,
- (c.name_first || ' ' || c.name_last) as regarding_client_name,
- (p.name_first) as from_pro_short_name,
- im.created_at,
- (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments,
- (select count(id) from internal_message imc
- where imc.regarding_client_id = im.regarding_client_id
- and imc.is_from_shadow_client is true
- and imc.is_removed = false
- and imc.is_read = false) as num_unread
- from internal_message im
- join client c on im.regarding_client_id = c.id
- join pro sp on c.shadow_pro_id = sp.id
- join pro p on im.from_pro_id= p.id
- where im.is_removed = false AND im.regarding_client_id IN (SELECT shadow_client_id FROM pro WHERE hr_rep_pro_id = ?)
- order by im.regarding_client_id, im.created_at desc
- ) as x ORDER BY x.created_at DESC", [$myProID]);
- $messages = [];
- $flattenedAttachments = [];
- $regardingClient = null;
- if($request->input('r')) {
- $regardingClient = Client::where('id', $request->input('r'))->first();
- }
- if($regardingClient) {
- $messages = DB::select("
- select
- im.uid, im.regarding_client_id, im.is_read, im.message_video_file_id, sf.uid as message_video_file_system_file_uid,
- im.content_text, im.id, im.from_pro_id, im.created_at,
- (p.name_first || ' ' || p.name_last) as from_name,
- im.is_from_shadow_client, im.is_to_shadow_client,
- im.is_removed, im.is_cleared, im.is_edited,
- im.original_content_text, im.cleared_content_text,
- (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments
- from internal_message im
- join pro p on im.from_pro_id = p.id
- left join system_file sf ON sf.id = im.message_video_file_id
- where im.regarding_client_id = ?
- order by im.created_at asc
- ",
- [$regardingClient->id]);
- }
- else {
- $regardingClient = null;
- }
- $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
- $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
- $otSessionId = $otSession->getSessionId();
- $otToken = $opentok->generateToken($otSessionId);
-
- $step = 'webcam-test';
- $agent = new Agent();
- $allow = !$agent->isPhone() && !$agent->isTablet();
- return view('app.messages.index', compact('conversations', 'regardingClient', 'messages', 'flattenedAttachments','otSessionId', 'otToken', 'step', 'allow'));
- }
- public function thread(Request $request)
- {
- $messages = [];
- $flattenedAttachments = [];
- $regardingClient = null;
- if($request->input('r')) {
- $regardingClient = Client::where('id', $request->input('r'))->first();
- }
- if($regardingClient) {
- $messages = DB::select("
- select
- im.uid, im.regarding_client_id, im.is_read, im.message_video_file_id, sf.uid as message_video_file_system_file_uid,
- im.content_text, im.id, im.from_pro_id, im.created_at,
- (p.name_first || ' ' || p.name_last) as from_name,
- im.is_from_shadow_client, im.is_to_shadow_client,
- im.is_removed, im.is_cleared, im.is_edited,
- im.original_content_text, im.cleared_content_text,
- (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments
- from internal_message im
- join pro p on im.from_pro_id = p.id
- left join system_file sf ON sf.id = im.message_video_file_id
- where im.regarding_client_id = ?
- order by im.created_at asc
- ",
- [$regardingClient->id]);
- }
- else {
- $regardingClient = null;
- }
- $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
- $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
- $otSessionId = $otSession->getSessionId();
- $otToken = $opentok->generateToken($otSessionId);
- return view('app.messages.thread', compact('regardingClient', 'messages', 'flattenedAttachments', 'otSessionId'));
- }
- public function proofread(Request $request)
- {
- $messages = [];
- $flattenedAttachments = [];
- $regardingClient = null;
- if($request->input('r')) {
- $regardingClient = Client::where('id', $request->input('r'))->first();
- }
- if($regardingClient) {
- $messages = DB::select("
- select
- im.uid, im.regarding_client_id, im.is_read, im.message_video_file_id, sf.uid as message_video_file_system_file_uid,
- im.content_text, im.id, im.from_pro_id, im.created_at,
- (p.name_first || ' ' || p.name_last) as from_name,
- im.is_from_shadow_client, im.is_to_shadow_client,
- im.is_removed, im.is_cleared, im.is_edited,
- im.original_content_text, im.cleared_content_text, im.proofreader_memo,
- (select count(ima.id) from internal_message_attachment ima where ima.internal_message_id = im.id) as num_attachments
- from internal_message im
- join pro p on im.from_pro_id = p.id
- left join system_file sf ON sf.id = im.message_video_file_id
- where im.regarding_client_id = ?
- order by im.created_at asc
- ",
- [$regardingClient->id]);
- }
- else {
- $regardingClient = null;
- }
- $opentok = new OpenTok(config('app.opentokApiKey'),config('app.opentokApiSecret'));
- $otSession = $opentok->createSession(array('mediaMode' => MediaMode::ROUTED));
- $otSessionId = $otSession->getSessionId();
- $otToken = $opentok->generateToken($otSessionId);
- return view('app.messages.proofread', compact('regardingClient', 'messages', 'flattenedAttachments', 'otSessionId'));
- }
- public function attachments(Request $request, InternalMessage $message) {
- if(!$message) return '';
- $output = [];
- foreach ($message->attachments as $attachment) {
- $output[] = '<a native target="_blank" ' .
- 'href="/api/internalMessageAttachment/download/' . $attachment->uid . '" ' .
- 'class="attachment text-sm my-1">' .
- '<i class="fa fa-paperclip"></i> ' .
- $attachment->systemFile->file_name .
- '</a>';
- }
- return implode("", $output);
- }
- public function clients(Request $request)
- {
- $term = $request->input('term') ? trim($request->input('term')) : '';
- if (empty($term)) return '';
- $term = strtolower($term);
- $clients = $this->performer->pro->getAccessibleClientsQuery()
- ->where(function ($q) use ($term) {
- $q->orWhereRaw('LOWER(name_first::text) LIKE ?', ['%' . $term . '%'])
- ->orWhereRaw('LOWER(name_last::text) LIKE ?', ['%' . $term . '%']);
- })
- ->orderBy('name_last', 'asc')
- ->orderBy('name_first', 'asc')
- ->get();
- $clients = $clients->map(function($_client) {
- return [
- "uid" => $_client->uid,
- "id" => $_client->uid,
- "text" => $_client->displayName()
- ];
- });
- return json_encode([
- "results" => $clients
- ]);
- }
- public function sendFromPros(Request $request)
- {
- $term = $request->input('term') ? trim($request->input('term')) : '';
- if (empty($term)) return '';
- $term = strtolower($term);
- $results = Pro::where(function ($q) use ($term) {
- $q->orWhereRaw('LOWER(name_first::text) LIKE ?', ['%' . $term . '%'])
- ->orWhereRaw('LOWER(name_last::text) LIKE ?', ['%' . $term . '%']);
- })
- ->orderBy('name_last', 'asc')
- ->orderBy('name_first', 'asc')
- ->get();
- $pros = $results->map(function($_pro) {
- return [
- "uid" => $_pro->uid,
- "id" => $_pro->uid,
- "text" => $_pro->name_first.' '.$_pro->name_last
- ];
- });
- return json_encode([
- "results" => $pros
- ]);
- }
- }
|