Share Pastebin
Guest
Public paste!

yingyes

By: a guest | Apr 4th, 2008 | Syntax: None | Size: 9.56 KB | Hits: 240 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3.  
  4. ENVIRON VARIABLES LIBRARY for ME & Smarty Template System
  5. by
  6. Aleksandr Ilyich Goretoy aka RussianPimp YingYes
  7.  
  8. This class is very early beta and will be rigourously tested and modified
  9. You are allowed to use this code as you wish as long as this sig stays here. Please don't delete it.
  10. 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.
  11.  
  12. In your templates you can use the <!--{$query_count}--> variable to get the sql query count per page load
  13. my goal for this lib is to my everything dynamic with loading of variables from db and or flat file.
  14. VERSION 1.0.5
  15. */
  16. class Environ {
  17.         var $template_name;
  18.         var $templates_folder;
  19.         var $template;
  20.         var $template_root;
  21.         var $template_dir;
  22.         var $data=array();
  23.  
  24.         function __construct()
  25.         {
  26.                 $this->ci =& get_instance();
  27.                 $this->ci->load->helper('url');
  28.                 define('ENVIRONMENT_VERSION', '1.0.5');
  29.                 log_message('debug', "Environment Class Initialized : Environ " . ENVIRONMENT_VERSION);
  30.                 $this->init();
  31.         }
  32.         function init($environ=array())
  33.         {      
  34.                 // Load the required files for this lib to
  35.                 // work correctly.
  36.                 $this->ci->load->model('template_model');
  37.                 $this->ci->config->load('environ');
  38.                
  39.                 //get environment variables from config file
  40.                 $this->document_path = $_SERVER['DOCUMENT_ROOT'].$this->ci->config->item('base_dir').APPPATH.'views/';
  41.  
  42.                 $this->template_location = $this->ci->config->item('template_location');
  43.                
  44.                 if(strtolower($this->template_location)=='file')
  45.                 {              
  46.                         $this->template_name = $this->ci->config->item('template_name');
  47.                         $this->templates_folder=$this->ci->config->item('templates_folder');
  48.                         $this->theme_config_file=$this->ci->config->item('theme_config_file');
  49.                         $this->_set_template();
  50.                         //this line loads a config file from your templates/template/config/ directory
  51.                         $this->ci->load->config('../views/'.$this->template.'config/'.$this->theme_config_file,true,true);
  52.                         $this->_theme_config($this->ci->config->item('../views/'.$this->template.'config/'.$this->theme_config_file));
  53.                         $environ=$this->ci->config->item('load_templates');
  54.                 }
  55.                 else
  56.                 {
  57.                         $this->template_id = $this->ci->template_model->get_config_item_value('template_id');
  58.                         $this->template_name = $this->ci->template_model->get_config_item_value('template_name');
  59.                         $this->templates_folder = $this->ci->template_model->get_config_item_value('templates_folder');
  60.                         $environ =  explode(',',$this->ci->template_model->get_config_item_value('load_templates'));
  61.                         $this->_set_template();
  62.                 }
  63.                 $this->_template_asset_dirs($this->template_location);
  64.                 $this->_template_theme('css',$this->template_location);
  65.                 $this->_template_theme('js',$this->template_location);
  66.                
  67.                
  68.                 foreach($environ as $tk =>$tp)
  69.                 {
  70.                         $this->_template($this->template_location,trim($tp));
  71.                         $this->data=array_merge($this->data['template_'.trim($tp)],$this->data);
  72.                 }
  73.                 //print_r($this->data);
  74.         }
  75.  
  76.         function _template_asset_dirs($loc='file')
  77.         {
  78.                 if($loc=='db')
  79.                 {
  80.                         $assets=$this->ci->template_model->get_asset_dirs_by_template_id($this->template_id);
  81.                         foreach($assets->result() as $asset=>$a)
  82.                         {
  83.                                 $this->data['template_images_dir']=$this->template_dir.$a->images_dir;
  84.                                 $this->data['template_swf_dir']=$this->template_dir.$a->swf_dir;
  85.                                 $this->data['template_pdf_dir']=$this->template_dir.$a->pdf_dir;
  86.                                 $this->data['template_dir']=$this->template_dir;
  87.                                 $this->data['template_root']=$this->template_root;
  88.                         }              
  89.                 }
  90.                 else
  91.                 {
  92.                         $this->data['template_images_dir']=$this->template_dir.$this->data['template_images_dir'][0];
  93.                         $this->data['template_swf_dir']=$this->template_dir.$this->data['template_swf_dir'][0];
  94.                         $this->data['template_pdf_dir']=$this->template_dir.$this->data['template_pdf_dir'][0];
  95.                         $this->data['template_dir']=$this->template_dir;
  96.                         $this->data['template_root']=$this->template_root;
  97.                 }
  98.         }
  99.         //type can be either css or js and loc can be db or file
  100.         function _template_theme($type='css',$loc='file')
  101.         {
  102.                 $func='link'.strtoupper($type);
  103.                 if($loc=='db')
  104.                 {                      
  105.                         $vart='theme_'.strtolower($type);
  106.                         $varg='get_'.$vart;
  107.                         $$vart=$this->ci->template_model->$varg($this->template_id);
  108.                         foreach($$vart->result() as $$type=>$c)
  109.                         {
  110.                                 $this->data['theme_'.$type]=$this->$func($this->template_dir.$c->url,'theme_'.$type,true);
  111.                         }
  112.                 }
  113.                 else
  114.                 {
  115.                         foreach($this->data['theme_'.$type] as $$type=>$c)
  116.                         {      
  117.                                 $this->data['theme_'.$type]=$this->$func($this->template_dir.$c,'theme_'.$type,true);
  118.                         }      
  119.                 }
  120.         }
  121.         function _navigation($nav)
  122.         {
  123.                
  124.                 if($nav->nav_link)
  125.                 $this->data['navigation'][$nav->orderby][$nav->name]=$nav->url;
  126.         }
  127.         function _template($loc='file',$string='layout',$type='template_id')
  128.         {
  129.                 if($loc=='db')
  130.                 {
  131.                         $func='get_'.$string.'_by_'.$type;
  132.                        
  133.                         $$string=$this->ci->template_model->$func($this->$type);
  134.                         foreach($$string->result() as $k=>$v)
  135.                         {
  136.                                 if($string=='pages')
  137.                                 {
  138.                                         if($v->status=='front_page')
  139.                                         {
  140.                                                 if(file_exists($this->template_root.$string.'/tpl_'.$v->url.EXT))
  141.                                                 if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v->url.EXT))
  142.                                                 {
  143.                                                         $this->data['template_'.$string]['index_tpl']='ci:'.$this->template.$string.'/tpl_'.$v->url.EXT;
  144.                                                         unset($this->data['template_'.$string][$v->url]);
  145.                                                 }
  146.                                         }
  147.                                         else
  148.                                         {
  149.                                                 $this->_navigation($v);
  150.                                         }      
  151.                                                                
  152.                                 }
  153.                                
  154.                                 if(file_exists($this->template_root.$string.'/tpl_'.$v->url.EXT))
  155.                                 if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v->url.EXT))
  156.                                 {
  157.                                         $this->data['template_'.$string][$v->url.'_tpl']='ci:'.$this->template.$string.'/tpl_'.$v->url.EXT;
  158.                                         unset($this->data['template_'.$string][$v->url]);
  159.                                 }
  160.                         }
  161.                 }
  162.                 else
  163.                 {
  164.                         foreach($this->data['template_'.$string] as $k=>$v)
  165.                         {
  166.                                 if($string=='pages')
  167.                                 if($k=='front_page')
  168.                                 {
  169.                                         if(file_exists($this->template_root.$string.'/tpl_'.$v.EXT))
  170.                                         if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v.EXT))
  171.                                         {
  172.                                                 $this->data['template_'.$string]['index_tpl']='ci:'.$this->template.$string.'/tpl_'.$v.EXT;
  173.                                                 unset($this->data['template_'.$string][$k]);
  174.                                         }
  175.                                 }                                      
  176.                                 if(file_exists($this->template_root.$string.'/tpl_'.$v.EXT))
  177.                                 if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v.EXT))
  178.                                 {
  179.                                         $this->data['template_'.$string][$v.'_tpl']='ci:'.$this->template.$string.'/tpl_'.$v.EXT;
  180.                                         unset($this->data['template_'.$string][$k]);
  181.                                 }
  182.                         }
  183.                        
  184.  
  185.                 }
  186.  
  187.         }
  188.  
  189.  
  190.  
  191.         //this function sets the template directory variable variants
  192.         function _set_template()
  193.         {
  194.                 if($this->template_name=='')
  195.                 {
  196.                         $this->template_name='default_template';
  197.                         $this->template='smarty/'.$this->template_name.'/';
  198.                         $this->template_root=$this->document_path.'smarty/'.$this->templates_folder.$this->template_name.'/';
  199.                         $this->template_dir=$this->template.'/';
  200.                 }
  201.                 else
  202.                 {
  203.                         $this->template='smarty/'.$this->templates_folder.$this->template_name.'/';
  204.                         $this->template_root=$this->document_path.'smarty/'.$this->templates_folder.$this->template_name.'/';
  205.                         $this->template_dir=site_url().$this->templates_folder.$this->template_name.'/';
  206.                 }
  207.         }
  208.         //when configuration is from flat file this function sets the template variables into data
  209.         function _theme_config($theme_config='')
  210.         {
  211.                 if(is_array($theme_config))
  212.                 {
  213.                         foreach($theme_config as $the =>$theme)
  214.                         {
  215.                                 if(is_array($theme))
  216.                                 {
  217.                                         foreach($theme as $var =>$val)
  218.                                         {
  219.                                                 $this->data[$val]=$the;
  220.                                         }
  221.                                 }
  222.                                 else
  223.                                 {
  224.                                         $this->data[$theme]=$the;
  225.                                 }
  226.                         }
  227.                 }
  228.                
  229.                 $this->data=$this->_array_invert($this->data);
  230.         }
  231.         //when config is from flat file this function inverts the data array
  232.         function _array_invert($arr)
  233.         {
  234.                 $res = Array();
  235.                 foreach(array_keys($arr) as $key)
  236.           {
  237.                   if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
  238.                 array_push($res[$arr[$key]], $key);
  239.           }
  240.           return $res;
  241.         }
  242.  
  243.         function linkCSS($href, $var = 'view_css',$return=false)
  244.         {
  245.                 if (!isset($this->vars[$var])) {
  246.                         $this->vars[$var] = "<!-- @{$var} -->\n\t";
  247.                 }
  248.                 if (is_array($href)) {
  249.                         foreach ($href as $v) {
  250.                                 $this->vars[$var] .= '<link type="text/css" rel="stylesheet" href="'.$v.'" />'."\n\t";
  251.                         }
  252.                 } else {
  253.                         $this->vars[$var] .= '<link type="text/css" rel="stylesheet" href="'.$href.'" />'."\n\t";
  254.                 }
  255.                 if($return)
  256.                 return $this->vars[$var];
  257.         }
  258.         function linkJS($src, $var = 'view_js',$return=false) {
  259.                 if (!isset($this->vars[$var])) {
  260.                         $this->vars[$var] = "<!-- @{$var} -->\n\t";
  261.                 }
  262.                 if (is_array($src)) {
  263.                         foreach ($src as $v) {
  264.                                 $this->vars[$var] .= '<script type="text/javascript" src="'.$v.'"></script>'."\n\t";
  265.                         }
  266.                 } else {
  267.                         $this->vars[$var] .= '<script type="text/javascript" src="'.$src.'"></script>'."\n\t";
  268.                 }
  269.                 if($return)
  270.                 return $this->vars[$var];
  271.         }
  272.  
  273.         function _symlink($target, $link) {
  274.                 return exec('ln -sf "' . $link . '" "' . $target . '"');
  275.         }
  276.         function _unlink($link) {
  277.                 unlink($link);
  278.         }
  279.         function page_hash( )
  280.         {
  281.                 // Hash mircotime for a unqiue value
  282.                 $hash = dohash( microtime() );
  283.  
  284.                 // Return the hashed password.
  285.                 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))))));
  286.         }
  287. }
  288.  
  289. ?>