clqms-be/app/Controllers/AreaGeoController.php

53 lines
1.9 KiB
PHP
Raw Normal View History

2025-12-01 16:47:52 +07:00
<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\AreaGeoModel;
class AreaGeoController extends BaseController {
2025-12-01 16:47:52 +07:00
use ResponseTrait;
protected $model;
public function __construct() {
$this->model = new AreaGeoModel();
}
public function index() {
try {
2025-12-02 12:52:23 +07:00
$filters = [
'AreaGeoID' => $this->request->getVar('AreaGeoID') ?? null,
'AreaName' => $this->request->getVar('AreaName') ?? null
];
$rows = $this->model->getAreaGeos( $filters );
2025-12-01 16:47:52 +07:00
if(empty($rows)){return $this->respond(['status'=>'success', 'message'=>"no data found.", 'data'=>$rows], 200);}
return $this->respond(['status'=>'success', 'message'=>"data fetched successfully", 'data'=>$rows], 200);
} catch (\Exception $e) {
return $this->respond([ 'status' => 'error', 'message' => $e->getMessage() ], 200);
}
}
public function getProvinces() {
$rows = $this->model->getProvinces();
$transformed = array_map(function($row) {
return ['value' => $row['AreaGeoID'], 'label' => $row['AreaName']];
}, $rows);
if (empty($transformed)) { return $this->respond([ 'status' => 'success', 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'data' => $transformed ], 200);
2025-12-01 16:47:52 +07:00
}
public function getCities() {
$filter = [ 'Parent' => $this->request->getVar('province_id') ?? null ];
2025-12-02 13:19:42 +07:00
$rows = $this->model->getCities($filter);
$transformed = array_map(function($row) {
return ['value' => $row['AreaGeoID'], 'label' => $row['AreaName']];
}, $rows);
if (empty($transformed)) { return $this->respond([ 'status' => 'success', 'data' => [] ], 200); }
return $this->respond([ 'status' => 'success', 'data' => $transformed ], 200);
2025-12-01 16:47:52 +07:00
}
}