183 lines
6.1 KiB
PHP
183 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../dbinfo.php';
|
|
|
|
if (!function_exists('lasuca_normalise_panel_key')) {
|
|
/**
|
|
* Normalises a dashboard panel key for consistent storage.
|
|
*/
|
|
function lasuca_normalise_panel_key(string $key): string
|
|
{
|
|
return trim($key);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('lasuca_get_section_layout_connection')) {
|
|
/**
|
|
* Opens a MySQL connection configured for the section layouts table.
|
|
*/
|
|
function lasuca_get_section_layout_connection(): mysqli|false
|
|
{
|
|
$connection = mysqli_connect($GLOBALS['DB_SERVER'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASS']);
|
|
if (!$connection) {
|
|
error_log('lasuca_get_section_layout_connection: cannot connect to MySQL server: ' . mysqli_connect_error());
|
|
return false;
|
|
}
|
|
|
|
if (!mysqli_select_db($connection, $GLOBALS['DB_NAME'])) {
|
|
error_log('lasuca_get_section_layout_connection: cannot select database: ' . mysqli_error($connection));
|
|
mysqli_close($connection);
|
|
return false;
|
|
}
|
|
|
|
if (!mysqli_set_charset($connection, 'utf8mb4')) {
|
|
error_log('lasuca_get_section_layout_connection: cannot set charset: ' . mysqli_error($connection));
|
|
mysqli_close($connection);
|
|
return false;
|
|
}
|
|
|
|
return $connection;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('lasuca_get_section_order')) {
|
|
/**
|
|
* Retrieves the saved section order for a member/panel combination.
|
|
*
|
|
* @return string[]
|
|
*/
|
|
function lasuca_get_section_order(mysqli $connection, string $sectionKey, string $memberId): array
|
|
{
|
|
$normalisedKey = lasuca_normalise_panel_key($sectionKey);
|
|
$safeMemberId = trim($memberId);
|
|
|
|
$sql = 'SELECT section_order FROM section_layouts WHERE panel_key = ? AND member_id = ? LIMIT 1';
|
|
$statement = mysqli_prepare($connection, $sql);
|
|
if (!$statement) {
|
|
error_log('lasuca_get_section_order: failed to prepare statement: ' . mysqli_error($connection));
|
|
return [];
|
|
}
|
|
|
|
mysqli_stmt_bind_param($statement, 'ss', $normalisedKey, $safeMemberId);
|
|
|
|
if (!mysqli_stmt_execute($statement)) {
|
|
error_log('lasuca_get_section_order: failed to execute statement: ' . mysqli_stmt_error($statement));
|
|
mysqli_stmt_close($statement);
|
|
return [];
|
|
}
|
|
|
|
$result = mysqli_stmt_get_result($statement);
|
|
mysqli_stmt_close($statement);
|
|
|
|
if (!$result instanceof mysqli_result) {
|
|
return [];
|
|
}
|
|
|
|
$row = mysqli_fetch_assoc($result);
|
|
mysqli_free_result($result);
|
|
|
|
if (!$row) {
|
|
return [];
|
|
}
|
|
|
|
$decoded = json_decode((string) $row['section_order'], true);
|
|
if (!is_array($decoded)) {
|
|
return [];
|
|
}
|
|
|
|
return array_values(array_filter($decoded, static fn($value): bool => is_string($value) && $value !== ''));
|
|
}
|
|
}
|
|
|
|
if (!function_exists('lasuca_save_section_order')) {
|
|
/**
|
|
* Persists a member's preferred section ordering.
|
|
*/
|
|
function lasuca_save_section_order(mysqli $connection, string $sectionKey, string $memberId, array $sectionOrder): bool
|
|
{
|
|
$normalisedKey = lasuca_normalise_panel_key($sectionKey);
|
|
$safeMemberId = trim($memberId);
|
|
$sanitisedOrder = array_values(array_filter($sectionOrder, static fn($value): bool => is_string($value) && $value !== ''));
|
|
|
|
$payload = json_encode($sanitisedOrder, JSON_THROW_ON_ERROR);
|
|
|
|
$sql = 'INSERT INTO section_layouts (panel_key, member_id, section_order, updated_at)
|
|
VALUES (?, ?, ?, NOW())
|
|
ON DUPLICATE KEY UPDATE section_order = VALUES(section_order), updated_at = NOW()';
|
|
|
|
$statement = mysqli_prepare($connection, $sql);
|
|
if (!$statement) {
|
|
error_log('lasuca_save_section_order: failed to prepare statement: ' . mysqli_error($connection));
|
|
return false;
|
|
}
|
|
|
|
mysqli_stmt_bind_param($statement, 'sss', $normalisedKey, $safeMemberId, $payload);
|
|
|
|
if (!mysqli_stmt_execute($statement)) {
|
|
error_log('lasuca_save_section_order: failed to execute statement: ' . mysqli_stmt_error($statement));
|
|
mysqli_stmt_close($statement);
|
|
return false;
|
|
}
|
|
|
|
$affected = mysqli_stmt_affected_rows($statement);
|
|
mysqli_stmt_close($statement);
|
|
|
|
return $affected > 0;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('lasuca_reset_section_order')) {
|
|
/**
|
|
* Removes any persisted ordering for the user/panel.
|
|
*/
|
|
function lasuca_reset_section_order(mysqli $connection, string $sectionKey, string $memberId): bool
|
|
{
|
|
$normalisedKey = lasuca_normalise_panel_key($sectionKey);
|
|
$safeMemberId = trim($memberId);
|
|
|
|
$sql = 'DELETE FROM section_layouts WHERE panel_key = ? AND member_id = ?';
|
|
$statement = mysqli_prepare($connection, $sql);
|
|
if (!$statement) {
|
|
error_log('lasuca_reset_section_order: failed to prepare statement: ' . mysqli_error($connection));
|
|
return false;
|
|
}
|
|
|
|
mysqli_stmt_bind_param($statement, 'ss', $normalisedKey, $safeMemberId);
|
|
|
|
if (!mysqli_stmt_execute($statement)) {
|
|
error_log('lasuca_reset_section_order: failed to execute statement: ' . mysqli_stmt_error($statement));
|
|
mysqli_stmt_close($statement);
|
|
return false;
|
|
}
|
|
|
|
$affected = mysqli_stmt_affected_rows($statement);
|
|
mysqli_stmt_close($statement);
|
|
|
|
return $affected > 0;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('lasuca_section_layout_health_check')) {
|
|
/**
|
|
* Performs a lightweight connectivity check for diagnostics.
|
|
*/
|
|
function lasuca_section_layout_health_check(): bool
|
|
{
|
|
$connection = lasuca_get_section_layout_connection();
|
|
if (!$connection) {
|
|
return false;
|
|
}
|
|
|
|
$result = mysqli_query($connection, 'SELECT 1');
|
|
if ($result instanceof mysqli_result) {
|
|
mysqli_free_result($result);
|
|
mysqli_close($connection);
|
|
return true;
|
|
}
|
|
|
|
mysqli_close($connection);
|
|
return false;
|
|
}
|
|
} |