WizdomWeb/resources/views/partials/sidebar.php

65 lines
2.8 KiB
PHP

<?php
use WizdomNetworks\WizeWeb\Utils\Logger;
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
/**
* Sidebar Partial (v3) - Refactored for Consistency
*
* This partial dynamically loads sidebar configurations and writes styles to a page-specific CSS file (`<pageid>.css`).
*
* ## Configuration Options:
* - `id` (string) - Unique identifier for the sidebar.
* - `width` (string) - Width of the sidebar (default: '300px').
* - `position` (string) - Allowed values: 'left', 'right' (default: 'right').
* - `background_color` (string) - Background color of the sidebar (default: '#f8f9fa').
*/
try {
if (!isset($sidebarConfig) || empty($sidebarConfig['id'])) {
throw new Exception("Missing or invalid sidebar configuration.");
}
$sidebarId = htmlspecialchars($sidebarConfig['id'] ?? 'default', ENT_QUOTES, 'UTF-8');
$pageCssFile = "/assets/css/" . htmlspecialchars($pageId, ENT_QUOTES, 'UTF-8') . ".css";
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . $pageCssFile;
$sidebarWidth = htmlspecialchars($sidebarConfig['width'] ?? '300px', ENT_QUOTES, 'UTF-8');
$sidebarPosition = in_array($sidebarConfig['position'] ?? 'right', ['left', 'right']) ? $sidebarConfig['position'] : 'right';
$backgroundColor = htmlspecialchars($sidebarConfig['background_color'] ?? '#f8f9fa', ENT_QUOTES, 'UTF-8');
Logger::info("Generating sidebar styles for page: {$pageId}");
$cssContent = "/* Auto-generated styles for {$pageId} sidebar */\n";
$cssContent .= "#sidebar-{$sidebarId} {\n";
$cssContent .= " width: {$sidebarWidth};\n";
$cssContent .= " background-color: {$backgroundColor};\n";
$cssContent .= " float: {$sidebarPosition};\n";
$cssContent .= " padding: 1rem;\n";
$cssContent .= "}\n";
if (!file_exists($cssFilePath) || file_get_contents($cssFilePath) !== $cssContent) {
file_put_contents($cssFilePath, $cssContent);
Logger::info("Sidebar styles written to {$pageCssFile}.");
}
} catch (Exception $e) {
ErrorHandler::exception($e);
Logger::error("Sidebar generation error: " . $e->getMessage());
}
?>
<aside id="sidebar-<?php echo $sidebarId; ?>" class="sidebar">
<div class="sidebar-content">
<?php if (!empty($sidebarConfig['title'])) : ?>
<h2><?php echo htmlspecialchars($sidebarConfig['title'], ENT_QUOTES, 'UTF-8'); ?></h2>
<?php endif; ?>
<div class="sidebar-items">
<?php foreach ($sidebarConfig['items'] ?? [] as $item): ?>
<div class="sidebar-item">
<a href="<?php echo htmlspecialchars($item['link'], ENT_QUOTES, 'UTF-8'); ?>">
<?php echo htmlspecialchars($item['text'], ENT_QUOTES, 'UTF-8'); ?>
</a>
</div>
<?php endforeach; ?>
</div>
</div>
</aside>