Added `delete`, `restore`, and other utility methods to enhance CRUD operations, including support for soft deletes. Introduced `objectChanged` hooks across objects for better cache or event handling, ensuring scalability and maintainability. Refactored and standardized object property handling while restructuring related methods.
203 lines
7.1 KiB
PHP
203 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use traits\db_object_t;
|
|
|
|
class departments_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $name;
|
|
public object_property $description;
|
|
public object_property $economic_department_id; // The id of the department in the economic system (Can be null)
|
|
public object_property $slack_webhook; // The slack webhook for the department
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('departments');
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
// Clear the cache
|
|
redis->clear_departments();
|
|
}
|
|
|
|
public function create(string $name, string $description): void
|
|
{
|
|
global $db;
|
|
// Avoid SQL injection
|
|
$name = $db->escape_string($name);
|
|
$description = $db->escape_string($description);
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO $this->table (name, description) VALUES ('$name', '$description')";
|
|
$db->query($sql);
|
|
|
|
// Get the id of the new record
|
|
$this->id = $db->insert_id();
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
|
|
// Clear the cache
|
|
redis->clear_departments();
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->name = new object_property($this->table, $this->id, 'name', 'string', true);
|
|
$this->description = new object_property($this->table, $this->id, 'description', 'string', false);
|
|
$this->economic_department_id = new object_property($this->table, $this->id, 'economic_department_id', 'int', false);
|
|
$this->slack_webhook = new object_property($this->table, $this->id, 'slack_webhook', 'string', false);
|
|
}
|
|
|
|
public function edit(int $id, string $name, string $description, int $economic_department_id): void
|
|
{
|
|
global $db;
|
|
$this->id = $id;
|
|
// Avoid SQL injection
|
|
$name = $db->escape_string($name);
|
|
$description = $db->escape_string($description);
|
|
// If the economic_department_id is 0, set it to null
|
|
if ($economic_department_id === 0) {
|
|
$economic_department_id = null;
|
|
}
|
|
// Update the record in the database
|
|
$sql = "UPDATE $this->table SET name = '$name', description = '$description', economic_department_id = " . ($economic_department_id ?? 'NULL') . " WHERE id = $this->id";
|
|
$db->query($sql);
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
|
|
// Clear the cache
|
|
redis->clear_department($id);
|
|
}
|
|
|
|
public function list($superUser = false): array
|
|
{
|
|
global $db;
|
|
// Check if the departments are cached
|
|
$departments = redis->get_departments();
|
|
// If the departments are not cached, get them from the database
|
|
if ($departments === null) {
|
|
$sql = "SELECT * FROM $this->table";
|
|
$result = $db->query($sql);
|
|
$departments = $db->fetch_all($result);
|
|
// Cache the departments (If it is not empty or null)
|
|
if (!empty($departments)) {
|
|
redis->cache_departments($departments);
|
|
}
|
|
}
|
|
// Only show the name, description and id if the user isn't a super user
|
|
if (!$superUser) {
|
|
$departments = array_map(function ($department) {
|
|
return [
|
|
'name' => $department['name'],
|
|
'description' => $department['description'],
|
|
'id' => $department['id']
|
|
];
|
|
}, $departments);
|
|
}
|
|
return $departments;
|
|
}
|
|
|
|
public function getDepartmentById(int $id, bool $force_all = false): array
|
|
{
|
|
global $db;
|
|
// Check if the department is cached
|
|
$department = redis->get_department($id);
|
|
// If the department is not cached, get it from the database
|
|
if ($department === null || $force_all) {
|
|
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
|
$result = $db->query($sql);
|
|
$department = $db->fetch_assoc($result);
|
|
// Cache the department (If it is not empty or null)
|
|
if (!empty($department)) {
|
|
redis->cache_department($id, $department);
|
|
}
|
|
}
|
|
return $department;
|
|
}
|
|
|
|
/**
|
|
* Get the price of a product in a department
|
|
* @param int $department_id
|
|
* @param int $product_id
|
|
* @param int $price
|
|
* @return void
|
|
*/
|
|
public function setDepartmentProductPrice(int $department_id, int $product_id, int $price): void
|
|
{
|
|
global $db;
|
|
// Check if the record already exists
|
|
$this->removeDepartmentProductPriceIfExist($department_id, $product_id);
|
|
// If the price is 0, return
|
|
if ($price === 0) {
|
|
return;
|
|
}
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO product_department_prices (department_id, product_id, price) VALUES ($department_id, $product_id, $price)";
|
|
$db->query($sql);
|
|
}
|
|
|
|
/**
|
|
* Remove the price of a product in a department
|
|
* @param int $department_id
|
|
* @param int $product_id
|
|
* @return void
|
|
*/
|
|
private function removeDepartmentProductPriceIfExist(int $department_id, int $product_id): void
|
|
{
|
|
global $db;
|
|
// Get the price from the database
|
|
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id AND product_id = $product_id";
|
|
$result = $db->query($sql);
|
|
|
|
if ($result->num_rows > 0) {
|
|
// Remove the record from the database
|
|
$sql = "DELETE FROM product_department_prices WHERE department_id = $department_id AND product_id = $product_id";
|
|
$db->query($sql);
|
|
}
|
|
}
|
|
|
|
public function getDepartmentProductPrices(int $department_id): array
|
|
{
|
|
global $db;
|
|
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id";
|
|
$result = $db->query($sql);
|
|
return $db->fetch_all($result);
|
|
}
|
|
|
|
public function selectId(int $department_id): self
|
|
{
|
|
$this->id = $department_id;
|
|
$this->getObjectProperties();
|
|
return $this;
|
|
}
|
|
|
|
public function getDepartmentName(int $department): string
|
|
{
|
|
global $db;
|
|
// Check if the department name is cached
|
|
$name = redis->get_department_name($department);
|
|
// If the department name is not cached, get it from the database
|
|
if ($name === null) {
|
|
try {
|
|
$this->id = $department;
|
|
$this->getObjectProperties();
|
|
$name = $this->name->value();
|
|
// Cache the department name (If it is not empty or null)
|
|
if (!empty($name)) {
|
|
redis->cache_department_name($department, $name);
|
|
}
|
|
} catch (\Exception $e) {
|
|
$name = 'Unable to get department name: ' . $department;
|
|
}
|
|
return $name;
|
|
}
|
|
return redis->get_department_name($department) ?? 'Unable to get department name: ' . $department;
|
|
}
|
|
} |