Skip to content

Commit 1cc9675

Browse files
committed
🐛 Fix notification datatable
1 parent b13019d commit 1cc9675

2 files changed

Lines changed: 163 additions & 0 deletions

File tree

src/Actions/MainAction.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Juzaweb\Notification\Actions;
4+
5+
use Juzaweb\Abstracts\Action;
6+
use Juzaweb\Facades\HookAction;
7+
8+
class MainAction extends Action
9+
{
10+
/**
11+
* Execute the actions.
12+
*
13+
* @return void
14+
*/
15+
public function handle()
16+
{
17+
$this->addAction(Action::BACKEND_CALL_ACTION, [$this, 'addAdminMenus']);
18+
$this->addAction(Action::BACKEND_CALL_ACTION, [$this, 'addSettingPage']);
19+
}
20+
21+
public function addAdminMenus()
22+
{
23+
HookAction::addAdminMenu(
24+
trans('juzaweb::app.notifications'),
25+
'notification',
26+
[
27+
'icon' => 'fa fa-bell',
28+
'position' => 100
29+
]
30+
);
31+
}
32+
33+
public function addSettingPage()
34+
{
35+
HookAction::addSettingForm('notification', [
36+
'name' => trans('juzaweb::app.notification'),
37+
'view' => view('juno::setting.form'),
38+
]);
39+
}
40+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Juzaweb\Notification\Http\Datatable;
4+
5+
use Illuminate\Database\Query\Builder;
6+
use Illuminate\Support\Arr;
7+
use Juzaweb\Abstracts\DataTable;
8+
use Juzaweb\Notification\Models\ManualNotification;
9+
use Juzaweb\Notification\SendNotification;
10+
use Juzaweb\Notification\Jobs\SendNotification as SendNotificationJob;
11+
12+
class NotificationDatatable extends DataTable
13+
{
14+
/**
15+
* Columns datatable
16+
*
17+
* @return array
18+
*/
19+
public function columns()
20+
{
21+
return [
22+
'subject' => [
23+
'label' => trans('juzaweb::app.subject'),
24+
'formatter' => [$this, 'rowActionsFormatter'],
25+
],
26+
'method' => [
27+
'label' => trans('juzaweb::app.via'),
28+
'width' => '15%',
29+
'formatter' => function ($value, $row, $index) {
30+
if ($row->method) {
31+
return $row->method;
32+
}
33+
34+
return trans('juzaweb::app.all');
35+
}
36+
],
37+
'error' => [
38+
'label' => trans('juzaweb::app.error'),
39+
'width' => '20%',
40+
],
41+
'created_at' => [
42+
'label' => trans('juzaweb::app.created_at'),
43+
'width' => '15%',
44+
'align' => 'center',
45+
'formatter' => function ($value, $row, $index) {
46+
return jw_date_format($row->created_at);
47+
}
48+
],
49+
'status' => [
50+
'label' => trans('juzaweb::app.status'),
51+
'width' => '15%',
52+
'formatter' => function ($value, $row, $index) {
53+
switch ($value) {
54+
case 0: return trans('juzaweb::app.error');
55+
case 1: return trans('juzaweb::app.sended');
56+
case 2: return trans('juzaweb::app.pending');
57+
case 3: return trans('juzaweb::app.sending');
58+
case 4: return trans('juzaweb::app.unsent');
59+
}
60+
}
61+
]
62+
];
63+
}
64+
65+
/**
66+
* Query data datatable
67+
*
68+
* @param array $data
69+
* @return Builder
70+
*/
71+
public function query($data)
72+
{
73+
$query = ManualNotification::query();
74+
if ($keyword = Arr::get($data, 'keyword')) {
75+
$query->where(function (Builder $q) use ($keyword) {
76+
$q->orWhere('name', 'like', '%'. $keyword .'%');
77+
$q->orWhere('subject', 'like', '%'. $keyword .'%');
78+
});
79+
}
80+
81+
if ($status = Arr::get($data, 'status')) {
82+
if (!is_null($status)) {
83+
$query->where('status', '=', $status);
84+
}
85+
}
86+
87+
return $query;
88+
}
89+
90+
public function bulkActions($action, $ids)
91+
{
92+
switch ($action) {
93+
case 'delete':
94+
ManualNotification::destroy($ids);
95+
break;
96+
case 'send':
97+
ManualNotification::whereIn('id', $ids)
98+
->update([
99+
'status' => 2
100+
]);
101+
102+
$useMethod = config('juzaweb.notification.method');
103+
if (in_array($useMethod, ['sync', 'queue'])) {
104+
foreach ($ids as $id) {
105+
$notification = ManualNotification::find($id);
106+
if (empty($notification)) {
107+
continue;
108+
}
109+
110+
switch ($useMethod) {
111+
case 'sync':
112+
(new SendNotification($notification))->send();
113+
break;
114+
case 'queue':
115+
SendNotificationJob::dispatch($notification);
116+
break;
117+
}
118+
}
119+
}
120+
break;
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)