Fix limited backoffice schema compatibility
This commit is contained in:
@@ -112,6 +112,11 @@ class limited_backoffice_service
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
private array $columnExistsCache = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
limited_backoffice_schema_bootstrap::ensureTables();
|
||||
@@ -320,6 +325,10 @@ class limited_backoffice_service
|
||||
return [];
|
||||
}
|
||||
|
||||
$userDeletedAtSelect = $this->tableHasColumn('users', 'deleted_at')
|
||||
? 'u.`deleted_at` AS `user_deleted_at`'
|
||||
: 'NULL AS `user_deleted_at`';
|
||||
|
||||
global $db;
|
||||
$rows = $db->fetch_all($db->query("
|
||||
SELECT
|
||||
@@ -330,7 +339,7 @@ class limited_backoffice_service
|
||||
u.`phone_country_code`,
|
||||
u.`phone`,
|
||||
u.`group_id`,
|
||||
u.`deleted_at` AS `user_deleted_at`
|
||||
{$userDeletedAtSelect}
|
||||
FROM `limited_backoffice_employees` lbe
|
||||
INNER JOIN `users` u ON u.`id` = lbe.`user_id`
|
||||
ORDER BY u.`display_name` ASC, lbe.`user_id` ASC
|
||||
@@ -497,13 +506,18 @@ class limited_backoffice_service
|
||||
if ($password !== null) {
|
||||
$userUpdates['password'] = password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
$usersHaveDeletedAt = $this->tableHasColumn('users', 'deleted_at');
|
||||
if ($active) {
|
||||
$userUpdates['group_id'] = $managedGroupId;
|
||||
$userUpdates['deleted_at'] = null;
|
||||
if ($usersHaveDeletedAt) {
|
||||
$userUpdates['deleted_at'] = null;
|
||||
}
|
||||
} else {
|
||||
$userUpdates['group_id'] = 0;
|
||||
$userUpdates['password'] = null;
|
||||
$userUpdates['deleted_at'] = date('Y-m-d H:i:s');
|
||||
if ($usersHaveDeletedAt) {
|
||||
$userUpdates['deleted_at'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
}
|
||||
$this->updateUserFields($employeeId, $userUpdates);
|
||||
|
||||
@@ -554,6 +568,28 @@ class limited_backoffice_service
|
||||
return $db->conn();
|
||||
}
|
||||
|
||||
private function tableHasColumn(string $table, string $column): bool
|
||||
{
|
||||
$cacheKey = $table . '.' . $column;
|
||||
if (array_key_exists($cacheKey, $this->columnExistsCache)) {
|
||||
return $this->columnExistsCache[$cacheKey];
|
||||
}
|
||||
|
||||
global $db;
|
||||
$tableSql = $this->escapeIdentifierLookup($table);
|
||||
$columnSql = $this->escapeIdentifierLookup($column);
|
||||
$result = $db->query("SHOW COLUMNS FROM `{$tableSql}` LIKE '{$columnSql}'");
|
||||
$exists = $result !== false && is_object($result) && property_exists($result, 'num_rows') && (int)$result->num_rows > 0;
|
||||
|
||||
$this->columnExistsCache[$cacheKey] = $exists;
|
||||
return $exists;
|
||||
}
|
||||
|
||||
private function escapeIdentifierLookup(string $value): string
|
||||
{
|
||||
return str_replace(['\\', "'", '`'], ['\\\\', "\\'", ''], $value);
|
||||
}
|
||||
|
||||
private function assertDepartmentAccess(users_o $user, int $departmentId): void
|
||||
{
|
||||
if ($departmentId <= 0) {
|
||||
@@ -605,6 +641,14 @@ class limited_backoffice_service
|
||||
{
|
||||
global $db;
|
||||
|
||||
$where = [
|
||||
'dc.`department_id` = ?',
|
||||
'dc.`deleted_at` IS NULL',
|
||||
];
|
||||
if ($this->tableHasColumn('products', 'deleted_at')) {
|
||||
$where[] = 'p.`deleted_at` IS NULL';
|
||||
}
|
||||
|
||||
$statement = $this->mysqli()->prepare(
|
||||
'SELECT
|
||||
c.`id` AS `category_id`,
|
||||
@@ -621,9 +665,7 @@ class limited_backoffice_service
|
||||
LEFT JOIN `product_department_prices` pdp
|
||||
ON pdp.`department_id` = dc.`department_id`
|
||||
AND pdp.`product_id` = p.`id`
|
||||
WHERE dc.`department_id` = ?
|
||||
AND dc.`deleted_at` IS NULL
|
||||
AND p.`deleted_at` IS NULL
|
||||
WHERE ' . implode(' AND ', $where) . '
|
||||
ORDER BY c.`name` ASC, c.`id` ASC, p.`order_priority` ASC, p.`name` ASC, p.`id` ASC'
|
||||
);
|
||||
if ($statement === false) {
|
||||
@@ -986,6 +1028,10 @@ class limited_backoffice_service
|
||||
private function loadManagedEmployee(int $employeeId): ?array
|
||||
{
|
||||
global $db;
|
||||
$userDeletedAtSelect = $this->tableHasColumn('users', 'deleted_at')
|
||||
? 'u.`deleted_at` AS `user_deleted_at`'
|
||||
: 'NULL AS `user_deleted_at`';
|
||||
|
||||
$statement = $this->mysqli()->prepare(
|
||||
'SELECT
|
||||
lbe.*,
|
||||
@@ -995,7 +1041,7 @@ class limited_backoffice_service
|
||||
u.`phone_country_code`,
|
||||
u.`phone`,
|
||||
u.`group_id`,
|
||||
u.`deleted_at` AS `user_deleted_at`
|
||||
' . $userDeletedAtSelect . '
|
||||
FROM `limited_backoffice_employees` lbe
|
||||
INNER JOIN `users` u ON u.`id` = lbe.`user_id`
|
||||
WHERE lbe.`user_id` = ?
|
||||
|
||||
Reference in New Issue
Block a user