|
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 Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Notification;
use App\Models\User;
use App\Helpers\Frontend;
use App\Models\Product;
class NotificationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$data['mainMenu'] = 'notificationManagement';
$data['subMenu'] = 'notificationList';
$admin_id = $request->session()->get('loggedIn')['id'];
if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
$search_text = $_GET['query'];
$data['notifications'] = DB::table('notifications')
->select('notifications.*', 'products.name as product_name')
->leftjoin('products', 'products.id', '=', 'notifications.product_id')
->where('title', 'LIKE', '%' . $search_text . '%')->paginate(10);
} else {
$data['notifications'] = DB::table('notifications')
->select('notifications.*', 'products.name as product_name')
->leftjoin('products', 'products.id', '=', 'notifications.product_id')->paginate(10);
}
return view('admin.notification.notification-list', $data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
{
$admin_id = $request->session()->get('loggedIn')['id'];
$data['mainMenu'] = 'notificationManagement';
$data['subMenu'] = 'addNotification';
$data['products'] = Product::where(['status' => 1])->get();
return view('admin.notification.add-notification', $data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$admin_id = $request->session()->get('loggedIn')['id'];
$request->validate([
'product_id' => 'required',
'title' => 'required',
'notification' => 'required'
]);
// ProductWise Notification
if ($request->product_id > 0) {
$fcmToken = User::select('users.*')
->join('billings', 'billings.user_unique_id', '=', 'users.user_unique_id')
->join('orderproductgroups', 'orderproductgroups.billing_id', '=', 'billings.id')
->where(['orderproductgroups.product_id' => $request->product_id, 'billings.payment_status' => 'Credit'])
->whereNotNull('users.fcm_token')
->groupBy('users.id')
->get();
if (!empty($fcmToken)) {
$total = count($fcmToken);
$pages = ceil($total / 1000);
for ($i = 1; $i <= $pages; $i++) {
$a_token = array();
$start = 1000;
if ($i == 1) {
$fcmTokens = User::select('users.*')
->join('billings', 'billings.user_unique_id', '=', 'users.user_unique_id')
->join('orderproductgroups', 'orderproductgroups.billing_id', '=', 'billings.id')
->where(['orderproductgroups.product_id' => $request->product_id, 'billings.payment_status' => 'Credit'])
->whereNotNull('users.fcm_token')
->groupBy('users.id')
->skip(0)->take($start)
->get();
//$fcmTokens = User::whereNotNull('fcm_token')->skip(0)->take($start)->get();
foreach ($fcmTokens as $value) {
if (!empty($value->fcm_token)) {
$a_token[] = $value->fcm_token;
}
}
$response = Frontend::PushNotification($a_token, $request->title, $request->notification);
} else {
$start1 = ($i * $start) - $start;
$end = ($i * $start);
$fcmTokens = User::select('users.*')
->join('billings', 'billings.user_unique_id', '=', 'users.user_unique_id')
->join('orderproductgroups', 'orderproductgroups.billing_id', '=', 'billings.id')
->where(['orderproductgroups.product_id' => $request->product_id, 'billings.payment_status' => 'Credit'])
->whereNotNull('users.fcm_token')
->groupBy('users.id')
->skip($start1)->take($end)
->get();
// $fcmTokens = User::whereNotNull('fcm_token')->skip($start1)->take($end)->get();
foreach ($fcmTokens as $value) {
if (!empty($value->fcm_token)) {
$a_token[] = $value->fcm_token;
}
}
$response = Frontend::PushNotification($a_token, $request->title, $request->notification);
}
}
$data = array(
'title' => $request->title,
'notification' => $request->notification,
'product_id' => $request->product_id,
'public' => 0,
'created_at' => date('Y-m-d H:i:s'),
);
$res = Notification::create($data);
if ($res) {
return back()
->with('success', 'Notification Has Been Sent!.');
} else {
return back()
->with('error', 'Something Went Wrong!');
}
}
} else {
// Public notification
$fcmToken = User::whereNotNull('fcm_token')->get();
if (!empty($fcmToken)) {
$total = count($fcmToken);
$pages = ceil($total / 1000);
for ($i = 1; $i <= $pages; $i++) {
$a_token = array();
$start = 1000;
if ($i == 1) {
$fcmTokens = User::whereNotNull('fcm_token')->skip(0)->take($start)->get();
foreach ($fcmTokens as $value) {
if (!empty($value->fcm_token)) {
$a_token[] = $value->fcm_token;
}
}
$response = Frontend::PushNotification($a_token, $request->title, $request->notification);
} else {
$start1 = ($i * $start) - $start;
$end = ($i * $start);
$fcmTokens = User::whereNotNull('fcm_token')->skip($start1)->take($end)->get();
foreach ($fcmTokens as $value) {
if (!empty($value->fcm_token)) {
$a_token[] = $value->fcm_token;
}
}
$response = Frontend::PushNotification($a_token, $request->title, $request->notification);
}
}
$data = array(
'title' => $request->title,
'notification' => $request->notification,
'product_id' => null,
'public' => 1,
'created_at' => date('Y-m-d H:i:s'),
);
$res = Notification::create($data);
if ($res) {
return back()
->with('success', 'Notification Has Been Sent!.');
} else {
return back()
->with('error', 'Something Went Wrong!');
}
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$data['mainMenu'] = 'notificationManagement';
$data['subMenu'] = 'notificationList';
$data['notification'] = Notification::where(['id' => $id])->first();
return view('admin.notification.view-notification', $data);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$data['mainMenu'] = 'notificationManagement';
$data['subMenu'] = 'notificationList';
$data['notification'] = Notification::where(['id' => $id])->first();
return view('admin.notification.edit-notification', $data);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
// dd($request->input());
$admin_id = $request->session()->get('loggedIn')['id'];
$request->validate([
'title' => 'required',
'notification' => 'required',
]);
$data = array(
'title' => $request->title,
'notification' => $request->notification,
);
$res = Notification::where(['id' => $id])->update($data);
if ($res) {
return back()
->with('success', $request->title . ' Has Been Added!.');
} else {
return back()
->with('error', 'Something Went Wrong!');
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Notification::where(['id' => $id])->delete();
return back()->with('success', 'Notification deleted successfully');
}
// private function PushNotification($registation_ids, $title, $message, $icon)
// {
// $url = 'https://fcm.googleapis.com/fcm/send';
// $registration_ids = $registation_ids;
// $title = htmlspecialchars($title, ENT_COMPAT);
// $message = htmlspecialchars($message, ENT_COMPAT);
// $icon = htmlspecialchars($icon, ENT_COMPAT);
// // Push Datas
// $fields = array(
// "registration_ids" => $registration_ids,
// "notification" => array(
// "title" => "{$title}",
// "body" => "{$message}",
// "icon" => "{$icon}"
// )
// );
// // Update your Google Cloud Messaging API Key
// if (!defined('GOOGLE_API_KEY')) {
// define("GOOGLE_API_KEY", "AAAAsez_Lf0:APA91bG1RNmbdArdeKtNuLCh5kkEDPQS9N2owZC5IgFd67DBsSbFt7Ty29B3YYcHrgy1u8dNaZod1ugX0nZLLrEVazpxGf8TbzPMpO96mCydUGRJIAaTJj5zAdAKg-0NS0n-z-r0yrW_");
// }
// $headers = array(
// 'Authorization: key=' . GOOGLE_API_KEY,
// 'Content-Type: application/json'
// );
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
// // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// $result = curl_exec($ch);
// // print_r($result);exit;
// if ($result === false) {
// exit('Curl failed: ' . curl_error($ch));
// }
// curl_close($ch);
// return $result;
// }
}