107 lines
4.5 KiB
PHP
107 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\departments_o;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
class departmentsRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/departments', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_departments');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENTS', 'Successfully listed departments');
|
|
// Return the list of departments
|
|
$response->success(
|
|
(new departments_o())->list()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENTS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->post('/departments', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('add_department');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['name'])) {
|
|
$response->error('Name is required', 400);
|
|
}
|
|
if (!isset($data['description'])) {
|
|
$response->error('Description is required', 400);
|
|
}
|
|
|
|
// Add the department
|
|
(new departments_o())->create($data['name'], $data['description']);
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'ADD_DEPARTMENT', 'Successfully added a department ' . $data['name']);
|
|
// Return a success message
|
|
$response->success(['message' => 'Department added successfully']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'ADD_DEPARTMENT', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->put('/departments', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('edit_department');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['id'])) {
|
|
$response->error('ID is required', 400);
|
|
}
|
|
if (!isset($data['name'])) {
|
|
$response->error('Name is required', 400);
|
|
}
|
|
if (!isset($data['description'])) {
|
|
$response->error('Description is required', 400);
|
|
}
|
|
if (!isset($data['economic_department_id'])) {
|
|
$response->error('Economic department ID is required', 400);
|
|
}
|
|
// Update the department
|
|
(new departments_o())->edit($data['id'], $data['name'], $data['description'], (int)$data['economic_department_id']);
|
|
// Log the incident
|
|
(new logs_o())->add('departments', $data['id'], 1, $user->id, 'EDIT_DEPARTMENT', 'Successfully updated a department ' . $data['name']);
|
|
// Return a success message
|
|
$response->success(['message' => 'Department updated successfully']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'EDIT_DEPARTMENT', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
}
|
|
} |