Adds theme switcher controller and view

main
Yasen Pramatarov 2025-05-22 14:33:54 +03:00
parent c2cfd503ee
commit 2d0c280a0a
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?php
/**
* Theme Management Controller
*
* Handles theme switching and management functionality.
* Allows users to view available themes and change the active theme.
*
* Actions:
* - switch_to: Changes the active theme for the current user
*/
// Only allow access to logged-in users
if (!Session::isValidSession()) {
header('Location: ' . $app_root . '?page=login');
exit;
}
// Handle theme switching
if (isset($_GET['switch_to'])) {
$themeName = $_GET['switch_to'];
// Validate CSRF token for state-changing operations
require_once '../app/helpers/security.php';
$security = SecurityHelper::getInstance();
if (!$security->verifyCsrfToken($_GET['csrf_token'] ?? '')) {
Feedback::flash('SECURITY', 'CSRF_INVALID');
header("Location: $app_root?page=theme");
exit();
}
if (\App\Helpers\Theme::setCurrentTheme($themeName)) {
// Set success message
Feedback::flash('THEME', 'THEME_CHANGED');
} else {
// Set error message
Feedback::flash('THEME', 'THEME_CHANGE_FAILED');
}
// Redirect back to prevent form resubmission
$redirect = $app_root . '?page=theme';
header("Location: $redirect");
exit;
}
// Get available themes and current theme for the view
$themes = \App\Helpers\Theme::getAvailableThemes();
$currentTheme = \App\Helpers\Theme::getCurrentThemeName();
// Generate CSRF token for the form
$csrf_token = $security->generateCsrfToken();
// Get any new feedback messages
include '../app/helpers/feedback.php';
// Load the template
include '../app/templates/theme.php';

View File

@ -0,0 +1,37 @@
<?php
/**
* Theme switcher template
*
* Displays available themes and allows the user to switch between them.
*
* @var array $themes List of available themes
* @var string $currentTheme Currently active theme ID
*/
?>
<div class="container mt-4">
<h2>Theme Switcher</h2>
<p class="text-muted">Select a theme to change the appearance of the application.</p>
<div class="row mt-4">
<?php foreach ($themes as $themeId => $themeName): ?>
<?php $isActive = $themeId === $currentTheme; ?>
<div class="col-md-4 mb-4">
<div class="card h-100 <?= $isActive ? 'border-primary' : '' ?>">
<?php if ($isActive) { ?>
<div class="card-header bg-primary text-white">Current Theme</div>
<?php } ?>
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?= htmlspecialchars($themeName) ?></h5>
<p class="card-text text-muted">Theme ID: <code><?= htmlspecialchars($themeId) ?></code></p>
<div class="mt-auto">
<?php if (!$isActive) { ?>
<a href="?page=theme&switch_to=<?= urlencode($themeId) ?>" class="btn btn-primary">Switch to this theme</a>
<?php } else { ?>
<button class="btn btn-outline-secondary" disabled>Currently active</button>
<?php } ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>