Modify New Menu Themes and save changes for any user

Hi All. Here is the code and instructions to save the menu colors when the user select another.

  1. Add a user_theme field (varchar 100) on sec_users table
  2. Add this code menu OnApplicationInit
 $sql_pref = "SELECT user_theme FROM sec_users WHERE login = " . sc_sql_injection([usr_login]);
    sc_lookup(ds_theme, $sql_pref);
    if (!empty({ds_theme[0][0]})) {
        $tema_db       = trim({ds_theme[0][0]});
        $flag_key      = 'tema_aplicado_' . [usr_login];
        $cookie_actual = $_COOKIE['menuTheme'] ?? '';
        $sc_path       = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');

        // Detectar cambio intencional del usuario
        if (!empty($_SESSION[$flag_key]) && 
            !empty($cookie_actual) && 
            $cookie_actual !== $_SESSION[$flag_key]) {
            $sql_upd = "UPDATE sec_users SET user_theme = " . sc_sql_injection($cookie_actual) .
                       " WHERE login = " . sc_sql_injection([usr_login]);
            sc_exec_sql($sql_upd);
            $_SESSION[$flag_key] = $cookie_actual;
            $tema_db = $cookie_actual;
        }

        // Setear flag si no existe
        if (empty($_SESSION[$flag_key])) {
            $_SESSION[$flag_key] = $tema_db;
        }

        // Si la cookie es distinta al tema de BD, forzar desde PHP inmediatamente
        if (empty($cookie_actual) || $cookie_actual !== $tema_db) {
            setcookie('menuTheme', $tema_db, 0, $sc_path);
            $_COOKIE['menuTheme'] = $tema_db;
            $_SESSION['scriptcase']['main_menu']['glo_nm_esquema'] = $tema_db;
        }

        $js = "
        (function() {
            var temaDB  = '" . addslashes($tema_db) . "';
            var scPath  = '" . addslashes($sc_path) . "';
            var flagKey = 'sc_tema_aplicado';

            function getCookieAll(name) {
                var result = [];
                var parts = document.cookie.split(';');
                for (var i = 0; i < parts.length; i++) {
                    var part = parts[i].trim();
                    if (part.indexOf(name + '=') === 0) {
                        result.push(decodeURIComponent(part.substring(name.length + 1)));
                    }
                }
                return result;
            }

            function setCookie(name, value, path) {
                document.cookie = name + '=' + encodeURIComponent(value) + '; path=' + path;
            }

            function iniciarMonitor() {
                var snapAntes = JSON.stringify(document.cookie.match(/menuTheme=[^;]+/g));
                setInterval(function() {
                    var snapAhora = JSON.stringify(document.cookie.match(/menuTheme=[^;]+/g));
                    if (snapAhora !== snapAntes) {
                        var todosAntes = JSON.parse(snapAntes) || [];
                        var todosAhora = JSON.parse(snapAhora) || [];
                        var temaNuevo = null;
                        for (var i = 0; i < todosAhora.length; i++) {
                            var val = todosAhora[i].replace('menuTheme=', '');
                            if (val && todosAntes.indexOf(todosAhora[i]) === -1) {
                                temaNuevo = val;
                                break;
                            }
                        }
                        if (temaNuevo) {
                            sessionStorage.setItem(flagKey, temaNuevo);
                            fetch('../blank_guardar_tema/?tema=' + encodeURIComponent(temaNuevo))
                                .catch(function(){});
                        }
                        snapAntes = snapAhora;
                    }
                }, 300);
            }

            var todos      = getCookieAll('menuTheme');
            var cookieActual = todos[0] || null;
            var yaAplicado = sessionStorage.getItem(flagKey);

            if (cookieActual !== temaDB && yaAplicado !== temaDB) {
                // Cookie aun no tiene el tema de BD: forzar desde JS y recargar una vez
                sessionStorage.setItem(flagKey, temaDB);
                setCookie('menuTheme', temaDB, scPath);
                window.location.replace(window.location.href);
            } else {
                // Cookie ya correcta: solo monitorear cambios del usuario
                sessionStorage.setItem(flagKey, temaDB);
                iniciarMonitor();
            }
        })();
        ";
        echo "<script>" . $js . "</script>";
    }
  1. Create a blank app in this case called blank_guardar_tema with this code
    if (!empty($_GET[‘tema’])) {
    $tema_nuevo = trim($_GET[‘tema’]);
    $usuario = [usr_login];

    $sql_upd = "UPDATE sec_users SET user_theme = " . sc_sql_injection($tema_nuevo) .
    " WHERE login = " . sc_sql_injection($usuario);
    sc_exec_sql($sql_upd);

    $SESSION['tema_aplicado’ . $usuario] = $tema_nuevo;
    }
    header(‘Content-Type: application/json’);
    echo json_encode([‘ok’ => true]);
    exit;

Enjoy!

Tested on multiple browsers and PCs from other networks.

2 Likes