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

/lib/Cake/View/Helper/HtmlHelper.php

https://github.com/Bancha/cakephp
PHP | 1063 lines | 894 code | 21 blank | 148 comment | 26 complexity | 0569ebfa126407872023abcb737b8434 MD5 | raw file
  1. <?php
  2. /**
  3. * Html Helper class file.
  4. *
  5. * Simplifies the construction of HTML elements.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs.view.helpers
  16. * @since CakePHP(tm) v 0.9.1
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AppHelper', 'View/Helper');
  20. /**
  21. * Html Helper class for easy use of HTML widgets.
  22. *
  23. * HtmlHelper encloses all methods needed while working with HTML pages.
  24. *
  25. * @package cake.libs.view.helpers
  26. * @link http://book.cakephp.org/view/1434/HTML
  27. */
  28. class HtmlHelper extends AppHelper {
  29. /**
  30. * html tags used by this helper.
  31. *
  32. * @var array
  33. */
  34. protected $_tags = array(
  35. 'meta' => '<meta%s/>',
  36. 'metalink' => '<link href="%s"%s/>',
  37. 'link' => '<a href="%s"%s>%s</a>',
  38. 'mailto' => '<a href="mailto:%s" %s>%s</a>',
  39. 'form' => '<form%s>',
  40. 'formend' => '</form>',
  41. 'input' => '<input name="%s"%s/>',
  42. 'textarea' => '<textarea name="%s"%s>%s</textarea>',
  43. 'hidden' => '<input type="hidden" name="%s"%s/>',
  44. 'checkbox' => '<input type="checkbox" name="%s" %s/>',
  45. 'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
  46. 'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
  47. 'selectstart' => '<select name="%s"%s>',
  48. 'selectmultiplestart' => '<select name="%s[]"%s>',
  49. 'selectempty' => '<option value=""%s>&nbsp;</option>',
  50. 'selectoption' => '<option value="%s"%s>%s</option>',
  51. 'selectend' => '</select>',
  52. 'optiongroup' => '<optgroup label="%s"%s>',
  53. 'optiongroupend' => '</optgroup>',
  54. 'checkboxmultiplestart' => '',
  55. 'checkboxmultipleend' => '',
  56. 'password' => '<input type="password" name="%s" %s/>',
  57. 'file' => '<input type="file" name="%s" %s/>',
  58. 'file_no_model' => '<input type="file" name="%s" %s/>',
  59. 'submit' => '<input %s/>',
  60. 'submitimage' => '<input type="image" src="%s" %s/>',
  61. 'button' => '<button type="%s"%s>%s</button>',
  62. 'image' => '<img src="%s" %s/>',
  63. 'tableheader' => '<th%s>%s</th>',
  64. 'tableheaderrow' => '<tr%s>%s</tr>',
  65. 'tablecell' => '<td%s>%s</td>',
  66. 'tablerow' => '<tr%s>%s</tr>',
  67. 'block' => '<div%s>%s</div>',
  68. 'blockstart' => '<div%s>',
  69. 'blockend' => '</div>',
  70. 'tag' => '<%s%s>%s</%s>',
  71. 'tagstart' => '<%s%s>',
  72. 'tagend' => '</%s>',
  73. 'para' => '<p%s>%s</p>',
  74. 'parastart' => '<p%s>',
  75. 'label' => '<label for="%s"%s>%s</label>',
  76. 'fieldset' => '<fieldset%s>%s</fieldset>',
  77. 'fieldsetstart' => '<fieldset><legend>%s</legend>',
  78. 'fieldsetend' => '</fieldset>',
  79. 'legend' => '<legend>%s</legend>',
  80. 'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
  81. 'style' => '<style type="text/css"%s>%s</style>',
  82. 'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
  83. 'ul' => '<ul%s>%s</ul>',
  84. 'ol' => '<ol%s>%s</ol>',
  85. 'li' => '<li%s>%s</li>',
  86. 'error' => '<div%s>%s</div>',
  87. 'javascriptblock' => '<script type="text/javascript"%s>%s</script>',
  88. 'javascriptstart' => '<script type="text/javascript">',
  89. 'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
  90. 'javascriptend' => '</script>'
  91. );
  92. /**
  93. * Minimized attributes
  94. *
  95. * @var array
  96. */
  97. protected $_minimizedAttributes = array(
  98. 'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected',
  99. 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize'
  100. );
  101. /**
  102. * Format to attribute
  103. *
  104. * @var string
  105. */
  106. protected $_attributeFormat = '%s="%s"';
  107. /**
  108. * Format to attribute
  109. *
  110. * @var string
  111. */
  112. protected $_minimizedAttributeFormat = '%s="%s"';
  113. /**
  114. * Breadcrumbs.
  115. *
  116. * @var array
  117. * @access protected
  118. */
  119. protected $_crumbs = array();
  120. /**
  121. * Names of script files that have been included once
  122. *
  123. * @var array
  124. * @access private
  125. */
  126. private $__includedScripts = array();
  127. /**
  128. * Options for the currently opened script block buffer if any.
  129. *
  130. * @var array
  131. * @access protected
  132. */
  133. protected $_scriptBlockOptions = array();
  134. /**
  135. * Document type definitions
  136. *
  137. * @var array
  138. * @access private
  139. */
  140. private $__docTypes = array(
  141. 'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
  142. 'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  143. 'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
  144. 'html5' => '<!DOCTYPE html>',
  145. 'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  146. 'xhtml-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
  147. 'xhtml-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
  148. 'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
  149. );
  150. /**
  151. * Default Constructor
  152. *
  153. * @param View $View The View this helper is being attached to.
  154. * @param array $settings Configuration settings for the helper.
  155. */
  156. public function __construct(View $View, $settings = array()) {
  157. parent::__construct($View, $settings);
  158. if (!empty($settings['configFile'])) {
  159. $this->loadConfig($settings['configFile']);
  160. }
  161. }
  162. /**
  163. * Adds a link to the breadcrumbs array.
  164. *
  165. * @param string $name Text for link
  166. * @param string $link URL for link (if empty it won't be a link)
  167. * @param mixed $options Link attributes e.g. array('id'=>'selected')
  168. * @return void
  169. * @see HtmlHelper::link() for details on $options that can be used.
  170. */
  171. public function addCrumb($name, $link = null, $options = null) {
  172. $this->_crumbs[] = array($name, $link, $options);
  173. }
  174. /**
  175. * Returns a doctype string.
  176. *
  177. * Possible doctypes:
  178. *
  179. * - html4-strict: HTML4 Strict.
  180. * - html4-trans: HTML4 Transitional.
  181. * - html4-frame: HTML4 Frameset.
  182. * - html5: HTML5.
  183. * - xhtml-strict: XHTML1 Strict.
  184. * - xhtml-trans: XHTML1 Transitional.
  185. * - xhtml-frame: XHTML1 Frameset.
  186. * - xhtml11: XHTML1.1.
  187. *
  188. * @param string $type Doctype to use.
  189. * @return string Doctype string
  190. * @access public
  191. * @link http://book.cakephp.org/view/1439/docType
  192. */
  193. public function docType($type = 'xhtml-strict') {
  194. if (isset($this->__docTypes[$type])) {
  195. return $this->__docTypes[$type];
  196. }
  197. return null;
  198. }
  199. /**
  200. * Creates a link to an external resource and handles basic meta tags
  201. *
  202. * ### Options
  203. *
  204. * - `inline` Whether or not the link element should be output inline, or in scripts_for_layout.
  205. *
  206. * @param string $type The title of the external resource
  207. * @param mixed $url The address of the external resource or string for content attribute
  208. * @param array $options Other attributes for the generated tag. If the type attribute is html,
  209. * rss, atom, or icon, the mime-type is returned.
  210. * @return string A completed `<link />` element.
  211. * @access public
  212. * @link http://book.cakephp.org/view/1438/meta
  213. */
  214. public function meta($type, $url = null, $options = array()) {
  215. $inline = isset($options['inline']) ? $options['inline'] : true;
  216. unset($options['inline']);
  217. if (!is_array($type)) {
  218. $types = array(
  219. 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
  220. 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
  221. 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
  222. 'keywords' => array('name' => 'keywords', 'content' => $url),
  223. 'description' => array('name' => 'description', 'content' => $url),
  224. );
  225. if ($type === 'icon' && $url === null) {
  226. $types['icon']['link'] = $this->webroot('favicon.ico');
  227. }
  228. if (isset($types[$type])) {
  229. $type = $types[$type];
  230. } elseif (!isset($options['type']) && $url !== null) {
  231. if (is_array($url) && isset($url['ext'])) {
  232. $type = $types[$url['ext']];
  233. } else {
  234. $type = $types['rss'];
  235. }
  236. } elseif (isset($options['type']) && isset($types[$options['type']])) {
  237. $type = $types[$options['type']];
  238. unset($options['type']);
  239. } else {
  240. $type = array();
  241. }
  242. } elseif ($url !== null) {
  243. $inline = $url;
  244. }
  245. $options = array_merge($type, $options);
  246. $out = null;
  247. if (isset($options['link'])) {
  248. if (isset($options['rel']) && $options['rel'] === 'icon') {
  249. $out = sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('link'), ' ', ' '));
  250. $options['rel'] = 'shortcut icon';
  251. } else {
  252. $options['link'] = $this->url($options['link'], true);
  253. }
  254. $out .= sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('link'), ' ', ' '));
  255. } else {
  256. $out = sprintf($this->_tags['meta'], $this->_parseAttributes($options, array('type'), ' ', ' '));
  257. }
  258. if ($inline) {
  259. return $out;
  260. } else {
  261. $this->_View->addScript($out);
  262. }
  263. }
  264. /**
  265. * Returns a charset META-tag.
  266. *
  267. * @param string $charset The character set to be used in the meta tag. If empty,
  268. * The App.encoding value will be used. Example: "utf-8".
  269. * @return string A meta tag containing the specified character set.
  270. * @access public
  271. * @link http://book.cakephp.org/view/1436/charset
  272. */
  273. public function charset($charset = null) {
  274. if (empty($charset)) {
  275. $charset = strtolower(Configure::read('App.encoding'));
  276. }
  277. return sprintf($this->_tags['charset'], (!empty($charset) ? $charset : 'utf-8'));
  278. }
  279. /**
  280. * Creates an HTML link.
  281. *
  282. * If $url starts with "http://" this is treated as an external link. Else,
  283. * it is treated as a path to controller/action and parsed with the
  284. * HtmlHelper::url() method.
  285. *
  286. * If the $url is empty, $title is used instead.
  287. *
  288. * ### Options
  289. *
  290. * - `escape` Set to false to disable escaping of title and attributes.
  291. *
  292. * @param string $title The content to be wrapped by <a> tags.
  293. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  294. * @param array $options Array of HTML attributes.
  295. * @param string $confirmMessage JavaScript confirmation message.
  296. * @return string An `<a />` element.
  297. * @access public
  298. * @link http://book.cakephp.org/view/1442/link
  299. */
  300. public function link($title, $url = null, $options = array(), $confirmMessage = false) {
  301. $escapeTitle = true;
  302. if ($url !== null) {
  303. $url = $this->url($url);
  304. } else {
  305. $url = $this->url($title);
  306. $title = $url;
  307. $escapeTitle = false;
  308. }
  309. if (isset($options['escape'])) {
  310. $escapeTitle = $options['escape'];
  311. }
  312. if ($escapeTitle === true) {
  313. $title = h($title);
  314. } elseif (is_string($escapeTitle)) {
  315. $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
  316. }
  317. if (!empty($options['confirm'])) {
  318. $confirmMessage = $options['confirm'];
  319. unset($options['confirm']);
  320. }
  321. if ($confirmMessage) {
  322. $confirmMessage = str_replace("'", "\'", $confirmMessage);
  323. $confirmMessage = str_replace('"', '\"', $confirmMessage);
  324. $options['onclick'] = "return confirm('{$confirmMessage}');";
  325. } elseif (isset($options['default']) && $options['default'] == false) {
  326. if (isset($options['onclick'])) {
  327. $options['onclick'] .= ' event.returnValue = false; return false;';
  328. } else {
  329. $options['onclick'] = 'event.returnValue = false; return false;';
  330. }
  331. unset($options['default']);
  332. }
  333. return sprintf($this->_tags['link'], $url, $this->_parseAttributes($options), $title);
  334. }
  335. /**
  336. * Creates a link element for CSS stylesheets.
  337. *
  338. * ### Options
  339. *
  340. * - `inline` If set to false, the generated tag appears in the head tag of the layout. Defaults to true
  341. *
  342. * @param mixed $path The name of a CSS style sheet or an array containing names of
  343. * CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
  344. * of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
  345. * @param string $rel Rel attribute. Defaults to "stylesheet". If equal to 'import' the stylesheet will be imported.
  346. * @param array $options Array of HTML attributes.
  347. * @return string CSS <link /> or <style /> tag, depending on the type of link.
  348. * @access public
  349. * @link http://book.cakephp.org/view/1437/css
  350. */
  351. public function css($path, $rel = null, $options = array()) {
  352. $options += array('inline' => true);
  353. if (is_array($path)) {
  354. $out = '';
  355. foreach ($path as $i) {
  356. $out .= "\n\t" . $this->css($i, $rel, $options);
  357. }
  358. if ($options['inline']) {
  359. return $out . "\n";
  360. }
  361. return;
  362. }
  363. if (strpos($path, '://') !== false) {
  364. $url = $path;
  365. } else {
  366. if ($path[0] !== '/') {
  367. $path = CSS_URL . $path;
  368. }
  369. if (strpos($path, '?') === false) {
  370. if (substr($path, -4) !== '.css') {
  371. $path .= '.css';
  372. }
  373. }
  374. $url = $this->assetTimestamp($this->webroot($path));
  375. if (Configure::read('Asset.filter.css')) {
  376. $pos = strpos($url, CSS_URL);
  377. if ($pos !== false) {
  378. $url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(CSS_URL));
  379. }
  380. }
  381. }
  382. if ($rel == 'import') {
  383. $out = sprintf($this->_tags['style'], $this->_parseAttributes($options, array('inline'), '', ' '), '@import url(' . $url . ');');
  384. } else {
  385. if ($rel == null) {
  386. $rel = 'stylesheet';
  387. }
  388. $out = sprintf($this->_tags['css'], $rel, $url, $this->_parseAttributes($options, array('inline'), '', ' '));
  389. }
  390. if ($options['inline']) {
  391. return $out;
  392. } else {
  393. $this->_View->addScript($out);
  394. }
  395. }
  396. /**
  397. * Returns one or many `<script>` tags depending on the number of scripts given.
  398. *
  399. * If the filename is prefixed with "/", the path will be relative to the base path of your
  400. * application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
  401. *
  402. * Can include one or many Javascript files.
  403. *
  404. * ### Options
  405. *
  406. * - `inline` - Whether script should be output inline or into scripts_for_layout.
  407. * - `once` - Whether or not the script should be checked for uniqueness. If true scripts will only be
  408. * included once, use false to allow the same script to be included more than once per request.
  409. *
  410. * @param mixed $url String or array of javascript files to include
  411. * @param mixed $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
  412. * @return mixed String of `<script />` tags or null if $inline is false or if $once is true and the file has been
  413. * included before.
  414. * @access public
  415. * @link http://book.cakephp.org/view/1589/script
  416. */
  417. public function script($url, $options = array()) {
  418. if (is_bool($options)) {
  419. list($inline, $options) = array($options, array());
  420. $options['inline'] = $inline;
  421. }
  422. $options = array_merge(array('inline' => true, 'once' => true), $options);
  423. if (is_array($url)) {
  424. $out = '';
  425. foreach ($url as $i) {
  426. $out .= "\n\t" . $this->script($i, $options);
  427. }
  428. if ($options['inline']) {
  429. return $out . "\n";
  430. }
  431. return null;
  432. }
  433. if ($options['once'] && isset($this->__includedScripts[$url])) {
  434. return null;
  435. }
  436. $this->__includedScripts[$url] = true;
  437. if (strpos($url, '://') === false) {
  438. if ($url[0] !== '/') {
  439. $url = JS_URL . $url;
  440. }
  441. if (strpos($url, '?') === false && substr($url, -3) !== '.js') {
  442. $url .= '.js';
  443. }
  444. $url = $this->assetTimestamp($this->webroot($url));
  445. if (Configure::read('Asset.filter.js')) {
  446. $url = str_replace(JS_URL, 'cjs/', $url);
  447. }
  448. }
  449. $attributes = $this->_parseAttributes($options, array('inline', 'once'), ' ');
  450. $out = sprintf($this->_tags['javascriptlink'], $url, $attributes);
  451. if ($options['inline']) {
  452. return $out;
  453. } else {
  454. $this->_View->addScript($out);
  455. }
  456. }
  457. /**
  458. * Wrap $script in a script tag.
  459. *
  460. * ### Options
  461. *
  462. * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
  463. * - `inline` (boolean) Whether or not the $script should be added to $scripts_for_layout or output inline
  464. *
  465. * @param string $script The script to wrap
  466. * @param array $options The options to use.
  467. * @return mixed string or null depending on the value of `$options['inline']`
  468. * @access public
  469. * @link http://book.cakephp.org/view/1604/scriptBlock
  470. */
  471. public function scriptBlock($script, $options = array()) {
  472. $options += array('safe' => true, 'inline' => true);
  473. if ($options['safe']) {
  474. $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
  475. }
  476. $inline = $options['inline'];
  477. unset($options['inline'], $options['safe']);
  478. $attributes = $this->_parseAttributes($options, ' ', ' ');
  479. if ($inline) {
  480. return sprintf($this->_tags['javascriptblock'], $attributes, $script);
  481. } else {
  482. $this->_View->addScript(sprintf($this->_tags['javascriptblock'], $attributes, $script));
  483. return null;
  484. }
  485. }
  486. /**
  487. * Begin a script block that captures output until HtmlHelper::scriptEnd()
  488. * is called. This capturing block will capture all output between the methods
  489. * and create a scriptBlock from it.
  490. *
  491. * ### Options
  492. *
  493. * - `safe` Whether the code block should contain a CDATA
  494. * - `inline` Should the generated script tag be output inline or in `$scripts_for_layout`
  495. *
  496. * @param array $options Options for the code block.
  497. * @return void
  498. * @access public
  499. * @link http://book.cakephp.org/view/1605/scriptStart
  500. */
  501. public function scriptStart($options = array()) {
  502. $options += array('safe' => true, 'inline' => true);
  503. $this->_scriptBlockOptions = $options;
  504. ob_start();
  505. return null;
  506. }
  507. /**
  508. * End a Buffered section of Javascript capturing.
  509. * Generates a script tag inline or in `$scripts_for_layout` depending on the settings
  510. * used when the scriptBlock was started
  511. *
  512. * @return mixed depending on the settings of scriptStart() either a script tag or null
  513. * @access public
  514. * @link http://book.cakephp.org/view/1606/scriptEnd
  515. */
  516. public function scriptEnd() {
  517. $buffer = ob_get_clean();
  518. $options = $this->_scriptBlockOptions;
  519. $this->_scriptBlockOptions = array();
  520. return $this->scriptBlock($buffer, $options);
  521. }
  522. /**
  523. * Builds CSS style data from an array of CSS properties
  524. *
  525. * ### Usage:
  526. *
  527. * {{{
  528. * echo $html->style(array('margin' => '10px', 'padding' => '10px'), true);
  529. *
  530. * // creates
  531. * 'margin:10px;padding:10px;'
  532. * }}}
  533. *
  534. * @param array $data Style data array, keys will be used as property names, values as property values.
  535. * @param boolean $oneline Whether or not the style block should be displayed on one line.
  536. * @return string CSS styling data
  537. * @access public
  538. * @link http://book.cakephp.org/view/1440/style
  539. */
  540. public function style($data, $oneline = true) {
  541. if (!is_array($data)) {
  542. return $data;
  543. }
  544. $out = array();
  545. foreach ($data as $key=> $value) {
  546. $out[] = $key.':'.$value.';';
  547. }
  548. if ($oneline) {
  549. return join(' ', $out);
  550. }
  551. return implode("\n", $out);
  552. }
  553. /**
  554. * Returns the breadcrumb trail as a sequence of &raquo;-separated links.
  555. *
  556. * @param string $separator Text to separate crumbs.
  557. * @param string $startText This will be the first crumb, if false it defaults to first crumb in array
  558. * @return string Composed bread crumbs
  559. */
  560. public function getCrumbs($separator = '&raquo;', $startText = false) {
  561. if (!empty($this->_crumbs)) {
  562. $out = array();
  563. if ($startText) {
  564. $out[] = $this->link($startText, '/');
  565. }
  566. foreach ($this->_crumbs as $crumb) {
  567. if (!empty($crumb[1])) {
  568. $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
  569. } else {
  570. $out[] = $crumb[0];
  571. }
  572. }
  573. return join($separator, $out);
  574. } else {
  575. return null;
  576. }
  577. }
  578. /**
  579. * Returns breadcrumbs as a (x)html list
  580. *
  581. * This method uses HtmlHelper::tag() to generate list and its elements. Works
  582. * similiary to HtmlHelper::getCrumbs(), so it uses options which every
  583. * crumb was added with.
  584. *
  585. * @param array $options Array of html attributes to apply to the generated list elements.
  586. * @return string breadcrumbs html list
  587. * @access public
  588. */
  589. function getCrumbList($options = array()) {
  590. if (!empty($this->_crumbs)) {
  591. $result = '';
  592. $crumbCount = count($this->_crumbs);
  593. $ulOptions = $options;
  594. foreach ($this->_crumbs as $which => $crumb) {
  595. $options = array();
  596. if (empty($crumb[1])) {
  597. $elementContent = $crumb[0];
  598. } else {
  599. $elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
  600. }
  601. if ($which == 0) {
  602. $options['class'] = 'first';
  603. } elseif ($which == $crumbCount - 1) {
  604. $options['class'] = 'last';
  605. }
  606. $result .= $this->tag('li', $elementContent, $options);
  607. }
  608. return $this->tag('ul', $result, $ulOptions);
  609. } else {
  610. return null;
  611. }
  612. }
  613. /**
  614. * Creates a formatted IMG element. If `$options['url']` is provided, an image link will be
  615. * generated with the link pointed at `$options['url']`. This method will set an empty
  616. * alt attribute if one is not supplied.
  617. *
  618. * ### Usage
  619. *
  620. * Create a regular image:
  621. *
  622. * `echo $html->image('cake_icon.png', array('alt' => 'CakePHP'));`
  623. *
  624. * Create an image link:
  625. *
  626. * `echo $html->image('cake_icon.png', array('alt' => 'CakePHP', 'url' => 'http://cakephp.org'));`
  627. *
  628. * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
  629. * @param array $options Array of HTML attributes.
  630. * @return string completed img tag
  631. * @access public
  632. * @link http://book.cakephp.org/view/1441/image
  633. */
  634. public function image($path, $options = array()) {
  635. if (is_array($path)) {
  636. $path = $this->url($path);
  637. } elseif (strpos($path, '://') === false) {
  638. if ($path[0] !== '/') {
  639. $path = IMAGES_URL . $path;
  640. }
  641. $path = $this->assetTimestamp($this->webroot($path));
  642. }
  643. if (!isset($options['alt'])) {
  644. $options['alt'] = '';
  645. }
  646. $url = false;
  647. if (!empty($options['url'])) {
  648. $url = $options['url'];
  649. unset($options['url']);
  650. }
  651. $image = sprintf($this->_tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
  652. if ($url) {
  653. return sprintf($this->_tags['link'], $this->url($url), null, $image);
  654. }
  655. return $image;
  656. }
  657. /**
  658. * Returns a row of formatted and named TABLE headers.
  659. *
  660. * @param array $names Array of tablenames.
  661. * @param array $trOptions HTML options for TR elements.
  662. * @param array $thOptions HTML options for TH elements.
  663. * @return string Completed table headers
  664. * @access public
  665. * @link http://book.cakephp.org/view/1446/tableHeaders
  666. */
  667. public function tableHeaders($names, $trOptions = null, $thOptions = null) {
  668. $out = array();
  669. foreach ($names as $arg) {
  670. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
  671. }
  672. return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
  673. }
  674. /**
  675. * Returns a formatted string of table rows (TR's with TD's in them).
  676. *
  677. * @param array $data Array of table data
  678. * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
  679. * @param array $evenTrOptions HTML options for even TR elements
  680. * @param bool $useCount adds class "column-$i"
  681. * @param bool $continueOddEven If false, will use a non-static $count variable,
  682. * so that the odd/even count is reset to zero just for that call.
  683. * @return string Formatted HTML
  684. * @access public
  685. * @link http://book.cakephp.org/view/1447/tableCells
  686. */
  687. public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
  688. if (empty($data[0]) || !is_array($data[0])) {
  689. $data = array($data);
  690. }
  691. if ($oddTrOptions === true) {
  692. $useCount = true;
  693. $oddTrOptions = null;
  694. }
  695. if ($evenTrOptions === false) {
  696. $continueOddEven = false;
  697. $evenTrOptions = null;
  698. }
  699. if ($continueOddEven) {
  700. static $count = 0;
  701. } else {
  702. $count = 0;
  703. }
  704. foreach ($data as $line) {
  705. $count++;
  706. $cellsOut = array();
  707. $i = 0;
  708. foreach ($line as $cell) {
  709. $cellOptions = array();
  710. if (is_array($cell)) {
  711. $cellOptions = $cell[1];
  712. $cell = $cell[0];
  713. } elseif ($useCount) {
  714. $cellOptions['class'] = 'column-' . ++$i;
  715. }
  716. $cellsOut[] = sprintf($this->_tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
  717. }
  718. $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
  719. $out[] = sprintf($this->_tags['tablerow'], $options, implode(' ', $cellsOut));
  720. }
  721. return implode("\n", $out);
  722. }
  723. /**
  724. * Returns a formatted block tag, i.e DIV, SPAN, P.
  725. *
  726. * ### Options
  727. *
  728. * - `escape` Whether or not the contents should be html_entity escaped.
  729. *
  730. * @param string $name Tag name.
  731. * @param string $text String content that will appear inside the div element.
  732. * If null, only a start tag will be printed
  733. * @param array $options Additional HTML attributes of the DIV tag, see above.
  734. * @return string The formatted tag element
  735. * @access public
  736. * @link http://book.cakephp.org/view/1443/tag
  737. */
  738. public function tag($name, $text = null, $options = array()) {
  739. if (is_array($options) && isset($options['escape']) && $options['escape']) {
  740. $text = h($text);
  741. unset($options['escape']);
  742. }
  743. if (!is_array($options)) {
  744. $options = array('class' => $options);
  745. }
  746. if ($text === null) {
  747. $tag = 'tagstart';
  748. } else {
  749. $tag = 'tag';
  750. }
  751. return sprintf($this->_tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
  752. }
  753. /**
  754. * Returns a formatted existent block of $tags
  755. *
  756. * @param string $tag Tag name
  757. * @return string Formatted block
  758. */
  759. public function useTag($tag) {
  760. if (!isset($this->_tags[$tag])) {
  761. return '';
  762. }
  763. $args = func_get_args();
  764. array_shift($args);
  765. foreach ($args as &$arg) {
  766. if (is_array($arg)) {
  767. $arg = $this->_parseAttributes($arg, null, ' ', '');
  768. }
  769. }
  770. return vsprintf($this->_tags[$tag], $args);
  771. }
  772. /**
  773. * Returns a formatted DIV tag for HTML FORMs.
  774. *
  775. * ### Options
  776. *
  777. * - `escape` Whether or not the contents should be html_entity escaped.
  778. *
  779. * @param string $class CSS class name of the div element.
  780. * @param string $text String content that will appear inside the div element.
  781. * If null, only a start tag will be printed
  782. * @param array $options Additional HTML attributes of the DIV tag
  783. * @return string The formatted DIV element
  784. * @access public
  785. * @link http://book.cakephp.org/view/1444/div
  786. */
  787. public function div($class = null, $text = null, $options = array()) {
  788. if (!empty($class)) {
  789. $options['class'] = $class;
  790. }
  791. return $this->tag('div', $text, $options);
  792. }
  793. /**
  794. * Returns a formatted P tag.
  795. *
  796. * ### Options
  797. *
  798. * - `escape` Whether or not the contents should be html_entity escaped.
  799. *
  800. * @param string $class CSS class name of the p element.
  801. * @param string $text String content that will appear inside the p element.
  802. * @param array $options Additional HTML attributes of the P tag
  803. * @return string The formatted P element
  804. * @access public
  805. * @link http://book.cakephp.org/view/1445/para
  806. */
  807. public function para($class, $text, $options = array()) {
  808. if (isset($options['escape'])) {
  809. $text = h($text);
  810. }
  811. if ($class != null && !empty($class)) {
  812. $options['class'] = $class;
  813. }
  814. if ($text === null) {
  815. $tag = 'parastart';
  816. } else {
  817. $tag = 'para';
  818. }
  819. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
  820. }
  821. /**
  822. * Build a nested list (UL/OL) out of an associative array.
  823. *
  824. * @param array $list Set of elements to list
  825. * @param array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
  826. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  827. * @param string $tag Type of list tag to use (ol/ul)
  828. * @return string The nested list
  829. */
  830. public function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
  831. if (is_string($options)) {
  832. $tag = $options;
  833. $options = array();
  834. }
  835. $items = $this->__nestedListItem($list, $options, $itemOptions, $tag);
  836. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
  837. }
  838. /**
  839. * Internal function to build a nested list (UL/OL) out of an associative array.
  840. *
  841. * @param array $items Set of elements to list
  842. * @param array $options Additional HTML attributes of the list (ol/ul) tag
  843. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  844. * @param string $tag Type of list tag to use (ol/ul)
  845. * @return string The nested list element
  846. * @access private
  847. * @see HtmlHelper::nestedList()
  848. */
  849. function __nestedListItem($items, $options, $itemOptions, $tag) {
  850. $out = '';
  851. $index = 1;
  852. foreach ($items as $key => $item) {
  853. if (is_array($item)) {
  854. $item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
  855. }
  856. if (isset($itemOptions['even']) && $index % 2 == 0) {
  857. $itemOptions['class'] = $itemOptions['even'];
  858. } else if (isset($itemOptions['odd']) && $index % 2 != 0) {
  859. $itemOptions['class'] = $itemOptions['odd'];
  860. }
  861. $out .= sprintf($this->_tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
  862. $index++;
  863. }
  864. return $out;
  865. }
  866. /**
  867. * Load Html configs
  868. *
  869. * @param mixed $configFile String with the config file (load using PhpReader) or an array with file and reader name
  870. * @param string $path Path with config file
  871. * @return mixed False to error or loaded configs
  872. */
  873. public function loadConfig($configFile, $path = null) {
  874. if (!$path) {
  875. $path = APP . 'Config' . DS;
  876. }
  877. $file = null;
  878. $reader = 'php';
  879. if (!is_array($configFile)) {
  880. $file = $configFile;
  881. } elseif (isset($configFile[0])) {
  882. $file = $configFile[0];
  883. if (isset($configFile[1])) {
  884. $reader = $configFile[1];
  885. }
  886. } else {
  887. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Wrong "configFile" configuration.'));
  888. }
  889. $readerClass = Inflector::camelize($reader) . 'Reader';
  890. App::uses($readerClass, 'Configure');
  891. if (!class_exists($readerClass)) {
  892. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Unknown reader.'));
  893. }
  894. $readerObj = new $readerClass($path);
  895. $configs = $readerObj->read($file);
  896. if (isset($configs['tags']) && is_array($configs['tags'])) {
  897. $this->_tags = array_merge($this->_tags, $configs['tags']);
  898. }
  899. if (isset($configs['minimizedAttributes']) && is_array($configs['minimizedAttributes'])) {
  900. $this->_minimizedAttributes = array_merge($this->_minimizedAttributes, $configs['minimizedAttributes']);
  901. }
  902. if (isset($configs['docTypes']) && is_array($configs['docTypes'])) {
  903. $this->__docTypes = array_merge($this->__docTypes, $configs['docTypes']);
  904. }
  905. if (isset($configs['attributeFormat'])) {
  906. $this->_attributeFormat = $configs['attributeFormat'];
  907. }
  908. if (isset($configs['minimizedAttributeFormat'])) {
  909. $this->_minimizedAttributeFormat = $configs['minimizedAttributeFormat'];
  910. }
  911. return $configs;
  912. }
  913. /**
  914. * Returns a space-delimited string with items of the $options array. If a
  915. * key of $options array happens to be one of:
  916. *
  917. * - 'compact'
  918. * - 'checked'
  919. * - 'declare'
  920. * - 'readonly'
  921. * - 'disabled'
  922. * - 'selected'
  923. * - 'defer'
  924. * - 'ismap'
  925. * - 'nohref'
  926. * - 'noshade'
  927. * - 'nowrap'
  928. * - 'multiple'
  929. * - 'noresize'
  930. *
  931. * And its value is one of:
  932. *
  933. * - '1' (string)
  934. * - 1 (integer)
  935. * - true (boolean)
  936. * - 'true' (string)
  937. *
  938. * Then the value will be reset to be identical with key's name.
  939. * If the value is not one of these 3, the parameter is not output.
  940. *
  941. * 'escape' is a special option in that it controls the conversion of
  942. * attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.
  943. *
  944. * If value for any option key is set to `null` or `false`, that option will be excluded from output.
  945. *
  946. * @param array $options Array of options.
  947. * @param array $exclude Array of options to be excluded, the options here will not be part of the return.
  948. * @param string $insertBefore String to be inserted before options.
  949. * @param string $insertAfter String to be inserted after options.
  950. * @return string Composed attributes.
  951. */
  952. public function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  953. if (is_array($options)) {
  954. $options = array_merge(array('escape' => true), $options);
  955. if (!is_array($exclude)) {
  956. $exclude = array();
  957. }
  958. $filtered = array_diff_key($options, array_merge(array_flip($exclude), array('escape' => true)));
  959. $escape = $options['escape'];
  960. $attributes = array();
  961. foreach ($filtered as $key => $value) {
  962. if ($value !== false && $value !== null) {
  963. $attributes[] = $this->_formatAttribute($key, $value, $escape);
  964. }
  965. }
  966. $out = implode(' ', $attributes);
  967. } else {
  968. $out = $options;
  969. }
  970. return $out ? $insertBefore . $out . $insertAfter : '';
  971. }
  972. /**
  973. * Formats an individual attribute, and returns the string value of the composed attribute.
  974. * Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
  975. *
  976. * @param string $key The name of the attribute to create
  977. * @param string $value The value of the attribute to create.
  978. * @return string The composed attribute.
  979. */
  980. protected function _formatAttribute($key, $value, $escape = true) {
  981. $attribute = '';
  982. if (is_array($value)) {
  983. $value = '';
  984. }
  985. if (is_numeric($key)) {
  986. $attribute = sprintf($this->_minimizedAttributeFormat, $value, $value);
  987. } elseif (in_array($key, $this->_minimizedAttributes)) {
  988. if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
  989. $attribute = sprintf($this->_minimizedAttributeFormat, $key, $key);
  990. }
  991. } else {
  992. $attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
  993. }
  994. return $attribute;
  995. }
  996. }