setTable('users'); } public function getObjectProperties(): void { $this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'string', true); $this->password = new object_property($this->table, $this->id, 'password', 'string', true); $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); } public function getUserByCustomerNumber(int $customer_number): users_o { global $db; // Get the record from the database $sql = "SELECT * FROM $this->table WHERE customer_number = '$customer_number'"; $result = $db->query($sql); if ($result->num_rows > 0) { $this->id = $result->fetch_assoc()['id']; $this->getObjectProperties(); } else { // Import the customer $this->importCustomerFromExternalSource($customer_number); } return $this; } public function getUserById(int $id): users_o { global $db; // Get the record from the database $sql = "SELECT * FROM $this->table WHERE id = $id"; $result = $db->query($sql); if ($result->num_rows > 0) { $this->id = $id; $this->getObjectProperties(); } return $this; } public function add(string $customer_number, mixed $password): void { global $db; // Avoid SQL injection $customer_number = $db->escape_string($customer_number); // Hash the password $password = password_hash($password, PASSWORD_DEFAULT); $password = $db->escape_string($password); // Create a new record in the database $sql = "INSERT INTO $this->table (customer_number, password) VALUES ('$customer_number', '$password')"; $db->query($sql); // Get the id of the new record $this->id = $db->insert_id(); // Set the values of the object properties $this->getObjectProperties(); } public function hasPermission(string $permission): bool { global $db; // Get the user's group id $group_id = $this->group_id->value(); // If the users is an admin, they have all permissions if ((int)$group_id === 1) { return true; } // Get the record from the database $sql = "SELECT * FROM groups_permissions WHERE group_id = $group_id AND permission = '$permission'"; $result = $db->query($sql); if ($result->num_rows > 0) { return true; } return false; } public function edit(int $id, string $customer_number, string|null $role, string|null $password): void { global $db; $this->id = $id; // Avoid SQL injection $customer_number = $db->escape_string($customer_number); if ($password !== null) { // Hash the password $password = password_hash($password, PASSWORD_DEFAULT); $password = $db->escape_string($password); } if ($role !== null) { $role = $db->escape_string($role); } // Update the record in the database $sql = "UPDATE $this->table SET customer_number = '$customer_number'"; if ($password !== null) { $sql .= ", password = '$password'"; } if ($role !== null) { $sql .= ", group_id = '$role'"; } $sql .= " WHERE id = $this->id"; $db->query($sql); // Set the values of the object properties $this->getObjectProperties(); } public function getCustomerByIdOrCustomerNumber(int $idOrCustomerNumber): users_o { global $db; // Get the record from the database $sql = "SELECT * FROM $this->table WHERE id = $idOrCustomerNumber OR customer_number = '$idOrCustomerNumber'"; $result = $db->query($sql); if ($result->num_rows > 0) { $this->id = $result->fetch_assoc()['id']; $this->getObjectProperties(); } else { // Import the customer $this->importCustomerFromExternalSource($idOrCustomerNumber); } return $this; } public function automaticGetTargetUserFromRequest(): users_o { // Get the data from the request if ($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'DELETE') { $data = $_GET; } else { $data = json_decode(file_get_contents('php://input'), true); } // Check if the user id is set in the request if (isset($data['user_id'])) { return $this->getUserById((int)$data['user_id']); } elseif (isset($data['customer_number'])) { return $this->getUserByCustomerNumber($data['customer_number']); } else { return $this; } } public function asArray(): array { $array = [ 'id' => (int)$this->id, 'customer_number' => (int)$this->customer_number->value(), 'group_id' => (int)$this->group_id->value(), 'created_at' => $this->created_at->value(), 'updated_at' => $this->updated_at->value(), ]; // If the economic customer data is set, add it to the array if (isset($this->economic_customer)) { $array['economic_customer'] = $this->economic_customer->asArray(); } // If the permissions are set, add them to the array if (isset($this->permissions)) { $array['permissions'] = $this->permissions; } return $array; } public function getNotes():array { global $db; // Create the customer notes object $customer_notes = new customer_notes_o(); // Get the customer notes return $customer_notes->getCustomerNotesAsArray($this->id); } public function addNote($customer_id, $note, $cashier_id): void { global $db; // Create the customer notes object $customer_notes = new customer_notes_o(); // Add the note $customer_notes->add($customer_id, $note, $cashier_id); } public function deleteNote(int $note_id): void { global $db; // Create the customer notes object $customer_notes = new customer_notes_o(); // Delete the note $customer_notes->delete($note_id); } public function getOrImportCustomerByCustomerNumber(int $customer_number): object|bool { global $db; // Check if the customer exists $sql = "SELECT * FROM $this->table WHERE customer_number = $customer_number"; $result = $db->query($sql); if ($result->num_rows > 0) { $this->id = $result->fetch_assoc()['id']; $this->getObjectProperties(); } else { // Import the customer return $this->importCustomerFromExternalSource($customer_number); } return $this; } private function importCustomerFromExternalSource(int $customer_number): object|bool { global $db; // Get the customer data from the external source $economic = new economicCustomers(); $customer_data = $economic->getCustomerId($customer_number); // DEBUG: Return the customer data // Check if the customer exists if (isset($customer_data[0])) { // Avoid SQL injection $customer_number = $db->escape_string($customer_data[0]->customerNumber); // Create a new record in the database $sql = "INSERT INTO $this->table (customer_number) VALUES ('$customer_number')"; $db->query($sql); // Get the id of the new record $this->id = $db->insert_id(); // Set the values of the object properties $this->getObjectProperties(); } // Else return false return false; } public function getCustomerEcocomicData(int $customer_number = null): users_o { // Get the customer data from the external source $economic = new economicCustomers(); // Check if the customer number is set if (!isset($this->customer_number) && $customer_number === null) { return $this; } $customer_number = $customer_number ?? $this->customer_number->value(); $this->economic_customer = (new economic_customer_mo())->getCustomerByCustomerNumber($customer_number); return $this; } public function getUserAttributes(int $user_id = null): array { global $db; if ($user_id === null) { $user_id = $this->id; } $sql = "SELECT * FROM maintenancemode_dbtest.customer_attributes WHERE user_id = $user_id"; $result = $db->query($sql); return $db->fetch_all($result); } public function doesUserHaveAttribute(string $attribute, int $user_id = null): bool { global $db; if ($user_id === null) { $user_id = $this->id; } $attribute = $db->escape_string($attribute); $sql = "SELECT * FROM maintenancemode_dbtest.customer_attributes WHERE user_id = $user_id AND attribute = '$attribute'"; $result = $db->query($sql); if ($result->num_rows > 0) { return true; } return false; } public function addAttribute(string $attribute, int $user_id = null): void { global $db; if ($user_id === null) { $user_id = $this->id; } $attribute = $db->escape_string($attribute); // Make sure the attribute does not already exist if ($this->doesUserHaveAttribute((string)$attribute, (int)$user_id)) { return; } $sql = "INSERT INTO maintenancemode_dbtest.customer_attributes (user_id, attribute) VALUES ($user_id, '$attribute')"; $db->query($sql); } public function deleteAttribute(string $attribute, int $user_id = null): void { global $db; if ($user_id === null) { $user_id = $this->id; } $attribute = $db->escape_string($attribute); // Make sure the attribute exists if (!$this->doesUserHaveAttribute((string)$attribute, (int)$user_id)) { return; } $sql = "DELETE FROM maintenancemode_dbtest.customer_attributes WHERE user_id = $user_id AND attribute = '$attribute'"; $db->query($sql); } public function requiresReference(): bool { return $this->doesUserHaveAttribute('requiresReferenceNumber'); } public function includeIncludes(array $includes = []): users_o { global /** @var response $response */ $response; $includeEverything = $response->getRequestParameter('include_all') === 'true' || in_array('all', $includes); /** * economicCustomer */ if ($includeEverything || $response->getRequestParameter('includeEconomicCustomer') === 'true' || in_array('economicCustomer', $includes)) { $this->getCustomerEcocomicData(); } /** * permissions */ if ($includeEverything || $response->getRequestParameter('includePermissions') === 'true' || in_array('permissions', $includes)) { $this->getPermissions(); } return $this; } private function getPermissions(): void { global $db; $sql = "SELECT permission FROM groups_permissions WHERE group_id = " . $this->group_id->value(); $result = $db->query($sql); $perms = []; while ($row = $result->fetch_assoc()) { $perms[] = $row['permission']; } $this->permissions = $perms; } }