0% found this document useful (0 votes)
3 views15 pages

API for Employee Data Retrieval

Uploaded by

halimaelganduz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

API for Employee Data Retrieval

Uploaded by

halimaelganduz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

: ‫نقطة االتصال الخاصة بجلب بيانات كافة الموظفين‬

$router->map('GET', '/api/employee', function() {


// Set UTF-8 encoding in the header
header('Content-Type: application/json; charset=utf-8');

if (!empty($_SESSION['jwtToken'])) {
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token = $_SESSION['jwtToken'];
$decoded = JWT::decode($token, new Key($secretKey,
'HS256'));
$decoded_array = (array) $decoded;

if ($_SESSION['user'] == $decoded_array['username']) {
ob_start();

// Pagination logic
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
// Current page, default is 1
$perPage = isset($_GET['perPage']) ?
(int)$_GET['perPage'] : 5; // Records per page, default is 5
$offset = ($page - 1) * $perPage; // Calculate offset

$db = app_db();

try {
// If ID is provided, fetch a single employee
if (isset($_GET["id"])) {
$EmpNo = $db->CleanDBData($_GET['id']);
$q0 = $db->select("SELECT * FROM employees WHERE
empNo = '$EmpNo'");

if ($q0) {
echo json_encode(array(
'status' => '200',
'msg' => 'found record',
'data' => $q0,
), JSON_UNESCAPED_UNICODE); // Use
JSON_UNESCAPED_UNICODE to support Arabic
} else {
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
), JSON_UNESCAPED_UNICODE);
}
} else {
// Fetch total number of records for pagination
calculation
$totalRecords = $db->select("SELECT COUNT(*) AS
count FROM employees")[0]['count'];
$totalPages = ceil($totalRecords / $perPage); //
Calculate total pages

// Fetch paginated results


$q0 = $db->select("SELECT * FROM employees LIMIT
$offset, $perPage");

if ($q0) {
echo json_encode(array(
'status' => '200',
'msg' => 'found records',
'data' => $q0,
'pagination' => array(
'currentPage' => $page,
'perPage' => $perPage,
'totalRecords' => $totalRecords,
'totalPages' => $totalPages,
'prevPage' => $page > 1 ? $page - 1
: null,
'nextPage' => $page < $totalPages ?
$page + 1 : null
)
), JSON_UNESCAPED_UNICODE);
} else {
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
), JSON_UNESCAPED_UNICODE);
}
}
} catch (PDOException $e) {
echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => $e->getMessage(),
), JSON_UNESCAPED_UNICODE);
}

ob_end_flush();
} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not match the
decoded payload',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}
});

[Link]
‫نقطة االتصال لجلب بيانات موظف واحد‬ -
- [Link]
‫نقطة االتصال لجلب بيانات دخول الشاحنات‬ -
$router->map('GET', '/api/truckEnter', function() {
// Set UTF-8 encoding in the response header
header('Content-Type: application/json; charset=utf-8');

if (!empty($_SESSION['jwtToken'])) {
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token = $_SESSION['jwtToken'];
$decoded = JWT::decode($token, new Key($secretKey,
'HS256'));
$decoded_array = (array) $decoded;

if ($_SESSION['user'] == $decoded_array['username']) {
if (isset($_GET['workCode'])) {
ob_start();

$db = app_db();
$workCode = $db->CleanDBData($_GET['workCode']);
$q0 = $db->select("SELECT * FROM `truck_enter` WHERE
workcode = '".$workCode."'");

try {
if ($q0) {
echo json_encode(array(
'status' => '200',
'msg' => 'found records',
'data' => $q0,
), JSON_UNESCAPED_UNICODE); // Support
Arabic characters
} else {
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
), JSON_UNESCAPED_UNICODE);
}
} catch (PDOException $e) {
echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}

ob_end_flush();
}
} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not match the
decoded payload',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}
});

