diff --git a/db/scitemcustom.php b/db/scitemcustom.php index 2647f25..4e3d04c 100644 --- a/db/scitemcustom.php +++ b/db/scitemcustom.php @@ -9,6 +9,14 @@ function scitemcustom_column_exists(PDO $db, string $table, string $column): boo return (bool) $stmt->fetch(); } +function scitemcustom_column_definition(PDO $db, string $table, string $column): ?array +{ + $stmt = $db->query("SHOW COLUMNS FROM `{$table}` LIKE " . $db->quote($column)); + $column_definition = $stmt->fetch(PDO::FETCH_ASSOC); + + return $column_definition ?: null; +} + function scitemcustom_index_exists(PDO $db, string $table, string $index): bool { $stmt = $db->query("SHOW INDEX FROM `{$table}` WHERE Key_name = " . $db->quote($index)); @@ -105,7 +113,7 @@ function scitemcustom_bootstrap(): void cl_scitemcustomstat_id INT(11) NOT NULL AUTO_INCREMENT, cl_scitemcustomstat_itemcustom_id INT(11) NOT NULL, cl_scitemcustomstat_stat_id INT(11) NOT NULL, - cl_scitemcustomstat_sign ENUM('+', '-') NOT NULL DEFAULT '+', + cl_scitemcustomstat_sign ENUM('+', '', '-') NOT NULL DEFAULT '+', cl_scitemcustomstat_value DECIMAL(10,2) NOT NULL DEFAULT 0.00, cl_scitemcustomstat_created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (cl_scitemcustomstat_id), @@ -123,5 +131,13 @@ function scitemcustom_bootstrap(): void ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" ); + $sign_column = scitemcustom_column_definition($db, 'tbl_scitemcustomstat', 'cl_scitemcustomstat_sign'); + if ($sign_column && ($sign_column['Type'] ?? '') !== "enum('+','','-')") { + $db->exec( + "ALTER TABLE tbl_scitemcustomstat + MODIFY COLUMN cl_scitemcustomstat_sign ENUM('+', '', '-') NOT NULL DEFAULT '+'" + ); + } + $scitemcustom_bootstrap_done = true; } diff --git a/scitemcustom.php b/scitemcustom.php index acf7466..179f3f4 100644 --- a/scitemcustom.php +++ b/scitemcustom.php @@ -16,7 +16,15 @@ if (!auth_is_admin()) { function scitemcustom_normalize_sign(?string $sign): string { - return $sign === '-' ? '-' : '+'; + if ($sign === '-') { + return '-'; + } + + if ($sign === '+') { + return '+'; + } + + return ''; } function scitemcustom_normalize_value(?string $value): ?string @@ -43,7 +51,9 @@ function scitemcustom_display_value($value): string function scitemcustom_preview(string $sign, $value, string $unit): string { - return ($sign === '-' ? '-' : '+') . scitemcustom_display_value($value) . $unit; + $prefix = $sign === '-' ? '-' : ($sign === '+' ? '+' : ''); + + return $prefix . scitemcustom_display_value($value) . $unit; } function scitemcustom_current_owner_auth_id(PDO $db): int @@ -70,7 +80,8 @@ $flash_message = $flash['message'] ?? ''; $db = db(); $csrf_token = auth_csrf_token(); -$allowed_signs = ['+', '-']; +$allowed_signs = ['+', '', '-']; +$sign_labels = ['+' => '+', '' => 'Aucun', '-' => '-']; $current_owner_auth_id = scitemcustom_current_owner_auth_id($db); if ($current_owner_auth_id <= 0) { @@ -871,8 +882,11 @@ $current_session_user = $_SESSION['user'] ?? '';