修復稽核日誌與任務報表頁面,並將「問題」改名為「任務」 ## Changes 變更內容 ### Bug Fixes 錯誤修復 1. Fixed audit logs page 500 error - Added missing $auditableTypes variable to controller - Changed $events to $actions in view - Added description and ip_address columns to audit_logs table - Updated AuditLog model fillable array 2. Fixed issue reports page SQLite compatibility errors - Replaced MySQL NOW() function with Laravel now() helper - Replaced TIMESTAMPDIFF() with PHP-based date calculation - Fixed request->date() default value handling ### Feature Changes 功能變更 3. Renamed "Issues" terminology to "Tasks" throughout the system - Updated navigation menus (Admin: Issues → Admin: Tasks) - Updated all issue-related views to use task terminology - Changed Chinese labels from "問題" to "任務" - Updated dashboard, issue tracker, and reports pages 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
113 lines
3.6 KiB
PHP
113 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\AuditLog;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AdminAuditLogController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = AuditLog::query()->with('user');
|
|
|
|
$search = $request->string('search')->toString();
|
|
$action = $request->string('action')->toString();
|
|
$userId = $request->integer('user_id');
|
|
$start = $request->date('start_date');
|
|
$end = $request->date('end_date');
|
|
|
|
if ($search) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('action', 'like', "%{$search}%")
|
|
->orWhere('metadata', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if ($action) {
|
|
$query->where('action', $action);
|
|
}
|
|
|
|
if ($userId) {
|
|
$query->where('user_id', $userId);
|
|
}
|
|
|
|
if ($start) {
|
|
$query->whereDate('created_at', '>=', $start);
|
|
}
|
|
|
|
if ($end) {
|
|
$query->whereDate('created_at', '<=', $end);
|
|
}
|
|
|
|
$logs = $query->orderByDesc('created_at')->paginate(25)->withQueryString();
|
|
|
|
$actions = AuditLog::select('action')->distinct()->orderBy('action')->pluck('action');
|
|
$users = AuditLog::with('user')->whereNotNull('user_id')->select('user_id')->distinct()->get()->map(function ($log) {
|
|
return $log->user;
|
|
})->filter();
|
|
$auditableTypes = AuditLog::select('auditable_type')->distinct()->whereNotNull('auditable_type')->orderBy('auditable_type')->pluck('auditable_type');
|
|
|
|
return view('admin.audit.index', [
|
|
'logs' => $logs,
|
|
'search' => $search,
|
|
'actionFilter' => $action,
|
|
'userFilter' => $userId,
|
|
'startDate' => $start,
|
|
'endDate' => $end,
|
|
'actions' => $actions,
|
|
'users' => $users,
|
|
'auditableTypes' => $auditableTypes,
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$query = AuditLog::query()->with('user');
|
|
|
|
if ($search = $request->string('search')->toString()) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('action', 'like', "%{$search}%")
|
|
->orWhere('metadata', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if ($action = $request->string('action')->toString()) {
|
|
$query->where('action', $action);
|
|
}
|
|
|
|
if ($userId = $request->integer('user_id')) {
|
|
$query->where('user_id', $userId);
|
|
}
|
|
|
|
if ($start = $request->date('start_date')) {
|
|
$query->whereDate('created_at', '>=', $start);
|
|
}
|
|
|
|
if ($end = $request->date('end_date')) {
|
|
$query->whereDate('created_at', '<=', $end);
|
|
}
|
|
|
|
return response()->stream(function () use ($query) {
|
|
$handle = fopen('php://output', 'w');
|
|
fputcsv($handle, ['Timestamp', 'User', 'Action', 'Metadata']);
|
|
|
|
$query->orderByDesc('created_at')->chunk(500, function ($logs) use ($handle) {
|
|
foreach ($logs as $log) {
|
|
fputcsv($handle, [
|
|
$log->created_at,
|
|
$log->user?->email ?? 'System',
|
|
$log->action,
|
|
json_encode($log->metadata, JSON_UNESCAPED_UNICODE),
|
|
]);
|
|
}
|
|
});
|
|
|
|
fclose($handle);
|
|
}, 200, [
|
|
'Content-Type' => 'text/csv',
|
|
'Content-Disposition' => 'attachment; filename="audit-logs-'.now()->format('Ymd_His').'.csv"',
|
|
]);
|
|
}
|
|
}
|