|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/parassaas.edukrypt.in/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use App\Helpers\Frontend;
use Illuminate\Http\Request;
use App\Models\Studio;
use App\Models\Product;
use App\Models\Orderproductgroup;
use App\Models\Notification;
use App\Models\Schedule_live_product;
use Illuminate\Support\Facades\DB;
use DateTime;
class LiveScheduleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request, $prod_id)
{
$data['mainMenu'] = 'productManagement';
$data['subMenu'] = 'productList';
$admin_id = $request->session()->get('loggedIn')['id'];
$data['event_id'] = last(request()->segments());
$data['product'] = Product::where(['id' => $prod_id])->first();
// dd($data['product']);
if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
$search_text = $_GET['query'];
// dd($search_text);
$data['studios'] = DB::table('schedule_live_products')
// ->select('schedule_live_products.*', 'products.name as product_name', 'products.status as product_status')
// ->join('products', 'products.id', '=', 'schedule_live_products.prod_id')
->where('program_name', 'LIKE', '%' . $search_text . '%')->where(['admin_id' => $admin_id, 'prod_id' => $prod_id])->paginate(10);
// $data['product'] = Product::where(['admin_id' => $admin_id, 'id' => $prod_id])->first();
return view('admin.live-schedule.schedule-list', $data);
} else {
// DB::enableQueryLog();
$data['liveSchedules'] = Schedule_live_product::join('studios', 'studios.std_id', '=', 'schedule_live_products.studio_id')
// ->join('products', 'products.id', '=', 'schedule_live_products.prod_id')
->where(['schedule_live_products.admin_id' => $admin_id])
->where(['schedule_live_products.prod_id' => $prod_id])
->paginate(10, ['schedule_live_products.*']);
// $data['product'] = Product::where(['admin_id' => $admin_id, 'id' => $prod_id])->first();
return view('admin.live-schedule.schedule-list', $data);
}
}
// Live Chat
public function livechat(Request $request, $event_id, $sch_id)
{
// dd('dhe');
$admin_id = $request->session()->get('loggedIn')['id'];
$data['mainMenu'] = 'productManagement';
$data['subMenu'] = 'productList';
$data['chatid'] = $event_id;
$data['sch_id'] = $sch_id;
$data['admin_email'] = $request->session()->get('loggedIn')['email'];
$data['f_name'] = "";
$data['live_event'] = Schedule_live_product::where(['id' => $event_id])->get();
return view('teacher.chat', $data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request, $prod_id)
{
$admin_id = $request->session()->get('loggedIn')['id'];
$data['mainMenu'] = 'productManagement';
$data['subMenu'] = 'productList';
$data['studios'] = Studio::where(['admin_id' => $admin_id])->get();
$data['product'] = Product::where(['admin_id' => $admin_id, 'id' => $prod_id])->first();
return view('admin.live-schedule.add-schedule', $data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $prod_id)
{
//dd($request->input());
$admin_id = $request->session()->get('loggedIn')['id'];
$request->validate([
'std_id' => 'required',
'program_name' => 'required',
'seldate' => 'required',
'starttime' => 'required',
'endtime' => 'required',
]);
$studio_id = $request->std_id;
$one_or_recusive = $request->seldate;
$program_name = $request->program_name;
$_date_single = $request->datesingle;
$star_date = $request->star_date;
$end_date = $request->end_date;
$_start_time = $request->starttime;
$_end_time = $request->endtime;
$_start_date = $_date_single;
$_end_date = $_date_single;
if ($one_or_recusive == 1) {
$_start_date = date("Y-m-d", strtotime($star_date));
$_end_date = date("Y-m-d", strtotime($end_date));
}
if (empty($studio_id) || empty($_start_date) || empty($_end_date) || empty($_start_time) || empty($_end_time) || empty($program_name)) {
return back()
->with('error', 'Please Insert All Require Fields');
}
// DB::enableQueryLog();
$checkSchedule = Schedule_live_product::where(['program_name' => $program_name, 'prod_id' => $prod_id, 'studio_id' => $studio_id, 'admin_id' => $admin_id])->get();
if ($checkSchedule->isEmpty()) {
$flag = 0;
$CheckPrograms = Schedule_live_product::select('*')
->where('start_date', '<=', $_start_date)
->where('end_date', '>=', $_end_date)
->orwhereBetween('start_date', [$_start_date, $_end_date])
->orwhereBetween('end_date', [$_start_date, $_end_date])
->where('studio_id', '<=', $studio_id)
->where('prod_id', '<=', $prod_id)
->get();
// dd($CheckPrograms);
$data1['admin_id'] = $admin_id;
$data1['prod_id'] = $prod_id;
$data1['studio_id'] = $studio_id;
$data1['program_name'] = $program_name;
$data1['start_date'] = $_start_date;
$data1['end_date'] = $_end_date;
$data1['start_time'] = str_replace(' ', '', $_start_time);
$data1['end_time'] = str_replace(' ', '', $_end_time);
$st_dateTime = date('d-m-Y h:i A', strtotime($_start_date . " " . $_start_time));
$ed_dateTime = date('d-m-Y h:i A', strtotime($_end_date . " " . $_end_time));
if (!$CheckPrograms->isEmpty()) {
foreach ($CheckPrograms as $CheckProgram) {
$start_time = strtotime($CheckProgram->start_time);
$end_time = strtotime($CheckProgram->end_time);
$new_start_time = strtotime($_start_time);
$new_end_time = strtotime($_end_time);
$Check_studio_id = $CheckProgram->studio_id;
// 13 < 15 && 13 <= 16
if (($new_start_time <= $start_time) && ($start_time <= $new_end_time) && ($studio_id == $Check_studio_id)) {
$flag = 1;
}
if (($new_start_time <= $end_time) && ($end_time <= $new_end_time) && ($studio_id == $Check_studio_id)) {
$flag = 1;
}
if ($flag == 1) {
return back()
->with('error', 'This time range is not available in this date range!');
}
}
$res = Schedule_live_product::create($data1);
if ($res) {
$this->sendPushNoti($prod_id, $program_name, $st_dateTime, $ed_dateTime);
return back()
->with('success', $program_name . ' Has Been Added!.');
} else {
return back()
->with('error', 'Something Went Wrong!');
}
}
$res = Schedule_live_product::create($data1);
if ($res) {
$this->sendPushNoti($prod_id, $program_name, $st_dateTime, $ed_dateTime);
return back()
->with('success', $program_name . ' Has Been Added!.');
} else {
return back()
->with('error', 'Something Went Wrong!');
}
} else {
return back()
->with('error', 'Duplicate program name found in studio!');
}
}
public function sendPushNoti($prod_id, $program_name, $st_dateTime, $ed_dateTime)
{
$findUsers = Orderproductgroup::select('users.fcm_token', 'products.name')
->join('products', 'products.id', '=', 'orderproductgroups.product_id')
->join('users', 'users.user_unique_id', '=', 'orderproductgroups.user_unique_id')
->groupBy('orderproductgroups.user_unique_id')
->where('users.fcm_token', '<>', '', 'and')
->where(['orderproductgroups.product_id' => $prod_id])->get();
if (count($findUsers) > 0) {
$length = 1000;
$total = count($findUsers);
$pages = ceil($total / $length);
for ($i = 1; $i <= $pages; $i++) {
$a_token = array();
$offset = $i == 1 ? 0 : (($i - 1) * $length);
$find_limited_Users = Orderproductgroup::select('users.fcm_token', 'products.name')
->join('products', 'products.id', '=', 'orderproductgroups.product_id')
->join('users', 'users.user_unique_id', '=', 'orderproductgroups.user_unique_id')
->groupBy('orderproductgroups.user_unique_id')
->where('users.fcm_token', '<>', '', 'and')
->where(['orderproductgroups.product_id' => $prod_id])
->skip($offset)->take($length)->get();
foreach ($find_limited_Users as $find_limited_User) {
$a_token[] = $find_limited_User->fcm_token;
}
$productName = $findUsers[0]->name;
$message = "New Live Schedule $program_name @ $st_dateTime TO $ed_dateTime";
Frontend::PushNotification($a_token, $productName, $message);
}
$data = array(
'title' => $productName,
'notification' => $message,
'public' => 0,
'product_id' => $prod_id,
'created_at' => date('Y-m-d H:i:s')
);
Notification::create($data);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Schedule_live_product::where(['id' => $id])->delete();
return back()->with('success', 'Studio deleted successfully');
}
}