From fae72cce75ba3582d1175266ca762d0445962ec6 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 16 Dec 2024 14:43:21 +0100 Subject: [PATCH] Added the customer codes, and abstract variable storage for user objects. --- classes/object_property.php | 6 ++ objects/customer_codes_o.php | 82 ++++++++++++++++++++++++++ objects/user_key_value_pairs_o.php | 76 ++++++++++++++++++++++++ objects/users_o.php | 19 ++++++ routes/customerCodeDepartmentRoute.php | 77 ++++++++++++++++++++++++ 5 files changed, 260 insertions(+) create mode 100644 objects/customer_codes_o.php create mode 100644 objects/user_key_value_pairs_o.php create mode 100644 routes/customerCodeDepartmentRoute.php diff --git a/classes/object_property.php b/classes/object_property.php index 55115f13..7892642a 100644 --- a/classes/object_property.php +++ b/classes/object_property.php @@ -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 diff --git a/objects/customer_codes_o.php b/objects/customer_codes_o.php new file mode 100644 index 00000000..432fe65c --- /dev/null +++ b/objects/customer_codes_o.php @@ -0,0 +1,82 @@ +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; + } + +} \ No newline at end of file diff --git a/objects/user_key_value_pairs_o.php b/objects/user_key_value_pairs_o.php new file mode 100644 index 00000000..9fdddbce --- /dev/null +++ b/objects/user_key_value_pairs_o.php @@ -0,0 +1,76 @@ +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; + } + +} \ No newline at end of file diff --git a/objects/users_o.php b/objects/users_o.php index 516d02a1..fa4bc4a0 100644 --- a/objects/users_o.php +++ b/objects/users_o.php @@ -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); + } + } \ No newline at end of file diff --git a/routes/customerCodeDepartmentRoute.php b/routes/customerCodeDepartmentRoute.php new file mode 100644 index 00000000..b142ce28 --- /dev/null +++ b/routes/customerCodeDepartmentRoute.php @@ -0,0 +1,77 @@ +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); + } + }); + } +} \ No newline at end of file