Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/www/app/Modules/Users/Http/Controllers/Admin/ |
<?php
namespace App\Modules\Users\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Users\Http\Requests\TherapistMessageRequest;
use App\Modules\Users\Http\Requests\TherapistMessageUpdRequest;
use App\Modules\Users\Repositories\MessageThreadRepository;
use App\User;
use Spatie\Permission\Models\Role;
use Carbon\Carbon;
// use Cmgmyr\Messenger\Models\Message;
// use Cmgmyr\Messenger\Models\Participant;
// use Cmgmyr\Messenger\Models\Thread;
use App\Modules\Users\Models\Message;
use App\Modules\Users\Models\Participant;
use App\Modules\Users\Models\Thread;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Laracasts\Flash\Flash;
class TherapistChats extends AdminController
{
/**
* Shows a message thread.
*
* @param $id
* @return mixed
*/
public function show($user_id,$id)
{
try {
$thread = Thread::findOrFail($id);
} catch (ModelNotFoundException $e) {
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
return redirect('admin.users.therapists.show',['id'=>$user_id]);
}
// show current user in list if not a current participant
// $users = User::whereNotIn('id', $thread->participantsUserIds())->get();
// don't show the current user in list
$userId = Auth::user()->id;
$therapist = User::find($user_id);
// $thread->markAsRead($userId);
Participant::where('thread_id', $id)
->where('user_id', $userId)
->update([
'last_read' => Carbon::now(),
]);
//create data array
$this->data['thread'] = $thread;
$this->data['therapist'] = $therapist;
$this->data['allMessages'] = $thread ? $thread->messages()->orderBy('created_at', 'asc')->get() : [];
// dd($this->data['allMessages']);
//show page
return view('users::admin.therapists_message_chat', $this->data);
}
/**
* Creates a new message thread.
*
* @return mixed
*/
public function create($user_id)
{
$therapist = User::find($user_id);
$therapist->first_name = $therapist->profile->first_name;
$therapist->last_name = $therapist->profile->last_name;
$therapist->mobile_number = $therapist->profile->mobile_number;
$therapist->about = $therapist->profile->about;
$therapist->role = $therapist->roles->pluck('name')->first();
$users = User::where('id', '!=', Auth::user()->id)->get();
$this->data['therapist'] = $therapist;
$this->data['users'] = $users;
//show page
return view('users::admin.therapists_message_create', $this->data);
}
/**
* Stores a new message thread.
*
* @return mixed
*/
public function store(TherapistMessageRequest $request)
{
$input = $request->all();
//begin transaction
DB::beginTransaction();
//create thread
$thread = Thread::create(
[
'subject' => $input['subject'],
]
);
// create message
Message::create(
[
'thread_id' => $thread->id,
'user_id' => Auth::user()->id,
'body' => $input['message'],
]
);
//add sender to theread as participant
Participant::create(
[
'thread_id' => $thread->id,
'user_id' => Auth::user()->id,
'last_read' => new Carbon,
]
);
//add recipients
if ($request->has('recipients')) {
$thread->addParticipant($input['recipients']);
}
if ($thread){
DB::commit();
//send email to participants
$threadRepo = new MessageThreadRepository($thread);
$threadRepo->new_message_participants(\Auth::user()->id);
}
else{
DB::rollBack();
}
//redirect
if ($request->save) {
//redirect to chat details page
return redirect(route('admin.users.therapist.messages.show',['id'=>$thread->id, 'user_id'=>$input['recipients']]));
} elseif ($request->save_exit) {
//redirect to therapist page
return redirect(route('admin.users.therapist.messages.show',['id'=>$thread->id, 'user_id'=>$input['recipients']]));
}
}
/**
* Adds a new message to a current thread.
*
* @param $id
* @return mixed
*/
public function update(TherapistMessageUpdRequest $request, $user_id, $id)
{
try {
$thread = Thread::findOrFail($id);
} catch (ModelNotFoundException $e) {
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
return redirect('messages');
}
$thread->activateAllParticipants();
//begin transaction
DB::beginTransaction();
// Message
Message::create(
[
'thread_id' => $thread->id,
'user_id' => Auth::id(),
'body' => Input::get('message'),
]
);
// Add replier as a participant
$participant = Participant::firstOrCreate(
[
'thread_id' => $thread->id,
'user_id' => Auth::user()->id,
]
);
$participant->last_read = new Carbon;
$participant->save();
// Recipients
if (Input::has('recipients')) {
$thread->addParticipant(Input::get('recipients'));
}
if ($thread){
DB::commit();
//send email
$threadRepo = new MessageThreadRepository($thread);
$threadRepo->new_message_participants(\Auth::user()->id);
}
else{
DB::rollBack();
}
//redirect to page
return redirect(route('admin.users.therapist.messages.show',['user_id'=>$user_id,'id'=>$id]));
}
}