File "create.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/staff_roles/create.php
File size: 1.56 KiB (1593 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
require_once '../helper/audit_log.php';
header('Content-Type: application/json');
try {
$role_name = trim($_POST['role_name'] ?? '');
$status = trim($_POST['status'] ?? 'Active');
$errors = [];
if ($role_name === '' || mb_strlen($role_name) < 2) { $errors[] = 'Role Name must be at least 2 characters.'; }
if (!in_array($status, ['Active','Inactive'])) { $errors[] = 'Invalid status.'; }
if ($errors) { echo json_encode(["status"=>"error","message"=>$errors]); exit; }
// Ensure unique role name
$chk = $pdo->prepare("SELECT COUNT(*) FROM staff_role_types WHERE LOWER(role_name)=LOWER(?)");
$chk->execute([$role_name]);
if ($chk->fetchColumn() > 0) { echo json_encode(["status"=>"error","message"=>"Role name already exists."]); exit; }
$stmt = $pdo->prepare("INSERT INTO staff_role_types (role_name, status, created_at, updated_at) VALUES (?, ?, NOW(), NOW())");
$stmt->execute([$role_name, $status]);
$newId = $pdo->lastInsertId();
write_audit_log($pdo, [
'module' => 'People',
'action' => 'create',
'entity_table' => 'staff_role_types',
'entity_id' => $newId,
'after_data' => ['role_name'=>$role_name,'status'=>$status],
'message' => 'Staff role created'
]);
echo json_encode(["status"=>"success","message"=>"Role created successfully.","id"=>$newId]);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode(["status"=>"error","message"=> $e->getMessage()]);
}
?>