91 lines
2.7 KiB
PHP
91 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\AssetExchange\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\AssetExchange\Http\Controllers\Concerns\AuthorizesAssetExchange;
|
|
use Modules\AssetExchange\Models\Notification;
|
|
use Modules\AssetExchange\Utils\AssetExchangeUtil;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
use AuthorizesAssetExchange;
|
|
|
|
public function __construct(
|
|
protected AssetExchangeUtil $aexUtil
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeAexAccess('aex.view');
|
|
|
|
$business_id = $this->aexUtil->getBusinessId();
|
|
$query = Notification::where('business_id', $business_id)
|
|
->orderByDesc('created_at');
|
|
|
|
if ($request->get('unread')) {
|
|
$query->whereNull('read_at');
|
|
}
|
|
|
|
$notifications = $query->paginate(30);
|
|
$unread_count = Notification::where('business_id', $business_id)->whereNull('read_at')->count();
|
|
|
|
return view('assetexchange::notifications.index', [
|
|
'notifications' => $notifications,
|
|
'aexUtil' => $this->aexUtil,
|
|
'unread_count' => $unread_count,
|
|
]);
|
|
}
|
|
|
|
public function markRead($id)
|
|
{
|
|
$this->authorizeAexAccess('aex.view');
|
|
|
|
$business_id = $this->aexUtil->getBusinessId();
|
|
$notification = Notification::where('business_id', $business_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 markAllRead()
|
|
{
|
|
$this->authorizeAexAccess('aex.view');
|
|
|
|
$business_id = $this->aexUtil->getBusinessId();
|
|
Notification::where('business_id', $business_id)->whereNull('read_at')->update(['read_at' => now()]);
|
|
|
|
return redirect()->back()->with('status', __('assetexchange::lang.notifications_all_marked_read'));
|
|
}
|
|
|
|
public function unreadCount()
|
|
{
|
|
$this->authorizeAexAccess('aex.view');
|
|
|
|
$business_id = $this->aexUtil->getBusinessId();
|
|
$user = auth()->user();
|
|
$count = Notification::where('business_id', $business_id)
|
|
->where(function ($q) use ($user) {
|
|
$q->where('user_id', $user->id);
|
|
if (! empty($user->crm_contact_id)) {
|
|
$q->orWhere('contact_id', $user->crm_contact_id);
|
|
}
|
|
})
|
|
->whereNull('read_at')
|
|
->count();
|
|
|
|
return response()->json(['unread_count' => $count]);
|
|
}
|
|
}
|
|
|