| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\UserNotification;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- class UserNotificationController extends Controller
- {
- protected array $data = [
- 'active' => 'notifications',
- 'title' => 'Уведомления',
- 'id' => 'notifications',
- 'header' => [
- 'created_at' => 'Дата',
- 'type' => 'Тип',
- 'event' => 'Событие',
- 'message' => 'Сообщение',
- 'read_at' => 'Прочитано',
- ],
- 'searchFields' => [
- 'message',
- ],
- 'ranges' => [],
- 'filters' => [],
- 'dates' => [],
- ];
- public function index(Request $request)
- {
- session(['gp_notifications' => $request->query()]);
- $model = new UserNotification;
- $userId = $request->user()->id;
- $q = $model::query()->where('user_id', $userId);
- $this->data['filters']['type'] = [
- 'title' => 'Тип',
- 'values' => UserNotification::TYPE_NAMES,
- ];
- $this->data['filters']['event'] = [
- 'title' => 'Событие',
- 'values' => UserNotification::EVENT_NAMES,
- ];
- $this->createDateFilters($model, 'created_at');
- $this->acceptFilters($q, $request);
- $this->acceptSearch($q, $request);
- $this->setSortAndOrderBy($model, $request);
- $this->applyStableSorting($q);
- $this->data['notifications'] = $q->paginate($this->data['per_page'])->withQueryString();
- return view('notifications.index', $this->data);
- }
- public function markRead(Request $request, UserNotification $notification): JsonResponse
- {
- if ($notification->user_id !== $request->user()->id) {
- abort(403);
- }
- if (!$notification->isRead()) {
- $notification->update(['read_at' => now()]);
- }
- return response()->json([
- 'ok' => true,
- 'read_at' => $notification->read_at->format('d.m.Y H:i:s'),
- 'unread' => UserNotification::query()
- ->where('user_id', $request->user()->id)
- ->whereNull('read_at')
- ->count(),
- ]);
- }
- public function unreadCount(Request $request): JsonResponse
- {
- return response()->json([
- 'count' => UserNotification::query()
- ->where('user_id', $request->user()->id)
- ->whereNull('read_at')
- ->count(),
- ]);
- }
- }
|