From b88b5873583afb651482a223036bf8c6867acb84 Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Tue, 21 Oct 2025 15:27:22 +0700 Subject: [PATCH 1/5] Update API Zones for city and province --- app/Config/Routes.php | 4 ++++ app/Controllers/Api/ZonesApi.php | 32 ++++++++++++++++++++++++++++++++ app/Models/ZonesModel.php | 12 ++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 app/Controllers/Api/ZonesApi.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 8220e7a..e25dfc8 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -256,10 +256,14 @@ $routes->group('', ['filter' => 'cors'], function($routes) { }); // REST API +$routes->get('/api/provinces', 'Api\ZonesApi::getProvinces'); +$routes->get('/api/cities', 'Api\ZonesApi::getCities'); +// $routes->get('/api/zones', 'Api::ZonesApi/'); // $routes->get('/api/getProductList', 'Api::getProductList'); $routes->get('/api/getProductAlias', 'Api::getProductAlias'); $routes->get('/api/getProductSites', 'Api::getProductSites'); + //CLQMS // $routes->get('/clqms', 'Clqms::index'); // for CLQMS Inst select /* diff --git a/app/Controllers/Api/ZonesApi.php b/app/Controllers/Api/ZonesApi.php new file mode 100644 index 0000000..934c7c6 --- /dev/null +++ b/app/Controllers/Api/ZonesApi.php @@ -0,0 +1,32 @@ +model = new ZonesModel(); + } + + public function getProvinces() { + + $rows = $this->model->getAllProvinces(); + + if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } + return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); + } + + public function getCities() { + + $rows = $this->model->getAllCities(); + + if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } + return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); + } +} \ No newline at end of file diff --git a/app/Models/ZonesModel.php b/app/Models/ZonesModel.php index c607bc7..4d99912 100644 --- a/app/Models/ZonesModel.php +++ b/app/Models/ZonesModel.php @@ -3,7 +3,19 @@ use CodeIgniter\Model; class ZonesModel extends Model { + protected $table = 'zones'; protected $primaryKey = 'zoneid'; protected $allowedFields = [ 'zonecode', 'zoneclass', 'parentzoneid', 'zonename' ]; + + public function getAllProvinces() { + $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false)->findAll(); + return $rows; + } + + public function getAllCities() { + $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false)->findAll(); + return $rows; + } + } \ No newline at end of file From 9deab403d783b7f9c030e80f4654291a128c5fcd Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Tue, 21 Oct 2025 21:59:15 +0700 Subject: [PATCH 2/5] Update Cors Policy for CLQMS --- app/Config/Filters.php | 5 +++-- app/Filters/Cors.php | 31 ++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/Config/Filters.php b/app/Config/Filters.php index 686e867..7752f4c 100644 --- a/app/Config/Filters.php +++ b/app/Config/Filters.php @@ -32,9 +32,9 @@ class Filters extends BaseFilters 'secureheaders' => SecureHeaders::class, 'forcehttps' => ForceHTTPS::class, 'pagecache' => PageCache::class, - 'performance' => PerformanceMetrics::class, - 'auth' => \App\Filters\Auth::class, + 'performance' => PerformanceMetrics::class, 'cors' => \App\Filters\Cors::class, + 'auth' => \App\Filters\Auth::class, ]; /** @@ -74,6 +74,7 @@ class Filters extends BaseFilters */ public array $globals = [ 'before' => [ + 'cors', 'auth' => [ 'except' => [ 'auth/*', 'lqms/*', 'key/*', 'api/*' ]] diff --git a/app/Filters/Cors.php b/app/Filters/Cors.php index 36947ac..6ed0c7b 100644 --- a/app/Filters/Cors.php +++ b/app/Filters/Cors.php @@ -6,21 +6,46 @@ use CodeIgniter\Filters\FilterInterface; class Cors implements FilterInterface { + protected $allowedOrigins = [ + 'http://localhost:5173', + 'https://clqms01.services-summit.my.id', + ]; + public function before(RequestInterface $request, $arguments = null) { - header('Access-Control-Allow-Origin: *'); - header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); - header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-CSRF-TOKEN'); + // header('Access-Control-Allow-Origin: *'); + // header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); + // header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-CSRF-TOKEN'); // Handle preflight requests // if ($request->getMethod() === 'options') { // header('HTTP/1.1 200 OK'); // exit(); // } + + // log_message('debug', 'Cors Filter Triggered First'); + $origin = $_SERVER['HTTP_ORIGIN'] ?? ''; + $response = service('response'); + + if (in_array($origin, $this->allowedOrigins)) { + $response->setHeader('Access-Control-Allow-Origin', $origin); + $response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS'); + $response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, Accept, Origin, Cache-Control, Pragma, X-CSRF-TOKEN'); + // $response->setHeader('Access-Control-Allow-Headers', '*'); + $response->setHeader('Access-Control-Allow-Credentials', 'true'); + } + + // Tangani preflight OPTIONS dengan return response + if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { + // log_message('debug', 'Cors Filter Triggered OK'); + return $response->setStatusCode(200)->setBody('OK'); + } + // log_message('debug', 'Cors Filter Triggered Second'); } public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { // No actions required after the request + return $response; } } From f4961f862e4c532ddabbc298fcc319f56c6c136f Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Wed, 22 Oct 2025 10:09:31 +0700 Subject: [PATCH 3/5] Update Zones Api With Params zoneid & zonename --- app/Controllers/Api/ZonesApi.php | 17 +++++++++++++---- app/Models/ZonesModel.php | 28 ++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/Controllers/Api/ZonesApi.php b/app/Controllers/Api/ZonesApi.php index 934c7c6..5752529 100644 --- a/app/Controllers/Api/ZonesApi.php +++ b/app/Controllers/Api/ZonesApi.php @@ -15,18 +15,27 @@ class ZonesApi extends BaseController { } public function getProvinces() { + $filters = [ + 'zoneid' => $this->request->getVar('zoneid') ?? null, + 'zonename' => $this->request->getVar('zonename') ?? null + ]; - $rows = $this->model->getAllProvinces(); + $rows = $this->model->getAllProvinces($filters); - if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } + if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } public function getCities() { - $rows = $this->model->getAllCities(); + $filters = [ + 'zoneid' => $this->request->getVar('zoneid') ?? null, + 'zonename' => $this->request->getVar('zonename') ?? null + ]; + + $rows = $this->model->getAllCities($filters); - if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200); } + if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } } \ No newline at end of file diff --git a/app/Models/ZonesModel.php b/app/Models/ZonesModel.php index 4d99912..f6fb33a 100644 --- a/app/Models/ZonesModel.php +++ b/app/Models/ZonesModel.php @@ -8,14 +8,30 @@ class ZonesModel extends Model { protected $primaryKey = 'zoneid'; protected $allowedFields = [ 'zonecode', 'zoneclass', 'parentzoneid', 'zonename' ]; - public function getAllProvinces() { - $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false)->findAll(); - return $rows; + public function getAllProvinces($filters = []) { + $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false); + + if (!empty($filters['zoneid'])) { + $this->where('zoneid', $filters['zoneid']); + } + if (!empty($filters['zonename'])) { + $this->like('zonename', $filters['zonename'], 'both'); + } + + return $this->findAll(); } - public function getAllCities() { - $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false)->findAll(); - return $rows; + public function getAllCities($filters = []) { + $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false); + + if (!empty($filters['zoneid'])) { + $this->where('zoneid', $filters['zoneid']); + } + if (!empty($filters['zonename'])) { + $this->like('zonename', $filters['zonename'], 'both'); + } + + return $this->findAll(); } } \ No newline at end of file From 34449ec836f8aa57ace2fe9b8cedfb05d1906d3a Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Wed, 22 Oct 2025 10:23:46 +0700 Subject: [PATCH 4/5] Update Zones Api Perbaikan hanya get cities pakai Params zoneid saja --- app/Controllers/Api/ZonesApi.php | 17 ++++++++--------- app/Models/ZonesModel.php | 23 ++++++++++------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/app/Controllers/Api/ZonesApi.php b/app/Controllers/Api/ZonesApi.php index 5752529..297fd62 100644 --- a/app/Controllers/Api/ZonesApi.php +++ b/app/Controllers/Api/ZonesApi.php @@ -15,12 +15,12 @@ class ZonesApi extends BaseController { } public function getProvinces() { - $filters = [ - 'zoneid' => $this->request->getVar('zoneid') ?? null, - 'zonename' => $this->request->getVar('zonename') ?? null - ]; + // $filters = [ + // 'zoneid' => $this->request->getVar('zoneid') ?? null, + // 'zonename' => $this->request->getVar('zonename') ?? null + // ]; - $rows = $this->model->getAllProvinces($filters); + $rows = $this->model->getAllProvinces(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); @@ -28,12 +28,11 @@ class ZonesApi extends BaseController { public function getCities() { - $filters = [ - 'zoneid' => $this->request->getVar('zoneid') ?? null, - 'zonename' => $this->request->getVar('zonename') ?? null + $filter = [ + 'zoneid' => $this->request->getVar('zoneid') ?? null ]; - $rows = $this->model->getAllCities($filters); + $rows = $this->model->getAllCities($filter); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); diff --git a/app/Models/ZonesModel.php b/app/Models/ZonesModel.php index f6fb33a..773460f 100644 --- a/app/Models/ZonesModel.php +++ b/app/Models/ZonesModel.php @@ -8,27 +8,24 @@ class ZonesModel extends Model { protected $primaryKey = 'zoneid'; protected $allowedFields = [ 'zonecode', 'zoneclass', 'parentzoneid', 'zonename' ]; - public function getAllProvinces($filters = []) { + public function getAllProvinces() { $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false); - if (!empty($filters['zoneid'])) { - $this->where('zoneid', $filters['zoneid']); - } - if (!empty($filters['zonename'])) { - $this->like('zonename', $filters['zonename'], 'both'); - } + // if (!empty($filters['zoneid'])) { + // $this->where('zoneid', $filters['zoneid']); + // } + // if (!empty($filters['zonename'])) { + // $this->like('zonename', $filters['zonename'], 'both'); + // } return $this->findAll(); } - public function getAllCities($filters = []) { + public function getAllCities($filter = []) { $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false); - if (!empty($filters['zoneid'])) { - $this->where('zoneid', $filters['zoneid']); - } - if (!empty($filters['zonename'])) { - $this->like('zonename', $filters['zonename'], 'both'); + if (!empty($filter['zoneid'])) { + $this->where('parentzoneid', $filter['zoneid']); } return $this->findAll(); From 6a77f66e87e5e3e6ea5db497790af701cb6d4f76 Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Thu, 23 Oct 2025 09:02:24 +0700 Subject: [PATCH 5/5] Update Cors Kembali ke Awal & API get all Zones --- app/Config/Filters.php | 2 +- app/Config/Routes.php | 7 +++-- app/Controllers/Api/ZonesApi.php | 41 ++++++++++++++++------------ app/Filters/Cors.php | 46 +++++++++++++++++--------------- app/Models/ZonesModel.php | 38 ++++++++++++++------------ 5 files changed, 73 insertions(+), 61 deletions(-) diff --git a/app/Config/Filters.php b/app/Config/Filters.php index 7752f4c..fb757f0 100644 --- a/app/Config/Filters.php +++ b/app/Config/Filters.php @@ -74,7 +74,7 @@ class Filters extends BaseFilters */ public array $globals = [ 'before' => [ - 'cors', + // 'cors', 'auth' => [ 'except' => [ 'auth/*', 'lqms/*', 'key/*', 'api/*' ]] diff --git a/app/Config/Routes.php b/app/Config/Routes.php index e25dfc8..4d33d21 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -256,10 +256,9 @@ $routes->group('', ['filter' => 'cors'], function($routes) { }); // REST API -$routes->get('/api/provinces', 'Api\ZonesApi::getProvinces'); -$routes->get('/api/cities', 'Api\ZonesApi::getCities'); -// $routes->get('/api/zones', 'Api::ZonesApi/'); -// $routes->get('/api/getProductList', 'Api::getProductList'); +$routes->get('/api/zones', 'Api\ZonesApi::getAll'); +// $routes->get('/api/provinces', 'Api\ZonesApi::getProvinces'); +// $routes->get('/api/cities', 'Api\ZonesApi::getCities'); $routes->get('/api/getProductAlias', 'Api::getProductAlias'); $routes->get('/api/getProductSites', 'Api::getProductSites'); diff --git a/app/Controllers/Api/ZonesApi.php b/app/Controllers/Api/ZonesApi.php index 297fd62..a71eb1f 100644 --- a/app/Controllers/Api/ZonesApi.php +++ b/app/Controllers/Api/ZonesApi.php @@ -14,27 +14,34 @@ class ZonesApi extends BaseController { $this->model = new ZonesModel(); } - public function getProvinces() { - // $filters = [ - // 'zoneid' => $this->request->getVar('zoneid') ?? null, - // 'zonename' => $this->request->getVar('zonename') ?? null - // ]; + public function getAll() { + $rows = $this->model->getAll(); - $rows = $this->model->getAllProvinces(); - if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } - return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); + return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } - public function getCities() { + // public function getProvinces() { + // // $filters = [ + // // 'zoneid' => $this->request->getVar('zoneid') ?? null, + // // 'zonename' => $this->request->getVar('zonename') ?? null + // // ]; - $filter = [ - 'zoneid' => $this->request->getVar('zoneid') ?? null - ]; - - $rows = $this->model->getAllCities($filter); + // $rows = $this->model->getAllProvinces(); - if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } - return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); - } + // if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } + // return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); + // } + + // public function getCities() { + + // $filter = [ + // 'zoneid' => $this->request->getVar('zoneid') ?? null + // ]; + + // $rows = $this->model->getAllCities($filter); + + // if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); } + // return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); + // } } \ No newline at end of file diff --git a/app/Filters/Cors.php b/app/Filters/Cors.php index 6ed0c7b..7104441 100644 --- a/app/Filters/Cors.php +++ b/app/Filters/Cors.php @@ -6,46 +6,48 @@ use CodeIgniter\Filters\FilterInterface; class Cors implements FilterInterface { - protected $allowedOrigins = [ - 'http://localhost:5173', - 'https://clqms01.services-summit.my.id', - ]; + // CORS BARU + // protected $allowedOrigins = [ + // 'http://localhost:5173', + // 'https://clqms01.services-summit.my.id', + // ]; public function before(RequestInterface $request, $arguments = null) { - // header('Access-Control-Allow-Origin: *'); - // header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); - // header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-CSRF-TOKEN'); - + // CORS LAMA + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); + header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, X-CSRF-TOKEN'); // Handle preflight requests // if ($request->getMethod() === 'options') { // header('HTTP/1.1 200 OK'); // exit(); // } + // CORS BARU // log_message('debug', 'Cors Filter Triggered First'); - $origin = $_SERVER['HTTP_ORIGIN'] ?? ''; - $response = service('response'); + // $origin = $_SERVER['HTTP_ORIGIN'] ?? ''; + // $response = service('response'); - if (in_array($origin, $this->allowedOrigins)) { - $response->setHeader('Access-Control-Allow-Origin', $origin); - $response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS'); - $response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, Accept, Origin, Cache-Control, Pragma, X-CSRF-TOKEN'); - // $response->setHeader('Access-Control-Allow-Headers', '*'); - $response->setHeader('Access-Control-Allow-Credentials', 'true'); - } + // if (in_array($origin, $this->allowedOrigins)) { + // $response->setHeader('Access-Control-Allow-Origin', $origin); + // $response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS'); + // $response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, Accept, Origin, Cache-Control, Pragma, X-CSRF-TOKEN'); + // // $response->setHeader('Access-Control-Allow-Headers', '*'); + // $response->setHeader('Access-Control-Allow-Credentials', 'true'); + // } // Tangani preflight OPTIONS dengan return response - if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { - // log_message('debug', 'Cors Filter Triggered OK'); - return $response->setStatusCode(200)->setBody('OK'); - } + // if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { + // // log_message('debug', 'Cors Filter Triggered OK'); + // return $response->setStatusCode(200)->setBody('OK'); + // } // log_message('debug', 'Cors Filter Triggered Second'); } public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { // No actions required after the request - return $response; + // return $response; } } diff --git a/app/Models/ZonesModel.php b/app/Models/ZonesModel.php index 773460f..6caf13b 100644 --- a/app/Models/ZonesModel.php +++ b/app/Models/ZonesModel.php @@ -8,27 +8,31 @@ class ZonesModel extends Model { protected $primaryKey = 'zoneid'; protected $allowedFields = [ 'zonecode', 'zoneclass', 'parentzoneid', 'zonename' ]; - public function getAllProvinces() { - $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false); - - // if (!empty($filters['zoneid'])) { - // $this->where('zoneid', $filters['zoneid']); - // } - // if (!empty($filters['zonename'])) { - // $this->like('zonename', $filters['zonename'], 'both'); - // } - + public function getAll() { return $this->findAll(); } - public function getAllCities($filter = []) { - $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false); + // public function getAllProvinces() { + // $this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false); - if (!empty($filter['zoneid'])) { - $this->where('parentzoneid', $filter['zoneid']); - } + // // if (!empty($filters['zoneid'])) { + // // $this->where('zoneid', $filters['zoneid']); + // // } + // // if (!empty($filters['zonename'])) { + // // $this->like('zonename', $filters['zonename'], 'both'); + // // } - return $this->findAll(); - } + // return $this->findAll(); + // } + + // public function getAllCities($filter = []) { + // $rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false); + + // if (!empty($filter['zoneid'])) { + // $this->where('parentzoneid', $filter['zoneid']); + // } + + // return $this->findAll(); + // } } \ No newline at end of file