PageRenderTime 69ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

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

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