PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/libs/view/helpers/html.php

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