HtmlHelper.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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
  16. * @subpackage cake.cake.libs.view.helpers
  17. * @since CakePHP(tm) v 0.9.1
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  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
  26. * @subpackage cake.cake.libs.view.helpers
  27. * @link http://book.cakephp.org/view/1434/HTML
  28. */
  29. class HtmlHelper extends AppHelper {
  30. /**
  31. * html tags used by this helper.
  32. *
  33. * @var array
  34. * @access public
  35. */
  36. public $tags = array(
  37. 'meta' => '<meta%s/>',
  38. 'metalink' => '<link href="%s"%s/>',
  39. 'link' => '<a href="%s"%s>%s</a>',
  40. 'mailto' => '<a href="mailto:%s" %s>%s</a>',
  41. 'form' => '<form %s>',
  42. 'formend' => '</form>',
  43. 'input' => '<input name="%s" %s/>',
  44. 'textarea' => '<textarea name="%s" %s>%s</textarea>',
  45. 'hidden' => '<input type="hidden" name="%s" %s/>',
  46. 'checkbox' => '<input type="checkbox" name="%s" %s/>',
  47. 'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
  48. 'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
  49. 'selectstart' => '<select name="%s"%s>',
  50. 'selectmultiplestart' => '<select name="%s[]"%s>',
  51. 'selectempty' => '<option value=""%s>&nbsp;</option>',
  52. 'selectoption' => '<option value="%s"%s>%s</option>',
  53. 'selectend' => '</select>',
  54. 'optiongroup' => '<optgroup label="%s"%s>',
  55. 'optiongroupend' => '</optgroup>',
  56. 'checkboxmultiplestart' => '',
  57. 'checkboxmultipleend' => '',
  58. 'password' => '<input type="password" name="%s" %s/>',
  59. 'file' => '<input type="file" name="%s" %s/>',
  60. 'file_no_model' => '<input type="file" name="%s" %s/>',
  61. 'submit' => '<input %s/>',
  62. 'submitimage' => '<input type="image" src="%s" %s/>',
  63. 'button' => '<button type="%s"%s>%s</button>',
  64. 'image' => '<img src="%s" %s/>',
  65. 'tableheader' => '<th%s>%s</th>',
  66. 'tableheaderrow' => '<tr%s>%s</tr>',
  67. 'tablecell' => '<td%s>%s</td>',
  68. 'tablerow' => '<tr%s>%s</tr>',
  69. 'block' => '<div%s>%s</div>',
  70. 'blockstart' => '<div%s>',
  71. 'blockend' => '</div>',
  72. 'tag' => '<%s%s>%s</%s>',
  73. 'tagstart' => '<%s%s>',
  74. 'tagend' => '</%s>',
  75. 'para' => '<p%s>%s</p>',
  76. 'parastart' => '<p%s>',
  77. 'label' => '<label for="%s"%s>%s</label>',
  78. 'fieldset' => '<fieldset%s>%s</fieldset>',
  79. 'fieldsetstart' => '<fieldset><legend>%s</legend>',
  80. 'fieldsetend' => '</fieldset>',
  81. 'legend' => '<legend>%s</legend>',
  82. 'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
  83. 'style' => '<style type="text/css"%s>%s</style>',
  84. 'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
  85. 'ul' => '<ul%s>%s</ul>',
  86. 'ol' => '<ol%s>%s</ol>',
  87. 'li' => '<li%s>%s</li>',
  88. 'error' => '<div%s>%s</div>',
  89. 'javascriptblock' => '<script type="text/javascript"%s>%s</script>',
  90. 'javascriptstart' => '<script type="text/javascript">',
  91. 'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
  92. 'javascriptend' => '</script>'
  93. );
  94. /**
  95. * Breadcrumbs.
  96. *
  97. * @var array
  98. * @access protected
  99. */
  100. protected $_crumbs = array();
  101. /**
  102. * Names of script files that have been included once
  103. *
  104. * @var array
  105. * @access private
  106. */
  107. private $__includedScripts = array();
  108. /**
  109. * Options for the currently opened script block buffer if any.
  110. *
  111. * @var array
  112. * @access protected
  113. */
  114. protected $_scriptBlockOptions = array();
  115. /**
  116. * Document type definitions
  117. *
  118. * @var array
  119. * @access private
  120. */
  121. private $__docTypes = array(
  122. 'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
  123. 'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  124. 'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
  125. 'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  126. 'xhtml-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
  127. 'xhtml-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
  128. 'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
  129. );
  130. /**
  131. * Adds a link to the breadcrumbs array.
  132. *
  133. * @param string $name Text for link
  134. * @param string $link URL for link (if empty it won't be a link)
  135. * @param mixed $options Link attributes e.g. array('id'=>'selected')
  136. * @return void
  137. * @see HtmlHelper::link() for details on $options that can be used.
  138. */
  139. public function addCrumb($name, $link = null, $options = null) {
  140. $this->_crumbs[] = array($name, $link, $options);
  141. }
  142. /**
  143. * Returns a doctype string.
  144. *
  145. * Possible doctypes:
  146. *
  147. * - html4-strict: HTML4 Strict.
  148. * - html4-trans: HTML4 Transitional.
  149. * - html4-frame: HTML4 Frameset.
  150. * - xhtml-strict: XHTML1 Strict.
  151. * - xhtml-trans: XHTML1 Transitional.
  152. * - xhtml-frame: XHTML1 Frameset.
  153. * - xhtml11: XHTML1.1.
  154. *
  155. * @param string $type Doctype to use.
  156. * @return string Doctype string
  157. * @access public
  158. * @link http://book.cakephp.org/view/1439/docType
  159. */
  160. public function docType($type = 'xhtml-strict') {
  161. if (isset($this->__docTypes[$type])) {
  162. return $this->__docTypes[$type];
  163. }
  164. return null;
  165. }
  166. /**
  167. * Creates a link to an external resource and handles basic meta tags
  168. *
  169. * ### Options
  170. *
  171. * - `inline` Whether or not the link element should be output inline, or in scripts_for_layout.
  172. *
  173. * @param string $type The title of the external resource
  174. * @param mixed $url The address of the external resource or string for content attribute
  175. * @param array $options Other attributes for the generated tag. If the type attribute is html,
  176. * rss, atom, or icon, the mime-type is returned.
  177. * @return string A completed `<link />` element.
  178. * @access public
  179. * @link http://book.cakephp.org/view/1438/meta
  180. */
  181. public function meta($type, $url = null, $options = array()) {
  182. $inline = isset($options['inline']) ? $options['inline'] : true;
  183. unset($options['inline']);
  184. if (!is_array($type)) {
  185. $types = array(
  186. 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
  187. 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
  188. 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
  189. 'keywords' => array('name' => 'keywords', 'content' => $url),
  190. 'description' => array('name' => 'description', 'content' => $url),
  191. );
  192. if ($type === 'icon' && $url === null) {
  193. $types['icon']['link'] = $this->webroot('favicon.ico');
  194. }
  195. if (isset($types[$type])) {
  196. $type = $types[$type];
  197. } elseif (!isset($options['type']) && $url !== null) {
  198. if (is_array($url) && isset($url['ext'])) {
  199. $type = $types[$url['ext']];
  200. } else {
  201. $type = $types['rss'];
  202. }
  203. } elseif (isset($options['type']) && isset($types[$options['type']])) {
  204. $type = $types[$options['type']];
  205. unset($options['type']);
  206. } else {
  207. $type = array();
  208. }
  209. } elseif ($url !== null) {
  210. $inline = $url;
  211. }
  212. $options = array_merge($type, $options);
  213. $out = null;
  214. if (isset($options['link'])) {
  215. if (isset($options['rel']) && $options['rel'] === 'icon') {
  216. $out = sprintf($this->tags['metalink'], $options['link'], $this->_parseAttributes($options, array('link'), ' ', ' '));
  217. $options['rel'] = 'shortcut icon';
  218. } else {
  219. $options['link'] = $this->url($options['link'], true);
  220. }
  221. $out .= sprintf($this->tags['metalink'], $options['link'], $this->_parseAttributes($options, array('link'), ' ', ' '));
  222. } else {
  223. $out = sprintf($this->tags['meta'], $this->_parseAttributes($options, array('type'), ' ', ' '));
  224. }
  225. if ($inline) {
  226. return $out;
  227. } else {
  228. $this->_View->addScript($out);
  229. }
  230. }
  231. /**
  232. * Returns a charset META-tag.
  233. *
  234. * @param string $charset The character set to be used in the meta tag. If empty,
  235. * The App.encoding value will be used. Example: "utf-8".
  236. * @return string A meta tag containing the specified character set.
  237. * @access public
  238. * @link http://book.cakephp.org/view/1436/charset
  239. */
  240. public function charset($charset = null) {
  241. if (empty($charset)) {
  242. $charset = strtolower(Configure::read('App.encoding'));
  243. }
  244. return sprintf($this->tags['charset'], (!empty($charset) ? $charset : 'utf-8'));
  245. }
  246. /**
  247. * Creates an HTML link.
  248. *
  249. * If $url starts with "http://" this is treated as an external link. Else,
  250. * it is treated as a path to controller/action and parsed with the
  251. * HtmlHelper::url() method.
  252. *
  253. * If the $url is empty, $title is used instead.
  254. *
  255. * ### Options
  256. *
  257. * - `escape` Set to false to disable escaping of title and attributes.
  258. *
  259. * @param string $title The content to be wrapped by <a> tags.
  260. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  261. * @param array $options Array of HTML attributes.
  262. * @param string $confirmMessage JavaScript confirmation message.
  263. * @return string An `<a />` element.
  264. * @access public
  265. * @link http://book.cakephp.org/view/1442/link
  266. */
  267. public function link($title, $url = null, $options = array(), $confirmMessage = false) {
  268. $escapeTitle = true;
  269. if ($url !== null) {
  270. $url = $this->url($url);
  271. } else {
  272. $url = $this->url($title);
  273. $title = $url;
  274. $escapeTitle = false;
  275. }
  276. if (isset($options['escape'])) {
  277. $escapeTitle = $options['escape'];
  278. }
  279. if ($escapeTitle === true) {
  280. $title = h($title);
  281. } elseif (is_string($escapeTitle)) {
  282. $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
  283. }
  284. if (!empty($options['confirm'])) {
  285. $confirmMessage = $options['confirm'];
  286. unset($options['confirm']);
  287. }
  288. if ($confirmMessage) {
  289. $confirmMessage = str_replace("'", "\'", $confirmMessage);
  290. $confirmMessage = str_replace('"', '\"', $confirmMessage);
  291. $options['onclick'] = "return confirm('{$confirmMessage}');";
  292. } elseif (isset($options['default']) && $options['default'] == false) {
  293. if (isset($options['onclick'])) {
  294. $options['onclick'] .= ' event.returnValue = false; return false;';
  295. } else {
  296. $options['onclick'] = 'event.returnValue = false; return false;';
  297. }
  298. unset($options['default']);
  299. }
  300. return sprintf($this->tags['link'], $url, $this->_parseAttributes($options), $title);
  301. }
  302. /**
  303. * Creates a link element for CSS stylesheets.
  304. *
  305. * ### Options
  306. *
  307. * - `inline` If set to false, the generated tag appears in the head tag of the layout. Defaults to true
  308. *
  309. * @param mixed $path The name of a CSS style sheet or an array containing names of
  310. * CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
  311. * of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
  312. * @param string $rel Rel attribute. Defaults to "stylesheet". If equal to 'import' the stylesheet will be imported.
  313. * @param array $options Array of HTML attributes.
  314. * @return string CSS <link /> or <style /> tag, depending on the type of link.
  315. * @access public
  316. * @link http://book.cakephp.org/view/1437/css
  317. */
  318. public function css($path, $rel = null, $options = array()) {
  319. $options += array('inline' => true);
  320. if (is_array($path)) {
  321. $out = '';
  322. foreach ($path as $i) {
  323. $out .= "\n\t" . $this->css($i, $rel, $options);
  324. }
  325. if ($options['inline']) {
  326. return $out . "\n";
  327. }
  328. return;
  329. }
  330. if (strpos($path, '://') !== false) {
  331. $url = $path;
  332. } else {
  333. if ($path[0] !== '/') {
  334. $path = CSS_URL . $path;
  335. }
  336. if (strpos($path, '?') === false) {
  337. if (substr($path, -4) !== '.css') {
  338. $path .= '.css';
  339. }
  340. }
  341. $url = $this->assetTimestamp($this->webroot($path));
  342. if (Configure::read('Asset.filter.css')) {
  343. $pos = strpos($url, CSS_URL);
  344. if ($pos !== false) {
  345. $url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(CSS_URL));
  346. }
  347. }
  348. }
  349. if ($rel == 'import') {
  350. $out = sprintf($this->tags['style'], $this->_parseAttributes($options, array('inline'), '', ' '), '@import url(' . $url . ');');
  351. } else {
  352. if ($rel == null) {
  353. $rel = 'stylesheet';
  354. }
  355. $out = sprintf($this->tags['css'], $rel, $url, $this->_parseAttributes($options, array('inline'), '', ' '));
  356. }
  357. if ($options['inline']) {
  358. return $out;
  359. } else {
  360. $this->_View->addScript($out);
  361. }
  362. }
  363. /**
  364. * Returns one or many `<script>` tags depending on the number of scripts given.
  365. *
  366. * If the filename is prefixed with "/", the path will be relative to the base path of your
  367. * application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
  368. *
  369. * Can include one or many Javascript files.
  370. *
  371. * ### Options
  372. *
  373. * - `inline` - Whether script should be output inline or into scripts_for_layout.
  374. * - `once` - Whether or not the script should be checked for uniqueness. If true scripts will only be
  375. * included once, use false to allow the same script to be included more than once per request.
  376. *
  377. * @param mixed $url String or array of javascript files to include
  378. * @param mixed $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
  379. * @return mixed String of `<script />` tags or null if $inline is false or if $once is true and the file has been
  380. * included before.
  381. * @access public
  382. * @link http://book.cakephp.org/view/1589/script
  383. */
  384. public function script($url, $options = array()) {
  385. if (is_bool($options)) {
  386. list($inline, $options) = array($options, array());
  387. $options['inline'] = $inline;
  388. }
  389. $options = array_merge(array('inline' => true, 'once' => true), $options);
  390. if (is_array($url)) {
  391. $out = '';
  392. foreach ($url as $i) {
  393. $out .= "\n\t" . $this->script($i, $options);
  394. }
  395. if ($options['inline']) {
  396. return $out . "\n";
  397. }
  398. return null;
  399. }
  400. if ($options['once'] && isset($this->__includedScripts[$url])) {
  401. return null;
  402. }
  403. $this->__includedScripts[$url] = true;
  404. if (strpos($url, '://') === false) {
  405. if ($url[0] !== '/') {
  406. $url = JS_URL . $url;
  407. }
  408. if (strpos($url, '?') === false && substr($url, -3) !== '.js') {
  409. $url .= '.js';
  410. }
  411. $url = $this->assetTimestamp($this->webroot($url));
  412. if (Configure::read('Asset.filter.js')) {
  413. $url = str_replace(JS_URL, 'cjs/', $url);
  414. }
  415. }
  416. $attributes = $this->_parseAttributes($options, array('inline', 'once'), ' ');
  417. $out = sprintf($this->tags['javascriptlink'], $url, $attributes);
  418. if ($options['inline']) {
  419. return $out;
  420. } else {
  421. $this->_View->addScript($out);
  422. }
  423. }
  424. /**
  425. * Wrap $script in a script tag.
  426. *
  427. * ### Options
  428. *
  429. * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
  430. * - `inline` (boolean) Whether or not the $script should be added to $scripts_for_layout or output inline
  431. *
  432. * @param string $script The script to wrap
  433. * @param array $options The options to use.
  434. * @return mixed string or null depending on the value of `$options['inline']`
  435. * @access public
  436. * @link http://book.cakephp.org/view/1604/scriptBlock
  437. */
  438. public function scriptBlock($script, $options = array()) {
  439. $options += array('safe' => true, 'inline' => true);
  440. if ($options['safe']) {
  441. $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
  442. }
  443. $inline = $options['inline'];
  444. unset($options['inline'], $options['safe']);
  445. $attributes = $this->_parseAttributes($options, ' ', ' ');
  446. if ($inline) {
  447. return sprintf($this->tags['javascriptblock'], $attributes, $script);
  448. } else {
  449. $this->_View->addScript(sprintf($this->tags['javascriptblock'], $attributes, $script));
  450. return null;
  451. }
  452. }
  453. /**
  454. * Begin a script block that captures output until HtmlHelper::scriptEnd()
  455. * is called. This capturing block will capture all output between the methods
  456. * and create a scriptBlock from it.
  457. *
  458. * ### Options
  459. *
  460. * - `safe` Whether the code block should contain a CDATA
  461. * - `inline` Should the generated script tag be output inline or in `$scripts_for_layout`
  462. *
  463. * @param array $options Options for the code block.
  464. * @return void
  465. * @access public
  466. * @link http://book.cakephp.org/view/1605/scriptStart
  467. */
  468. public function scriptStart($options = array()) {
  469. $options += array('safe' => true, 'inline' => true);
  470. $this->_scriptBlockOptions = $options;
  471. ob_start();
  472. return null;
  473. }
  474. /**
  475. * End a Buffered section of Javascript capturing.
  476. * Generates a script tag inline or in `$scripts_for_layout` depending on the settings
  477. * used when the scriptBlock was started
  478. *
  479. * @return mixed depending on the settings of scriptStart() either a script tag or null
  480. * @access public
  481. * @link http://book.cakephp.org/view/1606/scriptEnd
  482. */
  483. public function scriptEnd() {
  484. $buffer = ob_get_clean();
  485. $options = $this->_scriptBlockOptions;
  486. $this->_scriptBlockOptions = array();
  487. return $this->scriptBlock($buffer, $options);
  488. }
  489. /**
  490. * Builds CSS style data from an array of CSS properties
  491. *
  492. * ### Usage:
  493. *
  494. * {{{
  495. * echo $html->style(array('margin' => '10px', 'padding' => '10px'), true);
  496. *
  497. * // creates
  498. * 'margin:10px;padding:10px;'
  499. * }}}
  500. *
  501. * @param array $data Style data array, keys will be used as property names, values as property values.
  502. * @param boolean $oneline Whether or not the style block should be displayed on one line.
  503. * @return string CSS styling data
  504. * @access public
  505. * @link http://book.cakephp.org/view/1440/style
  506. */
  507. public function style($data, $oneline = true) {
  508. if (!is_array($data)) {
  509. return $data;
  510. }
  511. $out = array();
  512. foreach ($data as $key=> $value) {
  513. $out[] = $key.':'.$value.';';
  514. }
  515. if ($oneline) {
  516. return join(' ', $out);
  517. }
  518. return implode("\n", $out);
  519. }
  520. /**
  521. * Returns the breadcrumb trail as a sequence of &raquo;-separated links.
  522. *
  523. * @param string $separator Text to separate crumbs.
  524. * @param string $startText This will be the first crumb, if false it defaults to first crumb in array
  525. * @return string Composed bread crumbs
  526. */
  527. public function getCrumbs($separator = '&raquo;', $startText = false) {
  528. if (!empty($this->_crumbs)) {
  529. $out = array();
  530. if ($startText) {
  531. $out[] = $this->link($startText, '/');
  532. }
  533. foreach ($this->_crumbs as $crumb) {
  534. if (!empty($crumb[1])) {
  535. $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
  536. } else {
  537. $out[] = $crumb[0];
  538. }
  539. }
  540. return join($separator, $out);
  541. } else {
  542. return null;
  543. }
  544. }
  545. /**
  546. * Returns breadcrumbs as a (x)html list
  547. *
  548. * This method uses HtmlHelper::tag() to generate list and its elements. Works
  549. * similiary to HtmlHelper::getCrumbs(), so it uses options which every
  550. * crumb was added with.
  551. *
  552. * @param array $options Array of html attributes to apply to the generated list elements.
  553. * @return string breadcrumbs html list
  554. * @access public
  555. */
  556. function getCrumbList($options = array()) {
  557. if (!empty($this->_crumbs)) {
  558. $result = '';
  559. $crumbCount = count($this->_crumbs);
  560. $ulOptions = $options;
  561. foreach ($this->_crumbs as $which => $crumb) {
  562. $options = array();
  563. if (empty($crumb[1])) {
  564. $elementContent = $crumb[0];
  565. } else {
  566. $elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
  567. }
  568. if ($which == 0) {
  569. $options['class'] = 'first';
  570. } elseif ($which == $crumbCount - 1) {
  571. $options['class'] = 'last';
  572. }
  573. $result .= $this->tag('li', $elementContent, $options);
  574. }
  575. return $this->tag('ul', $result, $ulOptions);
  576. } else {
  577. return null;
  578. }
  579. }
  580. /**
  581. * Creates a formatted IMG element. If `$options['url']` is provided, an image link will be
  582. * generated with the link pointed at `$options['url']`. This method will set an empty
  583. * alt attribute if one is not supplied.
  584. *
  585. * ### Usage
  586. *
  587. * Create a regular image:
  588. *
  589. * `echo $html->image('cake_icon.png', array('alt' => 'CakePHP'));`
  590. *
  591. * Create an image link:
  592. *
  593. * `echo $html->image('cake_icon.png', array('alt' => 'CakePHP', 'url' => 'http://cakephp.org'));`
  594. *
  595. * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
  596. * @param array $options Array of HTML attributes.
  597. * @return string completed img tag
  598. * @access public
  599. * @link http://book.cakephp.org/view/1441/image
  600. */
  601. public function image($path, $options = array()) {
  602. if (is_array($path)) {
  603. $path = $this->url($path);
  604. } elseif (strpos($path, '://') === false) {
  605. if ($path[0] !== '/') {
  606. $path = IMAGES_URL . $path;
  607. }
  608. $path = $this->assetTimestamp($this->webroot($path));
  609. }
  610. if (!isset($options['alt'])) {
  611. $options['alt'] = '';
  612. }
  613. $url = false;
  614. if (!empty($options['url'])) {
  615. $url = $options['url'];
  616. unset($options['url']);
  617. }
  618. $image = sprintf($this->tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
  619. if ($url) {
  620. return sprintf($this->tags['link'], $this->url($url), null, $image);
  621. }
  622. return $image;
  623. }
  624. /**
  625. * Returns a row of formatted and named TABLE headers.
  626. *
  627. * @param array $names Array of tablenames.
  628. * @param array $trOptions HTML options for TR elements.
  629. * @param array $thOptions HTML options for TH elements.
  630. * @return string Completed table headers
  631. * @access public
  632. * @link http://book.cakephp.org/view/1446/tableHeaders
  633. */
  634. public function tableHeaders($names, $trOptions = null, $thOptions = null) {
  635. $out = array();
  636. foreach ($names as $arg) {
  637. $out[] = sprintf($this->tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
  638. }
  639. return sprintf($this->tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
  640. }
  641. /**
  642. * Returns a formatted string of table rows (TR's with TD's in them).
  643. *
  644. * @param array $data Array of table data
  645. * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
  646. * @param array $evenTrOptions HTML options for even TR elements
  647. * @param bool $useCount adds class "column-$i"
  648. * @param bool $continueOddEven If false, will use a non-static $count variable,
  649. * so that the odd/even count is reset to zero just for that call.
  650. * @return string Formatted HTML
  651. * @access public
  652. * @link http://book.cakephp.org/view/1447/tableCells
  653. */
  654. public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
  655. if (empty($data[0]) || !is_array($data[0])) {
  656. $data = array($data);
  657. }
  658. if ($oddTrOptions === true) {
  659. $useCount = true;
  660. $oddTrOptions = null;
  661. }
  662. if ($evenTrOptions === false) {
  663. $continueOddEven = false;
  664. $evenTrOptions = null;
  665. }
  666. if ($continueOddEven) {
  667. static $count = 0;
  668. } else {
  669. $count = 0;
  670. }
  671. foreach ($data as $line) {
  672. $count++;
  673. $cellsOut = array();
  674. $i = 0;
  675. foreach ($line as $cell) {
  676. $cellOptions = array();
  677. if (is_array($cell)) {
  678. $cellOptions = $cell[1];
  679. $cell = $cell[0];
  680. } elseif ($useCount) {
  681. $cellOptions['class'] = 'column-' . ++$i;
  682. }
  683. $cellsOut[] = sprintf($this->tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
  684. }
  685. $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
  686. $out[] = sprintf($this->tags['tablerow'], $options, implode(' ', $cellsOut));
  687. }
  688. return implode("\n", $out);
  689. }
  690. /**
  691. * Returns a formatted block tag, i.e DIV, SPAN, P.
  692. *
  693. * ### Options
  694. *
  695. * - `escape` Whether or not the contents should be html_entity escaped.
  696. *
  697. * @param string $name Tag name.
  698. * @param string $text String content that will appear inside the div element.
  699. * If null, only a start tag will be printed
  700. * @param array $options Additional HTML attributes of the DIV tag, see above.
  701. * @return string The formatted tag element
  702. * @access public
  703. * @link http://book.cakephp.org/view/1443/tag
  704. */
  705. public function tag($name, $text = null, $options = array()) {
  706. if (is_array($options) && isset($options['escape']) && $options['escape']) {
  707. $text = h($text);
  708. unset($options['escape']);
  709. }
  710. if (!is_array($options)) {
  711. $options = array('class' => $options);
  712. }
  713. if ($text === null) {
  714. $tag = 'tagstart';
  715. } else {
  716. $tag = 'tag';
  717. }
  718. return sprintf($this->tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
  719. }
  720. /**
  721. * Returns a formatted DIV tag for HTML FORMs.
  722. *
  723. * ### Options
  724. *
  725. * - `escape` Whether or not the contents should be html_entity escaped.
  726. *
  727. * @param string $class CSS class name of the div element.
  728. * @param string $text String content that will appear inside the div element.
  729. * If null, only a start tag will be printed
  730. * @param array $options Additional HTML attributes of the DIV tag
  731. * @return string The formatted DIV element
  732. * @access public
  733. * @link http://book.cakephp.org/view/1444/div
  734. */
  735. public function div($class = null, $text = null, $options = array()) {
  736. if (!empty($class)) {
  737. $options['class'] = $class;
  738. }
  739. return $this->tag('div', $text, $options);
  740. }
  741. /**
  742. * Returns a formatted P tag.
  743. *
  744. * ### Options
  745. *
  746. * - `escape` Whether or not the contents should be html_entity escaped.
  747. *
  748. * @param string $class CSS class name of the p element.
  749. * @param string $text String content that will appear inside the p element.
  750. * @param array $options Additional HTML attributes of the P tag
  751. * @return string The formatted P element
  752. * @access public
  753. * @link http://book.cakephp.org/view/1445/para
  754. */
  755. public function para($class, $text, $options = array()) {
  756. if (isset($options['escape'])) {
  757. $text = h($text);
  758. }
  759. if ($class != null && !empty($class)) {
  760. $options['class'] = $class;
  761. }
  762. if ($text === null) {
  763. $tag = 'parastart';
  764. } else {
  765. $tag = 'para';
  766. }
  767. return sprintf($this->tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
  768. }
  769. /**
  770. * Build a nested list (UL/OL) out of an associative array.
  771. *
  772. * @param array $list Set of elements to list
  773. * @param array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
  774. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  775. * @param string $tag Type of list tag to use (ol/ul)
  776. * @return string The nested list
  777. */
  778. public function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
  779. if (is_string($options)) {
  780. $tag = $options;
  781. $options = array();
  782. }
  783. $items = $this->__nestedListItem($list, $options, $itemOptions, $tag);
  784. return sprintf($this->tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
  785. }
  786. /**
  787. * Internal function to build a nested list (UL/OL) out of an associative array.
  788. *
  789. * @param array $list Set of elements to list
  790. * @param array $options Additional HTML attributes of the list (ol/ul) tag
  791. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  792. * @param string $tag Type of list tag to use (ol/ul)
  793. * @return string The nested list element
  794. * @access private
  795. * @see HtmlHelper::nestedList()
  796. */
  797. function __nestedListItem($items, $options, $itemOptions, $tag) {
  798. $out = '';
  799. $index = 1;
  800. foreach ($items as $key => $item) {
  801. if (is_array($item)) {
  802. $item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
  803. }
  804. if (isset($itemOptions['even']) && $index % 2 == 0) {
  805. $itemOptions['class'] = $itemOptions['even'];
  806. } else if (isset($itemOptions['odd']) && $index % 2 != 0) {
  807. $itemOptions['class'] = $itemOptions['odd'];
  808. }
  809. $out .= sprintf($this->tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
  810. $index++;
  811. }
  812. return $out;
  813. }
  814. }