Skip to content

A free open source IT asset/license management system

Added view composer forn sidebar counts, removed sidebar middleware

snipe0d745ad10f6b
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php
index ba0cb5e0d6..1774b4f9f7 100644
--- a/app/Http/Kernel.php
+++ b/app/Http/Kernel.php
@@ -2,7 +2,6 @@
namespace App\Http;
-use App\Http\Middleware\AssetCountForSidebar;
use App\Http\Middleware\CheckColorSettings;
use App\Http\Middleware\CheckForDebug;
use App\Http\Middleware\CheckForSetup;
@@ -75,7 +74,6 @@ class Kernel extends HttpKernel
CheckUserIsActivated::class,
CheckForTwoFactor::class,
CreateFreshApiToken::class,
- AssetCountForSidebar::class,
CheckColorSettings::class,
AuthenticateSession::class,
SubstituteBindings::class,
diff --git a/app/Http/Middleware/AssetCountForSidebar.php b/app/Http/Middleware/AssetCountForSidebar.php
deleted file mode 100644
index da63d66984..0000000000
--- a/app/Http/Middleware/AssetCountForSidebar.php
+++ /dev/null
@@ -1,119 +0,0 @@
-<?php
-
-namespace App\Http\Middleware;
-
-use App\Models\Asset;
-use App\Models\Setting;
-use Closure;
-use Illuminate\Http\Request;
-use Illuminate\Support\Facades\Log;
-
-class AssetCountForSidebar
-{
- /**
- * Handle an incoming request.
- *
- * @param Request $request
- * @return mixed
- */
- public function handle($request, Closure $next)
- {
- /**
- * This needs to be set for the /setup process, since the tables might not exist yet
- */
- $total_assets = 0;
- $total_due_for_checkin = 0;
- $total_overdue_for_checkin = 0;
- $total_due_for_audit = 0;
- $total_overdue_for_audit = 0;
-
- try {
- $settings = Setting::getSettings();
- view()->share('settings', $settings);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_assets = Asset::AssetsForShow()->count();
- view()->share('total_assets', $total_assets);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_rtd_sidebar = Asset::RTD()->count();
- view()->share('total_rtd_sidebar', $total_rtd_sidebar);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_deployed_sidebar = Asset::Deployed()->count();
- view()->share('total_deployed_sidebar', $total_deployed_sidebar);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_archived_sidebar = Asset::Archived()->count();
- view()->share('total_archived_sidebar', $total_archived_sidebar);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_pending_sidebar = Asset::Pending()->count();
- view()->share('total_pending_sidebar', $total_pending_sidebar);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_undeployable_sidebar = Asset::Undeployable()->count();
- view()->share('total_undeployable_sidebar', $total_undeployable_sidebar);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_byod_sidebar = Asset::where('byod', '=', '1')->count();
- view()->share('total_byod_sidebar', $total_byod_sidebar);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_due_for_audit = Asset::DueForAudit($settings)->count();
- view()->share('total_due_for_audit', $total_due_for_audit);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_overdue_for_audit = Asset::OverdueForAudit()->count();
- view()->share('total_overdue_for_audit', $total_overdue_for_audit);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_due_for_checkin = Asset::DueForCheckin($settings)->count();
- view()->share('total_due_for_checkin', $total_due_for_checkin);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- try {
- $total_overdue_for_checkin = Asset::OverdueForCheckin()->count();
- view()->share('total_overdue_for_checkin', $total_overdue_for_checkin);
- } catch (\Exception $e) {
- Log::debug($e);
- }
-
- view()->share('total_due_and_overdue_for_checkin', ($total_due_for_checkin + $total_overdue_for_checkin));
- view()->share('total_due_and_overdue_for_audit', ($total_due_for_audit + $total_overdue_for_audit));
-
- return $next($request);
- }
-}
diff --git a/app/View/Composers/SidebarComposer.php b/app/View/Composers/SidebarComposer.php
new file mode 100644
index 0000000000..5e12e41fcc
--- /dev/null
+++ b/app/View/Composers/SidebarComposer.php
@@ -0,0 +1,54 @@
+<?php
+
+// A View Composer is a callback Laravel runs right before a specific view renders.
+// It's registered in AppServiceProvider bound to 'layouts.default', so it only fires
+// when a full page is rendered — not on modal AJAX responses, select2 searches, or
+// API requests. This replaces the old AssetCountForSidebar middleware, which ran on
+// every web request regardless of what was returned.
+
+namespace App\View\Composers;
+
+use App\Models\Asset;
+use App\Models\Setting;
+use Illuminate\Support\Facades\Log;
+use Illuminate\View\View;
+
+class SidebarComposer
+{
+ public function compose(View $view): void
+ {
+ // Guard against the setup wizard, where DB tables may not exist yet
+ try {
+ $settings = Setting::getSettings();
+ } catch (\Exception $e) {
+ Log::debug($e);
+
+ return;
+ }
+
+ try {
+ $due_for_checkin = Asset::DueForCheckin($settings)->count();
+ $overdue_for_checkin = Asset::OverdueForCheckin()->count();
+ $due_for_audit = Asset::DueForAudit($settings)->count();
+ $overdue_for_audit = Asset::OverdueForAudit()->count();
+
+ $view->with([
+ 'total_assets' => Asset::AssetsForShow()->count(),
+ 'total_rtd_sidebar' => Asset::RTD()->count(),
+ 'total_deployed_sidebar' => Asset::Deployed()->count(),
+ 'total_archived_sidebar' => Asset::Archived()->count(),
+ 'total_pending_sidebar' => Asset::Pending()->count(),
+ 'total_undeployable_sidebar' => Asset::Undeployable()->count(),
+ 'total_byod_sidebar' => Asset::where('byod', 1)->count(),
+ 'total_due_for_audit' => $due_for_audit,
+ 'total_overdue_for_audit' => $overdue_for_audit,
+ 'total_due_for_checkin' => $due_for_checkin,
+ 'total_overdue_for_checkin' => $overdue_for_checkin,
+ 'total_due_and_overdue_for_checkin' => $due_for_checkin + $overdue_for_checkin,
+ 'total_due_and_overdue_for_audit' => $due_for_audit + $overdue_for_audit,
+ ]);
+ } catch (\Exception $e) {
+ Log::debug($e);
+ }
+ }
+}