* Copyright 2005-2007, 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 2005-2007, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.view.helpers * @since CakePHP(tm) 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 AppHelper { /************************************************************************* * Public variables *************************************************************************/ /**#@+ * @access public */ /** * html tags used by this helper. * * @var array */ var $tags = array( 'metalink' => '', 'link' => '%s', 'mailto' => '%s', 'form' => '
', 'formend' => '
', 'input' => '', 'textarea' => '', 'hidden' => '', 'textarea' => '', 'checkbox' => '', 'radio' => '%s', 'selectstart' => '', 'selectempty' => '', 'selectoption' => '', 'selectend' => '', 'optiongroup' => '', 'optiongroupend' => '', 'password' => '', 'file' => '', 'file_no_model' => '', 'submit' => '', 'submitimage' => '', 'image' => '', 'tableheader' => '%s', 'tableheaderrow' => '%s', 'tablecell' => '%s', 'tablerow' => '%s', 'block' => '%s', 'blockstart' => '', 'blockend' => '', 'para' => '%s

', 'parastart' => '', 'label' => '', 'fieldset' => '
%s%s
', 'fieldsetstart' => '
%s', 'fieldsetend' => '
', 'legend' => '%s', 'css' => '', 'style' => '', 'charset' => '' ); /** * 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; /**#@-*/ /************************************************************************* * 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. * @return string Doctype. */ function docType($type = 'xhtml-strict') { if (isset($this->__docTypes[$type])) { return $this->output($this->__docTypes[$type]); } } /** * Creates a link to an external resource * * @param string $title The title of the external resource * @param mixed $url The address of the external resource * @param array $attributes * @return string */ function meta($title = null, $url = null, $attributes = array(), $inline = true) { $types = array( 'html' => 'text/html', 'rss' => 'application/rss+xml', 'atom' => 'application/atom+xml', 'icon' => 'image/x-icon' ); if (!isset($attributes['type']) && is_array($url) && isset($url['ext'])) { if (in_array($url['ext'], array_keys($types))) { $attributes['type'] = $url['ext']; } else { $attributes['type'] = 'rss'; } } elseif (!isset($attributes['type'])) { $attributes['type'] = 'rss'; } if (isset($attributes['type']) && in_array($attributes['type'], array_keys($types))) { $attributes['type'] = $types[$attributes['type']]; } if (!isset($attributes['rel'])) { $attributes['rel'] = 'alternate'; } $out = $this->output(sprintf($this->tags['metalink'], $this->url($url, true), $title, $this->_parseAttributes($attributes))); if ($inline) { return $out; } else { $view =& ClassRegistry::getObject('view'); $view->addScript($out); } } /** * Returns a charset META-tag. * * @param string $charset * @return string */ function charset($charset = null) { if(is_null($charset)){ $charset = Configure::read('charset'); if(is_null($charset)){ $charset = 'utf-8'; } } return $this->output(sprintf($this->tags['charset'], $charset)); } /** * 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. * @return string An element. */ function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true) { if($url !== null) { $url = $this->url($url); } else { $url = $this->url($title); $title = $url; $escapeTitle = false; } if(isset($htmlAttributes['escape'])) { $escapeTitle = $htmlAttributes['escape']; unset($htmlAttributes['escape']); } if($escapeTitle === true) { $title = htmlspecialchars($title, ENT_QUOTES); } else if (is_string($escapeTitle)) { $title = htmlentities($title, $escapeTitle); } if(!empty($htmlAttributes['confirm'])) { $confirmMessage = $htmlAttributes['confirm']; unset($htmlAttributes['confirm']); } if ($confirmMessage) { $confirmMessage = str_replace("'", "\'", $confirmMessage); $confirmMessage = str_replace('"', '\"', $confirmMessage); $htmlAttributes['onclick'] = "return confirm('{$confirmMessage}');"; } elseif (isset($htmlAttributes['default'])) { if ($htmlAttributes['default'] == false) { if (isset($htmlAttributes['onclick'])) { $htmlAttributes['onclick'] .= ' return false;'; } else { $htmlAttributes['onclick'] = 'return false;'; } unset($htmlAttributes['default']); } } return $this->output(sprintf($this->tags['link'], $url, $this->_parseAttributes($htmlAttributes), $title)); } /** * 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. * @param boolean $inline * @return string CSS or