Files
cheerstothe_season_2.0/app/Http/Controllers/Admin/ManageNotificationsController.php

276 lines
11 KiB
PHP
Raw Normal View History

2024-05-23 15:20:21 +05:30
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
2024-06-11 16:03:19 +05:30
use App\Models\IamPrincipal;
2024-06-11 15:09:59 +05:30
use App\Models\NotificationDetails;
2024-05-23 15:20:21 +05:30
use Illuminate\Http\Request;
2024-06-11 16:03:19 +05:30
use Exception;
use Illuminate\Support\Facades\Log;
use App\Helpers\onesignalhelper;
2024-06-18 12:46:04 +05:30
use App\Models\ManageState;
2024-07-08 19:47:25 +05:30
use App\Models\Subscriptions;
2024-06-11 16:03:19 +05:30
use Illuminate\Support\Facades\DB;
2024-05-23 15:20:21 +05:30
class ManageNotificationsController extends Controller
{
2024-06-11 15:09:59 +05:30
public function index(Request $request)
{
/**
* Created By : Sayli Raut
* Created at : 11 June 2024
* Use : To list notifications.
*/
$activeQuery = $request->query('active');
2024-05-23 15:20:21 +05:30
2024-06-11 15:09:59 +05:30
if ($activeQuery == 4) { // for customer
$notifications = NotificationDetails::with('Notification')
2024-07-10 14:47:41 +05:30
->where('is_active', 1)
2024-06-11 15:09:59 +05:30
->whereHas('Notification', function ($query) {
$query->where('principal_type_xid', 3);
})
->latest()
->take(12)
->get();
} else if ($activeQuery == 3) { // for restaurant
$notifications = NotificationDetails::with('Notification')
2024-07-10 14:47:41 +05:30
->where('is_active', 1)
2024-06-11 15:09:59 +05:30
->whereHas('Notification', function ($query) {
$query->where('principal_type_xid', 4);
})
->latest()
->take(12)
->get();
// return $notifications;
} else {
$notificationsOfType3 = NotificationDetails::with('Notification')
2024-07-10 14:47:41 +05:30
->where('is_active', 1)
2024-06-11 15:09:59 +05:30
->whereHas('Notification', function ($query) {
$query->where('principal_type_xid', 3);
})
->latest()
->take(12)
->get();
$notificationsOfType4 = NotificationDetails::with('Notification')
2024-07-10 14:47:41 +05:30
->where('is_active', 1)
2024-06-11 15:09:59 +05:30
->whereHas('Notification', function ($query) {
$query->where('principal_type_xid', 4);
})
->latest()
->take(12)
->get();
$notifications = $notificationsOfType3->merge($notificationsOfType4);
}
return view('Admin.pages.manage_notification.manage_notification', compact('notifications'));
}
/**
* Created By : Sayli Raut
* Created at : 10 June 2024
* Use : To view notification details.s
*/
public function add()
{
2024-06-18 12:46:04 +05:30
$state = ManageState::where('is_active', 1)->get()->toArray();
return view('Admin.pages.manage_notification.manage_notifications_add', compact('state'));
2024-06-11 15:09:59 +05:30
}
2024-06-11 16:03:19 +05:30
/**
2024-06-11 15:09:59 +05:30
* Created By : Sayli Raut
* Created at : 10 June 2024
* Use : To add notification .
*/
2024-07-08 19:47:25 +05:30
2024-07-09 15:54:23 +05:30
public function store_notificaton_data(Request $request)
{
try {
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
DB::beginTransaction();
if (isset($request->image)) {
$image = $request->image;
$image_db = null;
} else {
$image = null;
$image_db = $request->image;
}
$tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db);
$imagePath = ListingImageUrl('notification_images', $tnormalImage);
$states = $request->states;
$dateTime = now();
$formattedDateTime = $dateTime->format('Y-m-d H:i:s');
$iamPrincipals = Subscriptions::select('iam_principal_xid')
->where('next_payment_date', '>=', $formattedDateTime)
->get();
$iamPrincipalIds = $iamPrincipals->pluck('iam_principal_xid');
$subscribe = IamPrincipal::whereIn('id', $iamPrincipalIds)
->where('is_active', 1)
->where('notification_status', 1)
->where('principal_type_xid', 3)
->whereIn('state_xid', $states)
->get();
if ($request->user_type == 1) {
$allCustomerOneSignalIds = $subscribe->pluck('id');
$UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get();
foreach ($UserData as $customerIdItem) {
2024-07-09 13:46:03 +05:30
// user_type 1 = subscribed user
2024-07-09 15:54:23 +05:30
if ($request->schedule_radio1 == 1 && $request->schedule_date) {
// Scheduled Notification
NotificationDetails::create([
'principal_xid' => $customerIdItem->id,
'description' => $request->description,
'type' => $request->title,
'image' => $imagePath,
'date_added' => $request->schedule_date,
'is_schedule' => 1,
'delivery_schedule' => $request->schedule_date,
'is_active' => 0,
]);
} else {
// Immediate Notification
if ($customerIdItem->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$customerIdItem->one_signal_player_id,
$request->title,
$request->description,
'Dashboard Notification',
$imagePath,
$id = null
);
}
onesignalhelper::StoreNotificationDetails($customerIdItem->id, 'Notification', $request->title, $imagePath);
}
}
} elseif ($request->user_type == 2) {
//user_type 2 unsubscribed users
$allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)
->pluck('id');
$subscribedIds = Subscriptions::select('iam_principal_xid')
->where('next_payment_date', '>=', $formattedDateTime)
->pluck('iam_principal_xid');
$unsubscribedIds = $allPrincipalIds->diff($subscribedIds);
$unsubscribedPrincipals = IamPrincipal::whereIn('id', $unsubscribedIds)
->where('is_active', 1)
->where('notification_status', 1)
->whereIn('state_xid', $states)
->get();
$allRestaurantOneSignalIds = $unsubscribedPrincipals->pluck('id');
$restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get();
foreach ($restaurantData as $restaurantsData) {
2024-07-09 13:46:03 +05:30
if ($request->schedule_radio1 == 1 && $request->schedule_date) {
// Scheduled Notification
NotificationDetails::create([
'principal_xid' => $restaurantsData->id,
'description' => $request->description,
2024-07-09 15:54:23 +05:30
'type' => $request->title,
2024-07-09 13:46:03 +05:30
'image' => $imagePath,
'date_added' => $request->schedule_date,
'is_schedule' => 1,
'delivery_schedule' => $request->schedule_date,
2024-07-09 15:54:23 +05:30
'is_active' => 0,
2024-07-09 13:46:03 +05:30
]);
} else {
// Immediate Notification
if ($restaurantsData->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$restaurantsData->one_signal_player_id,
$request->title,
$request->description,
2024-07-09 15:54:23 +05:30
$request->title,
2024-07-09 13:46:03 +05:30
$imagePath,
$id = null
);
}
onesignalhelper::StoreNotificationDetails($restaurantsData->id, 'Notification', $request->title, $imagePath);
2024-07-03 16:58:35 +05:30
}
}
2024-07-09 15:54:23 +05:30
} elseif ($request->user_type == 3) {
// user_type 3 = subscribed and unsubscribed users
$userQuery = IamPrincipal::where('is_active', 1)
->where('notification_status', 1)
->where('principal_type_xid', 3)
->whereIn('state_xid', $states);
2024-07-09 13:46:03 +05:30
2024-07-09 15:54:23 +05:30
$allUserOneSignalIds = $userQuery->pluck('id');
$UserData = IamPrincipal::whereIn('id', $allUserOneSignalIds)->get();
2024-07-09 13:46:03 +05:30
2024-07-09 15:54:23 +05:30
foreach ($UserData as $CustomerData) {
2024-07-09 13:46:03 +05:30
if ($request->schedule_radio1 == 1 && $request->schedule_date) {
// Scheduled Notification
NotificationDetails::create([
'principal_xid' => $CustomerData->id,
'description' => $request->description,
2024-07-09 15:54:23 +05:30
'type' => $request->title,
2024-07-09 13:46:03 +05:30
'image' => $imagePath,
'date_added' => $request->schedule_date,
'is_schedule' => 1,
'delivery_schedule' => $request->schedule_date,
2024-07-09 15:54:23 +05:30
'is_active' => 0,
2024-07-09 13:46:03 +05:30
]);
} else {
// Immediate Notification
if ($CustomerData->one_signal_player_id) {
onesignalhelper::sendNotificationApi(
$CustomerData->one_signal_player_id,
$request->title,
$request->description,
'Dashboard Notification',
$imagePath,
$id = null
);
}
onesignalhelper::StoreNotificationDetails($CustomerData->id, 'Notification', $request->title, $imagePath);
2024-07-03 16:58:35 +05:30
}
2024-06-11 15:09:59 +05:30
}
}
2024-06-19 16:20:52 +05:30
2024-07-09 15:54:23 +05:30
DB::commit();
return jsonResponseWithSuccessMessage(__('success.save_data'));
} catch (Exception $e) {
DB::rollBack();
Log::error("Notification send Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
2024-07-09 13:46:03 +05:30
2024-07-03 16:58:35 +05:30
2024-06-19 16:20:52 +05:30
2024-06-11 15:09:59 +05:30
/**
* Created By : Sayli Raut
* Created at : 10 June 2024
* Use : To view notification details.s
*/
public function view($id)
{
$notification = NotificationDetails::with('notification')->findOrFail($id);
return view('Admin.pages.manage_notification.manage_notifications_view', compact('notification'));
2024-05-23 15:20:21 +05:30
}
}