Source for file html.php
Documentation is available at html.php
* @package Joomla.Libraries
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
jimport('joomla.environment.browser');
jimport('joomla.utilities.arrayhelper');
* Utility class for all HTML drawing classes
* @package Joomla.Libraries
* Option values related to the generation of HTML output. Recognized
* fmtDepth, integer. The current indent depth.
* fmtEol, string. The end of line string, default is linefeed.
* fmtIndent, string. The string to use for indentation, default is
public static $formatOptions =
array('format.depth' =>
0, 'format.eol' =>
"\n", 'format.indent' =>
"\t");
* An array to hold included paths
protected static $includePaths =
array();
* An array to hold method references
protected static $registry =
array();
* Method to extract a key
* @param string $key The name of helper method to load, (prefix).(class).function
* prefix and class are optional and can be used to load custom html helpers.
* @return array Contains lowercase key, prefix, file, function.
protected static function extract($key)
// Check to see whether we need to load a helper file
return array(strtolower($prefix .
'.' .
$file .
'.' .
$func), $prefix, $file, $func);
* Additional arguments may be supplied and are passed to the sub-class.
* Additional include paths are also able to be specified for third-party use
* @param string $key The name of helper method to load, (prefix).(class).function
* prefix and class are optional and can be used to load custom
* @return mixed JHtml::call($function, $args) or False on error
* @throws InvalidArgumentException
public static function _($key)
list
($key, $prefix, $file, $func) =
static::extract($key);
$function =
static::$registry[$key];
// Remove function name from arguments
return static::call($function, $args);
$className =
$prefix .
ucfirst($file);
if (!class_exists($className))
$path =
JPath::find(static::$includePaths, strtolower($file) .
'.php');
if (!class_exists($className))
throw
new InvalidArgumentException(sprintf('%s not found.', $className), 500);
throw
new InvalidArgumentException(sprintf('%s %s not found.', $prefix, $file), 500);
$toCall =
array($className, $func);
static::register($key, $toCall);
// Remove function name from arguments
return static::call($toCall, $args);
throw
new InvalidArgumentException(sprintf('%s::%s not found.', $className, $func), 500);
* Registers a function to be called with a specific key
* @param string $key The name of the key
* @param string $function Function or method
* @return boolean True if the function is callable
public static function register($key, $function)
list
($key) =
static::extract($key);
static::$registry[$key] =
$function;
* Removes a key for a method from registry.
* @param string $key The name of the key
* @return boolean True if a set key is unset
list
($key) =
static::extract($key);
if (isset
(static::$registry[$key]))
unset
(static::$registry[$key]);
* Test if the key is registered.
* @param string $key The name of the key
* @return boolean True if the key is registered.
list
($key) =
static::extract($key);
return isset
(static::$registry[$key]);
* @param callable $function Function or method to call
* @param array $args Arguments to be passed to function
* @return mixed Function result or false on error.
* @see http://php.net/manual/en/function.call-user-func-array.php
* @throws InvalidArgumentException
protected static function call($function, $args)
throw
new InvalidArgumentException('Function not supported', 500);
* Write a <a></a> element
* @param string $url The relative URL to use for the href attribute
* @param string $text The target attribute to use
* @param array $attribs An associative array of attributes to add
* @return string <a></a> string
public static function link($url, $text, $attribs =
null)
return '<a href="' .
$url .
'" ' .
$attribs .
'>' .
$text .
'</a>';
* Write a <iframe></iframe> element
* @param string $url The relative URL to use for the src attribute.
* @param string $name The target attribute to use.
* @param array $attribs An associative array of attributes to add.
* @param string $noFrames The message to display if the iframe tag is not supported.
* @return string <iframe></iframe> element or message if not supported.
public static function iframe($url, $name, $attribs =
null, $noFrames =
'')
return '<iframe src="' .
$url .
'" ' .
$attribs .
' name="' .
$name .
'">' .
$noFrames .
'</iframe>';
* Compute the files to be included
* @param string $folder folder name to search into (images, css, js, ...).
* @param string $file path to file.
* @param boolean $relative path to file is relative to /media folder (and searches in template).
* @param boolean $detect_browser detect browser to include specific browser files.
* @param boolean $detect_debug detect debug to include compressed files if debug is on.
* @return array files to be included.
protected static function includeRelativeFiles($folder, $file, $relative, $detect_browser, $detect_debug)
// If http is present in filename
if (strpos($file, 'http') ===
0)
$includes =
array($file);
// Extract extension and strip the file
// Prepare array of files
// Detect browser and compute potential files
$browser =
$navigator->getBrowser();
$major =
$navigator->getMajor();
$minor =
$navigator->getMinor();
// Try to include files named filename.ext, filename_browser.ext, filename_browser_major.ext, filename_browser_major_minor.ext
// where major and minor are the browser version names
$potential =
array($strip, $strip .
'_' .
$browser, $strip .
'_' .
$browser .
'_' .
$major,
$strip .
'_' .
$browser .
'_' .
$major .
'_' .
$minor);
$potential =
array($strip);
// If relative search in template directory or media directory
// For each potential files
foreach ($potential as $strip)
* Detect if we received a file in the format name.min.ext
* If so, strip the .min part out, otherwise append -uncompressed
$position =
strrpos($strip, '.min', '-4');
$filename =
str_replace('.min', '.', $strip, $position);
$files[] =
$filename .
$ext;
$files[] =
$strip .
'-uncompressed.' .
$ext;
$files[] =
$strip .
'.' .
$ext;
* Loop on 1 or 2 files and break on first found.
* Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
* This MD5SUM file must represent the signature of the folder content
foreach ($files as $file)
// If the file is in the template folder
$includes[] =
JUri::base(true) .
"/templates/$template/$folder/$file" .
// If the file contains any /: it can be in an media extension subfolder
// Divide the file extracting the extension as the first part before /
list
($extension, $file) =
explode('/', $file, 2);
// If the file yet contains any /: it can be a plugin
// Divide the file extracting the element as the first part before /
list
($element, $file) =
explode('/', $file, 2);
// Try to deal with plugins group in the media folder
$path =
JPATH_ROOT .
"/media/$extension/$element/$folder/$file";
$includes[] =
JUri::root(true) .
"/media/$extension/$element/$folder/$file" .
// Try to deal with classical file in a a media subfolder called element
$path =
JPATH_ROOT .
"/media/$extension/$folder/$element/$file";
$includes[] =
JUri::root(true) .
"/media/$extension/$folder/$element/$file" .
// Try to deal with system files in the template folder
$path =
JPATH_THEMES .
"/$template/$folder/system/$element/$file";
$includes[] =
JUri::root(true) .
"/templates/$template/$folder/system/$element/$file" .
// Try to deal with system files in the media folder
$path =
JPATH_ROOT .
"/media/system/$folder/$element/$file";
$includes[] =
JUri::root(true) .
"/media/system/$folder/$element/$file" .
// Try to deals in the extension media folder
$path =
JPATH_ROOT .
"/media/$extension/$folder/$file";
$includes[] =
JUri::root(true) .
"/media/$extension/$folder/$file" .
// Try to deal with system files in the template folder
$includes[] =
JUri::root(true) .
"/templates/$template/$folder/system/$file" .
// Try to deal with system files in the media folder
$path =
JPATH_ROOT .
"/media/system/$folder/$file";
$includes[] =
JUri::root(true) .
"/media/system/$folder/$file" .
// Try to deal with system files in the media folder
$path =
JPATH_ROOT .
"/media/system/$folder/$file";
$includes[] =
JUri::root(true) .
"/media/system/$folder/$file" .
// If not relative and http is not present in filename
foreach ($potential as $strip)
* Detect if we received a file in the format name.min.ext
* If so, strip the .min part out, otherwise append -uncompressed
$position =
strrpos($strip, '.min', '-4');
$filename =
str_replace('.min', '.', $strip, $position);
$files[] =
$filename .
$ext;
$files[] =
$strip .
'-uncompressed.' .
$ext;
$files[] =
$strip .
'.' .
$ext;
* Loop on 1 or 2 files and break on first found.
* Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
* This MD5SUM file must represent the signature of the folder content
foreach ($files as $file)
$includes[] =
JUri::root(true) .
"/$file" .
* Write a <img></img> element
* @param string $file The relative or absolute URL to use for the src attribute.
* @param string $alt The alt text.
* @param mixed $attribs String or associative array of attribute(s) to use.
* @param boolean $relative Path to file is relative to /media folder (and searches in template).
* @param mixed $path_rel Return html tag without (-1) or with file computing(false). Return computed path only (true).
public static function image($file, $alt, $attribs =
null, $relative =
false, $path_rel =
false)
$includes =
static::includeRelativeFiles('images', $file, $relative, false, false);
$file =
count($includes) ?
$includes[0] :
null;
// If only path is required
return '<img src="' .
$file .
'" alt="' .
$alt .
'" '
* Write a <link rel="stylesheet" style="text/css" /> element
* @param string $file path to file
* @param array $attribs attributes to be added to the stylesheet
* @param boolean $relative path to file is relative to /media folder
* @param boolean $path_only return the path to the file only
* @param boolean $detect_browser detect browser to include specific browser css files
* will try to include file, file_*browser*, file_*browser*_*major*, file_*browser*_*major*_*minor*
* <tr><th>Navigator</th> <th>browser</th> <th>major.minor</th></tr>
* <tr><td>Safari 3.0.x</td> <td>konqueror</td> <td>522.x</td></tr>
* <tr><td>Safari 3.1.x and 3.2.x</td> <td>konqueror</td> <td>525.x</td></tr>
* <tr><td>Safari 4.0 to 4.0.2</td> <td>konqueror</td> <td>530.x</td></tr>
* <tr><td>Safari 4.0.3 to 4.0.4</td> <td>konqueror</td> <td>531.x</td></tr>
* <tr><td>iOS 4.0 Safari</td> <td>konqueror</td> <td>532.x</td></tr>
* <tr><td>Safari 5.0</td> <td>konqueror</td> <td>533.x</td></tr>
* <tr><td>Google Chrome 1.0</td> <td>konqueror</td> <td>528.x</td></tr>
* <tr><td>Google Chrome 2.0</td> <td>konqueror</td> <td>530.x</td></tr>
* <tr><td>Google Chrome 3.0 and 4.x</td> <td>konqueror</td> <td>532.x</td></tr>
* <tr><td>Google Chrome 5.0</td> <td>konqueror</td> <td>533.x</td></tr>
* <tr><td>Internet Explorer 5.5</td> <td>msie</td> <td>5.5</td></tr>
* <tr><td>Internet Explorer 6.x</td> <td>msie</td> <td>6.x</td></tr>
* <tr><td>Internet Explorer 7.x</td> <td>msie</td> <td>7.x</td></tr>
* <tr><td>Internet Explorer 8.x</td> <td>msie</td> <td>8.x</td></tr>
* <tr><td>Firefox</td> <td>mozilla</td> <td>5.0</td></tr>
* @param boolean $detect_debug detect debug to search for compressed files if debug is on
* @return mixed nothing if $path_only is false, null, path or array of path if specific css browser files were detected
public static function stylesheet($file, $attribs =
array(), $relative =
false, $path_only =
false, $detect_browser =
true, $detect_debug =
true)
$includes =
static::includeRelativeFiles('css', $file, $relative, $detect_browser, $detect_debug);
// If only path is required
if (count($includes) ==
0)
elseif (count($includes) ==
1)
// If inclusion is required
foreach ($includes as $include)
$document->addStylesheet($include, 'text/css', null, $attribs);
* Write a <script></script> element
* @param string $file path to file.
* @param boolean $framework load the JS framework.
* @param boolean $relative path to file is relative to /media folder.
* @param boolean $path_only return the path to the file only.
* @param boolean $detect_browser detect browser to include specific browser js files.
* @param boolean $detect_debug detect debug to search for compressed files if debug is on.
* @return mixed nothing if $path_only is false, null, path or array of path if specific js browser files were detected.
* @see JHtml::stylesheet()
public static function script($file, $framework =
false, $relative =
false, $path_only =
false, $detect_browser =
true, $detect_debug =
true)
// Include MooTools framework
static::_('behavior.framework');
$includes =
static::includeRelativeFiles('js', $file, $relative, $detect_browser, $detect_debug);
// If only path is required
if (count($includes) ==
0)
elseif (count($includes) ==
1)
// If inclusion is required
foreach ($includes as $include)
$document->addScript($include);
* Set format related options.
* Updates the formatOptions array with all valid values in the passed array.
* @param array $options Option key/value pairs.
* @see JHtml::$formatOptions
foreach ($options as $key =>
$val)
if (isset
(static::$formatOptions[$key]))
static::$formatOptions[$key] =
$val;
* Returns formated date according to a given format and time zone.
* @param string $input String in a format accepted by date(), defaults to "now".
* @param string $format The date format specification string (see {@link PHP_MANUAL#date}).
* @param mixed $tz Time zone to be used for the date. Special cases: boolean true for user
* setting, boolean false for server setting.
* @param boolean $gregorian True to use Gregorian calendar.
* @return string A date translated by the given format and time zone.
public static function date($input =
'now', $format =
null, $tz =
true, $gregorian =
false)
// Get some system objects.
$config =
JFactory::getConfig();
$user =
JFactory::getUser();
// UTC date converted to user time zone.
// Get a date object based on UTC.
$date =
JFactory::getDate($input, 'UTC');
// Set the correct time zone based on the user configuration.
$date->setTimeZone(new DateTimeZone($user->getParam('timezone', $config->get('offset'))));
// UTC date converted to server time zone.
// Get a date object based on UTC.
// Set the correct time zone based on the server configuration.
$date->setTimeZone(new DateTimeZone($config->get('offset')));
// UTC date converted to given time zone.
// Get a date object based on UTC.
// Set the correct time zone based on the server configuration.
$date->setTimeZone(new DateTimeZone($tz));
// If no format is given use the default locale based format.
$format =
JText::_('DATE_FORMAT_LC1');
// $format is an existing language key
return $date->format($format, true);
* Creates a tooltip with an image as button
* @param string $tooltip The tip string.
* @param mixed $title The title of the tooltip or an associative array with keys contained in
* {'title','image','text','href','alt'} and values corresponding to parameters of the same name.
* @param string $image The image for the tip, if no text is provided.
* @param string $text The text for the tip.
* @param string $href An URL that will be used to create the link.
* @param string $alt The alt attribute for img tag.
* @param string $class CSS class for the tool tip.
public static function tooltip($tooltip, $title =
'', $image =
'tooltip.png', $text =
'', $href =
'', $alt =
'Tooltip', $class =
'hasTooltip')
foreach (array('image', 'text', 'href', 'alt', 'class') as $param)
if (isset
($title[$param]))
$
$param =
$title[$param];
if (isset
($title['title']))
$title =
$title['title'];
$text =
static::image($image, $alt, null, true);
$tip =
'<a href="' .
$href .
'">' .
$text .
'</a>';
// Still using MooTools tooltips!
$tooltip =
$title .
'::' .
$tooltip;
$tooltip =
self::tooltipText($title, $tooltip, 0);
return '<span class="' .
$class .
'" title="' .
$tooltip .
'">' .
$tip .
'</span>';
* Converts a double colon seperated string or 2 separate strings to a string ready for bootstrap tooltips
* @param string $title The title of the tooltip (or combined '::' separated string).
* @param string $content The content to tooltip.
* @param int $translate If true will pass texts through JText.
* @param int $escape If true will pass texts through htmlspecialchars.
* @return string The tooltip string
public static function tooltipText($title =
'', $content =
'', $translate =
1, $escape =
1)
// Return empty in no title or content is given.
if ($title ==
'' &&
$content ==
'')
// Split title into title and content if the title contains '::' (old Mootools format).
if ($content ==
'' &&
!(strpos($title, '::') ===
false))
list
($title, $content) =
explode('::', $title, 2);
// Pass texts through the JText.
$content =
JText::_($content);
// Return only the content if no title is given.
// Return only the title if title and text are the same.
return '<strong>' .
$title .
'</strong>';
// Return the formated sting combining the title and content.
return '<strong>' .
$title .
'</strong><br />' .
$content;
// Return only the title.
* Displays a calendar control field
* @param string $value The date value
* @param string $name The name of the text field
* @param string $id The id of the text field
* @param string $format The date format
* @param array $attribs Additional HTML attributes
* @return string HTML markup for a calendar field
public static function calendar($value, $name, $id, $format =
'%Y-%m-%d', $attribs =
null)
$attribs['class'] = isset
($attribs['class']) ?
$attribs['class'] :
'input-medium';
$attribs['class'] =
trim($attribs['class'] .
' hasTooltip');
$readonly = isset
($attribs['readonly']) &&
$attribs['readonly'] ==
'readonly';
$disabled = isset
($attribs['disabled']) &&
$attribs['disabled'] ==
'disabled';
static::_('bootstrap.tooltip');
if (!$readonly &&
!$disabled)
// Load the calendar behavior
static::_('behavior.calendar');
// Only display the triggers once for each control.
'window.addEvent(\'domready\', function() {Calendar.setup({
inputField: "' .
$id .
'",
// Format of the input field
ifFormat: "' .
$format .
'",
// Trigger for the calendar (button ID)
button: "' .
$id .
'_img",
// Alignment (defaults to "Bl")
return '<div class="input-append"><input type="text" title="' .
(0 !== (int)
$value ?
static::_('date', $value, null, null) :
'')
.
'" name="' .
$name .
'" id="' .
$id .
'" value="' .
htmlspecialchars($value, ENT_COMPAT, 'UTF-8') .
'" ' .
$attribs .
' />'
.
'<button type="button" class="btn" id="' .
$id .
'_img"><i class="icon-calendar"></i></button></div>';
return '<input type="text" title="' .
(0 !== (int)
$value ?
static::_('date', $value, null, null) :
'')
.
'" value="' .
(0 !== (int)
$value ?
static::_('date', $value, 'Y-m-d H:i:s', null) :
'') .
'" ' .
$attribs
.
' /><input type="hidden" name="' .
$name .
'" id="' .
$id .
'" value="' .
htmlspecialchars($value, ENT_COMPAT, 'UTF-8') .
'" />';
* Add a directory where JHtml should search for helpers. You may
* either pass a string or an array of directories.
* @param string $path A path to search.
* @return array An array with directory elements
// Loop through the path directories
if (!empty($dir) &&
!in_array($dir, static::$includePaths))
array_unshift(static::$includePaths, JPath::clean($dir));
return static::$includePaths;
* Internal method to get a JavaScript object notation string from an array
* @param array $array The array to convert to JavaScript object notation
* @return string JavaScript object notation representation of the array
public static function getJSObject(array $array =
array())
foreach ($array as $k =>
$v)
// Don't encode either of these types
// Safely encode as a Javascript string
$elements[] =
$key .
': ' .
($v ?
'true' :
'false');
$elements[] =
$key .
': ' .
($v +
0);
// Items such as functions and JSON objects are prefixed with \, strip the prefix and don't encode them
$elements[] =
$key .
': ' .
substr($v, 1);
// The safest way to insert a string
$elements[] =
$key .
': ' .
static::getJSObject(is_object($v) ?
get_object_vars($v) :
$v);
return '{' .
implode(',', $elements) .
'}';
Documentation generated on Tue, 19 Nov 2013 15:04:51 +0100 by phpDocumentor 1.4.3