89 lines
2.5 KiB
PHP
Raw Normal View History

2021-06-19 18:16:09 +03:00
<?php
namespace App\Http\Livewire\Common\Notifications;
use App\Abstracts\Livewire\Document as Component;
use App\Models\Document\Document;
use Livewire\WithPagination;
use Illuminate\Support\Collection;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Str;
class Reminder extends Component
{
use WithPagination;
public function markRead($notification_id)
{
$notification = DatabaseNotification::find($notification_id);
$data = $notification->getAttribute('data');
$notification->markAsRead();
$this->dispatchBrowserEvent('mark-read', [
2021-06-19 23:02:07 +03:00
'type' => 'reminder-' . $this->type,
2021-06-19 18:16:09 +03:00
'message' => trans('notifications.messages.mark_read', ['type' => $data[$this->type . '_number']]),
]);
}
public function markReadAll()
{
2021-06-19 23:02:07 +03:00
$notifications = $this->getNotifications();
2021-06-19 18:16:09 +03:00
foreach ($notifications as $notification) {
$notification->markAsRead();
}
$this->dispatchBrowserEvent('mark-read-all', [
2021-06-19 23:02:07 +03:00
'type' => 'reminder-' . $this->type,
2021-06-19 18:16:09 +03:00
'message' => trans('notifications.messages.mark_read', ['type' => trans_choice('general.' . Str::plural($this->type) , 2)]),
]);
}
public function render()
{
$limit = 5;
2021-06-19 18:16:09 +03:00
2021-06-19 23:02:07 +03:00
$notifications = getNotifications($limit);
return view('livewire.common.notifications.reminder', compact('notifications'));
}
protected function getNotifications($limit = false)
{
2021-06-19 18:16:09 +03:00
$type = config('type.' . $this->type . '.notification.class');
2021-06-19 23:02:07 +03:00
$query = user()->notifications()->unread()
2021-06-19 18:16:09 +03:00
->where('type', $type)
2021-06-19 23:02:07 +03:00
->where('data', 'like', '%template_alias:{$this->type}_remind_admin%');
2021-06-19 18:16:09 +03:00
2021-06-19 23:02:07 +03:00
if ($limit) {
$notifications = $query->paginate($limit);
} else {
$notifications = $query->get();
}
2021-06-19 18:16:09 +03:00
2021-06-19 23:02:07 +03:00
if ($notifications->items()) {
$items = [];
2021-06-19 18:16:09 +03:00
2021-06-19 23:02:07 +03:00
foreach ($notifications->items() as $key => $notification) {
$data = (object) $notification->getAttribute('data');
2021-06-19 18:16:09 +03:00
2021-06-20 12:26:58 +03:00
$item = Document::{$this->type}()->where('id', $data[$this->type . '_id'])->first();
2021-06-19 23:02:07 +03:00
$item->notification_id = $notification->getAttribute('id');
$items[] = $item;
}
2021-06-19 18:16:09 +03:00
2021-06-19 23:02:07 +03:00
$notifications->setCollection(Collection::make($items));
2021-06-19 18:16:09 +03:00
}
2021-06-19 23:02:07 +03:00
return $notifications;
2021-06-19 18:16:09 +03:00
}
public function paginationView()
{
return 'vendor.livewire.default';
}
}