PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/html.php

https://github.com/joebushi/joomla
PHP | 499 lines | 262 code | 61 blank | 176 comment | 37 complexity | cd6f962265bd073e5a9fb9e33ddc1aa0 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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. public static function core($debug = null)
  161. {
  162. // If no debugging value is set, use the configuration setting
  163. if ($debug === null) {
  164. $debug = JFactory::getConfig()->getValue('config.debug');
  165. }
  166. // TODO NOTE: Here we are checking for Konqueror - If they fix thier issue with compressed, we will need to update this
  167. $konkcheck = strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "konqueror");
  168. $uncompressed = ($debug || $konkcheck) ? '-uncompressed' : '';
  169. $document = &JFactory::getDocument();
  170. $document->addScript(JURI::root(true).'/media/system/js/core'.$uncompressed.'.js');
  171. }
  172. /**
  173. * Write a <a></a> element
  174. *
  175. * @access public
  176. * @param string The relative URL to use for the href attribute
  177. * @param string The target attribute to use
  178. * @param array An associative array of attributes to add
  179. * @since 1.5
  180. */
  181. public static function link($url, $text, $attribs = null)
  182. {
  183. if (is_array($attribs)) {
  184. $attribs = JArrayHelper::toString($attribs);
  185. }
  186. return '<a href="'.$url.'" '.$attribs.'>'.$text.'</a>';
  187. }
  188. /**
  189. * Write a <img></img> element
  190. *
  191. * @access public
  192. * @param string The relative or absolute URL to use for the src attribute
  193. * @param string The target attribute to use
  194. * @param array An associative array of attributes to add
  195. * @param boolean If set to true, it tries to find an override for the file in the template
  196. * @since 1.5
  197. */
  198. public static function image($url, $alt, $attribs = null, $relative = false)
  199. {
  200. if (is_array($attribs)) {
  201. $attribs = JArrayHelper::toString($attribs);
  202. }
  203. if($relative)
  204. {
  205. $app = JFactory::getApplication();
  206. $cur_template = $app->getTemplate();
  207. if (file_exists(JPATH_THEMES .'/'. $cur_template .'/images/'. $url)) {
  208. $url = JURI::base(true).'/templates/'. $cur_template .'/images/'. $url;
  209. } else {
  210. $url = JURI::root(true).'/media/images/'.$url;
  211. }
  212. } elseif (strpos($url, 'http') !== 0) {
  213. $url = JURI::base(true).'/'.$url;
  214. }
  215. return '<img src="'.$url.'" alt="'.$alt.'" '.$attribs.' />';
  216. }
  217. /**
  218. * Write a <iframe></iframe> element
  219. *
  220. * @access public
  221. * @param string The relative URL to use for the src attribute
  222. * @param string The target attribute to use
  223. * @param array An associative array of attributes to add
  224. * @param string The message to display if the iframe tag is not supported
  225. * @since 1.5
  226. */
  227. public static function iframe($url, $name, $attribs = null, $noFrames = '')
  228. {
  229. if (is_array($attribs)) {
  230. $attribs = JArrayHelper::toString($attribs);
  231. }
  232. return '<iframe src="'.$url.'" '.$attribs.' name="'.$name.'">'.$noFrames.'</iframe>';
  233. }
  234. /**
  235. * Write a <script></script> element
  236. *
  237. * @access public
  238. * @param string The name of the script file
  239. * * @param string The relative or absolute path of the script file
  240. * @param boolean If true, the mootools library will be loaded
  241. * @since 1.5
  242. */
  243. public static function script($filename, $path = 'media/system/js/', $framework = false)
  244. {
  245. JHtml::core();
  246. // Include mootools framework
  247. if ($framework) {
  248. JHtml::_('behavior.framework');
  249. }
  250. if (strpos($path, 'http') !== 0) {
  251. $path = JURI::root(true).'/'.$path;
  252. };
  253. $document = &JFactory::getDocument();
  254. $document->addScript($path.$filename);
  255. return;
  256. }
  257. /**
  258. * Set format related options.
  259. *
  260. * Updates the formatOptions array with all valid values in the passed
  261. * array. See {@see JHtml::$formatOptions} for details.
  262. *
  263. * @param array Option key/value pairs.
  264. */
  265. public static function setFormatOptions($options)
  266. {
  267. foreach ($options as $key => $val) {
  268. if (isset(self::$formatOptions[$key])) {
  269. self::$formatOptions[$key] = $val;
  270. }
  271. }
  272. }
  273. /**
  274. * Write a <link rel="stylesheet" style="text/css" /> element
  275. *
  276. * @access public
  277. * @param string The relative URL to use for the href attribute
  278. * @since 1.5
  279. */
  280. public static function stylesheet($filename, $path = 'media/system/css/', $attribs = array())
  281. {
  282. if (strpos($path, 'http') !== 0) {
  283. $path = JURI::root(true).'/'.$path;
  284. };
  285. $document = &JFactory::getDocument();
  286. $document->addStylesheet($path.$filename, 'text/css', null, $attribs);
  287. return;
  288. }
  289. /**
  290. * Returns formated date according to a given format and time zone.
  291. *
  292. * @param string String in a format accepted by strtotime(), defaults to "now".
  293. * @param string format optional format for strftime
  294. * @param mixed Time zone to be used for the date. Special cases: boolean true for user
  295. * setting, boolean false for server setting.
  296. * @return string A date translated by the given format and time zone.
  297. * @see strftime
  298. * @since 1.5
  299. */
  300. public static function date($input = 'now', $format = null, $tz = true)
  301. {
  302. // Get some system objects.
  303. $config = JFactory::getConfig();
  304. $user = JFactory::getUser();
  305. // UTC date converted to user time zone.
  306. if ($tz === true)
  307. {
  308. // Get a date object based on UTC.
  309. $date = JFactory::getDate($input, 'UTC');
  310. // Set the correct time zone based on the user configuration.
  311. $date->setOffset($user->getParam('timezone', $config->getValue('config.offset')));
  312. }
  313. // UTC date converted to server time zone.
  314. elseif ($tz === false)
  315. {
  316. // Get a date object based on UTC.
  317. $date = JFactory::getDate($input, 'UTC');
  318. // Set the correct time zone based on the server configuration.
  319. $date->setOffset($config->getValue('config.offset'));
  320. }
  321. // No date conversion.
  322. elseif ($tz === null)
  323. {
  324. $date = JFactory::getDate($input);
  325. }
  326. // UTC date converted to given time zone.
  327. else
  328. {
  329. // Get a date object based on UTC.
  330. $date = JFactory::getDate($input, 'UTC');
  331. // Set the correct time zone based on the server configuration.
  332. $date->setOffset($tz);
  333. }
  334. // If no format is given use the default locale based format.
  335. if (!$format) {
  336. $format = JText::_('DATE_FORMAT_LC1');
  337. }
  338. return $date->toFormat($format);
  339. }
  340. /**
  341. * Creates a tooltip with an image as button
  342. *
  343. * @access public
  344. * @param string $tooltip The tip string
  345. * @param string $title The title of the tooltip
  346. * @param string $image The image for the tip, if no text is provided
  347. * @param string $text The text for the tip
  348. * @param string $href An URL that will be used to create the link
  349. * @param boolean depreciated
  350. * @return string
  351. * @since 1.5
  352. */
  353. public static function tooltip(
  354. $tooltip, $title = '', $image = 'tooltip.png', $text = '', $href = '', $link = 1
  355. )
  356. {
  357. $tooltip = addslashes(htmlspecialchars($tooltip));
  358. $title = addslashes(htmlspecialchars($title));
  359. if (!$text) {
  360. $image = JURI::root(true).'/includes/js/ThemeOffice/'. $image;
  361. $text = '<img src="'. $image .'" border="0" alt="'. JText::_('Tooltip') .'"/>';
  362. } else {
  363. $text = JText::_($text, true);
  364. }
  365. if ($title) {
  366. $title .= '::';
  367. }
  368. $style = 'style="text-decoration: none; color: #333;"';
  369. $tip = '<span class="editlinktip hasTip" title="' . $title . $tooltip . '" '
  370. . $style . '>';
  371. if ($href) {
  372. $href = JRoute::_($href);
  373. $style = '';
  374. $tip .= '<a href="' . $href . '">' . $text . '</a></span>';
  375. } else {
  376. $tip .= $text . '</span>';
  377. }
  378. return $tip;
  379. }
  380. /**
  381. * Displays a calendar control field
  382. *
  383. * @param string The date value
  384. * @param string The name of the text field
  385. * @param string The id of the text field
  386. * @param string The date format
  387. * @param array Additional html attributes
  388. */
  389. public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
  390. {
  391. static $done;
  392. if ($done === null) {
  393. $done = array();
  394. }
  395. // Load the calendar behavior
  396. JHtml::_('behavior.calendar');
  397. if (is_array($attribs)) {
  398. $attribs = JArrayHelper::toString($attribs);
  399. }
  400. // Only display the triggers once for each control.
  401. if (!in_array($id, $done))
  402. {
  403. $document = &JFactory::getDocument();
  404. $document->addScriptDeclaration('window.addEvent(\'domready\', function() {Calendar.setup({
  405. inputField : "'.$id.'", // id of the input field
  406. ifFormat : "'.$format.'", // format of the input field
  407. button : "'.$id.'_img", // trigger for the calendar (button ID)
  408. align : "Tl", // alignment (defaults to "Bl")
  409. singleClick : true
  410. });});');
  411. $done[] = $id;
  412. }
  413. return '<input type="text" name="'.$name.'" id="'.$id.'" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" '.$attribs.' />'.
  414. JHTML::_('image', 'system/calendar.png', JText::_('calendar'), array( 'class' => 'calendar', 'id' => $id.'_img'), true);
  415. }
  416. /**
  417. * Add a directory where JHtml should search for helpers. You may
  418. * either pass a string or an array of directories.
  419. *
  420. * @access public
  421. * @param string A path to search.
  422. * @return array An array with directory elements
  423. * @since 1.5
  424. */
  425. public static function addIncludePath($path = '')
  426. {
  427. // force path to array
  428. settype($path, 'array');
  429. // loop through the path directories
  430. foreach ($path as $dir)
  431. {
  432. if (!empty($dir) && !in_array($dir, JHtml::$includePaths)) {
  433. array_unshift(JHtml::$includePaths, JPath::clean($dir));
  434. }
  435. }
  436. return JHtml::$includePaths;
  437. }
  438. }