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\ClientMessageRequest;
use App\Modules\Users\Http\Requests\ClientMessageUpdRequest;
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 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 SalonChats 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;
$users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get();
$salon = User::find($user_id);
$thread->markAsRead($userId);
//create data array
$this->data['thread'] = $thread;
$this->data['salon'] = $salon;
//show page
return view('users::admin.salon_message_chat', $this->data);
}
/**
* Creates a new message thread.
*
* @return mixed
*/
public function create($user_id)
{
$salon = User::find($user_id);
$salon->first_name = $salon->profile->first_name;
$salon->last_name = $salon->profile->last_name;
$salon->mobile_number = $salon->profile->mobile_number;
$salon->about = $salon->profile->about;
$salon->role = $salon->getRoles()->first()->name;
$users = User::where('id', '!=', Auth::user()->id)->get();
$this->data['salon'] = $salon;
$this->data['users'] = $users;
//show page
return view('users::admin.salon_message_create', $this->data);
}
/**
* Stores a new message thread.
*
* @return mixed
*/
public function store(ClientMessageRequest $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 mail 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.salon.messages.show',['id'=>$thread->id]));
} elseif ($request->save_exit) {
//redirect to therapist page
return redirect(route('admin.users.salon.show',['id'=>$input['recipients']]));
}
}
/**
* Adds a new message to a current thread.
*
* @param $id
* @return mixed
*/
public function update(ClientMessageUpdRequest $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 mail to participants
$threadRepo = new MessageThreadRepository($thread);
$threadRepo->new_message_participants(\Auth::user()->id);
}
else{
DB::rollBack();
}
//redirect to page
return redirect(route('admin.users.salon.messages.show',['user_id'=>$user_id,'id'=>$id]));
}
}