PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/document/html/html.php

https://github.com/cosmocommerce/joomla
PHP | 427 lines | 257 code | 39 blank | 131 comment | 13 complexity | e0e7956130ded875aba11419992a8e2c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Document
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_BASE') or die;
  10. jimport('joomla.application.module.helper');
  11. /**
  12. * DocumentHTML class, provides an easy interface to parse and display an html document
  13. *
  14. * @package Joomla.Framework
  15. * @subpackage Document
  16. * @since 1.5
  17. */
  18. jimport('joomla.document.document');
  19. class JDocumentHTML extends JDocument
  20. {
  21. /**
  22. * Array of Header <link> tags
  23. *
  24. * @var array
  25. * @access private
  26. */
  27. var $_links = array();
  28. /**
  29. * Array of custom tags
  30. *
  31. * @var string
  32. * @access private
  33. */
  34. var $_custom = array();
  35. public $template = null;
  36. public $baseurl = null;
  37. public $params = null;
  38. public $_file = null;
  39. /**
  40. * String holding parsed template
  41. */
  42. protected $_template = '';
  43. /**
  44. * Array of parsed template JDoc tags
  45. */
  46. protected $_template_tags = array();
  47. /**
  48. * Class constructor
  49. *
  50. * @access public
  51. * @param array $options Associative array of options
  52. */
  53. function __construct($options = array())
  54. {
  55. parent::__construct($options);
  56. //set document type
  57. $this->_type = 'html';
  58. //set mime type
  59. $this->_mime = 'text/html';
  60. //set default document metadata
  61. $this->setMetaData('Content-Type', $this->_mime . '; charset=' . $this->_charset , true);
  62. $this->setMetaData('robots', 'index, follow');
  63. }
  64. /**
  65. * Get the html document head data
  66. *
  67. * @access public
  68. * @return array The document head data in array form
  69. */
  70. function getHeadData()
  71. {
  72. $data = array();
  73. $data['title'] = $this->title;
  74. $data['description']= $this->description;
  75. $data['link'] = $this->link;
  76. $data['metaTags'] = $this->_metaTags;
  77. $data['links'] = $this->_links;
  78. $data['styleSheets']= $this->_styleSheets;
  79. $data['style'] = $this->_style;
  80. $data['scripts'] = $this->_scripts;
  81. $data['script'] = $this->_script;
  82. $data['custom'] = $this->_custom;
  83. return $data;
  84. }
  85. /**
  86. * Set the html document head data
  87. *
  88. * @access public
  89. * @param array $data The document head data in array form
  90. */
  91. function setHeadData($data)
  92. {
  93. $this->title = (isset($data['title'])) ? $data['title'] : $this->title;
  94. $this->description = (isset($data['description'])) ? $data['description'] : $this->description;
  95. $this->link = (isset($data['link'])) ? $data['link'] : $this->link;
  96. $this->_metaTags = (isset($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
  97. $this->_links = (isset($data['links'])) ? $data['links'] : $this->_links;
  98. $this->_styleSheets = (isset($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
  99. $this->_style = (isset($data['style'])) ? $data['style'] : $this->_style;
  100. $this->_scripts = (isset($data['scripts'])) ? $data['scripts'] : $this->_scripts;
  101. $this->_script = (isset($data['script'])) ? $data['script'] : $this->_script;
  102. $this->_custom = (isset($data['custom'])) ? $data['custom'] : $this->_custom;
  103. }
  104. /**
  105. * Adds <link> tags to the head of the document
  106. *
  107. * <p>$relType defaults to 'rel' as it is the most common relation type used.
  108. * ('rev' refers to reverse relation, 'rel' indicates normal, forward relation.)
  109. * Typical tag: <link href="index.php" rel="Start"></p>
  110. *
  111. * @access public
  112. * @param string $href The link that is being related.
  113. * @param string $relation Relation of link.
  114. * @param string $relType Relation type attribute. Either rel or rev (default: 'rel').
  115. * @param array $attributes Associative array of remaining attributes.
  116. * @return void
  117. */
  118. function addHeadLink($href, $relation, $relType = 'rel', $attribs = array())
  119. {
  120. $attribs = JArrayHelper::toString($attribs);
  121. $generatedTag = '<link href="'.$href.'" '.$relType.'="'.$relation.'" '.$attribs;
  122. $this->_links[] = $generatedTag;
  123. }
  124. /**
  125. * Adds a shortcut icon (favicon)
  126. *
  127. * <p>This adds a link to the icon shown in the favorites list or on
  128. * the left of the url in the address bar. Some browsers display
  129. * it on the tab, as well.</p>
  130. *
  131. * @param string $href The link that is being related.
  132. * @param string $type File type
  133. * @param string $relation Relation of link
  134. * @access public
  135. */
  136. function addFavicon($href, $type = 'image/x-icon', $relation = 'shortcut icon')
  137. {
  138. $href = str_replace('\\', '/', $href);
  139. $this->_links[] = '<link href="'.$href.'" rel="'.$relation.'" type="'.$type.'"';
  140. }
  141. /**
  142. * Adds a custom html string to the head block
  143. *
  144. * @param string The html to add to the head
  145. * @access public
  146. * @return void
  147. */
  148. function addCustomTag($html)
  149. {
  150. $this->_custom[] = trim($html);
  151. }
  152. /**
  153. * Get the contents of a document include
  154. *
  155. * @access public
  156. * @param string $type The type of renderer
  157. * @param string $name The name of the element to render
  158. * @param array $attribs Associative array of remaining attributes.
  159. * @return The output of the renderer
  160. */
  161. function getBuffer($type = null, $name = null, $attribs = array())
  162. {
  163. // If no type is specified, return the whole buffer
  164. if ($type === null) {
  165. return $this->_buffer;
  166. }
  167. $result = null;
  168. if (isset($this->_buffer[$type][$name])) {
  169. return $this->_buffer[$type][$name];
  170. }
  171. // If the buffer has been explicitly turned off don't display or attempt to render
  172. if ($result === false) {
  173. return null;
  174. }
  175. $renderer = &$this->loadRenderer($type);
  176. $this->setBuffer($renderer->render($name, $attribs, $result), $type, $name);
  177. return $this->_buffer[$type][$name];
  178. }
  179. /**
  180. * Set the contents a document include
  181. *
  182. * @param string $content The content to be set in the buffer.
  183. * @param array $options Array of optional elements.
  184. */
  185. public function setBuffer($content, $options = array())
  186. {
  187. // The following code is just for backward compatibility.
  188. if (func_num_args() > 1 && !is_array($options)) {
  189. $args = func_get_args(); $options = array();
  190. $options['type'] = $args[1];
  191. $options['name'] = (isset($args[2])) ? $args[2] : null;
  192. }
  193. $this->_buffer[$options['type']][$options['name']] = $content;
  194. }
  195. /**
  196. * Parses the template and populates the buffer
  197. *
  198. * @access public
  199. * @param array parameters for fetching the template
  200. */
  201. function parse($params = array()) {
  202. $this->_fetchTemplate($params);
  203. $this->_parseTemplate();
  204. }
  205. /**
  206. * Outputs the template to the browser.
  207. *
  208. * @access public
  209. * @param boolean $cache If true, cache the output
  210. * @param array $params Associative array of attributes
  211. * @return The rendered data
  212. */
  213. function render($caching = false, $params = array())
  214. {
  215. if (!empty($this->_template)) {
  216. $data = $this->_renderTemplate();
  217. } else {
  218. $this->parse($params);
  219. $data = $this->_renderTemplate();
  220. }
  221. parent::render();
  222. return $data;
  223. }
  224. /**
  225. * Count the modules based on the given condition
  226. *
  227. * @access public
  228. * @param string $condition The condition to use
  229. * @return integer Number of modules found
  230. */
  231. function countModules($condition)
  232. {
  233. $result = '';
  234. $operators = '(\+|\-|\*|\/|==|\!=|\<\>|\<|\>|\<\=|\>\=|and|or|xor) ';
  235. $words = preg_split('# '.$operators.' #', $condition, null, PREG_SPLIT_DELIM_CAPTURE);
  236. for ($i = 0, $n = count($words); $i < $n; $i+=2)
  237. {
  238. // odd parts (modules)
  239. $name = strtolower($words[$i]);
  240. $words[$i] = ((isset($this->_buffer['modules'][$name])) && ($this->_buffer['modules'][$name] === false)) ? 0 : count(JModuleHelper::getModules($name));
  241. }
  242. $str = 'return '.implode(' ', $words).';';
  243. return eval($str);
  244. }
  245. /**
  246. * Count the number of child menu items
  247. *
  248. * @access public
  249. * @return integer Number of child menu items
  250. */
  251. function countMenuChildren() {
  252. static $children;
  253. if (!isset($children)) {
  254. $dbo = &JFactory::getDbo();
  255. $menu = &JSite::getMenu();
  256. $where = Array();
  257. $active = $menu->getActive();
  258. if ($active) {
  259. $where[] = 'parent = ' . $active->id;
  260. $where[] = 'published = 1';
  261. $dbo->setQuery('SELECT COUNT(*) FROM #__menu WHERE '. implode(' AND ', $where));
  262. $children = $dbo->loadResult();
  263. } else {
  264. $children = 0;
  265. }
  266. }
  267. return $children;
  268. }
  269. /**
  270. * Load a template file
  271. *
  272. * @param string $template The name of the template
  273. * @param string $filename The actual filename
  274. * @return string The contents of the template
  275. */
  276. function _loadTemplate($directory, $filename)
  277. {
  278. // $component = JApplicationHelper::getComponentName();
  279. $contents = '';
  280. //Check to see if we have a valid template file
  281. if (file_exists($directory.DS.$filename))
  282. {
  283. //store the file path
  284. $this->_file = $directory.DS.$filename;
  285. //get the file content
  286. ob_start();
  287. require_once $directory.DS.$filename;
  288. $contents = ob_get_contents();
  289. ob_end_clean();
  290. }
  291. // Try to find a favicon by checking the template and root folder
  292. $path = $directory . DS;
  293. $dirs = array($path, JPATH_BASE.DS);
  294. foreach ($dirs as $dir)
  295. {
  296. $icon = $dir.'favicon.ico';
  297. if (file_exists($icon))
  298. {
  299. $path = str_replace(JPATH_BASE . DS, '', $dir);
  300. $path = str_replace('\\', '/', $path);
  301. $this->addFavicon(JURI::base(true).'/'.$path.'favicon.ico');
  302. break;
  303. }
  304. }
  305. return $contents;
  306. }
  307. /**
  308. * Fetch the template, and initialise the params
  309. *
  310. * @param array parameters to determine the template
  311. */
  312. protected function _fetchTemplate($params = array())
  313. {
  314. // check
  315. $directory = isset($params['directory']) ? $params['directory'] : 'templates';
  316. $filter = JFilterInput::getInstance();
  317. $template = $filter->clean($params['template'], 'cmd');
  318. $file = $filter->clean($params['file'], 'cmd');
  319. if (!file_exists($directory.DS.$template.DS.$file)) {
  320. $template = 'system';
  321. }
  322. // Load the language file for the template
  323. $lang = &JFactory::getLanguage();
  324. // 1.5 or core then
  325. // 1.6
  326. $lang->load('tpl_'.$template, JPATH_BASE, null, false, false)
  327. || $lang->load('tpl_'.$template, $directory.DS.$template, null, false, false)
  328. || $lang->load('tpl_'.$template, JPATH_BASE, $lang->getDefault(), false, false)
  329. || $lang->load('tpl_'.$template, $directory.DS.$template, $lang->getDefault(), false, false);
  330. // Assign the variables
  331. $this->template = $template;
  332. $this->baseurl = JURI::base(true);
  333. $this->params = isset($params['params']) ? $params['params'] : new JParameter;
  334. // load
  335. $this->_template = $this->_loadTemplate($directory.DS.$template, $file);
  336. }
  337. /**
  338. * Parse a document template
  339. *
  340. * @access public
  341. * @return The parsed contents of the template
  342. */
  343. function _parseTemplate()
  344. {
  345. $replace = array();
  346. $matches = array();
  347. if (preg_match_all('#<jdoc:include\ type="([^"]+)" (.*)\/>#iU', $this->_template, $matches))
  348. {
  349. $matches[0] = array_reverse($matches[0]);
  350. $matches[1] = array_reverse($matches[1]);
  351. $matches[2] = array_reverse($matches[2]);
  352. $count = count($matches[1]);
  353. for ($i = 0; $i < $count; $i++)
  354. {
  355. $attribs = JUtility::parseAttributes($matches[2][$i]);
  356. $type = $matches[1][$i];
  357. $name = isset($attribs['name']) ? $attribs['name'] : null;
  358. $this->_template_tags[$matches[0][$i]] = array('type'=>$type, 'name' => $name, 'attribs' => $attribs);
  359. }
  360. }
  361. }
  362. /**
  363. * Render pre-parsed template
  364. *
  365. * @return string rendered template
  366. */
  367. protected function _renderTemplate() {
  368. $replace = array();
  369. $with = array();
  370. foreach($this->_template_tags AS $jdoc => $args) {
  371. $replace[] = $jdoc;
  372. $with[] = $this->getBuffer($args['type'], $args['name'], $args['attribs']);
  373. }
  374. return str_replace($replace, $with, $this->_template);
  375. }
  376. }