208 lines
7.9 KiB
PHP
208 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\Chat;
|
|
use App\Models\User;
|
|
use App\Models\UserKyc;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
class ManageChatController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('Admin.Pages.manage_chat.manage_chat', [
|
|
'users' => $this->getUserList(1),
|
|
'chat' => Chat::with(['user', 'admin'])->where('user_id', auth()->user()->id)->get()
|
|
]);
|
|
}
|
|
|
|
public function getUserList($initial = null)
|
|
{
|
|
$usersId = Chat::query()
|
|
->latest()
|
|
->pluck('user_id')
|
|
->filter()
|
|
->unique();
|
|
|
|
$users = collect();
|
|
foreach ($usersId as $id) {
|
|
$user = User::query()
|
|
->withCount('total')
|
|
->where('id', $id)
|
|
->first();
|
|
$users->push($user);
|
|
}
|
|
if($initial === 1){
|
|
return $users;
|
|
}
|
|
$userList = View::make('Admin.Pages.manage_chat.user-list')->with('users', $users)->render();
|
|
return $userList;
|
|
// return response()->json(['userlist' => $userList]);
|
|
}
|
|
|
|
public function initialUsers($search = null)
|
|
{
|
|
return User::query()
|
|
->when($search !== null, function ($query) use ($search) {
|
|
$query->where('name', 'like', '%' . $search . '%');
|
|
})
|
|
->withCount('total')
|
|
->orderBy('total_count', 'desc')
|
|
->get();
|
|
}
|
|
|
|
public function getUser(Request $request)
|
|
{
|
|
$id = $request->id;
|
|
// $user = User::find($id);
|
|
$user = User::where('id', $id)->first();
|
|
// $userKYC = UserKyc::where('users_id',$id)->exists();
|
|
$kycStatus = UserKyc::where('users_id', $id)->exists() == true ? UserKyc::where('users_id', $id)->value('status') : 'Not Submitted';
|
|
$userPage = View::make('Admin.Pages.manage_chat.user-details')->with('user', $user)->with('kycStatus', $kycStatus)->render();
|
|
$chat = Chat::where('user_id', $id)->get();
|
|
$updateTimestamp = Chat::where(['user_id' => $id, 'read_at' => null])->update([
|
|
'read_at' => Carbon::now()
|
|
]);
|
|
foreach ($chat as $data) {
|
|
if ($data->file !== null) {
|
|
$data['filename'] = $data->getRawOriginal('file');
|
|
}
|
|
}
|
|
$username = "<div class='card-header justify-content-center' id='kt_drawer_chat_messenger_header'>
|
|
<a href='#' class='fs-4 fw-bold text-gray-900 text-hover-primary'>$user->name</a>
|
|
</div>
|
|
</div>
|
|
<hr>";
|
|
$chatPage = View::make('Admin.Pages.manage_chat.chat')->with('chat', $chat)->with('user', $user)->render();
|
|
return response()->json(['user' => $userPage, 'chat' => $chatPage, 'username' => $username]);
|
|
}
|
|
|
|
public function getChat(Request $request)
|
|
{
|
|
$id = $request->id;
|
|
// return $id;
|
|
$chat = Chat::where('user_id', $id)->get();
|
|
$chatPage = View::make('Admin.Pages.manage_chat.chat')->with('user', $chat);
|
|
return $chatPage;
|
|
}
|
|
|
|
public function sendAdminMsg(Request $request)
|
|
{
|
|
if (!$request->message) {
|
|
return response()->json(['status' => 400, 'message' => 'Please Drop A Message!']);
|
|
}
|
|
|
|
$addChat = Chat::create([
|
|
'user_id' => $request->user_id,
|
|
'admin_id' => auth()->user()->id,
|
|
'message' => $request->message,
|
|
'by' => 'Admin'
|
|
]);
|
|
|
|
if ($addChat) {
|
|
return response()->json(['status' => 200, 'message' => 'Chat Send']);
|
|
}
|
|
return response()->json(['status' => 200, 'message' => 'Error Sending Message']);
|
|
}
|
|
|
|
public function usersDetail(Request $request)
|
|
{
|
|
$search = $request->search;
|
|
// $userData = $this->initialUsers($search);
|
|
$userData = $this->getUserList(1)->toArray();
|
|
$searchResults=[];
|
|
// $count = 0;
|
|
foreach ($userData as $user) {
|
|
// dd(strpos(strtolower($user['name']), strtolower($search)) !== false);
|
|
// Check if the 'name' field is like the search string
|
|
if (strpos(strtolower($user['name']), strtolower($search)) !== false) {
|
|
// If the name matches, add the user data to the search results array
|
|
$searchResults[] = $user;
|
|
}
|
|
}
|
|
// dd($searchResults);
|
|
// dd($searchResults, $search);
|
|
// $userData->filter(function($key) use ($search){
|
|
// // return $key->name == "ravindra gawade";
|
|
// if($key->name != null)
|
|
// {
|
|
// return preg_match("/$search/",$key['name']);
|
|
// }
|
|
// // return preg_match("/one/","one");
|
|
// });
|
|
// dd($searchResults);
|
|
$data = '';
|
|
// if($searchResults)
|
|
// {
|
|
foreach($searchResults as $user) {
|
|
// dd($user['profile_image']);
|
|
|
|
// if($user['profile_image'] != null)
|
|
// {
|
|
// $profile_image = "public/uploads/profile/img/" . $user->getRawOriginal('profile_image');
|
|
// }else{
|
|
// $profile_image = 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png';
|
|
// }
|
|
// $profile_image = $user['profile_image'] != null ? "public/uploads/profile/img/" . $user->getRawOriginal('profile_image') : 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png';
|
|
$profile_image = $user['profile_image'] != null ? $user['profile_image'] : 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png';
|
|
$user_name = $user['name'];
|
|
$user_email = $user['email'];
|
|
$user_id = $user['id'];
|
|
|
|
$data .= " " . "
|
|
<div class='d-flex flex-stack py-4'>
|
|
<div class='d-flex align-items-center'>
|
|
<div class='symbol symbol-45px symbol-circle'>
|
|
<img alt='Pic' src='$profile_image' />
|
|
";
|
|
if ($user['total_count']) {
|
|
$count = $user['total_count'];
|
|
$data .= "<span class='symbol-badge badge badge-sm badge-circle badge-white badge-outline bg-danger start-100'>$count</span>";
|
|
}
|
|
$data .= "</div>";
|
|
|
|
$data .= "<div class='ms-5'>
|
|
<a href='javascript:void(0)' class='fs-5 fw-bold text-gray-900 text-hover-primary mb-2' onclick='getUser($user_id)'>$user_name</a>
|
|
<div class='fw-semibold text-muted'>$user_email</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
";
|
|
// }
|
|
}
|
|
// }
|
|
|
|
if ($data) {
|
|
return $data;
|
|
} else {
|
|
return "No data found";
|
|
}
|
|
}
|
|
|
|
public function getMessageCount(Request $request)
|
|
{
|
|
$id = auth('sanctum')->user()->id;
|
|
$chat = Chat::where(['user_id'=>$id,'is_read'=>false])->whereNotNull('admin_id')->count();
|
|
return response()->json([
|
|
'data'=>$chat,
|
|
]);
|
|
}
|
|
|
|
public function getAllMessage(Request $request)
|
|
{
|
|
$id = auth('sanctum')->user()->id;
|
|
$getChat = Chat::where(['user_id'=>$id,'is_read'=>false])->get();
|
|
foreach($getChat as $singleChat)
|
|
{
|
|
// return response()->json(['data'=>$singleChat]);
|
|
$updateChat = Chat::where('id',$singleChat->id)->update(['is_read'=>true,'read_at'=>Carbon::now()]);
|
|
}
|
|
return response()->json(['data'=>Chat::where(['user_id'=>$id])->get()]);
|
|
// $chat = Chat::where('user_id',$id)->update
|
|
}
|
|
}
|