Files
api/services/nginx/app/objects/branding_o.php
T
Jepp9350 4deef9dc09 Add branding feature with form, route, and object implementations
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.
2025-03-26 11:04:12 +01:00

136 lines
4.7 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use Exception;
use traits\db_object_t;
class branding_o extends db
{
use db_object_t;
public object_property $name;
public object_property $description;
public object_property $cvr;
public object_property $address;
public object_property $phone_country_code;
public object_property $phone;
public object_property $email;
public object_property $website;
public object_property $banner;
public object_property $logo;
public object_property $favicon;
public object_property $signature;
public function structure(): void
{
$this->setTable('branding');
}
/**
* Add a branding object
* @param array $data The additional data of the branding (e.g. ["key" => "value"])
* @return void
* @throws Exception If the object was not created successfully
*/
public function add(
array $data,
): void
{
global /** @var db $db */
$db;
// Define the default values
$default_values = [
'name' => null,
'description' => null,
'cvr' => null,
'address' => null,
'phone_country_code' => null,
'phone' => null,
'email' => null,
'website' => null,
'banner' => null,
'logo' => null,
'favicon' => null,
'signature' => null,
];
// Sanitize the input
$sanitized_data = [];
foreach ( $data as $key => $value ) {
// Sanitize the input
if (array_key_exists($key, $default_values)) {
// If the value is null, skip it
if (is_null($value)) {
continue;
} elseif (is_string($value)) {
$sanitized_data[$key] = $db->escape_string($value);
} elseif (is_int($value)) {
$sanitized_data[$key] = (int)$value;
} elseif (is_array($value)) {
$sanitized_data[$key] = json_encode($value);
} else {
throw new Exception("Invalid value type for key: $key");
}
} else {
throw new Exception("Invalid key: $key");
}
}
// Define the actual object values
$new_object = array_merge(
$default_values,
$sanitized_data
);
// Remove null values
$new_object = array_filter($new_object, function ($value) {
return !is_null($value);
});
// Add the object to the database
$new_id = self::add_object($new_object);
self::select($new_id);
}
public function getObjectProperties(): void
{
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
$this->description = new object_property($this->table, $this->id, 'description', 'string', false);
$this->cvr = new object_property($this->table, $this->id, 'cvr', 'int', false);
$this->address = new object_property($this->table, $this->id, 'address', 'string', false);
$this->phone_country_code = new object_property($this->table, $this->id, 'phone_country_code', 'int', false);
$this->phone = new object_property($this->table, $this->id, 'phone', 'int', false);
$this->email = new object_property($this->table, $this->id, 'email', 'string', false);
$this->website = new object_property($this->table, $this->id, 'website', 'string', false);
$this->banner = new object_property($this->table, $this->id, 'banner', 'string', false);
$this->logo = new object_property($this->table, $this->id, 'logo', 'string', false);
$this->favicon = new object_property($this->table, $this->id, 'favicon', 'string', false);
$this->signature = new object_property($this->table, $this->id, 'signature', 'string', false);
}
public function objectChanged(): void
{
//TODO: Add cache invalidation
}
public function asArray(): array
{
return [
'id' => (int)$this->id,
'name' => $this->name->value(),
'description' => $this->description->value(),
'cvr' => $this->cvr->value(),
'address' => $this->address->value(),
'phone_country_code' => $this->phone_country_code->value(),
'phone' => $this->phone->value(),
'email' => $this->email->value(),
'website' => $this->website->value(),
'banner' => $this->banner->value(),
'logo' => $this->logo->value(),
'favicon' => $this->favicon->value(),
'signature' => $this->signature->value(),
];
}
}