ldc-legaulois/app/Http/Controllers/Club/NotificationController.php

75 lines
2.5 KiB
PHP
Raw Normal View History

2024-07-05 10:35:26 +02:00
<?php
namespace App\Http\Controllers\Club;
use App\Http\Controllers\Controller;
use App\Http\Requests\Club\Notifications\DeleteNotificationsRequest;
use App\Models\Notification;
use Illuminate\Support\Facades\Auth;
2024-07-18 12:13:35 +02:00
use Illuminate\Support\Facades\View as FacadesView;
2024-07-05 10:35:26 +02:00
class NotificationController extends Controller
{
/**
* Show index page
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
2024-07-05 10:35:26 +02:00
{
/** @var \App\Models\Customer $customerModel */
$customerModel = auth('club')->user();
$notificationModels = $customerModel->notifications()
->whereNotIn('id', $customerModel->readNotifications()->pluck('notification_id'))
->orderBy('created_at', 'DESC')
->get();
return view('club.pages.notifications.index', compact('notificationModels'));
2024-07-05 10:35:26 +02:00
}
2024-07-18 12:13:35 +02:00
/**
* Get notifications count
*
* @return \Illuminate\Http\JsonResponse
*/
public function notificationsCount(): \Illuminate\Http\JsonResponse
2024-07-18 12:13:35 +02:00
{
return response()->json(FacadesView::shared('notifications_count', 0));
}
/**
* Delete notification from list
* @param Notification $notification
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Notification $notification): \Illuminate\Http\JsonResponse
{
/** @var \App\Models\Customer $customer */
$customer = Auth::guard('club')->user();
$customer->readNotifications()->syncWithoutDetaching($notification->id);
session()->flash('success', __('Notification supprimée.'));
return response()->json(['success' => true]);
}
/**
* Delete notification from list
* @param \App\Http\Requests\Club\Notifications\DeleteNotificationsRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function destroyMultiple(DeleteNotificationsRequest $request): \Illuminate\Http\JsonResponse
{
/** @var \App\Models\Customer $customer */
$customer = Auth::guard('club')->user();
$customer->readNotifications()->syncWithoutDetaching($request->validated('ids'));
$count = count($request->validated(('ids')));
session()->flash('success', trans_choice(
'{1} Notification supprimée.|[2,*] :count notifications supprimées.',
$count,
['count' => $count]
));
return response()->json(['success' => true]);
}
}