WizdomWeb/resources/views/partials/slider.php

80 lines
4.0 KiB
PHP

<?php
use WizdomNetworks\WizeWeb\Utils\Logger;
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
/**
* Slider Partial (v14) - Refactored to Remove Inline Styles
*
* This partial dynamically loads different types of sliders based on the provided configuration.
* All styles are written to a page-specific CSS file (`<pageid>.css`) instead of using inline styles.
* Debug logging and error handling are implemented using the Logger and ErrorHandler utilities.
*
* ## Configuration Options:
* - `id` (string) - Unique identifier for the slider (used for styling and debugging)
* - `type` (string) - Allowed values: 'standard', 'testimonial'
* - `height` (string) - Defines the slider height (default: '65vh')
* - `object_fit` (string) - Image object-fit property ('cover', 'contain', etc.)
* - `interval` (int) - Time in milliseconds between slides (default: 5000ms)
* - `pause_on_hover` (bool) - Pauses autoplay when hovered (default: true)
* - `controls` (bool) - Shows/hides navigation controls (default: true)
* - `indicators` (bool) - Shows/hides carousel indicators (default: true)
* - `animation` (string) - Allows 'slide' or 'fade' animation (default: 'slide')
* - `cta_width` (string) - Defines CTA container width (default: 'auto')
* - `cta_text_align` (string) - Text alignment within CTA (default: 'center')
* - `lazy_load` (bool) - Enables lazy loading for images (default: true)
* - `cta_top` (string) - Vertical positioning of CTA (default: varies per slide)
* - `cta_left` (string) - Horizontal positioning of CTA (default: varies per slide)
* - `text_color` (string) - Defines text color for each slide (default: 'white')
*/
try {
if (!isset($sliderConfig) || empty($sliderConfig['type'])) {
throw new Exception("Missing or invalid slider configuration.");
}
$validTypes = ['standard', 'testimonial'];
$sliderType = in_array($sliderConfig['type'], $validTypes) ? $sliderConfig['type'] : 'standard';
$sliderId = htmlspecialchars($sliderConfig['id'] ?? 'default', ENT_QUOTES, 'UTF-8');
$pageCssFile = "/assets/css/" . htmlspecialchars($pageId, ENT_QUOTES, 'UTF-8') . ".css";
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . $pageCssFile;
$sliderHeight = $sliderConfig['height'] ?? '65vh';
$objectFit = htmlspecialchars($sliderConfig['object_fit'] ?? 'cover', ENT_QUOTES, 'UTF-8');
$interval = $sliderConfig['interval'] ?? 5000;
$pauseOnHover = isset($sliderConfig['pause_on_hover']) && $sliderConfig['pause_on_hover'] ? 'hover' : 'false';
$showControls = $sliderConfig['controls'] ?? true;
$showIndicators = $sliderConfig['indicators'] ?? true;
$animationType = $sliderConfig['animation'] ?? 'slide';
$ctaWidth = htmlspecialchars($sliderConfig['cta_width'] ?? 'auto', ENT_QUOTES, 'UTF-8');
$ctaTextAlign = htmlspecialchars($sliderConfig['cta_text_align'] ?? 'center', ENT_QUOTES, 'UTF-8');
$lazyLoad = isset($sliderConfig['lazy_load']) && $sliderConfig['lazy_load'] ? 'loading="lazy"' : '';
$sliderImages = $sliderConfig['slides'] ?? [];
if (empty($sliderImages)) {
Logger::error("Slider '$sliderId' has no slides defined.");
throw new Exception("Slider '$sliderId' has no slides defined.");
}
$cssContent = "/* Auto-generated styles for {$pageId} sliders */\n";
foreach ($sliderImages as $index => $image) {
$cssContent .= "#slider-{$sliderId}-slide-{$index} {\n";
if (!empty($image['cta_top'])) {
$cssContent .= " top: {$image['cta_top']};\n";
}
if (!empty($image['cta_left'])) {
$cssContent .= " left: {$image['cta_left']};\n";
}
if (!empty($image['text_color'])) {
$cssContent .= " color: {$image['text_color']};\n";
}
$cssContent .= "}\n";
}
if (!file_exists($cssFilePath) || file_get_contents($cssFilePath) !== $cssContent) {
file_put_contents($cssFilePath, $cssContent);
}
Logger::info("Slider '$sliderId' styles successfully written to '$pageCssFile'.");
} catch (Exception $e) {
ErrorHandler::exception($e);
}
?>