- Redesign Account detail page as "Account Command Center" - Compact metric tiles, financial snapshot strip - Quick actions row and tab navigation - Two-column layout with quotes, orders, invoices, tasks - Fix Alpine.js modal initialization timing issues - send-menu-modal: register component and init if Alpine started - create-opportunity-modal: same fix - Fix user dropdown not opening (remove style="display:none", use x-cloak) - Add search routes and SearchController for seller context - Various CRM view updates for consistency
155 lines
4.4 KiB
PHP
155 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Seller;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Brand;
|
|
use Illuminate\Http\Request;
|
|
|
|
class BrandSwitcherController extends Controller
|
|
{
|
|
/**
|
|
* Switch the active brand context for the current session.
|
|
*
|
|
* Accepts brand_id (integer) or brand_hashid (string).
|
|
* Stores selection in session for persistence across page loads.
|
|
*/
|
|
public function switch(Request $request)
|
|
{
|
|
$brandId = $request->input('brand_id');
|
|
$brandHashid = $request->input('brand_hashid');
|
|
$redirectTo = $request->input('redirect_to');
|
|
|
|
// If both are empty, clear the session (show all brands)
|
|
if (empty($brandId) && empty($brandHashid)) {
|
|
// Clear cache for current user before removing session
|
|
$user = auth()->user();
|
|
$business = $user?->primaryBusiness();
|
|
$oldBrandId = session('selected_brand_id');
|
|
|
|
if ($user && $business && $oldBrandId) {
|
|
\Illuminate\Support\Facades\Cache::forget("selected_brand:{$user->id}:{$business->id}:{$oldBrandId}");
|
|
}
|
|
|
|
session()->forget('selected_brand_id');
|
|
|
|
return $redirectTo ? redirect($redirectTo) : back();
|
|
}
|
|
|
|
// Verify the brand exists and belongs to user's business
|
|
$user = auth()->user();
|
|
$business = $user->primaryBusiness();
|
|
|
|
if (! $business) {
|
|
return back()->with('error', 'No business associated with your account');
|
|
}
|
|
|
|
// Find brand by ID or hashid
|
|
$query = Brand::forBusiness($business);
|
|
if ($brandId) {
|
|
$query->where('id', $brandId);
|
|
} elseif ($brandHashid) {
|
|
$query->where('hashid', $brandHashid);
|
|
}
|
|
|
|
$brand = $query->first();
|
|
|
|
if (! $brand) {
|
|
return back()->with('error', 'Brand not found or you do not have access');
|
|
}
|
|
|
|
// Store selected brand in session
|
|
session(['selected_brand_id' => $brand->id]);
|
|
|
|
return back();
|
|
}
|
|
|
|
/**
|
|
* Get the currently selected brand (helper method).
|
|
* Cached for 5 minutes to avoid repeated queries on every page load.
|
|
*/
|
|
public static function getSelectedBrand(): ?Brand
|
|
{
|
|
$brandId = session('selected_brand_id');
|
|
|
|
if (! $brandId) {
|
|
return null;
|
|
}
|
|
|
|
$user = auth()->user();
|
|
$business = $user?->primaryBusiness();
|
|
|
|
if (! $business) {
|
|
return null;
|
|
}
|
|
|
|
// Cache by user + business + brand to avoid repeated queries
|
|
$cacheKey = "selected_brand:{$user->id}:{$business->id}:{$brandId}";
|
|
|
|
return \Illuminate\Support\Facades\Cache::remember($cacheKey, 300, function () use ($business, $brandId) {
|
|
return Brand::forBusiness($business)
|
|
->where('id', $brandId)
|
|
->first();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Set the selected brand in session.
|
|
*
|
|
* @param Brand|int|null $brand Brand model or ID
|
|
*/
|
|
public static function setSelectedBrand($brand): void
|
|
{
|
|
if ($brand === null) {
|
|
session()->forget('selected_brand_id');
|
|
|
|
return;
|
|
}
|
|
|
|
$brandId = $brand instanceof Brand ? $brand->id : $brand;
|
|
session(['selected_brand_id' => $brandId]);
|
|
}
|
|
|
|
/**
|
|
* Get brand IDs to filter queries by.
|
|
* Returns array of brand IDs (empty array = all brands for the business).
|
|
*
|
|
* @return array<int>
|
|
*/
|
|
public static function getFilteredBrandIds(): array
|
|
{
|
|
$selectedBrand = self::getSelectedBrand();
|
|
$user = auth()->user();
|
|
$business = $user?->primaryBusiness();
|
|
|
|
if (! $business) {
|
|
return [];
|
|
}
|
|
|
|
// If specific brand selected, return only that brand ID
|
|
if ($selectedBrand) {
|
|
return [$selectedBrand->id];
|
|
}
|
|
|
|
// Otherwise, return all brand IDs for the business
|
|
return $business->brands()->pluck('id')->toArray();
|
|
}
|
|
|
|
/**
|
|
* Check if a specific brand is currently selected.
|
|
*
|
|
* @param Brand|int $brand Brand model or ID
|
|
*/
|
|
public static function isSelected($brand): bool
|
|
{
|
|
$selectedBrand = self::getSelectedBrand();
|
|
if (! $selectedBrand) {
|
|
return false;
|
|
}
|
|
|
|
$brandId = $brand instanceof Brand ? $brand->id : $brand;
|
|
|
|
return $selectedBrand->id === $brandId;
|
|
}
|
|
}
|