Added the customer codes, and abstract variable storage for user objects.

This commit is contained in:
Jepp9350
2024-12-16 14:43:21 +01:00
parent 03515a83d6
commit fae72cce75
5 changed files with 260 additions and 0 deletions
+6
View File
@@ -21,6 +21,12 @@ class object_property
$this->default = $default;
}
public function __toString(): string
{
// Return the value of the field in the database table
return $this->value();
}
/**
* Get the value of the field in the database table
* @return mixed The value of the field in the database table
+82
View File
@@ -0,0 +1,82 @@
<?php
namespace objects;
use classes\db;
use classes\object_property;
use traits\db_object_t;
class customer_codes_o extends db
{
use db_object_t;
public object_property $user_id;
public object_property $code;
public function structure(): void
{
$this->setTable('customer_codes');
}
public function getObjectProperties(): void
{
$this->user_id = new object_property($this->table, $this->id, 'user_id', 'int', true);
$this->code = new object_property($this->table, $this->id, 'code', 'string', true);
}
public function set(int $user_id, string|null $code): customer_codes_o
{
global $db, $response;
try {
// Avoid SQL injection
if (!is_null($code)) {
$code = $db->escape_string($code);
}
// Create a new record in the database ( Replace the code, if an entry already exists )
$sql = "INSERT INTO $this->table (user_id, code) VALUES ($user_id, '$code') ON DUPLICATE KEY UPDATE code = '$code'";
$db->query($sql);
// Get the id of the new record
$this->id = $db->insert_id();
// Set the values of the object properties
$this->getObjectProperties();
} catch (\Exception $e) {
$response->error($e->getMessage());
}
return $this;
}
public function getCode(int $user_id): customer_codes_o
{
global $db;
// Get the record from the database
$sql = "SELECT * FROM $this->table WHERE user_id = $user_id";
$result = $db->query($sql);
if ($result->num_rows > 0) {
$this->id = $user_id;
$this->getObjectProperties();
} else {
$this->set($user_id, null);
}
return $this;
}
public function setCode(int $id, mixed $code): customer_codes_o
{
global $db, $response;
try {
// Avoid SQL injection
$code = $db->escape_string($code);
// Update the record in the database
$sql = "UPDATE $this->table SET code = '$code' WHERE user_id = $id";
$db->query($sql);
// Set the values of the object properties
$this->id = $id;
$this->getObjectProperties();
} catch (\Exception $e) {
$response->error($e->getMessage());
}
return $this;
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace objects;
use classes\db;
use classes\object_property;
use traits\db_object_t;
class user_key_value_pairs_o extends db
{
use db_object_t;
public int $user_id;
public object_property $var;
public object_property $val;
public function structure(): void
{
$this->setTable('user_key_value_pairs');
}
public function getObjectProperties(): void
{
$this->var = new object_property($this->table, $this->id, 'var', 'string', true);
$this->val = new object_property($this->table, $this->id, 'val', 'mixed', true);
}
public function setUser($user_id): user_key_value_pairs_o
{
$this->user_id = $user_id;
return $this;
}
public function getValue($var): mixed
{
global $db;
// Avoid SQL injection
$var = $db->escape_string($var);
// Get the record from the database
$sql = "SELECT val FROM $this->table WHERE user_id = $this->user_id AND var = '$var'";
$result = $db->query($sql);
if ($result->num_rows > 0) {
return $db->fetch_assoc($result)['val'];
}
return null;
}
public function setValue($var, $val): user_key_value_pairs_o
{
global $db;
// Avoid SQL injection
$var = $db->escape_string($var);
$val = $db->escape_string($val);
// Check if the value exists in the database
$exists = $this->getValue($var);
// Create a new record in the database if it doesn't exist
if ($exists) {
$sql = "UPDATE $this->table SET val = '$val' WHERE user_id = $this->user_id AND var = '$var'";
} else {
$sql = "INSERT INTO $this->table (user_id, var, val) VALUES ($this->user_id, '$var', '$val')";
}
$db->query($sql);
return $this;
}
public function deleteValue($var): user_key_value_pairs_o
{
global $db;
// Avoid SQL injection
$var = $db->escape_string($var);
// Create a new record in the database
$sql = "DELETE FROM $this->table WHERE user_id = $this->user_id AND var = '$var'";
$db->query($sql);
return $this;
}
}
+19
View File
@@ -20,6 +20,8 @@ class users_o extends db
public object_property $created_at;
public object_property $updated_at;
public array $permissions;
public customer_codes_o $customer_codes;
public user_key_value_pairs_o $keys;
public function structure(): void
{
@@ -34,6 +36,7 @@ class users_o extends db
$this->group_id = new object_property($this->table, $this->id, 'group_id', 'int', true);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'string', false);
$this->keys = (new user_key_value_pairs_o())->setUser($this->id);
}
public function getUserByCustomerNumber(int $customer_number): users_o
@@ -364,4 +367,20 @@ class users_o extends db
}
$this->permissions = $perms;
}
public function getCode(): string|null
{
// Get the customer code
$this->customer_codes = new customer_codes_o();
$this->customer_codes->getCode($this->id);
return $this->customer_codes->code->value();
}
public function setCode(mixed $code): customer_codes_o
{
// Set the customer code
$this->customer_codes = new customer_codes_o();
return $this->customer_codes->setCode($this->id, $code);
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace routes;
use classes\authentication;
use objects\customer_notes_o;
use objects\logs_o;
use objects\users_o;
use traits\route_t;
class customerCodeDepartmentRoute
{
use route_t;
public function run(): void
{
$this->get('/admin/customer/code', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('get_customer_code');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Get the query parameters from the URL
$data = $_GET;
// Check if the required fields are set
if (!isset($data['customer_number']) && !isset($data['user_id'])) { $response->error('User ID or Customer Number is required', 400); }
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, $user->id, 'GET_CUSTOMER_CODE', 'Successfully retrieved customer code');
// Check if the user exists
if (!(new users_o())->automaticGetTargetUserFromRequest()->exists()) {
$response->error('Customer not found', 400);
}
// Return the customer code
$response->success(
['code' => (new users_o())->automaticGetTargetUserFromRequest()->getCode()]
);
} else {
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, 0, 'GET_CUSTOMER_CODE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
$this->post('/admin/customer/code', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('add_customer_code');
// 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['customer_number']) && !isset($data['user_id']) && !isset($data['code'])) { $response->error('User ID, Customer Number, and Code are required', 400); }
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, $user->id, 'ADD_CUSTOMER_CODE', 'Successfully added customer code');
// Check if the user exists
if (!(new users_o())->automaticGetTargetUserFromRequest()->exists()) {
$response->error('Customer not found', 400);
}
// Add the customer code
(new users_o())->automaticGetTargetUserFromRequest()->setCode($data['code']);
// Return a success message
$response->success(['message' => 'Customer code added']);
} else {
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, 0, 'ADD_CUSTOMER_CODE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}