- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Loader Class
- *
- * Extends standart CI_Loader for loading
- * joomla-like templates.
- *
- * @package SimpleCMS
- * @category Loader
- */
- class MY_Loader extends CI_Loader{
- /**
- * Template source code.
- * @var bool|string
- */
- private $_template = FALSE;
- /**
- * Content to load on main page.
- * @var object|array|string
- */
- private $_content = '';
- /**
- * Template file path in views directory.
- * @var string
- */
- private $_file = 'index.php';
- /**
- * Setter for template file.
- * @param $file
- */
- public function setFile($file)
- {
- $this->_file = $file;
- }
- /**
- * Getter for template file.
- * @return string
- */
- public function getFile()
- {
- return $this->_file;
- }
- /**
- * Main function. Output Generated code.
- *
- * @param bool|object|array $content
- * @param bool $file
- * @throws ErrorException
- *
- * return void
- */
- public function template($content = FALSE, $file = FALSE){
- $_CI = & get_instance();
- //set content
- $content ? $this->_content = $content : $this->_content = $_CI->_page;
- //set file if it's specified in params
- $file && !empty($file) && $this->_file = $file;
- //load the template as a CI view to allow usage of php variables in templates
- $this->_template = $this->view($this->_file, (array)$this->_content ,TRUE);
- //parse template, replace all additional content
- $this->_parseTemplate($_CI);
- if (!empty($this->_template))
- $_CI->output->append_output($this->_template);
- }
- /**
- * Function to load template file.
- *
- * @return bool|string
- */
- private function _loadTemplate()
- {
- $tmpl_file = $this->_file;
- !(substr($tmpl_file, 0, 1) == '/') && $tmpl_file = '/'.$tmpl_file;
- (count(explode('.', $tmpl_file)) == 1) && $tmpl_file .= '.php';
- $tmpl_file = APPPATH.'views'.$tmpl_file;
- if (file_exists($tmpl_file)){
- return file_get_contents($tmpl_file);
- }
- else
- return FALSE;
- }
- /**
- * Function parses template, finds include tags and replace it
- * with value passed with content object|array. In case value doesn't
- * specified in content object|array then it passes tag attributes in
- * template_handler function.
- *
- * For tag with attribute type witch equals to "content" in any case
- * return property|value or $this->_content, according to $this->_content type.
- *
- * $matches[0][$i] - markup to replace;
- * $matches[1][$i] - type of item;
- * $matches[2][$i] - name of item;
- *
- * @param MY_Controller $_CI
- */
- private function _parseTemplate(MY_Controller $_CI)
- {
- $matches = array();
- if (preg_match_all('#<scms:include\ type="([^"]+)" (.*)\/>#iU', $this->_template, $matches))
- {
- if (count($matches[0])>0)
- {
- for ($i=0; $i< count($matches[0]); $i++)
- {
- $attr = $this->_fetchAttributes($matches[2][$i]);
- if (!empty($attr) && isset($attr['name'])){
- $name = $attr['name'];
- unset($attr['name']);
- }
- else $name = $matches[1][$i];
- //if match exists in given scope pass use it, else load from module content
- //todo: fully replace usage of module_content function and use data objects instead
- //todo: check notice in line 112
- $subject = '';
- switch ($this->_content)
- {
- case is_object($this->_content):
- @property_exists($this->_content,$name)
- ? $subject = $this->_content->$name
- : $subject = $_CI->template_handler($matches[1][$i],$name,$attr);
- break;
- case is_array($this->_content):
- isset($this->_content[$name])
- ? $subject = $this->_content[$name]
- : $subject = $_CI->template_handler($matches[1][$i],$name,$attr);
- break;
- default:
- $matches[1][$i] == 'content' && $subject = &$this->_content;
- break;
- }
- $this->_template = str_replace($matches[0][$i], $subject, $this->_template);
- }
- }
- }
- //var_dump($matches);
- }
- /**
- * Get attributes string and parse it to array,
- * where key - attribute name and value - attribute value.
- *
- * @param $attribute
- * @return array|bool|null
- */
- private function _fetchAttributes($attribute)
- {
- if (!empty($attribute))
- {
- $arr = explode('=', $attribute);
- $keys = array();
- $values = array();
- for($i=0;$i<count($arr);$i++)
- {
- if (count($spl = explode(' ', trim($arr[$i]))) > 0)
- {
- foreach ($spl as $str)
- substr($str, 0,1) == '"' ? $values[] = trim($str, '"')
- : $keys[] = $str;
- }
- }
- count($keys) == count($values) && count($values) != 0 ?
- $arr = array_combine($keys, $values) : $arr = null;
- return $arr;
- }
- return FALSE;
- }
- }