ultimatepos/Modules/AssetExchange/Http/Controllers/PortalNotificationController.php

92 lines
2.6 KiB
PHP

<?php
namespace Modules\AssetExchange\Http\Controllers;
use Modules\AssetExchange\Models\Notification;
use Modules\AssetExchange\Utils\AssetExchangeUtil;
use Modules\Portal\Http\Controllers\BasePortalController;
class PortalNotificationController extends BasePortalController
{
public function __construct(
protected AssetExchangeUtil $aexUtil
) {
}
public function index()
{
$this->ensurePortalAccess();
$business_id = $this->businessId();
$contact_id = $this->contactId();
$notifications = Notification::where('business_id', $business_id)
->where('contact_id', $contact_id)
->orderByDesc('created_at')
->paginate(30);
$unread_count = Notification::where('business_id', $business_id)
->where('contact_id', $contact_id)
->whereNull('read_at')
->count();
return view('assetexchange::portal.notifications.index', [
'notifications' => $notifications,
'aexUtil' => $this->aexUtil,
'unread_count' => $unread_count,
]);
}
public function read($id)
{
$this->ensurePortalAccess();
$business_id = $this->businessId();
$contact_id = $this->contactId();
$notification = Notification::where('business_id', $business_id)
->where('contact_id', $contact_id)
->findOrFail($id);
if (! $notification->read_at) {
$notification->update(['read_at' => now()]);
}
$redirect = $notification->data['url'] ?? null;
if ($redirect) {
return redirect()->to($redirect);
}
return redirect()->back()->with('status', __('assetexchange::lang.notification_marked_read'));
}
public function readAll()
{
$this->ensurePortalAccess();
$business_id = $this->businessId();
$contact_id = $this->contactId();
Notification::where('business_id', $business_id)
->where('contact_id', $contact_id)
->whereNull('read_at')
->update(['read_at' => now()]);
return redirect()->back()->with('status', __('assetexchange::lang.notifications_all_marked_read'));
}
public function unreadCount()
{
$this->ensurePortalAccess();
$business_id = $this->businessId();
$contact_id = $this->contactId();
$count = Notification::where('business_id', $business_id)
->where('contact_id', $contact_id)
->whereNull('read_at')
->count();
return response()->json(['unread_count' => $count]);
}
}