Files
WMS/Modules/Countries/Http/Controllers/CountriesController.php
T
2026-09-21 10:14:57 +01:00

141 lines
4.9 KiB
PHP
Executable File

<?php
namespace Modules\Countries\Http\Controllers;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Countries\Http\Requests\StoreCountryRequest;
use Modules\Countries\Http\Requests\UpdateCountryRequest;
use Modules\Countries\Models\Country;
use Modules\Countries\Support\CountryUsageReport;
class CountriesController extends Controller
{
public function __invoke(Request $request): View
{
$search = trim((string) $request->query('search', ''));
$status = (string) $request->query('status', 'all');
$region = (string) $request->query('region', 'all');
$countries = Country::query()
->when($search !== '', function ($query) use ($search): void {
$query->where(function ($nested) use ($search): void {
$nested->where('name', 'ilike', "%{$search}%")
->orWhere('official_name', 'ilike', "%{$search}%")
->orWhere('common_name', 'ilike', "%{$search}%")
->orWhere('display_name', 'ilike', "%{$search}%")
->orWhere('native_name', 'ilike', "%{$search}%")
->orWhere('alpha2', 'ilike', "%{$search}%")
->orWhere('alpha3', 'ilike', "%{$search}%")
->orWhere('numeric', 'ilike', "%{$search}%")
->orWhere('currency_code', 'ilike', "%{$search}%")
->orWhere('calling_code', 'ilike', "%{$search}%");
});
})
->when($status === 'active', fn ($query) => $query->where('is_active', true))
->when($status === 'inactive', fn ($query) => $query->where('is_active', false))
->when($region !== 'all' && $region !== '', fn ($query) => $query->where('region', $region))
->orderByRaw('LOWER(COALESCE(display_name, common_name, name)) ASC')
->paginate(50)
->withQueryString();
$activeCount = Country::query()->where('is_active', true)->count();
$inactiveCount = Country::query()->where('is_active', false)->count();
$totalCount = Country::query()->count();
$regions = Country::query()
->whereNotNull('region')
->distinct()
->orderBy('region')
->pluck('region');
return view('countries::index', compact(
'countries',
'activeCount',
'inactiveCount',
'totalCount',
'search',
'status',
'region',
'regions',
));
}
public function create(): View
{
return view('countries::create', ['country' => new Country]);
}
public function store(StoreCountryRequest $request): RedirectResponse
{
$country = Country::query()->create($this->payload($request));
return redirect()
->route('countries.show', $country)
->with('success', 'Country created.');
}
public function show(Country $country, CountryUsageReport $usageReport): View
{
$country->load(['subdivisions' => fn ($query) => $query->ordered()->limit(250)]);
$usageRows = $usageReport->forCountry($country);
return view('countries::show', compact('country', 'usageRows'));
}
public function edit(Country $country): View
{
return view('countries::edit', compact('country'));
}
public function update(UpdateCountryRequest $request, Country $country): RedirectResponse
{
$country->update($this->payload($request));
return redirect()
->route('countries.show', $country)
->with('success', 'Country updated.');
}
public function destroy(Country $country): RedirectResponse
{
$country->delete();
return redirect()
->route('countries.index')
->with('success', 'Country archived.');
}
private function payload(StoreCountryRequest|UpdateCountryRequest $request): array
{
$data = $request->validated();
foreach (['alpha2', 'alpha3', 'currency_code', 'ioc_code', 'fifa_code'] as $key) {
if (isset($data[$key]) && is_string($data[$key])) {
$data[$key] = strtoupper(trim($data[$key]));
}
}
if (isset($data['numeric']) && is_string($data['numeric'])) {
$data['numeric'] = str_pad(trim($data['numeric']), 3, '0', STR_PAD_LEFT);
}
foreach (['languages', 'timezones', 'metadata'] as $jsonField) {
$value = $data[$jsonField] ?? null;
$data[$jsonField] = blank($value)
? null
: json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR);
}
foreach (['is_active', 'independent', 'un_member'] as $booleanField) {
$data[$booleanField] = $request->boolean($booleanField);
}
return $data;
}
}