- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /*
- ENVIRON VARIABLES LIBRARY for ME & Smarty Template System
- by
- Aleksandr Ilyich Goretoy aka RussianPimp YingYes
- This class is very early beta and will be rigourously tested and modified
- You are allowed to use this code as you wish as long as this sig stays here. Please don't delete it.
- Currently works for loading templates from config file located inside template dir config or from database that points to the templates located on the file system by passing the config file located inside the template altogether.
- In your templates you can use the <!--{$query_count}--> variable to get the sql query count per page load
- my goal for this lib is to my everything dynamic with loading of variables from db and or flat file.
- VERSION 1.0.5
- */
- class Environ {
- var $template_name;
- var $templates_folder;
- var $template;
- var $template_root;
- var $template_dir;
- var $data=array();
- function __construct()
- {
- $this->ci =& get_instance();
- $this->ci->load->helper('url');
- define('ENVIRONMENT_VERSION', '1.0.5');
- log_message('debug', "Environment Class Initialized : Environ " . ENVIRONMENT_VERSION);
- $this->init();
- }
- function init($environ=array())
- {
- // Load the required files for this lib to
- // work correctly.
- $this->ci->load->model('template_model');
- $this->ci->config->load('environ');
- //get environment variables from config file
- $this->document_path = $_SERVER['DOCUMENT_ROOT'].$this->ci->config->item('base_dir').APPPATH.'views/';
- $this->template_location = $this->ci->config->item('template_location');
- if(strtolower($this->template_location)=='file')
- {
- $this->template_name = $this->ci->config->item('template_name');
- $this->templates_folder=$this->ci->config->item('templates_folder');
- $this->theme_config_file=$this->ci->config->item('theme_config_file');
- $this->_set_template();
- //this line loads a config file from your templates/template/config/ directory
- $this->ci->load->config('../views/'.$this->template.'config/'.$this->theme_config_file,true,true);
- $this->_theme_config($this->ci->config->item('../views/'.$this->template.'config/'.$this->theme_config_file));
- $environ=$this->ci->config->item('load_templates');
- }
- else
- {
- $this->template_id = $this->ci->template_model->get_config_item_value('template_id');
- $this->template_name = $this->ci->template_model->get_config_item_value('template_name');
- $this->templates_folder = $this->ci->template_model->get_config_item_value('templates_folder');
- $environ = explode(',',$this->ci->template_model->get_config_item_value('load_templates'));
- $this->_set_template();
- }
- $this->_template_asset_dirs($this->template_location);
- $this->_template_theme('css',$this->template_location);
- $this->_template_theme('js',$this->template_location);
- foreach($environ as $tk =>$tp)
- {
- $this->_template($this->template_location,trim($tp));
- $this->data=array_merge($this->data['template_'.trim($tp)],$this->data);
- }
- //print_r($this->data);
- }
- function _template_asset_dirs($loc='file')
- {
- if($loc=='db')
- {
- $assets=$this->ci->template_model->get_asset_dirs_by_template_id($this->template_id);
- foreach($assets->result() as $asset=>$a)
- {
- $this->data['template_images_dir']=$this->template_dir.$a->images_dir;
- $this->data['template_swf_dir']=$this->template_dir.$a->swf_dir;
- $this->data['template_pdf_dir']=$this->template_dir.$a->pdf_dir;
- $this->data['template_dir']=$this->template_dir;
- $this->data['template_root']=$this->template_root;
- }
- }
- else
- {
- $this->data['template_images_dir']=$this->template_dir.$this->data['template_images_dir'][0];
- $this->data['template_swf_dir']=$this->template_dir.$this->data['template_swf_dir'][0];
- $this->data['template_pdf_dir']=$this->template_dir.$this->data['template_pdf_dir'][0];
- $this->data['template_dir']=$this->template_dir;
- $this->data['template_root']=$this->template_root;
- }
- }
- //type can be either css or js and loc can be db or file
- function _template_theme($type='css',$loc='file')
- {
- $func='link'.strtoupper($type);
- if($loc=='db')
- {
- $vart='theme_'.strtolower($type);
- $varg='get_'.$vart;
- $$vart=$this->ci->template_model->$varg($this->template_id);
- foreach($$vart->result() as $$type=>$c)
- {
- $this->data['theme_'.$type]=$this->$func($this->template_dir.$c->url,'theme_'.$type,true);
- }
- }
- else
- {
- foreach($this->data['theme_'.$type] as $$type=>$c)
- {
- $this->data['theme_'.$type]=$this->$func($this->template_dir.$c,'theme_'.$type,true);
- }
- }
- }
- function _navigation($nav)
- {
- if($nav->nav_link)
- $this->data['navigation'][$nav->orderby][$nav->name]=$nav->url;
- }
- function _template($loc='file',$string='layout',$type='template_id')
- {
- if($loc=='db')
- {
- $func='get_'.$string.'_by_'.$type;
- $$string=$this->ci->template_model->$func($this->$type);
- foreach($$string->result() as $k=>$v)
- {
- if($string=='pages')
- {
- if($v->status=='front_page')
- {
- if(file_exists($this->template_root.$string.'/tpl_'.$v->url.EXT))
- if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v->url.EXT))
- {
- $this->data['template_'.$string]['index_tpl']='ci:'.$this->template.$string.'/tpl_'.$v->url.EXT;
- unset($this->data['template_'.$string][$v->url]);
- }
- }
- else
- {
- $this->_navigation($v);
- }
- }
- if(file_exists($this->template_root.$string.'/tpl_'.$v->url.EXT))
- if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v->url.EXT))
- {
- $this->data['template_'.$string][$v->url.'_tpl']='ci:'.$this->template.$string.'/tpl_'.$v->url.EXT;
- unset($this->data['template_'.$string][$v->url]);
- }
- }
- }
- else
- {
- foreach($this->data['template_'.$string] as $k=>$v)
- {
- if($string=='pages')
- if($k=='front_page')
- {
- if(file_exists($this->template_root.$string.'/tpl_'.$v.EXT))
- if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v.EXT))
- {
- $this->data['template_'.$string]['index_tpl']='ci:'.$this->template.$string.'/tpl_'.$v.EXT;
- unset($this->data['template_'.$string][$k]);
- }
- }
- if(file_exists($this->template_root.$string.'/tpl_'.$v.EXT))
- if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v.EXT))
- {
- $this->data['template_'.$string][$v.'_tpl']='ci:'.$this->template.$string.'/tpl_'.$v.EXT;
- unset($this->data['template_'.$string][$k]);
- }
- }
- }
- }
- //this function sets the template directory variable variants
- function _set_template()
- {
- if($this->template_name=='')
- {
- $this->template_name='default_template';
- $this->template='smarty/'.$this->template_name.'/';
- $this->template_root=$this->document_path.'smarty/'.$this->templates_folder.$this->template_name.'/';
- $this->template_dir=$this->template.'/';
- }
- else
- {
- $this->template='smarty/'.$this->templates_folder.$this->template_name.'/';
- $this->template_root=$this->document_path.'smarty/'.$this->templates_folder.$this->template_name.'/';
- $this->template_dir=site_url().$this->templates_folder.$this->template_name.'/';
- }
- }
- //when configuration is from flat file this function sets the template variables into data
- function _theme_config($theme_config='')
- {
- if(is_array($theme_config))
- {
- foreach($theme_config as $the =>$theme)
- {
- if(is_array($theme))
- {
- foreach($theme as $var =>$val)
- {
- $this->data[$val]=$the;
- }
- }
- else
- {
- $this->data[$theme]=$the;
- }
- }
- }
- $this->data=$this->_array_invert($this->data);
- }
- //when config is from flat file this function inverts the data array
- function _array_invert($arr)
- {
- $res = Array();
- foreach(array_keys($arr) as $key)
- {
- if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
- array_push($res[$arr[$key]], $key);
- }
- return $res;
- }
- function linkCSS($href, $var = 'view_css',$return=false)
- {
- if (!isset($this->vars[$var])) {
- $this->vars[$var] = "<!-- @{$var} -->\n\t";
- }
- if (is_array($href)) {
- foreach ($href as $v) {
- $this->vars[$var] .= '<link type="text/css" rel="stylesheet" href="'.$v.'" />'."\n\t";
- }
- } else {
- $this->vars[$var] .= '<link type="text/css" rel="stylesheet" href="'.$href.'" />'."\n\t";
- }
- if($return)
- return $this->vars[$var];
- }
- function linkJS($src, $var = 'view_js',$return=false) {
- if (!isset($this->vars[$var])) {
- $this->vars[$var] = "<!-- @{$var} -->\n\t";
- }
- if (is_array($src)) {
- foreach ($src as $v) {
- $this->vars[$var] .= '<script type="text/javascript" src="'.$v.'"></script>'."\n\t";
- }
- } else {
- $this->vars[$var] .= '<script type="text/javascript" src="'.$src.'"></script>'."\n\t";
- }
- if($return)
- return $this->vars[$var];
- }
- function _symlink($target, $link) {
- return exec('ln -sf "' . $link . '" "' . $target . '"');
- }
- function _unlink($link) {
- unlink($link);
- }
- function page_hash( )
- {
- // Hash mircotime for a unqiue value
- $hash = dohash( microtime() );
- // Return the hashed password.
- return dohash(dohash($this->ci->config->item('encryption_key').$hash.time().dohash(time().dohash(__CLASS__.rand(0,666).dohash($this->ci->config->item('encryption_key').time().$hash.__FUNCTION__).dohash(rand(0,9).dohash($hash.microtime()+4))))));
- }
- }
- ?>
