* Copyright (c) 2006, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright (c) 2006, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project * @package cake * @subpackage cake.cake.libs.view.helpers * @since CakePHP v 0.9.1 * @version $Revision$ * @modifiedby $LastChangedBy$ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ /** * Html Helper class for easy use of HTML widgets. * * HtmlHelper encloses all methods needed while working with HTML pages. * * @package cake * @subpackage cake.cake.libs.view.helpers */ class HtmlHelper extends Helper { /************************************************************************* * Public variables *************************************************************************/ /**#@+ * @access public */ /** * Base URL * * @var string */ var $base = null; /** * URL to current action. * * @var string */ var $here = null; /** * Parameter array. * * @var array */ var $params = array(); /** * Current action. * * @var string */ var $action = null; /** * Enter description here... * * @var array */ var $data = null; /** * Name of model this helper is attached to. * * @var string */ var $model = null; /** * Enter description here... * * @var string */ var $field = null; /**#@-*/ /************************************************************************* * Private variables *************************************************************************/ /**#@+ * @access private */ /** * Breadcrumbs. * * @var array * @access private */ var $_crumbs = array(); /** * Document type definitions * * @var array * @access private */ var $__docTypes = array( 'html4-strict' => '', 'html4-trans' => '', 'html4-frame' => '', 'xhtml-strict' => '', 'xhtml-trans' => '', 'xhtml-frame' => '', 'xhtml11' => '' ); /** * Adds a link to the breadcrumbs array. * * @param string $name Text for link * @param string $link URL for link */ function addCrumb($name, $link) { $this->_crumbs[] = array($name, $link); } /** * Returns a doctype string. * * Possible doctypes: * + html4-strict: HTML4 Strict. * + html4-trans: HTML4 Transitional. * + html4-frame: HTML4 Frameset. * + xhtml-strict: XHTML1 Strict. * + xhtml-trans: XHTML1 Transitional. * + xhtml-frame: XHTML1 Frameset. * + xhtml11: XHTML1.1. * * @param string $type Doctype to use. * @param boolean $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT. * @return string Doctype. */ function docType($type = 'xhtml-strict', $return = false) { if (isset($this->__docTypes[$type])) { return $this->output($this->__docTypes[$type], $return); } } /** * Returns a charset META-tag. * * @param string $charset * @param boolean $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT. * @return mixed Either string or boolean value, depends on AUTO_OUTPUT and $return. */ function charset($charset, $return = false) { return $this->output(sprintf($this->tags['charset'], $charset), $return); } /** * Finds URL for specified action. * * Returns an URL pointing to a combination of controller and action. Param * $url can be: * + Empty - the method will find adress to actuall controller/action. * + '/' - the method will find base URL of application. * + A combination of controller/action - the method will find url for it. * * @param mixed $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4" * or an array specifying any of the following: 'controller', 'action', * and/or 'plugin', in addition to named arguments (keyed array elements), * and standard URL arguments (indexed array elements) * @return string Full translated URL with base path. */ function url($url = null) { $base = strip_plugin($this->base, $this->plugin); if (is_array($url) && !empty($url)) { if (!isset($url['action'])) { $url['action'] = $this->params['action']; } if (!isset($url['controller'])) { $url['controller'] = $this->params['controller']; } if (!isset($url['plugin'])) { $url['plugin'] = $this->plugin; } $named = $args = array(); $keys = array_keys($url); $count = count($keys); for ($i = 0; $i < $count; $i++) { if (is_numeric($keys[$i])) { $args[] = $url[$keys[$i]]; } else { if (!in_array($keys[$i], array('action', 'controller', 'plugin'))) { $named[] = array($keys[$i], $url[$keys[$i]]); } } } $combined = ''; if ($this->namedArgs) { if ($this->namedArgs === true) { $sep = $this->argSeparator; } elseif (is_array($this->namedArgs)) { $sep = '/'; } $count = count($named); for ($i = 0; $i < $count; $i++) { $named[$i] = join($this->argSeparator, $named[$i]); } $combined = join('/', $named); } $url = array_filter(array($url['plugin'], $url['controller'], $url['action'], join('/', array_filter($args)), $combined)); $output = $base . '/' . join('/', $url); } else { if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0))) { return $this->output($url); } if (empty($url)) { return $this->here; } elseif($url{0} == '/') { $output = $base . $url; } else { $output = $base . '/' . strtolower($this->params['controller']) . '/' . $url; } } return $this->output($output); } /** * Creates an HTML link. * * If $url starts with "http://" this is treated as an external link. Else, * it is treated as a path to controller/action and parsed with the * HtmlHelper::url() method. * * If the $url is empty, $title is used instead. * * @param string $title The content of the A tag. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://) * @param array $htmlAttributes Array of HTML attributes. * @param string $confirmMessage Confirmation message. * @param boolean $escapeTitle Whether or not the text in the $title variable should be HTML escaped. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. * @return string An element. */ function link($title, $url = null, $htmlAttributes = null, $confirmMessage = false, $escapeTitle = true, $return = false) { if ($escapeTitle) { $title = htmlspecialchars($title, ENT_QUOTES); } $url = $url ? $url : $title; if ($confirmMessage) { $confirmMessage = htmlspecialchars($confirmMessage, ENT_NOQUOTES); $confirmMessage = str_replace("'", "\'", $confirmMessage); $confirmMessage = str_replace('"', '"', $confirmMessage); $htmlAttributes['onclick'] = "return confirm('{$confirmMessage}');"; } $output = sprintf($this->tags['link'], $this->url($url), $this->_parseAttributes($htmlAttributes), $title); return $this->output($output, $return); } /** * Creates a link element for CSS stylesheets. * * @param string $path Path to CSS file * @param string $rel Rel attribute. Defaults to "stylesheet". * @param array $htmlAttributes Array of HTML attributes. * @return string CSS or