PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

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

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