Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 11th, 2012  |  syntax: None  |  size: 5.69 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4.  * Loader Class
  5.  *
  6.  * Extends standart CI_Loader for loading
  7.  * joomla-like templates.
  8.  *
  9.  * @package             SimpleCMS
  10.  * @category    Loader
  11.  */
  12. class MY_Loader extends CI_Loader{
  13.  
  14.  
  15.     /**
  16.      * Template source code.
  17.      * @var bool|string
  18.      */
  19.     private $_template = FALSE;
  20.  
  21.     /**
  22.      * Content to load on main page.
  23.      * @var object|array|string
  24.      */
  25.     private $_content = '';
  26.  
  27.     /**
  28.      * Template file path in views directory.
  29.      * @var string
  30.      */
  31.     private $_file    = 'index.php';
  32.  
  33.     /**
  34.      * Setter for template file.
  35.      * @param $file
  36.      */
  37.     public function setFile($file)
  38.     {
  39.         $this->_file = $file;
  40.     }
  41.  
  42.     /**
  43.      * Getter for template file.
  44.      * @return string
  45.      */
  46.     public function getFile()
  47.     {
  48.         return $this->_file;
  49.     }
  50.  
  51.     /**
  52.      *  Main function. Output Generated code.
  53.      *
  54.      * @param bool|object|array $content
  55.      * @param bool $file
  56.      * @throws ErrorException
  57.      *
  58.      * return void
  59.      */
  60.     public function template($content = FALSE,  $file = FALSE){
  61.  
  62.  
  63.         $_CI = & get_instance();
  64.  
  65.         //set content
  66.         $content ? $this->_content = $content : $this->_content = $_CI->_page;
  67.         //set file if it's specified in params
  68.         $file && !empty($file) && $this->_file = $file;
  69.         //load the template as a CI view to allow usage of php variables in templates
  70.         $this->_template = $this->view($this->_file, (array)$this->_content ,TRUE);
  71.         //parse template, replace all additional content
  72.         $this->_parseTemplate($_CI);
  73.  
  74.         if (!empty($this->_template))
  75.             $_CI->output->append_output($this->_template);
  76.  
  77.     }
  78.  
  79.  
  80.     /**
  81.      * Function to load template file.
  82.      *
  83.      * @return bool|string
  84.      */
  85.     private function _loadTemplate()
  86.     {
  87.        $tmpl_file = $this->_file;
  88.  
  89.         !(substr($tmpl_file, 0, 1) == '/')       && $tmpl_file = '/'.$tmpl_file;
  90.         (count(explode('.', $tmpl_file)) == 1)   && $tmpl_file .= '.php';
  91.  
  92.         $tmpl_file = APPPATH.'views'.$tmpl_file;
  93.  
  94.         if (file_exists($tmpl_file)){
  95.             return file_get_contents($tmpl_file);
  96.         }
  97.         else
  98.             return FALSE;
  99.     }
  100.  
  101.     /**
  102.      * Function parses template, finds include tags and replace it
  103.      * with value passed with content object|array. In case value doesn't
  104.      * specified in content object|array then it passes tag attributes in
  105.      * template_handler function.
  106.      *
  107.      * For tag with attribute type witch equals to "content" in any case
  108.      * return property|value or $this->_content, according to $this->_content type.
  109.      *
  110.      * $matches[0][$i] - markup to replace;
  111.      * $matches[1][$i] - type of item;
  112.      * $matches[2][$i] - name of item;
  113.      *
  114.      * @param MY_Controller $_CI
  115.      */
  116.     private function _parseTemplate(MY_Controller $_CI)
  117.     {
  118.         $matches = array();
  119.  
  120.         if (preg_match_all('#<scms:include\ type="([^"]+)" (.*)\/>#iU', $this->_template, $matches))
  121.         {
  122.  
  123.             if (count($matches[0])>0)
  124.             {
  125.  
  126.                 for ($i=0; $i< count($matches[0]); $i++)
  127.                 {
  128.                     $attr = $this->_fetchAttributes($matches[2][$i]);
  129.  
  130.                     if (!empty($attr) && isset($attr['name'])){
  131.  
  132.                         $name = $attr['name'];
  133.                         unset($attr['name']);
  134.  
  135.                     }
  136.                     else $name = $matches[1][$i];
  137.  
  138.                     //if match exists in given scope pass use it, else load from module content
  139.                     //todo: fully replace usage of module_content function and use data objects instead
  140.                     //todo: check notice in line 112
  141.                     $subject = '';
  142.  
  143.                    switch ($this->_content)
  144.                    {
  145.                         case is_object($this->_content):
  146.                                 @property_exists($this->_content,$name)
  147.                                     ? $subject = $this->_content->$name
  148.                                     : $subject = $_CI->template_handler($matches[1][$i],$name,$attr);
  149.                             break;
  150.                         case is_array($this->_content):
  151.  
  152.                                 isset($this->_content[$name])
  153.                                     ? $subject = $this->_content[$name]
  154.                                     : $subject = $_CI->template_handler($matches[1][$i],$name,$attr);
  155.  
  156.                            break;
  157.                         default:
  158.                             $matches[1][$i] == 'content' && $subject = &$this->_content;
  159.                            break;
  160.  
  161.                    }
  162.  
  163.                     $this->_template  = str_replace($matches[0][$i], $subject, $this->_template);
  164.  
  165.                 }
  166.             }
  167.  
  168.         }
  169.         //var_dump($matches);
  170.     }
  171.  
  172.  
  173.     /**
  174.      * Get attributes string and parse it to array,
  175.      * where key - attribute name and value - attribute value.
  176.      *
  177.      * @param $attribute
  178.      * @return array|bool|null
  179.      */
  180.     private function _fetchAttributes($attribute)
  181.     {
  182.         if (!empty($attribute))
  183.         {
  184.             $arr = explode('=', $attribute);
  185.  
  186.             $keys   = array();
  187.             $values = array();
  188.  
  189.             for($i=0;$i<count($arr);$i++)
  190.             {
  191.                 if (count($spl = explode(' ', trim($arr[$i]))) > 0)
  192.                 {
  193.                     foreach ($spl as $str)
  194.                         substr($str, 0,1) == '"' ? $values[] = trim($str, '"')
  195.                             : $keys[] = $str;
  196.                 }
  197.  
  198.             }
  199.  
  200.             count($keys) == count($values) && count($values) != 0  ?
  201.                 $arr = array_combine($keys, $values) : $arr = null;
  202.  
  203.             return $arr;
  204.         }
  205.  
  206.         return FALSE;
  207.     }
  208.  
  209.  
  210.  
  211. }