Normalize formatting/line endings across configs, controllers, models, tests, and OpenAPI specs. Update rule expression/rule engine implementation and remove obsolete RuleAction controller/model. Add unit tests for rule expression syntax and multi-action behavior, and include docs updates.
103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
<?php
|
|
namespace App\Controllers\Organization;
|
|
|
|
use App\Traits\ResponseTrait;
|
|
use App\Controllers\BaseController;
|
|
|
|
use App\Models\Organization\SiteModel;
|
|
|
|
class SiteController extends BaseController {
|
|
use ResponseTrait;
|
|
|
|
protected $db;
|
|
protected $model;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->model = new SiteModel();
|
|
}
|
|
|
|
public function index() {
|
|
$filter = [
|
|
'SiteCode' => $this->request->getVar('SiteCode'),
|
|
'SiteName' => $this->request->getVar('SiteName'),
|
|
];
|
|
$rows = $this->model->getSites($filter);
|
|
if (empty($rows)) {
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
|
|
}
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
|
}
|
|
|
|
|
|
|
|
public function show($SiteID = null) {
|
|
//$rows = $this->model->where('SiteID', $SiteID)->findAll();
|
|
$row = $this->model->getSite($SiteID);
|
|
|
|
if (empty($row)) {
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
|
|
}
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 200);
|
|
}
|
|
|
|
public function delete() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$id = $input["SiteID"];
|
|
if (!$id) { return $this->failValidationErrors('ID is required.'); }
|
|
$this->model->delete($id);
|
|
return $this->respondDeleted([ 'status' => 'success', 'message' => "{$id} deleted successfully."]);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function create() {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
$validation = service('validation');
|
|
$validation->setRules([
|
|
'SiteCode' => 'required|regex_match[/^[A-Z0-9]{2}$/]',
|
|
'SiteName' => 'required',
|
|
]);
|
|
|
|
if (!$validation->run($input)) {
|
|
return $this->failValidationErrors($validation->getErrors());
|
|
}
|
|
|
|
try {
|
|
$id = $this->model->insert($input,true);
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update() {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
$id = $input['SiteID'];
|
|
if (!$id) { return $this->failValidationErrors('ID is required.'); }
|
|
|
|
if (!empty($input['SiteCode'])) {
|
|
$validation = service('validation');
|
|
$validation->setRules([
|
|
'SiteCode' => 'regex_match[/^[A-Z0-9]{2}$/]',
|
|
]);
|
|
|
|
if (!$validation->run($input)) {
|
|
return $this->failValidationErrors($validation->getErrors());
|
|
}
|
|
}
|
|
|
|
try {
|
|
$this->model->update($id, $input);
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
|
|
} catch (\Throwable $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|