added countries support expanded for ISO 3166 standard merged in new support
This commit is contained in:
@@ -6,15 +6,21 @@ 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
|
||||
{
|
||||
/**
|
||||
* Display ISO 3166 country records.
|
||||
*/
|
||||
public function index(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 {
|
||||
@@ -23,13 +29,17 @@ class CountriesController extends Controller
|
||||
->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('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))
|
||||
->ordered()
|
||||
->paginate(50)
|
||||
->withQueryString();
|
||||
@@ -37,32 +47,97 @@ class CountriesController extends Controller
|
||||
$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'));
|
||||
return view('countries::index', compact('countries', 'activeCount', 'inactiveCount', 'totalCount', 'search', 'status', 'region', 'regions'));
|
||||
}
|
||||
|
||||
public function show(Country $country): View
|
||||
/**
|
||||
* Show the country creation form.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('countries::show', compact('country'));
|
||||
return view('countries::create', ['country' => new Country()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new ISO country record.
|
||||
*/
|
||||
public function store(StoreCountryRequest $request): RedirectResponse
|
||||
{
|
||||
$country = Country::query()->create($this->payload($request));
|
||||
|
||||
return redirect()->route('countries.show', $country)->with('success', 'Country created.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a country record.
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the edit form.
|
||||
*/
|
||||
public function edit(Country $country): View
|
||||
{
|
||||
return view('countries::edit', compact('country'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all editable ISO/support fields.
|
||||
*/
|
||||
public function update(UpdateCountryRequest $request, Country $country): RedirectResponse
|
||||
{
|
||||
$country->update([
|
||||
'display_name' => $request->validated('display_name'),
|
||||
'is_active' => $request->boolean('is_active'),
|
||||
'sort_order' => (int) ($request->validated('sort_order') ?? 0),
|
||||
'iso_status' => $request->validated('iso_status'),
|
||||
'independent' => $request->boolean('independent'),
|
||||
'region' => $request->validated('region'),
|
||||
'subregion' => $request->validated('subregion'),
|
||||
]);
|
||||
$country->update($this->payload($request));
|
||||
|
||||
return redirect()->route('countries.show', $country)->with('success', 'Country settings updated.');
|
||||
return redirect()->route('countries.show', $country)->with('success', 'Country updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-delete a custom/retired country record.
|
||||
*/
|
||||
public function destroy(Country $country): RedirectResponse
|
||||
{
|
||||
$country->delete();
|
||||
|
||||
return redirect()->route('countries.index')->with('success', 'Country archived.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the database payload from validated request data.
|
||||
*
|
||||
* @param StoreCountryRequest|UpdateCountryRequest $request
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user