2026-10-17 09:28:35 +07:00
<? php
namespace App\Http\Controllers\Admin ;
use App\Http\Controllers\Controller ;
use App\Models\AdminActivityLog ;
use Carbon\Carbon ;
2026-11-03 15:14:07 +07:00
use Illuminate\Http\RedirectResponse ;
2026-10-17 09:28:35 +07:00
use Illuminate\Http\Request ;
2026-11-03 15:14:07 +07:00
use Illuminate\Support\Facades\Hash ;
2026-10-17 09:28:35 +07:00
use Illuminate\View\View ;
class AdminActivityLogController extends Controller
{
/**
* Tampilkan riwayat log aktivitas yang dilakukan Super Admin.
*/
public function index ( Request $request ) : View
{
$query = AdminActivityLog :: with ( 'user' ) -> latest ();
// 1. Filter Pencarian Kata Kunci
if ( $request -> filled ( 'search' )) {
$search = trim ( $request -> input ( 'search' ));
$query -> where ( function ( $q ) use ( $search ) {
$q -> where ( 'description' , 'like' , "% { $search } %" )
-> orWhere ( 'user_name' , 'like' , "% { $search } %" )
-> orWhere ( 'user_username' , 'like' , "% { $search } %" )
-> orWhere ( 'target_name' , 'like' , "% { $search } %" );
});
}
// 2. Filter Tipe Aksi
if ( $request -> filled ( 'action_type' )) {
$query -> where ( 'action_type' , $request -> input ( 'action_type' ));
}
// 3. Filter Rentang Tanggal
if ( $request -> filled ( 'start_date' )) {
$query -> whereDate ( 'created_at' , '>=' , $request -> input ( 'start_date' ));
}
if ( $request -> filled ( 'end_date' )) {
$query -> whereDate ( 'created_at' , '<=' , $request -> input ( 'end_date' ));
}
$logs = $query -> paginate ( 20 ) -> withQueryString ();
// Metrik Ringkasan di Bagian Atas
$today = Carbon :: today ();
$stats = [
'today_count' => AdminActivityLog :: whereDate ( 'created_at' , $today ) -> count (),
'create_count' => AdminActivityLog :: where ( 'action_type' , 'create' ) -> count (),
'update_count' => AdminActivityLog :: whereIn ( 'action_type' , [ 'update' , 'permission' ]) -> count (),
'delete_count' => AdminActivityLog :: where ( 'action_type' , 'delete' ) -> count (),
];
return view ( 'admin.activity_logs.index' , compact ( 'logs' , 'stats' ));
}
2026-11-03 15:14:07 +07:00
/**
* Kosongkan seluruh riwayat log aktivitas admin (Hanya Super Admin).
*/
public function clear ( Request $request ) : RedirectResponse
{
$user = $request -> user ();
if ( ! $user || ( ! $user -> hasRole ( 'super_admin' ) && $user -> role !== 'super_admin' )) {
abort ( 403 , 'Akses ditolak. Hanya Super Admin yang berhak mengosongkan log aktivitas sistem.' );
}
$confirmed = false ;
if ( $request -> filled ( 'confirmation' ) && trim ( $request -> input ( 'confirmation' )) === 'KOSONGKAN' ) {
$confirmed = true ;
} elseif ( $request -> filled ( 'password' ) && Hash :: check ( $request -> input ( 'password' ), $user -> password )) {
$confirmed = true ;
}
if ( ! $confirmed ) {
return back () -> with ( 'error' , 'Konfirmasi keamanan tidak sesuai. Silakan ketik kata "KOSONGKAN" atau masukkan password Super Admin Anda.' );
}
$count = AdminActivityLog :: count ();
AdminActivityLog :: truncate ();
return back () -> with ( 'success' , "Seluruh rekaman log aktivitas admin berhasil dikosongkan ( { $count } catatan log dibersihkan). Sistem siap untuk fase operasional baru." );
}
2026-10-17 09:28:35 +07:00
}