Remove language pack logic and introduce Redis caching for customer-user mapping.
- Removed all language pack-related files, traits, and classes. - Enhanced `users_o.php` with Redis caching for mapping `customer_number` to `user_id` and vice versa. - Added Redis checks and caching for improved performance in user retrieval methods.
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace classes;
|
||||
|
||||
use interfaces\language_pack_i;
|
||||
|
||||
class language_packs
|
||||
{
|
||||
/**
|
||||
* The language packs
|
||||
* @var array
|
||||
*/
|
||||
protected array $language_packs = [];
|
||||
protected string $default_language = 'en_us';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loadLanguagePacks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the language packs
|
||||
*/
|
||||
protected function loadLanguagePacks(): void
|
||||
{
|
||||
$files = glob('languages/*.php');
|
||||
foreach ( $files as $file ) {
|
||||
require_once $file;
|
||||
$class = 'languages\\' . str_replace('.php', '', basename($file));
|
||||
$this->language_packs[] = new $class();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default language pack
|
||||
* @return language_pack_i The default language pack
|
||||
*/
|
||||
public function getDefaultLanguagePack(): object
|
||||
{
|
||||
return $this->getLanguagePack($this->default_language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the language pack for a language
|
||||
* @param string $language The language code of the language pack
|
||||
* @return language_pack_i The language pack
|
||||
*/
|
||||
|
||||
public function getLanguagePack(string $language): object
|
||||
{
|
||||
foreach ( $this->language_packs as $language_pack ) {
|
||||
if ($language_pack->getLanguage() === $language) {
|
||||
return $language_pack;
|
||||
}
|
||||
}
|
||||
return $this->getLanguagePack($this->default_language);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace interfaces;
|
||||
|
||||
interface language_pack_i
|
||||
{
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace languages;
|
||||
|
||||
use interfaces\language_pack_i;
|
||||
use traits\language_pack_t;
|
||||
|
||||
class language_pack_en_us implements language_pack_i
|
||||
{
|
||||
use language_pack_t;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setLanguage('en_us');
|
||||
$this->addTranslations([
|
||||
'Order ID is required' => 'Order ID is required ( Translated )',
|
||||
'Product ID is required' => 'Product ID is required ( Translated )',
|
||||
'Quantity is required' => 'Quantity is required ( Translated )',
|
||||
'Order items added' => 'Order items added ( Translated )',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getLanguage(): string
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,19 @@ class users_o extends db
|
||||
{
|
||||
global $db;
|
||||
$this->id = $id;
|
||||
|
||||
// Clear old cache mapping if customer number changed
|
||||
$old_res = $db->query("SELECT customer_number FROM $this->table WHERE id = $this->id");
|
||||
if ($old_res && $old_res->num_rows > 0) {
|
||||
$old_cn = (int)$old_res->fetch_assoc()['customer_number'];
|
||||
if ($old_cn !== 0 && $old_cn !== (int)$customer_number) {
|
||||
redis->clear_user_id_from_customer_number($old_cn);
|
||||
}
|
||||
}
|
||||
// Cache the mapping from customer_number to user_id (new value)
|
||||
redis->cache_user_id_from_customer_number((int)$customer_number, $this->id);
|
||||
redis->cache_customer_number_from_user_id($this->id, (int)$customer_number);
|
||||
|
||||
// Avoid SQL injection
|
||||
$customer_number = $db->escape_string($customer_number);
|
||||
if ($password !== null) {
|
||||
@@ -218,7 +231,11 @@ class users_o extends db
|
||||
$db->query($sql);
|
||||
|
||||
// Get the id of the new record
|
||||
$this->id = $db->insert_id();
|
||||
$this->id = (int)$db->insert_id();
|
||||
|
||||
// Cache the mapping from customer_number to user_id
|
||||
redis->cache_user_id_from_customer_number((int)$customer_number, $this->id);
|
||||
redis->cache_customer_number_from_user_id($this->id, (int)$customer_number);
|
||||
|
||||
// Set the values of the object properties
|
||||
$this->getObjectProperties();
|
||||
@@ -315,11 +332,22 @@ class users_o extends db
|
||||
public function getUserById(int $id): users_o
|
||||
{
|
||||
global $db;
|
||||
// Check Redis for existence (by checking if we have the customer number)
|
||||
$customer_number = redis->get_customer_number_from_user_id($id);
|
||||
if ($customer_number !== null) {
|
||||
$this->id = $id;
|
||||
$this->getObjectProperties();
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Get the record from the database
|
||||
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
||||
$sql = "SELECT customer_number FROM $this->table WHERE id = $id";
|
||||
$result = $db->query($sql);
|
||||
if ($result->num_rows > 0) {
|
||||
$this->id = $id;
|
||||
$customer_number = (int)$result->fetch_assoc()['customer_number'];
|
||||
// Cache the result
|
||||
redis->cache_customer_number_from_user_id($id, $customer_number);
|
||||
$this->getObjectProperties();
|
||||
}
|
||||
return $this;
|
||||
@@ -328,11 +356,21 @@ class users_o extends db
|
||||
public function getUserByCustomerNumber(int $customer_number): users_o
|
||||
{
|
||||
global $db;
|
||||
// Check Redis first
|
||||
$user_id = redis->get_user_id_from_customer_number($customer_number);
|
||||
if ($user_id !== null) {
|
||||
$this->id = (int)$user_id;
|
||||
$this->getObjectProperties();
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Get the record from the database
|
||||
$sql = "SELECT id FROM $this->table WHERE customer_number = '$customer_number'";
|
||||
$result = $db->query($sql);
|
||||
if ($result->num_rows > 0) {
|
||||
$this->id = $result->fetch_assoc()['id'];
|
||||
$this->id = (int)$result->fetch_assoc()['id'];
|
||||
// Cache the result
|
||||
redis->cache_user_id_from_customer_number($customer_number, $this->id);
|
||||
$this->getObjectProperties();
|
||||
} else {
|
||||
// Import the customer
|
||||
@@ -1041,6 +1079,12 @@ class users_o extends db
|
||||
|
||||
public function isImportedFromEconomic($customerNumber): bool
|
||||
{
|
||||
// Check Redis first
|
||||
$user_id = redis->get_user_id_from_customer_number((int)$customerNumber);
|
||||
if ($user_id !== null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if the user is imported from the external source
|
||||
if (self::countRowsWhere(['customer_number' => $customerNumber]) > 0) {
|
||||
return true;
|
||||
@@ -1050,9 +1094,20 @@ class users_o extends db
|
||||
|
||||
public function getUserIdFromEconomic($customerNumber): int
|
||||
{
|
||||
// Check Redis first
|
||||
$user_id = redis->get_user_id_from_customer_number((int)$customerNumber);
|
||||
if ($user_id !== null) {
|
||||
return (int)$user_id;
|
||||
}
|
||||
|
||||
// Get the user id from the external source
|
||||
$user = self::getFieldsWhere(['customer_number' => $customerNumber], ['id']);
|
||||
return $user[0]['id'];
|
||||
$id = (int)$user[0]['id'];
|
||||
|
||||
// Cache the result
|
||||
redis->cache_user_id_from_customer_number((int)$customerNumber, $id);
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace traits;
|
||||
|
||||
trait language_pack_t
|
||||
{
|
||||
|
||||
/**
|
||||
* The language code of the language pack e.g. en_us
|
||||
* @var string
|
||||
*/
|
||||
public string $language; // The language code of the language pack e.g. en_us
|
||||
|
||||
/**
|
||||
* The translations of the language pack
|
||||
* @var array
|
||||
*/
|
||||
protected array $translations = []; // The translations of the language pack
|
||||
|
||||
/**
|
||||
* Get the translation of a string
|
||||
* @param string $string The string to translate
|
||||
* @return string The translated string
|
||||
*/
|
||||
public function translate(string $string): string
|
||||
{
|
||||
return $this->translations[$string] ?? $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the language code of the language pack
|
||||
* @return string The language code of the language pack
|
||||
*/
|
||||
public function getLanguage(): string
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the language code of the language pack
|
||||
* @param string $language The language code of the language pack
|
||||
*/
|
||||
public function setLanguage(string $language): void
|
||||
{
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add multiple translations to the language pack
|
||||
* @param array $strings The strings to translate
|
||||
*/
|
||||
public function addTranslations(array $strings): void
|
||||
{
|
||||
foreach ( $strings as $string => $translation ) {
|
||||
$this->addTranslation($string, $translation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a translation to the language pack
|
||||
* @param string $string The string to translate
|
||||
*/
|
||||
public function addTranslation(string $string, string $translation): void
|
||||
{
|
||||
$this->translations[$string] = $translation;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user