10 defined(
'JPATH_PLATFORM') or die;
12 jimport('joomla.utilities.utility');
29 public $_links = array();
37 public $_custom = array();
45 public $template = null;
53 public $baseurl = null;
61 public $params = null;
77 protected $_template =
'';
85 protected $_template_tags = array();
93 protected $_caching = null;
101 private $_html5 = null;
110 public function __construct($options = array())
112 parent::__construct($options);
115 $this->_type =
'html';
118 $this->setMimeEncoding(
'text/html');
128 public function getHeadData()
131 $data[
'title'] = $this->title;
132 $data[
'description'] = $this->description;
133 $data[
'link'] = $this->link;
134 $data[
'metaTags'] = $this->_metaTags;
135 $data[
'links'] = $this->_links;
136 $data[
'styleSheets'] = $this->_styleSheets;
137 $data[
'style'] = $this->_style;
138 $data[
'scripts'] = $this->_scripts;
139 $data[
'script'] = $this->_script;
140 $data[
'custom'] = $this->_custom;
154 public function setHeadData($data)
156 if (empty($data) || !is_array($data))
161 $this->title = (isset($data[
'title']) && !empty($data[
'title'])) ? $data[
'title'] : $this->title;
162 $this->description = (isset($data[
'description']) && !empty($data[
'description'])) ? $data[
'description'] : $this->description;
163 $this->link = (isset($data[
'link']) && !empty($data[
'link'])) ? $data[
'link'] : $this->link;
164 $this->_metaTags = (isset($data[
'metaTags']) && !empty($data[
'metaTags'])) ? $data[
'metaTags'] : $this->_metaTags;
165 $this->_links = (isset($data[
'links']) && !empty($data[
'links'])) ? $data[
'links'] : $this->_links;
166 $this->_styleSheets = (isset($data[
'styleSheets']) && !empty($data[
'styleSheets'])) ? $data[
'styleSheets'] : $this->_styleSheets;
167 $this->_style = (isset($data[
'style']) && !empty($data[
'style'])) ? $data[
'style'] : $this->_style;
168 $this->_scripts = (isset($data[
'scripts']) && !empty($data[
'scripts'])) ? $data[
'scripts'] : $this->_scripts;
169 $this->_script = (isset($data[
'script']) && !empty($data[
'script'])) ? $data[
'script'] : $this->_script;
170 $this->_custom = (isset($data[
'custom']) && !empty($data[
'custom'])) ? $data[
'custom'] : $this->_custom;
172 if (isset($data[
'scriptText']) && !empty($data[
'scriptText']))
174 foreach ($data[
'scriptText'] as $key => $string)
192 public function mergeHeadData($data)
195 if (empty($data) || !is_array($data))
200 $this->title = (isset($data[
'title']) && !empty($data[
'title']) && !stristr($this->title, $data[
'title']))
201 ? $this->title . $data[
'title']
203 $this->description = (isset($data[
'description']) && !empty($data[
'description']) && !stristr($this->description, $data[
'description']))
204 ? $this->description . $data[
'description']
205 : $this->description;
206 $this->link = (isset($data[
'link'])) ? $data[
'link'] : $this->link;
208 if (isset($data[
'metaTags']))
210 foreach ($data[
'metaTags'] as $type1 => $data1)
212 $booldog = $type1 ==
'http-equiv' ?
true :
false;
213 foreach ($data1 as $name2 => $data2)
215 $this->setMetaData($name2, $data2, $booldog);
220 $this->_links = (isset($data[
'links']) && !empty($data[
'links']) && is_array($data[
'links']))
221 ? array_unique(array_merge($this->_links, $data[
'links']))
223 $this->_styleSheets = (isset($data[
'styleSheets']) && !empty($data[
'styleSheets']) && is_array($data[
'styleSheets']))
224 ? array_merge($this->_styleSheets, $data[
'styleSheets'])
225 : $this->_styleSheets;
227 if (isset($data[
'style']))
229 foreach ($data[
'style'] as $type => $stdata)
231 if (!isset($this->_style[strtolower($type)]) || !stristr($this->_style[strtolower($type)], $stdata))
233 $this->addStyleDeclaration($stdata, $type);
238 $this->_scripts = (isset($data[
'scripts']) && !empty($data[
'scripts']) && is_array($data[
'scripts']))
239 ? array_merge($this->_scripts, $data[
'scripts'])
242 if (isset($data[
'script']))
244 foreach ($data[
'script'] as $type => $sdata)
246 if (!isset($this->_script[strtolower($type)]) || !stristr($this->_script[strtolower($type)], $sdata))
248 $this->addScriptDeclaration($sdata, $type);
253 $this->_custom = (isset($data[
'custom']) && !empty($data[
'custom']) && is_array($data[
'custom']))
254 ? array_unique(array_merge($this->_custom, $data[
'custom']))
276 public function addHeadLink($href, $relation, $relType =
'rel', $attribs = array())
278 $this->_links[$href][
'relation'] = $relation;
279 $this->_links[$href][
'relType'] = $relType;
280 $this->_links[$href][
'attribs'] = $attribs;
300 public function addFavicon($href, $type =
'image/vnd.microsoft.icon', $relation =
'shortcut icon')
302 $href = str_replace(
'\\',
'/', $href);
303 $this->addHeadLink($href, $relation,
'rel', array(
'type' => $type));
317 public function addCustomTag($html)
319 $this->_custom[] = trim($html);
331 public function isHtml5()
333 return $this->_html5;
345 public function setHtml5($state)
349 $this->_html5 = $state;
364 public function getBuffer($type = null, $name = null, $attribs = array())
369 return parent::$_buffer;
372 $title = (isset($attribs[
'title'])) ? $attribs[
'title'] : null;
373 if (isset(parent::$_buffer[$type][$name][$title]))
375 return parent::$_buffer[$type][$name][$title];
378 $renderer = $this->loadRenderer($type);
379 if ($this->_caching ==
true && $type ==
'modules')
382 $hash = md5(serialize(array($name, $attribs, null, $renderer)));
383 $cbuffer = $cache->get(
'cbuffer_' . $type);
385 if (isset($cbuffer[$hash]))
392 $options[
'nopathway'] = 1;
393 $options[
'nomodules'] = 1;
394 $options[
'modulemode'] = 1;
396 $this->setBuffer($renderer->render($name, $attribs, null), $type, $name);
397 $data = parent::$_buffer[$type][$name][$title];
401 $cbuffer[$hash] = $tmpdata;
403 $cache->store($cbuffer,
'cbuffer_' . $type);
408 $this->setBuffer($renderer->render($name, $attribs, null), $type, $name, $title);
411 return parent::$_buffer[$type][$name][$title];
424 public function setBuffer($content, $options = array())
427 if (func_num_args() > 1 && !is_array($options))
429 $args = func_get_args();
431 $options[
'type'] = $args[1];
432 $options[
'name'] = (isset($args[2])) ? $args[2] : null;
433 $options[
'title'] = (isset($args[3])) ? $args[3] : null;
436 parent::$_buffer[$options[
'type']][$options[
'name']][$options[
'title']] = $content;
450 public function parse($params = array())
452 return $this->_fetchTemplate($params)->_parseTemplate();
465 public function render($caching =
false, $params = array())
467 $this->_caching = $caching;
469 if (!empty($this->_template))
471 $data = $this->_renderTemplate();
475 $this->parse($params);
476 $data = $this->_renderTemplate();
492 public function countModules($condition)
494 $operators =
'(\+|\-|\*|\/|==|\!=|<>|<|>|<=|>=|and|or|xor)';
495 $words = preg_split(
'# ' . $operators .
' #', $condition, null, PREG_SPLIT_DELIM_CAPTURE);
496 for ($i = 0, $n = count($words); $i < $n; $i += 2)
499 $name = strtolower($words[$i]);
500 $words[$i] = ((isset(parent::$_buffer[
'modules'][$name])) && (parent::$_buffer[
'modules'][$name] ===
false))
502 : count(JModuleHelper::getModules($name));
505 $str =
'return ' . implode(
' ', $words) .
';';
517 public function countMenuChildren()
521 if (!isset($children))
525 $menu = $app->getMenu();
526 $active = $menu->getActive();
529 $query = $db->getQuery(
true)
532 ->where(
'parent_id = ' . $active->id)
533 ->where(
'published = 1');
534 $db->setQuery($query);
535 $children = $db->loadResult();
556 protected function _loadTemplate($directory, $filename)
563 if (file_exists($directory .
'/' . $filename))
566 $this->_file = $directory .
'/' . $filename;
570 require $directory .
'/' . $filename;
571 $contents = ob_get_contents();
576 $path = $directory .
'/';
577 $dirs = array($path, JPATH_BASE .
'/');
578 foreach ($dirs as $dir)
580 $icon = $dir .
'favicon.ico';
581 if (file_exists($icon))
583 $path = str_replace(JPATH_BASE .
'/',
'', $dir);
584 $path = str_replace(
'\\',
'/', $path);
585 $this->addFavicon(
JUri::base(
true) .
'/' . $path .
'favicon.ico');
602 protected function _fetchTemplate($params = array())
605 $directory = isset($params[
'directory']) ? $params[
'directory'] :
'templates';
607 $template = $filter->clean($params[
'template'],
'cmd');
608 $file = $filter->clean($params[
'file'],
'cmd');
610 if (!file_exists($directory .
'/' . $template .
'/' . $file))
612 $template =
'system';
619 $lang->load(
'tpl_' . $template, JPATH_BASE, null,
false,
false)
620 || $lang->load(
'tpl_' . $template, $directory .
'/' . $template, null,
false,
false)
621 || $lang->load(
'tpl_' . $template, JPATH_BASE, $lang->getDefault(),
false,
false)
622 || $lang->load(
'tpl_' . $template, $directory .
'/' . $template, $lang->getDefault(),
false,
false);
625 $this->
template = $template;
627 $this->params = isset($params[
'params']) ? $params[
'params'] :
new JRegistry;
630 $this->_template = $this->_loadTemplate($directory .
'/' . $template, $file);
642 protected function _parseTemplate()
646 if (preg_match_all(
'#<jdoc:include\ type="([^"]+)"(.*)\/>#iU', $this->_template, $matches))
648 $template_tags_first = array();
649 $template_tags_last = array();
652 for ($i = count($matches[0]) - 1; $i >= 0; $i--)
654 $type = $matches[1][$i];
656 $name = isset($attribs[
'name']) ? $attribs[
'name'] : null;
659 if ($type ==
'module' || $type ==
'modules')
661 $template_tags_first[$matches[0][$i]] = array(
'type' => $type,
'name' => $name,
'attribs' => $attribs);
665 $template_tags_last[$matches[0][$i]] = array(
'type' => $type,
'name' => $name,
'attribs' => $attribs);
669 $template_tags_last = array_reverse($template_tags_last);
671 $this->_template_tags = $template_tags_first + $template_tags_last;
684 protected function _renderTemplate()
689 foreach ($this->_template_tags as $jdoc => $args)
692 $with[] = $this->getBuffer($args[
'type'], $args[
'name'], $args[
'attribs']);
695 return str_replace($replace, $with, $this->_template);