97 lines
3.6 KiB
PHP
97 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Activitylog\Models\Activity;
|
|
use Illuminate\Http\Request;
|
|
|
|
class GlobalSearchController extends Controller
|
|
{
|
|
public function __invoke(Request $request)
|
|
{
|
|
$query = $request->input('query');
|
|
|
|
if (empty($query)) {
|
|
return response()->json([]);
|
|
}
|
|
|
|
$results = [];
|
|
|
|
// Search Users
|
|
$users = User::where('first_name', 'like', "%{$query}%")
|
|
->orWhere('last_name', 'like', "%{$query}%")
|
|
->orWhere('email', 'like', "%{$query}%")
|
|
->limit(5)
|
|
->get()
|
|
->map(fn($u) => [
|
|
'type' => 'User',
|
|
'title' => "{$u->first_name} {$u->last_name}",
|
|
'subtitle' => $u->email,
|
|
'url' => route('users.show', $u->id),
|
|
'icon' => 'user'
|
|
]);
|
|
$results = array_merge($results, $users->toArray());
|
|
|
|
// Search Roles
|
|
$roles = Role::where('name', 'like', "%{$query}%")
|
|
->limit(3)
|
|
->get()
|
|
->map(fn($r) => [
|
|
'type' => 'Role',
|
|
'title' => $r->name,
|
|
'subtitle' => "{$r->permissions()->count()} permissions",
|
|
'url' => route('roles.index'),
|
|
'icon' => 'shield'
|
|
]);
|
|
$results = array_merge($results, $roles->toArray());
|
|
|
|
// Search Activity Logs
|
|
$logs = Activity::where('description', 'like', "%{$query}%")
|
|
->orWhere('log_name', 'like', "%{$query}%")
|
|
->latest()
|
|
->limit(5)
|
|
->get()
|
|
->map(fn($l) => [
|
|
'type' => 'Log',
|
|
'title' => $l->description,
|
|
'subtitle' => $l->created_at->diffForHumans(),
|
|
'url' => route('activity-logs.index'),
|
|
'icon' => 'clock'
|
|
]);
|
|
$results = array_merge($results, $logs->toArray());
|
|
|
|
// Search Navigation Pages
|
|
$pages = [
|
|
['title' => 'Dashboard', 'url' => route('dashboard'), 'icon' => 'clock', 'keywords' => ['home', 'index', 'dashboard']],
|
|
['title' => 'User Management', 'url' => route('users.index'), 'icon' => 'user', 'keywords' => ['users', 'people', 'staff', 'accounts']],
|
|
['title' => 'Role Management', 'url' => route('roles.index'), 'icon' => 'shield', 'keywords' => ['roles', 'permissions', 'access', 'security']],
|
|
['title' => 'Activity Logs', 'url' => route('activity-logs.index'), 'icon' => 'clock', 'keywords' => ['logs', 'audit', 'history', 'events']],
|
|
['title' => 'System Settings', 'url' => route('system.settings.index'), 'icon' => 'shield', 'keywords' => ['settings', 'config', 'setup', 'system']],
|
|
['title' => 'Profile Settings', 'url' => route('profile.edit'), 'icon' => 'user', 'keywords' => ['profile', 'me', 'account', 'password']],
|
|
];
|
|
|
|
$matchedPages = array_filter($pages, function($page) use ($query) {
|
|
$q = strtolower($query);
|
|
if (str_contains(strtolower($page['title']), $q)) return true;
|
|
foreach ($page['keywords'] as $keyword) {
|
|
if (str_contains($keyword, $q)) return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
foreach ($matchedPages as $page) {
|
|
$results[] = [
|
|
'type' => 'Page',
|
|
'title' => $page['title'],
|
|
'subtitle' => 'System Navigation',
|
|
'url' => $page['url'],
|
|
'icon' => $page['icon']
|
|
];
|
|
}
|
|
|
|
return response()->json($results);
|
|
}
|
|
}
|