CaiWuXiTong/app/Http/Livewire/Menu/Notifications.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

164 lines
4.4 KiB
PHP

<?php
namespace App\Http\Livewire\Menu;
use App\Events\Menu\NotificationsCreated;
use App\Events\Menu\NotificationsCreating;
use Illuminate\Contracts\View\View;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Str;
use Livewire\Component;
class Notifications extends Component
{
public $user = null;
public $keyword = '';
public $notifications = [];
protected $listeners = ['resetKeyword'];
public function render(): View
{
$this->user = user();
$this->notifications = $this->getNotifications();
return view('livewire.menu.notifications');
}
public function markRead($type, $notification_id, $message = true)
{
switch ($type) {
case 'updates':
$this->markUpdateRead($notification_id);
break;
case 'new-apps':
$this->markNewAppRead($notification_id);
break;
default:
$notification = DatabaseNotification::find($notification_id);
$data = $notification->getAttribute('data');
$notification->markAsRead();
}
if (! $message) {
return;
}
$type = isset($data['file_name']) ?: trans('general.export');
$this->dispatch('mark-read', type: 'notification', message: trans('notifications.messages.mark_read', ['type' => $type]));
}
public function markReadAll()
{
// Mark all DB notifications as read using the loaded collection.
// Matches Akaunting's own pattern (Auth\Users, Portal\Profile) —
// iterates unreadNotifications so each markAsRead() fires properly.
foreach (user()->unreadNotifications as $notification) {
$notification->markAsRead();
}
// Handle non-DB notification types (new-apps, updates).
foreach ($this->getNotifications() as $notification) {
if ($notification->type !== 'new-apps' || empty($notification->data['alias'])) {
continue;
}
setting()->set(
'notifications.' . $notification->notifiable_id . '.' . $notification->data['alias'],
'1'
);
}
setting()->save();
$this->dispatch('mark-read-all', type: 'notification', message: trans('notifications.messages.mark_read_all', ['type' => trans('general.export')]));
}
public function markUpdateRead($notification_id)
{
//
}
public function markNewAppRead($notification_id)
{
$notifications = $this->getNotifications();
foreach ($notifications as $notification) {
if ($notification->id == $notification_id) {
setting()->set('notifications.' . $notification->notifiable_id . '.' . $notification->data['alias'], '1');
setting()->save();
break;
}
}
}
public function getNotifications(): array
{
$notifications = new \stdClass();
$notifications->notifications = collect();
$notifications->keyword = $this->keyword;
event(new NotificationsCreating($notifications));
event(new NotificationsCreated($notifications));
$rows = [];
foreach ($notifications->notifications as $notification) {
if (! $this->availableInSearch($notification)) {
continue;
}
$rows[] = $notification;
}
return $rows;
}
public function availableInSearch($notification): bool
{
if (empty($this->keyword)) {
return true;
}
return $this->search($notification);
}
public function search($notification): bool
{
$status = false;
$keywords = explode(' ', $this->keyword);
foreach ($keywords as $keyword) {
if (Str::contains(Str::lower($notification->data['title']), Str::lower($keyword))) {
$status = true;
break;
}
if (
!empty($notification->data['description'])
&& Str::contains(Str::lower($notification->data['description']), Str::lower($keyword))
) {
$status = true;
break;
}
}
return $status;
}
public function resetKeyword(): void
{
$this->keyword = '';
}
}