Introduced a new branding functionality, including a `branding_o` object, `create_branding_f` form, and associated routes in `BrandingRoute`. Refactored the forms directory for better organization and added support for branding in departments. Extended validation, object handling, and response handling to accommodate this new feature.
175 lines
7.5 KiB
PHP
175 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\branding_o;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
class BrandingRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/branding', function () {
|
|
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_branding_options');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, $user->id, 'LIST_BRANDING_OPTIONS', 'User listed branding options');
|
|
// Check the user has specified the branding id
|
|
if (self::isParametersSet(['id'])) {
|
|
// Check if the parameters are of the correct type
|
|
self::requireType(self::getParameter('id'), self::TYPE_INT());
|
|
// Select the object
|
|
$branding = (new branding_o())->select(self::getParameter('id'));
|
|
// Check if the object exists
|
|
if ($branding->exists()) {
|
|
// Return the object as an array
|
|
$response->success(
|
|
(new branding_o())->select(self::getParameter('id'))->__toString()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, $user->id, 'LIST_BRANDING_OPTIONS', 'User tried to list branding options with an invalid id');
|
|
// Return an error
|
|
$response->error('Invalid id', 400);
|
|
}
|
|
}
|
|
// Return the list of departments
|
|
$response->success(
|
|
(new branding_o())
|
|
->listObjectsWithPaginationIfSet(
|
|
function ($branding) use ($user) {
|
|
// Return the object as an array
|
|
$tmp_branding = (new branding_o())->select($branding['id']);
|
|
return $tmp_branding->asArray();
|
|
}
|
|
)
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, 0, 'LIST_BRANDING_OPTIONS', 'User tried to list branding options without being logged in');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'list_branding_options' => 'List all product options'
|
|
]
|
|
);
|
|
|
|
$this->post('/branding', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('add_branding_option');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the required parameters are set
|
|
self::requireParameters(['name', 'description', 'cvr']);
|
|
// Check if the parameters are of the correct type
|
|
self::requireType(self::getParameter('name'), self::TYPE_STRING());
|
|
self::requireType(self::getParameter('description'), self::TYPE_STRING());
|
|
self::requireType(self::getParameter('cvr'), self::TYPE_INT());
|
|
// Create the object
|
|
$branding = new branding_o();
|
|
// Add the object
|
|
$branding->add(
|
|
[
|
|
'name' => self::getParameter('name'),
|
|
'description' => self::getParameter('description'),
|
|
'cvr' => self::getParameter('cvr')
|
|
]
|
|
);
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, $user->id, 'ADD_BRANDING_OPTION', 'User added a branding option');
|
|
// Return the object
|
|
$response->success(
|
|
$branding->__toString()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, 0, 'ADD_BRANDING_OPTION', 'User tried to add a branding option without being logged in');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'add_branding_option' => 'Add a branding option'
|
|
]
|
|
);
|
|
|
|
$this->put('/branding', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('edit_branding_option');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the required parameters are set
|
|
self::requireParameters(['id']);
|
|
// Check if the parameters are of the correct type
|
|
self::requireType(self::getParameter('id'), self::TYPE_INT());
|
|
// Create the object
|
|
$branding = new branding_o();
|
|
// Select the object
|
|
$branding->select(self::getParameter('id'));
|
|
// Check what the user wants to edit
|
|
|
|
// Option name
|
|
if (self::isParametersSet(['name'])) {
|
|
// Check if the parameters are of the correct type
|
|
self::requireType(self::getParameter('name'), self::TYPE_STRING());
|
|
// Set the name
|
|
$branding->name->set(
|
|
(string)self::getParameter('name')
|
|
);
|
|
}
|
|
// Option description
|
|
if (self::isParametersSet(['description'])) {
|
|
// Check if the parameters are of the correct type
|
|
self::requireType(self::getParameter('description'), self::TYPE_STRING());
|
|
// Set the description
|
|
$branding->description->set(
|
|
(string)self::getParameter('description')
|
|
);
|
|
}
|
|
// Option cvr
|
|
if (self::isParametersSet(['cvr'])) {
|
|
// Check if the parameters are of the correct type
|
|
self::requireType(self::getParameter('cvr'), self::TYPE_INT());
|
|
// Set the cvr value
|
|
$branding->cvr->set(
|
|
(int)self::getParameter('cvr')
|
|
);
|
|
}
|
|
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, $user->id, 'EDIT_BRANDING_OPTION', 'User edited a branding option');
|
|
// Return the object
|
|
$response->success(
|
|
$branding->__toString()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('branding', 'global', 1, 0, 'EDIT_BRANDING_OPTION', 'User tried to edit a branding option without being logged in');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'edit_branding_option' => 'Edit a branding option'
|
|
]
|
|
);
|
|
}
|
|
} |