231 lines
8.1 KiB
PHP
231 lines
8.1 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
class Location extends Controller {
|
|
use ResponseTrait;
|
|
|
|
protected $db;
|
|
protected $rules;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
$this->rules = [
|
|
'LocCode' => 'required|max_length[6]',
|
|
'LocFull' => 'required',
|
|
];
|
|
}
|
|
|
|
public function index() {
|
|
$LocName = $this->request->getVar('LocName');
|
|
$LocCode = $this->request->getVar('LocCode');
|
|
|
|
$sql = $this->db->table('location l')
|
|
->select("l.LocationID, LocCode, Parent, LocFull, LocType, v.VDesc ")
|
|
->join("locationaddress la", "l.LocationID=la.LocationID", 'left')
|
|
->join("valueset v", "v.VSetID=12 and v.VValue=l.loctype", 'left');
|
|
|
|
if($LocName != '') { $sql->like('LocFull', $LocName, 'both'); $rows[] = 'LocName'; }
|
|
if($LocCode != '') { $sql->like('LocCode', $LocCode, 'both'); $rows[] = 'LocCode'; }
|
|
|
|
$rows[] = $sql->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "Location no Data.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "Locations fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function show($LocationID = null) {
|
|
$rows = $this->db->table('location l')
|
|
->select("l.*, la.*, v.*")
|
|
->join("locationaddress la", "l.LocationID=la.LocationID", "left")
|
|
->join("valueset v", "v.VSetID=12 and v.VValue=l.loctype", "left")
|
|
->where('l.LocationID', (int) $LocationID)
|
|
->get()->getResultArray();
|
|
|
|
if (empty($rows)) {
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "Data not found.",
|
|
'data' => [],
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "Locations fetched successfully",
|
|
'data' => $rows,
|
|
], 200);
|
|
}
|
|
|
|
public function create() {
|
|
$input = $this->request->getJSON(true);
|
|
$dataLocation = $this->prepareLocationData($input);
|
|
$dataLocationAddress = $this->prepareLocationAddressData($input);
|
|
if (!$this->validateData($dataLocation, $this->rules)) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
try {
|
|
$this->db->transStart();
|
|
$this->db->table('location')->insert($dataLocation);
|
|
$newLocationID = $this->db->insertID();
|
|
|
|
if (!empty($dataLocationAddress)) {
|
|
$dataLocationAddress['LocationID'] = $newLocationID;
|
|
$this->db->table('locationaddress')->insert($dataLocationAddress);
|
|
}
|
|
$this->db->transComplete();
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
$dbError = $this->db->error();
|
|
return $this->failServerError(
|
|
'Failed to create location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
|
|
);
|
|
}
|
|
|
|
return $this->respondCreated([
|
|
'status' => 'success',
|
|
'message' => 'Location created successfully',
|
|
'data' => $dataLocation,
|
|
], 201);
|
|
|
|
} catch (\Throwable $e) {
|
|
// Ensure rollback if something goes wrong
|
|
if ($this->db->transStatus() !== false) {
|
|
$this->db->transRollback();
|
|
}
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
|
|
// Prepare data
|
|
$dataLocation = $this->prepareLocationData($input);
|
|
$dataLocationAddress = $this->prepareLocationAddressData($input);
|
|
|
|
if (!$this->validateData($dataLocation, $this->rules)) {
|
|
return $this->failValidationErrors( $this->validator->getErrors());
|
|
}
|
|
|
|
// Start transaction
|
|
$this->db->transStart();
|
|
|
|
// Insert location
|
|
$this->db->table('location')->where('LocationID', $dataLocation["LocationID"])->update($dataLocation);
|
|
|
|
// Insert address if available
|
|
if (!empty($dataLocationAddress)) {
|
|
$dataLocationAddress['LocationID'] = $input["LocationID"];
|
|
$this->db->table('locationaddress')->upsert($dataLocationAddress);
|
|
}
|
|
|
|
// Complete transaction
|
|
$this->db->transComplete();
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
$dbError = $this->db->error();
|
|
return $this->failServerError(
|
|
'Failed to update location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
|
|
);
|
|
}
|
|
|
|
return $this->respondCreated([
|
|
'status' => 'success',
|
|
'message' => 'Location updated successfully',
|
|
'data' => $dataLocation,
|
|
], 201);
|
|
|
|
} catch (\Throwable $e) {
|
|
// Ensure rollback if something goes wrong
|
|
if ($this->db->transStatus() !== false) {
|
|
$this->db->transRollback();
|
|
}
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
$LocationID = $input["LocationID"];
|
|
if (!$LocationID) {
|
|
return $this->failValidationErrors('LocationID is required.');
|
|
}
|
|
|
|
|
|
$location = $this->db->table('location')->where('LocationID', $LocationID)->get()->getRow();
|
|
if (!$location) {
|
|
return $this->failNotFound("LocationID with {$LocationID} not found.");
|
|
}
|
|
|
|
$this->db->table('location')->where('LocationID', $LocationID)->update(['DelDate' => NOW()]);
|
|
|
|
return $this->respondDeleted([
|
|
'status' => 'success',
|
|
'message' => "Location with {$LocationID} deleted successfully."
|
|
]);
|
|
|
|
} catch (\Throwable $e) {
|
|
// Ensure rollback if something goes wrong
|
|
if ($this->db->transStatus() !== false) {
|
|
$this->db->transRollback();
|
|
}
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function prepareLocationData(array $input): array {
|
|
$LinkTo = null;
|
|
if (!empty($input['LinkTo'])) {
|
|
$ids = array_column($input['LinkTo'], 'InternalPID');
|
|
$LinkTo = implode(',', $ids);
|
|
}
|
|
|
|
$data = [
|
|
"LocCode" => $input['LocCode'] ?? null,
|
|
"Parent" => $input['Parent'] ?? null,
|
|
"LocFull" => $input['LocFull'] ?? null,
|
|
"LocType" => $input['LocType'] ?? null,
|
|
"Description" => $input['Description'] ?? null,
|
|
];
|
|
|
|
if(!empty($input["LocationID"])) { $data["LocationID"] = $input["LocationID"]; }
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function prepareLocationAddressData(array $input): array {
|
|
$data = [
|
|
"LocationID" => $input['LocationID'] ?? null,
|
|
"Street1" => $input['Street1'] ?? null,
|
|
"Street2" => $input['Street2'] ?? null,
|
|
"City" => $input['City'] ?? null,
|
|
"Province" => $input['Province'] ?? null,
|
|
"PostCode" => $input['PostCode'] ?? null,
|
|
"GeoLocationSystem" => $input['GeoLocationSystem'] ?? null,
|
|
"GeoLocationData" => $input['GeoLocationData'] ?? null,
|
|
"Email" => $input['Email'] ?? null,
|
|
"Phone" => $input['Phone'] ?? null,
|
|
"Mobile" => $input['Mobile'] ?? null,
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
} |