<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class themes {
private static $active; //Active theme reference
private static $THM; //Active theme object
private static $CC; //client configs object
/*public static function isloaded(){ return isset(self::$THM); }*/
public static function load(){
if ( isset(self::$THM) ){ return TRUE; }
if ( ! isset(self::$CC) ){
$run = Array(client_configs::SESSION=>'profile');
$store = Array(client_configs::PROFILE=>'site_profile');
$run_update = Array(client_configs::GET=>'', client_configs::POST=>'');
$store_update= Array(client_configs::POST=>'');
self::$CC = new client_configs($run, $store, $run_update, $store_update);
unset($run, $store, $run_update, $store_update);
}
self::get_active();
errors::raise('Themes is using theme : '.self::$active.', '.self::getname(), CORE_LOG_NOTICE, 'THEME');
$class = configs::get('themes', 'themes', Array(self::$active, 'class'));
if ( FALSE === $class ){
errors::raise("Missing class setting for Theme self::$active, plz check your theme config, trying failsave", CORE_LOG_WARNING, 'THEME');
$class = 'thm_failsave';
} elseif ( FALSE === CORE::getdeps(PATH_LIBS, $class, 'thm_', 'class', 'php') ){
errors::raise("Theme object '".$class."' was not found in ".PATH_LIBS.", trying failsave", CORE_LOG_WARNING, 'THEME');
$class = 'thm_failsave';
} else {
$class = 'thm_'.$class;
}
self::$THM = new $class();
return TRUE;
}
public static function get_active(){
if ( isset(self::$active) ){ return self::$active; }
if ( self::$CC->is_updated('theme') ){
$new = self::$CC->get_updated('theme');
if ( FALSE === self::istheme($new) ){
unset($new);self::$CC->del_updated('theme');
} else {
//store to sess, only profile page update profile eh
self::$CC->store_updated('theme');
}
} elseif ( self::$CC->is_stored('theme') ){
$new = self::$CC->get_stored('theme');
if ( FALSE === self::istheme($new) ){ unset($new);self::$CC->del_stored('theme'); }
} else {
$new = self::negotiateTheme();
}
if ( ! isset($new) ){ $new = configs::get('themes', 'default'); }
if ( FALSE === $new ){
errors::raise('Active theme could be determined !, plz check your themes.opt.php file', CORE_LOG_ERROR, 'THEME');
}
self::$active = $new;
return self::$active;
}
public static function getname($ref=NULL){
if ( $ref === NULL ){ $ref = self::$active; }
return configs::get('themes', 'themes', Array($ref, 'name'));
}
public static function gethemes(){
$conf = configs::get('themes', 'themes');
foreach($conf as $k => &$theme){ $o[] = $k; }
return $o;
}
public static function istheme($thm){
$thms = self::gethemes();
return (FALSE !== array_search($thm, $thms));
}
private static function get_default(){ return configs::get('themes', 'default'); }
private static function negotiateTheme(){
$thms = self::gethemes();
$bval = 0;
foreach($thms as $k => $ref){
if (!isset($first) ){ $first = $ref; }
$brws = configs::get('themes', 'themes', Array($ref, 'browser'));
if ( is_array($brws) ){
foreach($brws as $brw => $val){
if ( $brw == client::browser() AND $bval < $val ){ $bthm = $ref; }
}
}
}
if ( !isset($bthm) ){ return self::get_default(); }
errors::raise("Theme negotiation for client browser (".client::browser().") : $bthm", CORE_LOG_NOTICE, 'THEME');
return $bthm;
}
public static function get_active_conf($var){
$b = configs::get('themes', 'themes', Array(self::$active, 'vars'));
if ( FALSE === $b ){ return $b; }
if ( isset($b[$var]) ){
$active = self::get_variant_key($var, self::$active);
if ( $active == 'custum' ){ return self::get_variant_key("custum_$var", self::$active); }
if ( !is_array($b[$var]) ){ return $b[$var]; }
//$var is an array, so many variant possible, let's see which one is active
return $b[$var][$active][$var];
}
return FALSE;
}
public static function get_variant_key($var, $theme){
//multi img/css variant support, only return the key of the active option
if ( self::$CC->is_updated($var.'_'.$theme) ){
$new = self::$CC->get_updated($var.'_'.$theme);
//errors::raise("Theme $theme, variant $var is updated to => $new", CORE_LOG_WARNING, 'THEME');
if ( self::is_valid_variant_key($var, $new) ){
//errors::raise("Theme $theme, variant $var is updated and valid, storing value", CORE_LOG_WARNING, 'THEME');
self::$CC->store_updated($var.'_'.$theme);
} else {
unset($new);
}
self::$CC->del_updated($var.'_'.$theme);
} elseif ( self::$CC->is_stored($var.'_'.$theme) ){ //check storage
$new = self::$CC->get_stored($var.'_'.$theme);
if ( ! self::is_valid_variant_key($var, $new) ){
unset($new);self::$CC->del_stored($var.'_'.$theme);
}
}
if ( isset($new) ){ return $new; }
return FALSE;
}
private static function is_valid_variant_key($var, $key){
if ( $var = 'custum_css' ){ return TRUE; }
if ( $var = 'custum_img' ){ return TRUE; }
if ( $key = 'custum' ){ return configs::get('themes', 'themes', Array(self::$active, "custum_$var") ); }
return ( FALSE !== configs::get('themes', 'themes', Array(self::$active, 'vars', $var, $key)) );
}
public static function template_define(){
if ( FALSE === self::load() ){ return FALSE; }
$o['actif'] = self::$active;
$o['theme'] = self::getname();
$o['js'] = self::get_active_conf('js');
$o['img'] = self::get_active_conf('img');
$o['css'] = self::get_active_conf('css');
//maybe active theme got some vars too.
if ( method_exists(self::$THM, 'template_define') ){ $o = array_merge($o, self::$THM->template_define); }
return $o;
}
//CALLBACKS to active theme
public static function special_events(){
if ( FALSE === self::load() ){ return FALSE; }
return self::$THM->special_events();
}
public static function send_header($data=Array()){
if ( FALSE === self::load() ){ return FALSE; }
return self::$THM->send_header($data);
}
public static function send_content($c){
if ( FALSE === self::load() ){ return FALSE; }
return self::$THM->send_content($c);
}
public static function send_footer($data=Array()){
if ( FALSE === self::load() ){ return FALSE; }
return self::$THM->send_footer($data);
}
public static function get_features(){
if ( FALSE === self::load() ){ return FALSE; }
return self::$THM->getfeats();
}
}