[Link]
‫ نقطة اتصال خروج الحاويات‬-
$router->map('GET', '/api/container', function()
{
if (!empty($_SESSION['jwtToken']))
{
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token=$_SESSION['jwtToken'];
$decoded = JWT::decode($token, new Key($secretKey,
'HS256'));
$decoded_array = (array) $decoded;

if ($_SESSION['user'] ==$decoded_array['username']) {

if(isset($_GET['id']))
{
ob_start();

$db = app_db();
$container_number = $db->CleanDBData($_GET['id']);
$q0 = $db->select("SELECT * FROM `container_exit` WHERE
container_number='".$container_number."'");
try{
if($q0 > 0)
{
$record = $q0;
echo json_encode(array(
'status' => '200',
'msg' => 'found records',
'data' => $q0,
));
}
else
{
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
));
}
}
catch(PDOException $e)
{

echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}

} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not match
the decoded payload',
'data' => array(),
));

ob_end_flush();
}});

[Link]
‫ نقطة اتصال خروج البضائع العامة‬-
$router->map('GET', '/api/othergoods', function()
{
if (!empty($_SESSION['jwtToken']))
{
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token=$_SESSION['jwtToken'];
$decoded = JWT::decode($token, new
Key($secretKey, 'HS256'));
$decoded_array = (array) $decoded;

if ($_SESSION['user']
==$decoded_array['username']) {

if(isset($_GET['workCode']))
{
ob_start();

$db = app_db();
$workCode = $db->CleanDBData($_GET['workCode']);
$q0 = $db->select("SELECT * FROM `othergoods` WHERE
workcode='".$workCode."'");

try{
if($q0 > 0)
{
$record = $q0;
echo json_encode(array(
'status' => '200',
'msg' => 'found records',
'data' => $q0,
));
}
else
{
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
));
}
}
catch(PDOException $e)
{

echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}

} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not
match the decoded payload',
'data' => array(),
));
}

ob_end_flush();
}});

[Link]
‫ نقطة اتصال خروج االليات والمعدات‬-
$router->map('GET', '/api/equipment', function()
{
if (!empty($_SESSION['jwtToken']))
{
$secretKey =
'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token=$_SESSION['jwtToken'];
$decoded = JWT::decode($token, new
Key($secretKey, 'HS256'));
$decoded_array = (array) $decoded;
if ($_SESSION['user']
==$decoded_array['username']) {

if(isset($_GET['tag']))
{
ob_start();

$db = app_db();
$tagNumber = $db->CleanDBData($_GET['tag']);
$q0 = $db->select("SELECT * FROM `equipment`
WHERE tagNumber='". $tagNumber."'");

try{
if($q0 > 0)
{
$record = $q0;
echo json_encode(array(
'status' => '200',
'msg' => 'found records',
'data' => $q0,
));
}
else
{
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
));
}
}
catch(PDOException $e)
{

echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}

} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does
not match the decoded payload',
'data' => array(),
));
}

ob_end_flush();
}});

[Link]

‫نقطة اتصال التحقق من الزائر‬ -


$router->map('GET', '/api/visitor', function()
{
if (!empty($_SESSION['jwtToken']))
{
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token=$_SESSION['jwtToken'];
$decoded = JWT::decode($token, new Key($secretKey,
'HS256'));
$decoded_array = (array) $decoded;
if ($_SESSION['user'] ==$decoded_array['username']) {

if(isset($_GET['code']))
{
ob_start();

$db = app_db();
$visit_code = $db->CleanDBData($_GET['code']);
$q0 = $db->select("SELECT
`visit_code`,`status`,`vist_start_date`,`vist_end_date` FROM vistor
WHERE visit_code='". $visit_code."'");

try {
if ($q0) {
echo json_encode(array(
'status' => '200',
'msg' => 'found records',
'data' => $q0,
), JSON_UNESCAPED_UNICODE); // Support Arabic characters
} else {
echo json_encode(array(
'status' => '204',
'msg' => 'no records found',
'data' => null,
), JSON_UNESCAPED_UNICODE);
}
} catch (PDOException $e) {
echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}

} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not match the
decoded payload',
'data' => array(),
));
}

ob_end_flush();
}});

