Skip to content

Commit

Permalink
🐛 Fix notification datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
juzaweb committed Oct 1, 2021
1 parent b13019d commit 1cc9675
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Actions/MainAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Juzaweb\Notification\Actions;

use Juzaweb\Abstracts\Action;
use Juzaweb\Facades\HookAction;

class MainAction extends Action
{
/**
* Execute the actions.
*
* @return void
*/
public function handle()
{
$this->addAction(Action::BACKEND_CALL_ACTION, [$this, 'addAdminMenus']);
$this->addAction(Action::BACKEND_CALL_ACTION, [$this, 'addSettingPage']);
}

public function addAdminMenus()
{
HookAction::addAdminMenu(
trans('juzaweb::app.notifications'),
'notification',
[
'icon' => 'fa fa-bell',
'position' => 100
]
);
}

public function addSettingPage()
{
HookAction::addSettingForm('notification', [
'name' => trans('juzaweb::app.notification'),
'view' => view('juno::setting.form'),
]);
}
}
123 changes: 123 additions & 0 deletions src/Http/Datatable/NotificationDatatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Juzaweb\Notification\Http\Datatable;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Juzaweb\Abstracts\DataTable;
use Juzaweb\Notification\Models\ManualNotification;
use Juzaweb\Notification\SendNotification;
use Juzaweb\Notification\Jobs\SendNotification as SendNotificationJob;

class NotificationDatatable extends DataTable
{
/**
* Columns datatable
*
* @return array
*/
public function columns()
{
return [
'subject' => [
'label' => trans('juzaweb::app.subject'),
'formatter' => [$this, 'rowActionsFormatter'],
],
'method' => [
'label' => trans('juzaweb::app.via'),
'width' => '15%',
'formatter' => function ($value, $row, $index) {
if ($row->method) {
return $row->method;
}

return trans('juzaweb::app.all');
}
],
'error' => [
'label' => trans('juzaweb::app.error'),
'width' => '20%',
],
'created_at' => [
'label' => trans('juzaweb::app.created_at'),
'width' => '15%',
'align' => 'center',
'formatter' => function ($value, $row, $index) {
return jw_date_format($row->created_at);
}
],
'status' => [
'label' => trans('juzaweb::app.status'),
'width' => '15%',
'formatter' => function ($value, $row, $index) {
switch ($value) {
case 0: return trans('juzaweb::app.error');
case 1: return trans('juzaweb::app.sended');
case 2: return trans('juzaweb::app.pending');
case 3: return trans('juzaweb::app.sending');
case 4: return trans('juzaweb::app.unsent');
}
}
]
];
}

/**
* Query data datatable
*
* @param array $data
* @return Builder
*/
public function query($data)
{
$query = ManualNotification::query();
if ($keyword = Arr::get($data, 'keyword')) {
$query->where(function (Builder $q) use ($keyword) {
$q->orWhere('name', 'like', '%'. $keyword .'%');
$q->orWhere('subject', 'like', '%'. $keyword .'%');
});
}

if ($status = Arr::get($data, 'status')) {
if (!is_null($status)) {
$query->where('status', '=', $status);
}
}

return $query;
}

public function bulkActions($action, $ids)
{
switch ($action) {
case 'delete':
ManualNotification::destroy($ids);
break;
case 'send':
ManualNotification::whereIn('id', $ids)
->update([
'status' => 2
]);

$useMethod = config('juzaweb.notification.method');
if (in_array($useMethod, ['sync', 'queue'])) {
foreach ($ids as $id) {
$notification = ManualNotification::find($id);
if (empty($notification)) {
continue;
}

switch ($useMethod) {
case 'sync':
(new SendNotification($notification))->send();
break;
case 'queue':
SendNotificationJob::dispatch($notification);
break;
}
}
}
break;
}
}
}

0 comments on commit 1cc9675

Please sign in to comment.