Update Quick Action Button
This commit is contained in:
@@ -23,22 +23,52 @@ class activitypurgesExtensionController extends Controller
|
|||||||
$message = null;
|
$message = null;
|
||||||
|
|
||||||
if ($request->isMethod('post')) {
|
if ($request->isMethod('post')) {
|
||||||
// Validate the input timestamp.
|
// Validate the input.
|
||||||
|
// If "quick" is provided, the manual timestamp is not required.
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'timestamp' => 'required|date'
|
'tables' => 'required|array',
|
||||||
|
'tables.*' => 'in:activity_logs,api_logs,audit_logs',
|
||||||
|
'timestamp' => 'required_without:quick|nullable|date',
|
||||||
|
'quick' => 'nullable|in:7,14,30,90,180,365'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Convert the HTML datetime-local value to MySQL datetime format.
|
// Determine the cutoff timestamp:
|
||||||
$rawTimestamp = $validated['timestamp'];
|
if ($request->has('quick')) {
|
||||||
$mysqlTimestamp = date('Y-m-d H:i:s', strtotime($rawTimestamp));
|
$days = (int)$validated['quick'];
|
||||||
|
// Use Laravel's now() helper to subtract days.
|
||||||
|
$mysqlTimestamp = now()->subDays($days)->format('Y-m-d H:i:s');
|
||||||
|
} else {
|
||||||
|
$rawTimestamp = $validated['timestamp'];
|
||||||
|
$mysqlTimestamp = date('Y-m-d H:i:s', strtotime($rawTimestamp));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map each table to its respective date column.
|
||||||
|
$columnsMapping = [
|
||||||
|
'activity_logs' => 'timestamp',
|
||||||
|
'api_logs' => 'updated_at',
|
||||||
|
'audit_logs' => 'created_at', // Adjust if your audit_logs table uses a different column name.
|
||||||
|
];
|
||||||
|
|
||||||
|
$deletedRecords = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Purge records older than the provided timestamp.
|
// Loop through each selected table and delete records older than the computed timestamp.
|
||||||
$deleted = DB::table('activity_logs')
|
foreach ($validated['tables'] as $table) {
|
||||||
->where('timestamp', '<', $mysqlTimestamp)
|
// Get the date column for this table.
|
||||||
->delete();
|
$dateColumn = $columnsMapping[$table] ?? 'timestamp';
|
||||||
|
|
||||||
$message = "{$deleted} log(s) have been purged successfully.";
|
$deleted = DB::table($table)
|
||||||
|
->where($dateColumn, '<', $mysqlTimestamp)
|
||||||
|
->delete();
|
||||||
|
$deletedRecords[$table] = $deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare a message summarizing the deletion results.
|
||||||
|
$msgParts = [];
|
||||||
|
foreach ($deletedRecords as $table => $count) {
|
||||||
|
$msgParts[] = "{$table}: {$count} log(s)";
|
||||||
|
}
|
||||||
|
$message = "Purged records - " . implode(", ", $msgParts);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
\Log::error('Purge error: ' . $e->getMessage());
|
\Log::error('Purge error: ' . $e->getMessage());
|
||||||
$message = "An error occurred while purging logs.";
|
$message = "An error occurred while purging logs.";
|
||||||
@@ -46,9 +76,9 @@ class activitypurgesExtensionController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $this->view->make('admin.extensions.activitypurges.index', [
|
return $this->view->make('admin.extensions.activitypurges.index', [
|
||||||
'root' => "/admin/extensions/activitypurges",
|
'root' => "/admin/extensions/activitypurges",
|
||||||
'blueprint' => $this->blueprint,
|
'blueprint' => $this->blueprint,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,25 +6,67 @@
|
|||||||
<h3 class="box-title">Information</h3>
|
<h3 class="box-title">Information</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="box-body">
|
<div class="box-body">
|
||||||
<!-- Display success message if available -->
|
<p>
|
||||||
|
An Extension Used To <code>Clear/Purge</code> selected logs in order to clear database storage.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="box box-info">
|
||||||
|
<div class="box-header with-border">
|
||||||
|
<h3 class="box-title">Management</h3>
|
||||||
|
</div>
|
||||||
|
<div class="box-body">
|
||||||
|
<!-- Display success or error message if available -->
|
||||||
@if(isset($message) && $message)
|
@if(isset($message) && $message)
|
||||||
<div class="alert alert-success">
|
<div class="alert alert-success">
|
||||||
{{ $message }}
|
{{ $message }}
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
<p>
|
|
||||||
An Extension Used To <code>Clear/Purge</code> old Activity Logs, in order to clear database storage used.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<!-- The form posts to the same page -->
|
<!-- The form posts to the same page -->
|
||||||
<form method="POST" action="{{ $root }}">
|
<form method="POST" action="{{ $root }}">
|
||||||
@csrf
|
@csrf
|
||||||
|
|
||||||
|
<!-- Multi-selection for tables -->
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="timestamp">Purge logs older than:</label>
|
<label>Selection Table Area</label>
|
||||||
<input type="datetime-local" name="timestamp" id="timestamp" class="form-control" required>
|
<div class="d-flex">
|
||||||
|
<div class="form-check mr-3">
|
||||||
|
<input class="form-check-input" type="checkbox" id="activity_logs" name="tables[]" value="activity_logs">
|
||||||
|
<label class="form-check-label" for="activity_logs">Activity Logs</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-check mr-3">
|
||||||
|
<input class="form-check-input" type="checkbox" id="api_logs" name="tables[]" value="api_logs">
|
||||||
|
<label class="form-check-label" for="api_logs">API Logs</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" type="checkbox" id="audit_logs" name="tables[]" value="audit_logs">
|
||||||
|
<label class="form-check-label" for="audit_logs">Audit Logs</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Quick Delete Buttons -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Quick Action</label>
|
||||||
|
<div class="d-flex align-items-center">
|
||||||
|
<button type="submit" name="quick" value="7" class="btn btn-danger mr-2">1 Week</button>
|
||||||
|
<button type="submit" name="quick" value="14" class="btn btn-danger mr-2">2 Weeks</button>
|
||||||
|
<button type="submit" name="quick" value="30" class="btn btn-danger mr-2">1 Month</button>
|
||||||
|
<button type="submit" name="quick" value="90" class="btn btn-danger mr-2">3 Months</button>
|
||||||
|
<button type="submit" name="quick" value="180" class="btn btn-danger mr-2">6 Months</button>
|
||||||
|
<button type="submit" name="quick" value="365" class="btn btn-danger">1 Year</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Manual Purge Button -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="timestamp">Manual Purge</label>
|
||||||
|
<input type="datetime-local" name="timestamp" id="timestamp" class="form-control">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<button type="submit" class="btn btn-danger">Purge</button>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-danger">Purge Logs</button>
|
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
8
conf.yml
8
conf.yml
@@ -3,15 +3,15 @@ info:
|
|||||||
identifier: "activitypurges"
|
identifier: "activitypurges"
|
||||||
description: "Purgin Old Activity Logs From Database"
|
description: "Purgin Old Activity Logs From Database"
|
||||||
flags: ""
|
flags: ""
|
||||||
version: "1.10"
|
version: "1.1.2"
|
||||||
target: "beta-2024-12"
|
target: "beta-2024-12"
|
||||||
author: "nekomonci12"
|
author: "nekomonci12"
|
||||||
icon: "public/icon.jpg"
|
icon: ":public/icon.jpg"
|
||||||
website: ""
|
website: ""
|
||||||
|
|
||||||
admin:
|
admin:
|
||||||
view: "admin/view.blade.php"
|
view: ":admin/view.blade.php"
|
||||||
controller: "admin/controller.php"
|
controller: ":admin/controller.php"
|
||||||
css: ""
|
css: ""
|
||||||
wrapper: ""
|
wrapper: ""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user