Files
API-KTA/app/Controllers/PersonelController.php
2026-04-25 04:51:56 +07:00

143 lines
3.6 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\PersonelModel;
use Dhiva\Core\DhivaAES;
class PersonelController extends BaseController
{
protected $personelModel;
public function __construct()
{
$this->personelModel = new PersonelModel();
}
/**
* Get basic profile info by NRP (supports encrypted NRP)
*/
public function getProfil()
{
$inputNrp = $this->request->getGet('nrp');
if (!$inputNrp) {
return $this->response(UNAUTHORIZED, 1);
}
// Try to decrypt if it's not a numeric NRP
$nrp = $inputNrp;
if (!is_numeric($inputNrp)) {
try {
$nrp = DhivaAES::base64url_decode($inputNrp);
if (!$nrp) {
$nrp = $inputNrp; // Fallback to original if decryption fails
}
} catch (\Exception $e) {
$nrp = $inputNrp;
}
}
$personel = $this->personelModel->where('nrp', $nrp)->first();
if (!$personel) {
$response = [
'status' => 'error',
'message' => 'Personel tidak ditemukan',
'data' => null
];
return $this->respond($response, 404);
}
$response = [
'status' => 'success',
'message' => 'Data personel ditemukan',
'data' => $personel
];
$this->respond($response);
}
/**
* Get full DRH by NRP and OTP (supports encrypted NRP)
*/
public function getDaftarRiwayatHidup()
{
$inputNrp = $this->request->getGet('nrp');
$otp = $this->request->getGet('otp');
// Try to decrypt if it's not a numeric NRP
$nrp = $inputNrp;
if (!is_numeric($inputNrp)) {
$nrp = DhivaAES::base64url_decode($inputNrp) ?: $inputNrp;
}
if ($otp !== '4444') {
$this->respond([
'status' => 'error',
'message' => 'OTP tidak valid',
'data' => null
], 401);
}
if (!$nrp) {
$this->respond([
'status' => 'error',
'message' => 'NRP wajib diisi',
'data' => null
], 400);
}
$drh = $this->personelModel->getFullDRH($nrp);
if (!$drh) {
$this->respond([
'status' => 'error',
'message' => 'Data riwayat hidup tidak ditemukan',
'data' => null
], 404);
}
$response = [
'status' => 'success',
'message' => 'Data riwayat hidup berhasil diambil',
'data' => $drh
];
$this->respond($response);
}
/**
* Generate Encrypted URL for Barcode (Admin only)
*/
public function generateEncryptedLink()
{
$nrp = $this->request->getPost('nrp');
if (!$nrp) {
$this->respond(['status' => 'error', 'message' => 'NRP wajib diisi'], 400);
}
$encryptedNrp = DhivaAES::base64url_encode($nrp);
$response = [
'status' => 'success',
'data' => [
'nrp' => $nrp,
'encrypted_nrp' => $encryptedNrp,
'url' => "http://localhost:4200/kta/profil/" . $encryptedNrp
]
];
$this->respond($response);
}
private function respond($data, $code = 200)
{
header('Content-Type: application/json');
http_response_code($code);
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
die;
}
}