PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/html.php

https://github.com/cosmocommerce/joomla
PHP | 540 lines | 299 code | 64 blank | 177 comment | 47 complexity | da3755461490dcc6a1c120151e439461 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage HTML
  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. JHtml::addIncludePath(JPATH_LIBRARIES.DS.'joomla'.DS.'html'.DS.'html');
  10. /**
  11. * Utility class for all HTML drawing classes
  12. *
  13. * @static
  14. * @package Joomla.Framework
  15. * @subpackage HTML
  16. * @since 1.5
  17. */
  18. abstract class JHtml
  19. {
  20. /**
  21. * Option values related to the generation of HTML output. Recognized
  22. * options are:
  23. * <ul><li>fmtDepth, integer. The current indent depth.
  24. * </li><li>fmtEol, string. The end of line string, default is linefeed.
  25. * </li><li>fmtIndent, string. The string to use for indentation, default is
  26. * tab.
  27. * </ul>
  28. *
  29. * @var array
  30. */
  31. static $formatOptions = array(
  32. 'format.depth' => 0,
  33. 'format.eol' => "\n",
  34. 'format.indent' => "\t"
  35. );
  36. private static $includePaths = array();
  37. /**
  38. * An array to hold method references
  39. *
  40. * @var array
  41. */
  42. private static $registry = array();
  43. /**
  44. * Class loader method
  45. *
  46. * Additional arguments may be supplied and are passed to the sub-class.
  47. * Additional include paths are also able to be specified for third-party use
  48. *
  49. * @param string The name of helper method to load, (prefix).(class).function
  50. * prefix and class are optional and can be used to load custom
  51. * html helpers.
  52. */
  53. public static function _($type)
  54. {
  55. $type = preg_replace('#[^A-Z0-9_\.]#i', '', $type);
  56. // Check to see if we need to load a helper file
  57. $parts = explode('.', $type);
  58. $prefix = (count($parts) == 3 ? array_shift($parts) : 'JHtml');
  59. $file = (count($parts) == 2 ? array_shift($parts) : '');
  60. $func = array_shift($parts);
  61. $key = strtolower($prefix.'.'.$file.'.'.$func);
  62. if (array_key_exists($key, self::$registry))
  63. {
  64. $function = self::$registry[$key];
  65. $args = func_get_args();
  66. // remove function name from arguments
  67. array_shift($args);
  68. return JHtml::call($function, $args);
  69. }
  70. $className = $prefix.ucfirst($file);
  71. if (!class_exists($className))
  72. {
  73. jimport('joomla.filesystem.path');
  74. if ($path = JPath::find(JHtml::$includePaths, strtolower($file).'.php'))
  75. {
  76. require_once $path;
  77. if (!class_exists($className))
  78. {
  79. JError::raiseError(500, $className.'::' .$func. ' not found in file.');
  80. return false;
  81. }
  82. }
  83. else
  84. {
  85. JError::raiseError(500, $prefix.$file . ' not supported. File not found.');
  86. return false;
  87. }
  88. }
  89. $toCall = array($className, $func);
  90. if (is_callable($toCall))
  91. {
  92. JHtml::register($key, $toCall);
  93. $args = func_get_args();
  94. // remove function name from arguments
  95. array_shift($args);
  96. return JHtml::call($toCall, $args);
  97. }
  98. else
  99. {
  100. JError::raiseError(500, $className.'::'.$func.' not supported.');
  101. return false;
  102. }
  103. }
  104. /**
  105. * Registers a function to be called with a specific key
  106. *
  107. * @param string The name of the key
  108. * @param string Function or method
  109. */
  110. public static function register($key, $function)
  111. {
  112. $parts = explode('.', $key);
  113. $prefix = (count($parts) == 3 ? array_shift($parts) : 'JHtml');
  114. $file = (count($parts) == 2 ? array_shift($parts) : '');
  115. $func = array_shift($parts);
  116. $key = strtolower($prefix.'.'.$file.'.'.$func);
  117. if (is_callable($function))
  118. {
  119. self::$registry[$key] = $function;
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Removes a key for a method from registry.
  126. *
  127. * @param string The name of the key
  128. */
  129. public static function unregister($key)
  130. {
  131. $key = strtolower($key);
  132. if (isset(self::$registry[$key])) {
  133. unset(self::$registry[$key]);
  134. return true;
  135. }
  136. return false;
  137. }
  138. /**
  139. * Function caller method
  140. *
  141. * @param string Function or method to call
  142. * @param array Arguments to be passed to function
  143. */
  144. private static function call($function, $args)
  145. {
  146. if (is_callable($function))
  147. {
  148. // PHP 5.3 workaround
  149. $temp = array();
  150. foreach ($args AS &$arg) {
  151. $temp[] = &$arg;
  152. }
  153. return call_user_func_array($function, $temp);
  154. }
  155. else {
  156. JError::raiseError(500, 'Function not supported.');
  157. return false;
  158. }
  159. }
  160. /**
  161. * Write a <a></a> element
  162. *
  163. * @access public
  164. * @param string The relative URL to use for the href attribute
  165. * @param string The target attribute to use
  166. * @param array An associative array of attributes to add
  167. * @since 1.5
  168. */
  169. public static function link($url, $text, $attribs = null)
  170. {
  171. if (is_array($attribs)) {
  172. $attribs = JArrayHelper::toString($attribs);
  173. }
  174. return '<a href="'.$url.'" '.$attribs.'>'.$text.'</a>';
  175. }
  176. /**
  177. * Write a <iframe></iframe> element
  178. *
  179. * @access public
  180. * @param string The relative URL to use for the src attribute
  181. * @param string The target attribute to use
  182. * @param array An associative array of attributes to add
  183. * @param string The message to display if the iframe tag is not supported
  184. * @since 1.5
  185. */
  186. public static function iframe($url, $name, $attribs = null, $noFrames = '')
  187. {
  188. if (is_array($attribs)) {
  189. $attribs = JArrayHelper::toString($attribs);
  190. }
  191. return '<iframe src="'.$url.'" '.$attribs.' name="'.$name.'">'.$noFrames.'</iframe>';
  192. }
  193. /**
  194. * Write a <img></img> element
  195. *
  196. * @access public
  197. * @param string The relative or absolute URL to use for the src attribute
  198. * @param string The target attribute to use
  199. * @param array An associative array of attributes to add
  200. * @param boolean If set to true, it tries to find an override for the file in the template
  201. * @since 1.5
  202. */
  203. public static function image($url, $alt, $attribs = null, $relative = false, $path_only = false)
  204. {
  205. if (is_array($attribs)) {
  206. $attribs = JArrayHelper::toString($attribs);
  207. }
  208. if($relative)
  209. {
  210. $app = JFactory::getApplication();
  211. $cur_template = $app->getTemplate();
  212. if (file_exists(JPATH_THEMES .'/'. $cur_template .'/images/'. $url)) {
  213. $url = JURI::base(true).'/templates/'. $cur_template .'/images/'. $url;
  214. } else {
  215. list($extension, $url) = explode('/', $url, 2);
  216. $url = JURI::root(true).'/media/'.$extension.'/images/'.$url;
  217. }
  218. if($path_only)
  219. {
  220. return $url;
  221. }
  222. } elseif (strpos($url, 'http') !== 0) {
  223. $url = JURI::root(true).'/'.$url;
  224. }
  225. return '<img src="'.$url.'" alt="'.$alt.'" '.$attribs.' />';
  226. }
  227. /**
  228. * Write a <link rel="stylesheet" style="text/css" /> element
  229. *
  230. * @param string path to file
  231. * @param array attributes to be added to the stylesheet
  232. * @param boolean path to file is relative to /media folder
  233. * @param boolean return the path to the file only
  234. * @since 1.6
  235. */
  236. public static function stylesheet($file, $attribs = array(), $relative = false, $path_only = false)
  237. {
  238. if (is_array($attribs)) {
  239. $attribs = JArrayHelper::toString($attribs);
  240. }
  241. if($relative)
  242. {
  243. $app = JFactory::getApplication();
  244. $cur_template = $app->getTemplate();
  245. if (file_exists(JPATH_THEMES .'/'. $cur_template .'/css/'. $file)) {
  246. $file = JURI::base(true).'/templates/'. $cur_template .'/css/'. $file;
  247. } else {
  248. list($extension, $file) = explode('/', $file, 2);
  249. $file = JURI::root(true).'/media/'.$extension.'/css/'.$file;
  250. }
  251. if($path_only)
  252. {
  253. return $file;
  254. }
  255. } elseif (strpos($file, 'http') !== 0) {
  256. $file = JURI::root(true).'/'.$file;
  257. }
  258. $document = &JFactory::getDocument();
  259. $document->addStylesheet($file, 'text/css', null, $attribs);
  260. return;
  261. }
  262. /**
  263. * Write a <script></script> element
  264. * @param string path to file
  265. * @param boolean load the JS framework
  266. * @param boolean path to file is relative to /media folder
  267. * @param boolean return the path to the file only
  268. * @since 1.6
  269. */
  270. public static function script($file, $framework = false, $relative = false, $path_only = false)
  271. {
  272. JHtml::core();
  273. // Include mootools framework
  274. if ($framework) {
  275. JHtml::_('behavior.framework');
  276. }
  277. if($relative)
  278. {
  279. $app = JFactory::getApplication();
  280. $cur_template = $app->getTemplate();
  281. if (file_exists(JPATH_THEMES .'/'. $cur_template .'/js/'. $file)) {
  282. $file = JURI::base(true).'/templates/'. $cur_template .'/js/'. $file;
  283. } else {
  284. list($extension, $file) = explode('/', $file, 2);
  285. $file = JURI::root(true).'/media/'.$extension.'/js/'.$file;
  286. }
  287. if($path_only)
  288. {
  289. return $file;
  290. }
  291. } elseif (strpos($file, 'http') !== 0) {
  292. $file = JURI::root(true).'/'.$file;
  293. }
  294. $document = &JFactory::getDocument();
  295. $document->addScript($file);
  296. return;
  297. }
  298. public static function core($debug = null)
  299. {
  300. // If no debugging value is set, use the configuration setting
  301. if ($debug === null) {
  302. $debug = JFactory::getConfig()->getValue('config.debug');
  303. }
  304. // TODO NOTE: Here we are checking for Konqueror - If they fix their issue with compressed, we will need to update this
  305. $konkcheck = strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "konqueror");
  306. $uncompressed = ($debug || $konkcheck) ? '-uncompressed' : '';
  307. $document = &JFactory::getDocument();
  308. $document->addScript(JURI::root(true).'/media/system/js/core'.$uncompressed.'.js');
  309. }
  310. /**
  311. * Set format related options.
  312. *
  313. * Updates the formatOptions array with all valid values in the passed
  314. * array. See {@see JHtml::$formatOptions} for details.
  315. *
  316. * @param array Option key/value pairs.
  317. */
  318. public static function setFormatOptions($options)
  319. {
  320. foreach ($options as $key => $val) {
  321. if (isset(self::$formatOptions[$key])) {
  322. self::$formatOptions[$key] = $val;
  323. }
  324. }
  325. }
  326. /**
  327. * Returns formated date according to a given format and time zone.
  328. *
  329. * @param string String in a format accepted by strtotime(), defaults to "now".
  330. * @param string format optional format for strftime
  331. * @param mixed Time zone to be used for the date. Special cases: boolean true for user
  332. * setting, boolean false for server setting.
  333. * @return string A date translated by the given format and time zone.
  334. * @see strftime
  335. * @since 1.5
  336. */
  337. public static function date($input = 'now', $format = null, $tz = true)
  338. {
  339. // Get some system objects.
  340. $config = JFactory::getConfig();
  341. $user = JFactory::getUser();
  342. // UTC date converted to user time zone.
  343. if ($tz === true)
  344. {
  345. // Get a date object based on UTC.
  346. $date = JFactory::getDate($input, 'UTC');
  347. // Set the correct time zone based on the user configuration.
  348. $date->setOffset($user->getParam('timezone', $config->getValue('config.offset')));
  349. }
  350. // UTC date converted to server time zone.
  351. elseif ($tz === false)
  352. {
  353. // Get a date object based on UTC.
  354. $date = JFactory::getDate($input, 'UTC');
  355. // Set the correct time zone based on the server configuration.
  356. $date->setOffset($config->getValue('config.offset'));
  357. }
  358. // No date conversion.
  359. elseif ($tz === null)
  360. {
  361. $date = JFactory::getDate($input);
  362. }
  363. // UTC date converted to given time zone.
  364. else
  365. {
  366. // Get a date object based on UTC.
  367. $date = JFactory::getDate($input, 'UTC');
  368. // Set the correct time zone based on the server configuration.
  369. $date->setOffset($tz);
  370. }
  371. // If no format is given use the default locale based format.
  372. if (!$format) {
  373. $format = JText::_('DATE_FORMAT_LC1');
  374. }
  375. return $date->toFormat($format);
  376. }
  377. /**
  378. * Creates a tooltip with an image as button
  379. *
  380. * @access public
  381. * @param string $tooltip The tip string
  382. * @param string $title The title of the tooltip
  383. * @param string $image The image for the tip, if no text is provided
  384. * @param string $text The text for the tip
  385. * @param string $href An URL that will be used to create the link
  386. * @param boolean depreciated
  387. * @return string
  388. * @since 1.5
  389. */
  390. public static function tooltip(
  391. $tooltip, $title = '', $image = 'tooltip.png', $text = '', $href = '', $link = 1
  392. )
  393. {
  394. $tooltip = addslashes(htmlspecialchars($tooltip, ENT_COMPAT, 'UTF-8'));
  395. $title = addslashes(htmlspecialchars($title, ENT_COMPAT, 'UTF-8'));
  396. if (!$text) {
  397. $image = JURI::root(true).'/includes/js/ThemeOffice/'. $image;
  398. $text = '<img src="'. $image .'" border="0" alt="'. JText::_('Tooltip') .'"/>';
  399. } else {
  400. $text = JText::_($text, true);
  401. }
  402. if ($title) {
  403. $title .= '::';
  404. }
  405. $style = 'style="text-decoration: none; color: #333;"';
  406. $tip = '<span class="editlinktip hasTip" title="' . $title . $tooltip . '" '
  407. . $style . '>';
  408. if ($href) {
  409. $href = JRoute::_($href);
  410. $style = '';
  411. $tip .= '<a href="' . $href . '">' . $text . '</a></span>';
  412. } else {
  413. $tip .= $text . '</span>';
  414. }
  415. return $tip;
  416. }
  417. /**
  418. * Displays a calendar control field
  419. *
  420. * @param string The date value
  421. * @param string The name of the text field
  422. * @param string The id of the text field
  423. * @param string The date format
  424. * @param array Additional html attributes
  425. */
  426. public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
  427. {
  428. static $done;
  429. if ($done === null) {
  430. $done = array();
  431. }
  432. // Load the calendar behavior
  433. JHtml::_('behavior.calendar');
  434. $readonly=isset($attribs['readonly']) && $attribs['readonly']=='readonly';
  435. if (is_array($attribs)) {
  436. $attribs = JArrayHelper::toString($attribs);
  437. }
  438. // Only display the triggers once for each control.
  439. if (!in_array($id, $done) && !$readonly)
  440. {
  441. $document = &JFactory::getDocument();
  442. $document->addScriptDeclaration('window.addEvent(\'domready\', function() {Calendar.setup({
  443. inputField: "'.$id.'", // id of the input field
  444. ifFormat: "'.$format.'", // format of the input field
  445. button: "'.$id.'_img", // trigger for the calendar (button ID)
  446. align: "Tl", // alignment (defaults to "Bl")
  447. singleClick: true
  448. });});');
  449. $done[] = $id;
  450. }
  451. return '<input type="text" name="'.$name.'" id="'.$id.'" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" '.$attribs.' />'.
  452. JHTML::_('image', 'system/calendar.png', JText::_('calendar'), array( 'class' => 'calendar', 'id' => $id.'_img'), true);
  453. }
  454. /**
  455. * Add a directory where JHtml should search for helpers. You may
  456. * either pass a string or an array of directories.
  457. *
  458. * @access public
  459. * @param string A path to search.
  460. * @return array An array with directory elements
  461. * @since 1.5
  462. */
  463. public static function addIncludePath($path = '')
  464. {
  465. // force path to array
  466. settype($path, 'array');
  467. // loop through the path directories
  468. foreach ($path as $dir)
  469. {
  470. if (!empty($dir) && !in_array($dir, JHtml::$includePaths)) {
  471. array_unshift(JHtml::$includePaths, JPath::clean($dir));
  472. }
  473. }
  474. return JHtml::$includePaths;
  475. }
  476. }