[Link]

‫تعديل لوحة السيارة للموظف‬ -


$router->map('GET', '/api/edit', function() {
if (!empty($_SESSION['jwtToken'])) {
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token = $_SESSION['jwtToken'];
$decoded = JWT::decode($token, new Key($secretKey,
'HS256'));
$decoded_array = (array) $decoded;

if ($_SESSION['user'] == $decoded_array['username']) {
if (isset($_GET['id']) && isset($_GET['plate'])) {
ob_start();

$db = app_db();
$plate = $db->CleanDBData($_GET['plate']);
$ID = $db->CleanDBData($_GET['id']);

// Prepare data for updating


$update_data = array(
'carPlate_number' => $plate
);

// Prepare where condition


$where_condition = array(
'empNo' => $ID
);

// Call the Update function


$update_result = $db->Update('employees',
$update_data, $where_condition);

try {
if ($update_result === 1) {
echo json_encode(array(
'status' => '200',
'msg' => 'Data updated
successfully',
'data' => $update_result,
), JSON_UNESCAPED_UNICODE); // Support
Arabic characters
} else {
echo json_encode(array(
'status' => '501',
'msg' => 'Update not implemented
or failed',
'data' => null,
), JSON_UNESCAPED_UNICODE);
}
} catch (PDOException $e) {
echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}
} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not match
the decoded payload',
'data' => array(),
));
}
} else {
echo json_encode(array(
'status' => '401',
'msg' => 'Authorization token is missing.',
'data' => array(),
));
}

ob_end_flush();
});

[Link]
‫اضافة لوحة جديدة للموظف‬ -
$router->map('GET', '/api/add', function() {
if (!empty($_SESSION['jwtToken'])) {
$secretKey = 'a5D8F1r3Z6M9c2Q7L0B4T7xP8yW4v6G';
$token = $_SESSION['jwtToken'];
$decoded = JWT::decode($token, new Key($secretKey,
'HS256'));
$decoded_array = (array) $decoded;
if ($_SESSION['user'] == $decoded_array['username']) {
if (isset($_GET['id']) && isset($_GET['plate'])) {
ob_start();

$db = app_db();
$plate = $db->CleanDBData($_GET['plate']);
$ID = $db->CleanDBData($_GET['id']);

// Prepare data for insertion


$insert_data = array(
'empNo' => $ID,
'carPlate_number' => $plate,
'valid_date' => date("Y-m-d")
);

// Call the Insert function


$insert_id = $db->Insert('other_plates',
$insert_data);

try {
if ($insert_id) {
echo json_encode(array(
'status' => '200',
'msg' => 'Data inserted successfully',
'data' => $insert_id, // Return the
inserted ID
), JSON_UNESCAPED_UNICODE); // Support
Arabic characters
} else {
echo json_encode(array(
'status' => '501',
'msg' => 'Insert not implemented or
failed',
'data' => null,
), JSON_UNESCAPED_UNICODE);
}
} catch (PDOException $e) {
echo json_encode(array(
'status' => '503',
'msg' => 'Database error occurred',
'data' => array(),
), JSON_UNESCAPED_UNICODE);
}
}
} else {
echo json_encode(array(
'status' => '401',
'msg' => 'The original payload does not match the
decoded payload',
'data' => array(),
));
}
} else {
echo json_encode(array(
'status' => '401',
'msg' => 'Authorization token is missing.',
'data' => array(),
));
}

ob_end_flush();
});

[Link]

qr ‫واجهة تعبئة بيانات الزيارة واستخراج رمز ال‬ -

[Link]

[Link]

You might also like