|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Livewire; |
| 4 | + |
| 5 | +use App\Http\Livewire; |
| 6 | +use Rappasoft\LaravelLivewireTables\DataTableComponent; |
| 7 | +use Rappasoft\LaravelLivewireTables\Views\Column; |
| 8 | +use App\Models\User; |
| 9 | +use App\Models\Link; |
| 10 | + |
| 11 | +class UserTable extends DataTableComponent |
| 12 | +{ |
| 13 | + protected $model = User::class; |
| 14 | + |
| 15 | + public function configure(): void |
| 16 | + { |
| 17 | + $this->setPrimaryKey('id'); |
| 18 | + } |
| 19 | + |
| 20 | + public function columns(): array |
| 21 | + { |
| 22 | + return [ |
| 23 | + Column::make("Id", "id") |
| 24 | + ->sortable() |
| 25 | + ->searchable(), |
| 26 | + Column::make("Name", "name") |
| 27 | + ->sortable() |
| 28 | + ->searchable(), |
| 29 | + Column::make("Email", "email") |
| 30 | + ->sortable() |
| 31 | + ->searchable(), |
| 32 | + Column::make("Littlelink name", "littlelink_name") |
| 33 | + ->sortable() |
| 34 | + ->searchable() |
| 35 | + ->format(function ($value, $row, Column $column) { |
| 36 | + if (!$row->littlelink_name == NULL) { |
| 37 | + return "<a href='" . url('') . "/@" . $row->littlelink_name . "' target='_blank' class='text-info'><i class='bi bi-box-arrow-up-right'></i> " . $row->littlelink_name . " </a>"; |
| 38 | + } else { |
| 39 | + return 'N/A'; |
| 40 | + } |
| 41 | + }) |
| 42 | + ->html(), |
| 43 | + Column::make("Role", "role") |
| 44 | + ->sortable() |
| 45 | + ->searchable(), |
| 46 | + Column::make("Test", "id") |
| 47 | + ->sortable() |
| 48 | + ->format(function ($value, $row) { |
| 49 | + $linkCount = Link::where('user_id', $row->id)->count(); |
| 50 | + return $linkCount; |
| 51 | + }), |
| 52 | + Column::make("Clicks Sum", "id") |
| 53 | + ->sortable() |
| 54 | + ->format(function ($value, $row) { |
| 55 | + $clicksSum = Link::where('user_id', $row->id)->sum('click_number'); |
| 56 | + return $clicksSum; |
| 57 | + }), |
| 58 | + Column::make("E-mail", "email_verified_at") |
| 59 | + ->sortable() |
| 60 | + ->format(function ($value, $row, Column $column) { |
| 61 | + if (env('REGISTER_AUTH') !== 'auth') { |
| 62 | + if ($row->role == 'admin' && $row->email_verified_at != '') { |
| 63 | + return '<center>-</center>'; |
| 64 | + } else { |
| 65 | + $verifyLink = route('verifyUser', [ |
| 66 | + 'verify' => '-' . $row->email_verified_at, |
| 67 | + 'id' => $row->id |
| 68 | + ]); |
| 69 | + if ($row->email_verified_at == '') { |
| 70 | + return '<a href="' . $verifyLink . '" class="text-danger"><span class="badge bg-danger">' . __('messages.Pending') . '</span></a>'; |
| 71 | + } else { |
| 72 | + return '<a href="' . $verifyLink . '" class="text-danger"><span class="badge bg-success">' . __('messages.Verified') . '</span></a>'; |
| 73 | + } |
| 74 | + } |
| 75 | + } else { |
| 76 | + return '<center>-</center>'; |
| 77 | + } |
| 78 | + return ''; |
| 79 | + })->html(), |
| 80 | + Column::make("Blocked", "block") |
| 81 | + ->sortable() |
| 82 | + ->format(function ($value, $row, Column $column) { |
| 83 | + if ($row->role === 'admin' && $row->id === 1) { |
| 84 | + return '<center>-</center>'; |
| 85 | + } else { |
| 86 | + $route = route('blockUser', ['block' => $row->block, 'id' => $row->id]); |
| 87 | + if ($row->block === 'yes') { |
| 88 | + $badge = '<span class="badge bg-danger">'.__('messages.Pending').'</span>'; |
| 89 | + } elseif ($row->block === 'no') { |
| 90 | + $badge = '<span class="badge bg-success">'.__('messages.Approved').'</span>'; |
| 91 | + } |
| 92 | + return "<a href=\"$route\">$badge</a>"; |
| 93 | + } |
| 94 | + }) |
| 95 | + ->html(), |
| 96 | + Column::make("Created at", "created_at") |
| 97 | + ->sortable() |
| 98 | + ->format(function ($value) { |
| 99 | + if ($value) { |
| 100 | + return $value->format('d/m/y'); |
| 101 | + } else { |
| 102 | + return ''; |
| 103 | + } |
| 104 | + }), |
| 105 | + Column::make("Last seen", "updated_at") |
| 106 | + ->sortable() |
| 107 | + ->format(function ($value) { |
| 108 | + $now = now(); |
| 109 | + $diff = $now->diff($value); |
| 110 | + |
| 111 | + if ($diff->d < 1 && $diff->h < 1) { |
| 112 | + return 'Now'; |
| 113 | + } elseif ($diff->d < 1 && $diff->h < 24) { |
| 114 | + return $diff->h . ' hours ago'; |
| 115 | + } elseif ($diff->d < 365) { |
| 116 | + return $diff->d . ' days ago'; |
| 117 | + } else { |
| 118 | + return $diff->y . ' years ago'; |
| 119 | + } |
| 120 | + }), |
| 121 | + Column::make('Actions', "id") |
| 122 | + ->format(function ($value, $row, Column $column) { |
| 123 | + return view('components.table-components.action', ['user' => $row]); |
| 124 | + }), |
| 125 | + ]; |
| 126 | + } |
| 127 | +} |
0 commit comments