83 lines
2.7 KiB
PHP
83 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Administration\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Pagination\Paginator;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Audit\Models\AuditLog;
|
|
use Modules\Media\Models\Media;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
final class DashboardController extends Controller
|
|
{
|
|
public function __invoke()
|
|
{
|
|
return stylist_backend_view(stylist()->backend()->entryView(), [
|
|
'userCount' => User::count(),
|
|
'roleCount' => Schema::hasTable('roles') ? Role::count() : 0,
|
|
'mediaCount' => Schema::hasTable('media') ? Media::count() : 0,
|
|
'auditCount' => Schema::hasTable('audit_logs') ? AuditLog::count() : 0,
|
|
'recentAudit' => $this->recentAudit(),
|
|
'modules' => $this->modules(),
|
|
]);
|
|
}
|
|
|
|
private function recentAudit(): LengthAwarePaginator
|
|
{
|
|
if (! Schema::hasTable('audit_logs')) {
|
|
return new LengthAwarePaginator(
|
|
items: [],
|
|
total: 0,
|
|
perPage: 8,
|
|
currentPage: Paginator::resolveCurrentPage('audit_page'),
|
|
options: [
|
|
'path' => request()->url(),
|
|
'pageName' => 'audit_page',
|
|
],
|
|
);
|
|
}
|
|
|
|
return AuditLog::query()
|
|
->latest('created_at')
|
|
->paginate(8, ['*'], 'audit_page')
|
|
->withQueryString();
|
|
}
|
|
|
|
private function modules(): LengthAwarePaginator
|
|
{
|
|
$statuses = json_decode((string) file_get_contents(base_path('modules_statuses.json')), true) ?: [];
|
|
$modules = [];
|
|
|
|
foreach (glob(base_path('Modules/*/module.json')) ?: [] as $manifest) {
|
|
$data = json_decode((string) file_get_contents($manifest), true) ?: [];
|
|
$name = $data['name'] ?? basename(dirname($manifest));
|
|
|
|
$modules[] = [
|
|
'name' => $name,
|
|
'version' => $data['version'] ?? '1.0.0',
|
|
'enabled' => (bool) ($statuses[$name] ?? false),
|
|
];
|
|
}
|
|
|
|
usort($modules, fn (array $a, array $b): int => strcasecmp($a['name'], $b['name']));
|
|
|
|
$pageName = 'modules_page';
|
|
$perPage = 8;
|
|
$currentPage = Paginator::resolveCurrentPage($pageName);
|
|
|
|
return (new LengthAwarePaginator(
|
|
items: array_slice($modules, ($currentPage - 1) * $perPage, $perPage),
|
|
total: count($modules),
|
|
perPage: $perPage,
|
|
currentPage: $currentPage,
|
|
options: [
|
|
'path' => request()->url(),
|
|
'pageName' => $pageName,
|
|
],
|
|
))->withQueryString();
|
|
}
|
|
}
|