83 lines
4.0 KiB
PHP
83 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Konfigurasi;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Konfigurasi\FilterLogAktivitasRequest;
|
|
use App\Models\Konfigurasi\LogAktivitas;
|
|
use App\Models\Pertanian\Petani;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
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 (tanpa COUNT total)
|
|
$aktivitas = LogAktivitas::query()
|
|
->with('causer:id,name,role')
|
|
->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')
|
|
->simplePaginate((int) ($filter['per_page'] ?? self::PER_PAGE[0]))
|
|
->withQueryString();
|
|
|
|
// Nama petani untuk log yang belum menyimpan nama (satu query)
|
|
$idPetani = $aktivitas->getCollection()
|
|
->map(fn ($a) => $a->properties['petani_id'] ?? ($a->subject_type === Petani::class ? $a->subject_id : null))
|
|
->filter()
|
|
->unique();
|
|
|
|
$namaPetani = $idPetani->isEmpty() ? collect() : Petani::withoutGlobalScopes()->whereIn('id', $idPetani)->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 (memakai index)
|
|
private function cari(Builder $query, string $cari): void
|
|
{
|
|
$petani = Petani::withoutGlobalScopes()->select('id')->whereRaw('lower(nama) like ?', [$cari]);
|
|
$pengguna = User::withoutGlobalScopes()->select('id')->whereRaw('lower(name) like ?', [$cari]);
|
|
|
|
$query->where(fn ($q) => $q
|
|
->whereRaw('lower(description) like ?', [$cari])
|
|
->orWhere(fn ($q) => $q->where('causer_type', User::class)->whereIn('causer_id', $pengguna))
|
|
->orWhere(fn ($q) => $q->where('subject_type', Petani::class)->whereIn('subject_id', $petani))
|
|
->orWhereIn(DB::raw("(properties->>'petani_id')::bigint"), $petani));
|
|
}
|
|
}
|