Update Log aktivitas
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Konfigurasi;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Konfigurasi\FilterLogAktivitasRequest;
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
class LogAktivitasController extends Controller
|
||||
{
|
||||
public const JENIS = [
|
||||
'auth' => ['label' => 'Login', 'warna' => 'info'],
|
||||
'petani' => ['label' => 'Profil Petani', 'warna' => 'primary'],
|
||||
'verifikasi' => ['label' => 'Status Petani', 'warna' => 'success'],
|
||||
'pertanian' => ['label' => 'Data Pertanian', 'warna' => 'primary'],
|
||||
'map' => ['label' => 'Peta & Panen', 'warna' => 'primary'],
|
||||
'user' => ['label' => 'Pengguna', 'warna' => 'warning'],
|
||||
'profile' => ['label' => 'Profil Pengguna', 'warna' => 'warning'],
|
||||
'konfigurasi' => ['label' => 'Konfigurasi', 'warna' => 'warning'],
|
||||
'program' => ['label' => 'Program', 'warna' => 'danger'],
|
||||
'core' => ['label' => 'Program (Inti)', 'warna' => 'danger'],
|
||||
'bumdes' => ['label' => 'BUMDes', 'warna' => 'dark'],
|
||||
'koperasi' => ['label' => 'Koperasi', 'warna' => 'dark'],
|
||||
'ekosistem' => ['label' => 'Ekosistem', 'warna' => 'dark'],
|
||||
'master' => ['label' => 'Master Data', 'warna' => 'secondary'],
|
||||
'wilayah' => ['label' => 'Wilayah', 'warna' => 'secondary'],
|
||||
'penduduk' => ['label' => 'Penduduk', 'warna' => 'secondary'],
|
||||
'distribusi' => ['label' => 'Distribusi', 'warna' => 'secondary'],
|
||||
'facilitator' => ['label' => 'Fasilitator', 'warna' => 'secondary'],
|
||||
];
|
||||
|
||||
public const PER_PAGE = [25, 50, 100];
|
||||
|
||||
public function index(FilterLogAktivitasRequest $request)
|
||||
{
|
||||
$filter = $request->validated();
|
||||
|
||||
// Ambil log sesuai filter
|
||||
$aktivitas = Activity::query()
|
||||
->with(['causer', 'subject'])
|
||||
->when($filter['jenis'] ?? null, fn ($q, $jenis) => $q->where('log_name', $jenis))
|
||||
->when($filter['dari'] ?? null, fn ($q, $tgl) => $q->where('created_at', '>=', $tgl))
|
||||
->when($filter['sampai'] ?? null, fn ($q, $tgl) => $q->where('created_at', '<', Carbon::parse($tgl)->addDay()))
|
||||
->when($filter['search'] ?? null, fn ($q, $cari) => $this->cari($q, '%' . mb_strtolower($cari) . '%'))
|
||||
->latest('id')
|
||||
->paginate((int) ($filter['per_page'] ?? self::PER_PAGE[0]))
|
||||
->withQueryString();
|
||||
|
||||
// Nama petani untuk log tab petani (satu query)
|
||||
$namaPetani = Petani::withoutGlobalScopes()->withTrashed()
|
||||
->whereIn('id', $aktivitas->getCollection()->pluck('properties.petani_id')->filter()->unique())
|
||||
->pluck('nama', 'id');
|
||||
|
||||
return view('konfigurasi.log-aktivitas.index', [
|
||||
'aktivitas' => $aktivitas,
|
||||
'jenis' => self::JENIS,
|
||||
'perPage' => self::PER_PAGE,
|
||||
'namaPetani' => $namaPetani,
|
||||
]);
|
||||
}
|
||||
|
||||
// Cari di aktivitas, nama pengguna, dan nama petani
|
||||
private function cari($query, string $cari): void
|
||||
{
|
||||
$query->where(fn ($q) => $q
|
||||
->whereRaw('lower(description) like ?', [$cari])
|
||||
->orWhereHasMorph('causer', [User::class], fn ($q) => $q->whereRaw('lower(name) like ?', [$cari]))
|
||||
->orWhereRaw(
|
||||
"coalesce((properties->>'petani_id')::bigint, case when subject_type = ? then subject_id end) in (select id from petani where lower(nama) like ?)",
|
||||
[Petani::class, $cari]
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user