|
| 1 | +# Theme Selection and Session Variable Handling in HCP Framework |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document explains the interaction between initialization variables, session handling, and theme selection in the HCP Framework, specifically focusing on how theme persistence is managed across page requests. |
| 5 | + |
| 6 | +## Variable Flow and Initialization |
| 7 | + |
| 8 | +### Initial State |
| 9 | +The global configuration object `$g` starts with default values: |
| 10 | +```php |
| 11 | +public $in = [ |
| 12 | + 't' => 'Basic', // Default theme |
| 13 | + 'o' => 'Home', // Default content object |
| 14 | + 'm' => 'list', // Default method |
| 15 | + // ... other defaults |
| 16 | +]; |
| 17 | +``` |
| 18 | + |
| 19 | +### Request Processing |
| 20 | +1. In `Init::__construct()`, the following sequence occurs: |
| 21 | + ```php |
| 22 | + // Sanitize input values |
| 23 | + $g->in = Util::esc($g->in); |
| 24 | + |
| 25 | + // This processes each input var against $_REQUEST |
| 26 | + // If a matching $_REQUEST key exists, it overrides the default |
| 27 | + // If not, the default value remains |
| 28 | + ``` |
| 29 | + |
| 30 | +### Session Handling |
| 31 | +The `Util::ses()` method manages session variable persistence: |
| 32 | +```php |
| 33 | +public static function ses(string $k, string $v = '', ?string $x = null): string |
| 34 | +{ |
| 35 | + // k: session key |
| 36 | + // v: default value |
| 37 | + // x: override value (if provided) |
| 38 | + |
| 39 | + return $_SESSION[$k] = |
| 40 | + (!is_null($x) && (!isset($_SESSION[$k]) || ($_SESSION[$k] != $x))) |
| 41 | + ? $x |
| 42 | + : (((isset($_REQUEST[$k]) && !isset($_SESSION[$k])) |
| 43 | + || (isset($_REQUEST[$k], $_SESSION[$k]) |
| 44 | + && ($_REQUEST[$k] != $_SESSION[$k]))) |
| 45 | + ? self::enc($_REQUEST[$k]) |
| 46 | + : ($_SESSION[$k] ?? $v)); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Theme Selection Process |
| 51 | + |
| 52 | +### Priority Order |
| 53 | +Theme selection follows this priority sequence: |
| 54 | +1. URL Parameter (`?t=ThemeName`) |
| 55 | +2. Existing Session Value |
| 56 | +3. Default Value (`Basic`) |
| 57 | + |
| 58 | +### Selection Flow |
| 59 | +```php |
| 60 | +// In Init.php |
| 61 | +$t = Util::ses('t', $g->in['t']); // Correct usage for theme persistence |
| 62 | + |
| 63 | +// This evaluates as: |
| 64 | +// 1. If t exists in $_REQUEST, use that value |
| 65 | +// 2. If t exists in $_SESSION, use that value |
| 66 | +// 3. Otherwise, fall back to $g->in['t'] (usually 'Basic') |
| 67 | +``` |
| 68 | + |
| 69 | +### Theme Class Resolution |
| 70 | +After theme selection: |
| 71 | +```php |
| 72 | +$t2 = "HCP\\Themes\\$t"; // Constructs theme class name |
| 73 | +if (class_exists($t2)) { |
| 74 | + $thm->themeImpl = new $t2($g); // Instantiates theme |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Example Scenarios |
| 79 | + |
| 80 | +### First Visit (No Session) |
| 81 | +- Default: `$g->in['t'] = 'Basic'` |
| 82 | +- No session or URL parameters |
| 83 | +- Result: Basic theme used and stored in session |
| 84 | + |
| 85 | +### Theme Change via URL |
| 86 | +- URL: `?t=TopNav` |
| 87 | +- Session updated to store 'TopNav' |
| 88 | +- Theme persists on subsequent pages |
| 89 | + |
| 90 | +### Subsequent Visits |
| 91 | +- No URL parameter |
| 92 | +- Existing session value used |
| 93 | +- Default 'Basic' ignored if session exists |
| 94 | + |
| 95 | +## Important Notes |
| 96 | +1. Session persistence relies on proper parameter ordering in `Util::ses()` |
| 97 | +2. URL parameters always override existing session values |
| 98 | +3. Default values only apply when no session or URL parameter exists |
| 99 | +4. Theme class must exist in `HCP\Themes` namespace |
| 100 | +5. Invalid theme names fall back to base Theme class |
| 101 | + |
| 102 | +This implementation allows for dynamic theme switching while maintaining user preference across page loads through session persistence. |
| 103 | + |
0 commit comments