|
| 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