HtmlHelper.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.View.Helper
  17. * @since CakePHP(tm) v 0.9.1
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('AppHelper', 'View/Helper');
  21. App::uses('CakeResponse', 'Network');
  22. /**
  23. * Html Helper class for easy use of HTML widgets.
  24. *
  25. * HtmlHelper encloses all methods needed while working with HTML pages.
  26. *
  27. * @package Cake.View.Helper
  28. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html
  29. */
  30. class HtmlHelper extends AppHelper {
  31. /**
  32. * Reference to the Response object
  33. *
  34. * @var CakeResponse
  35. */
  36. public $response;
  37. /**
  38. * html tags used by this helper.
  39. *
  40. * @var array
  41. */
  42. protected $_tags = array(
  43. 'meta' => '<meta%s/>',
  44. 'metalink' => '<link href="%s"%s/>',
  45. 'link' => '<a href="%s"%s>%s</a>',
  46. 'mailto' => '<a href="mailto:%s" %s>%s</a>',
  47. 'form' => '<form action="%s"%s>',
  48. 'formend' => '</form>',
  49. 'input' => '<input name="%s"%s/>',
  50. 'textarea' => '<textarea name="%s"%s>%s</textarea>',
  51. 'hidden' => '<input type="hidden" name="%s"%s/>',
  52. 'checkbox' => '<input type="checkbox" name="%s" %s/>',
  53. 'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
  54. 'radio' => '<input type="radio" name="%s" id="%s"%s />%s',
  55. 'selectstart' => '<select name="%s"%s>',
  56. 'selectmultiplestart' => '<select name="%s[]"%s>',
  57. 'selectempty' => '<option value=""%s>&nbsp;</option>',
  58. 'selectoption' => '<option value="%s"%s>%s</option>',
  59. 'selectend' => '</select>',
  60. 'optiongroup' => '<optgroup label="%s"%s>',
  61. 'optiongroupend' => '</optgroup>',
  62. 'checkboxmultiplestart' => '',
  63. 'checkboxmultipleend' => '',
  64. 'password' => '<input type="password" name="%s" %s/>',
  65. 'file' => '<input type="file" name="%s" %s/>',
  66. 'file_no_model' => '<input type="file" name="%s" %s/>',
  67. 'submit' => '<input %s/>',
  68. 'submitimage' => '<input type="image" src="%s" %s/>',
  69. 'button' => '<button%s>%s</button>',
  70. 'image' => '<img src="%s" %s/>',
  71. 'tableheader' => '<th%s>%s</th>',
  72. 'tableheaderrow' => '<tr%s>%s</tr>',
  73. 'tablecell' => '<td%s>%s</td>',
  74. 'tablerow' => '<tr%s>%s</tr>',
  75. 'block' => '<div%s>%s</div>',
  76. 'blockstart' => '<div%s>',
  77. 'blockend' => '</div>',
  78. 'hiddenblock' => '<div style="display:none;">%s</div>',
  79. 'tag' => '<%s%s>%s</%s>',
  80. 'tagstart' => '<%s%s>',
  81. 'tagend' => '</%s>',
  82. 'tagselfclosing' => '<%s%s/>',
  83. 'para' => '<p%s>%s</p>',
  84. 'parastart' => '<p%s>',
  85. 'label' => '<label for="%s"%s>%s</label>',
  86. 'fieldset' => '<fieldset%s>%s</fieldset>',
  87. 'fieldsetstart' => '<fieldset><legend>%s</legend>',
  88. 'fieldsetend' => '</fieldset>',
  89. 'legend' => '<legend>%s</legend>',
  90. 'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
  91. 'style' => '<style type="text/css"%s>%s</style>',
  92. 'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
  93. 'ul' => '<ul%s>%s</ul>',
  94. 'ol' => '<ol%s>%s</ol>',
  95. 'li' => '<li%s>%s</li>',
  96. 'error' => '<div%s>%s</div>',
  97. 'javascriptblock' => '<script%s>%s</script>',
  98. 'javascriptstart' => '<script>',
  99. 'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
  100. 'javascriptend' => '</script>'
  101. );
  102. /**
  103. * Breadcrumbs.
  104. *
  105. * @var array
  106. */
  107. protected $_crumbs = array();
  108. /**
  109. * Names of script files that have been included once
  110. *
  111. * @var array
  112. */
  113. protected $_includedScripts = array();
  114. /**
  115. * Options for the currently opened script block buffer if any.
  116. *
  117. * @var array
  118. */
  119. protected $_scriptBlockOptions = array();
  120. /**
  121. * Document type definitions
  122. *
  123. * @var array
  124. */
  125. protected $_docTypes = array(
  126. 'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
  127. 'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
  128. 'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
  129. 'html5' => '<!DOCTYPE html>',
  130. 'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
  131. 'xhtml-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
  132. 'xhtml-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
  133. 'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
  134. );
  135. /**
  136. * Constructor
  137. *
  138. * ### Settings
  139. *
  140. * - `configFile` A file containing an array of tags you wish to redefine.
  141. *
  142. * ### Customizing tag sets
  143. *
  144. * Using the `configFile` option you can redefine the tag HtmlHelper will use.
  145. * The file named should be compatible with HtmlHelper::loadConfig().
  146. *
  147. * @param View $View The View this helper is being attached to.
  148. * @param array $settings Configuration settings for the helper.
  149. */
  150. public function __construct(View $View, $settings = array()) {
  151. parent::__construct($View, $settings);
  152. if (is_object($this->_View->response)) {
  153. $this->response = $this->_View->response;
  154. } else {
  155. $this->response = new CakeResponse();
  156. }
  157. if (!empty($settings['configFile'])) {
  158. $this->loadConfig($settings['configFile']);
  159. }
  160. }
  161. /**
  162. * Adds a link to the breadcrumbs array.
  163. *
  164. * @param string $name Text for link
  165. * @param string $link URL for link (if empty it won't be a link)
  166. * @param string|array $options Link attributes e.g. array('id' => 'selected')
  167. * @return void
  168. * @see HtmlHelper::link() for details on $options that can be used.
  169. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
  170. */
  171. public function addCrumb($name, $link = null, $options = null) {
  172. $this->_crumbs[] = array($name, $link, $options);
  173. }
  174. /**
  175. * Returns a doctype string.
  176. *
  177. * Possible doctypes:
  178. *
  179. * - html4-strict: HTML4 Strict.
  180. * - html4-trans: HTML4 Transitional.
  181. * - html4-frame: HTML4 Frameset.
  182. * - html5: HTML5. Default value.
  183. * - xhtml-strict: XHTML1 Strict.
  184. * - xhtml-trans: XHTML1 Transitional.
  185. * - xhtml-frame: XHTML1 Frameset.
  186. * - xhtml11: XHTML1.1.
  187. *
  188. * @param string $type Doctype to use.
  189. * @return string Doctype string
  190. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::docType
  191. */
  192. public function docType($type = 'html5') {
  193. if (isset($this->_docTypes[$type])) {
  194. return $this->_docTypes[$type];
  195. }
  196. return null;
  197. }
  198. /**
  199. * Creates a link to an external resource and handles basic meta tags
  200. *
  201. * Create a meta tag that is output inline:
  202. *
  203. * `$this->Html->meta('icon', 'favicon.ico');
  204. *
  205. * Append the meta tag to `$scripts_for_layout`:
  206. *
  207. * `$this->Html->meta('description', 'A great page', array('inline' => false));`
  208. *
  209. * Append the meta tag to custom view block:
  210. *
  211. * `$this->Html->meta('description', 'A great page', array('block' => 'metaTags'));`
  212. *
  213. * ### Options
  214. *
  215. * - `inline` Whether or not the link element should be output inline. Set to false to
  216. * have the meta tag included in `$scripts_for_layout`, and appended to the 'meta' view block.
  217. * - `block` Choose a custom block to append the meta tag to. Using this option
  218. * will override the inline option.
  219. *
  220. * @param string $type The title of the external resource
  221. * @param string|array $url The address of the external resource or string for content attribute
  222. * @param array $options Other attributes for the generated tag. If the type attribute is html,
  223. * rss, atom, or icon, the mime-type is returned.
  224. * @return string A completed `<link />` element.
  225. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::meta
  226. */
  227. public function meta($type, $url = null, $options = array()) {
  228. $options += array('inline' => true, 'block' => null);
  229. if (!$options['inline'] && empty($options['block'])) {
  230. $options['block'] = __FUNCTION__;
  231. }
  232. unset($options['inline']);
  233. if (!is_array($type)) {
  234. $types = array(
  235. 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
  236. 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
  237. 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
  238. 'keywords' => array('name' => 'keywords', 'content' => $url),
  239. 'description' => array('name' => 'description', 'content' => $url),
  240. );
  241. if ($type === 'icon' && $url === null) {
  242. $types['icon']['link'] = $this->webroot('favicon.ico');
  243. }
  244. if (isset($types[$type])) {
  245. $type = $types[$type];
  246. } elseif (!isset($options['type']) && $url !== null) {
  247. if (is_array($url) && isset($url['ext'])) {
  248. $type = $types[$url['ext']];
  249. } else {
  250. $type = $types['rss'];
  251. }
  252. } elseif (isset($options['type']) && isset($types[$options['type']])) {
  253. $type = $types[$options['type']];
  254. unset($options['type']);
  255. } else {
  256. $type = array();
  257. }
  258. }
  259. $options = array_merge($type, $options);
  260. $out = null;
  261. if (isset($options['link'])) {
  262. if (isset($options['rel']) && $options['rel'] === 'icon') {
  263. $out = sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('block', 'link'), ' ', ' '));
  264. $options['rel'] = 'shortcut icon';
  265. } else {
  266. $options['link'] = $this->url($options['link'], true);
  267. }
  268. $out .= sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('block', 'link'), ' ', ' '));
  269. } else {
  270. $out = sprintf($this->_tags['meta'], $this->_parseAttributes($options, array('block', 'type'), ' ', ' '));
  271. }
  272. if (empty($options['block'])) {
  273. return $out;
  274. }
  275. $this->_View->append($options['block'], $out);
  276. }
  277. /**
  278. * Returns a charset META-tag.
  279. *
  280. * @param string $charset The character set to be used in the meta tag. If empty,
  281. * The App.encoding value will be used. Example: "utf-8".
  282. * @return string A meta tag containing the specified character set.
  283. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::charset
  284. */
  285. public function charset($charset = null) {
  286. if (empty($charset)) {
  287. $charset = strtolower(Configure::read('App.encoding'));
  288. }
  289. return sprintf($this->_tags['charset'], (!empty($charset) ? $charset : 'utf-8'));
  290. }
  291. /**
  292. * Creates an HTML link.
  293. *
  294. * If $url starts with "http://" this is treated as an external link. Else,
  295. * it is treated as a path to controller/action and parsed with the
  296. * HtmlHelper::url() method.
  297. *
  298. * If the $url is empty, $title is used instead.
  299. *
  300. * ### Options
  301. *
  302. * - `escape` Set to false to disable escaping of title and attributes.
  303. * - `escapeTitle` Set to false to disable escaping of title. (Takes precedence over value of `escape`)
  304. * - `confirm` JavaScript confirmation message.
  305. *
  306. * @param string $title The content to be wrapped by <a> tags.
  307. * @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  308. * @param array $options Array of options and HTML attributes.
  309. * @param string $confirmMessage JavaScript confirmation message.
  310. * @return string An `<a />` element.
  311. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
  312. */
  313. public function link($title, $url = null, $options = array(), $confirmMessage = false) {
  314. $escapeTitle = true;
  315. if ($url !== null) {
  316. $url = $this->url($url);
  317. } else {
  318. $url = $this->url($title);
  319. $title = htmlspecialchars_decode($url, ENT_QUOTES);
  320. $title = h(urldecode($title));
  321. $escapeTitle = false;
  322. }
  323. if (isset($options['escapeTitle'])) {
  324. $escapeTitle = $options['escapeTitle'];
  325. unset($options['escapeTitle']);
  326. } elseif (isset($options['escape'])) {
  327. $escapeTitle = $options['escape'];
  328. }
  329. if ($escapeTitle === true) {
  330. $title = h($title);
  331. } elseif (is_string($escapeTitle)) {
  332. $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
  333. }
  334. if (!empty($options['confirm'])) {
  335. $confirmMessage = $options['confirm'];
  336. unset($options['confirm']);
  337. }
  338. if ($confirmMessage) {
  339. $options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
  340. } elseif (isset($options['default']) && !$options['default']) {
  341. if (isset($options['onclick'])) {
  342. $options['onclick'] .= ' ';
  343. } else {
  344. $options['onclick'] = '';
  345. }
  346. $options['onclick'] .= 'event.returnValue = false; return false;';
  347. unset($options['default']);
  348. }
  349. return sprintf($this->_tags['link'], $url, $this->_parseAttributes($options), $title);
  350. }
  351. /**
  352. * Creates a link element for CSS stylesheets.
  353. *
  354. * ### Usage
  355. *
  356. * Include one CSS file:
  357. *
  358. * `echo $this->Html->css('styles.css');`
  359. *
  360. * Include multiple CSS files:
  361. *
  362. * `echo $this->Html->css(array('one.css', 'two.css'));`
  363. *
  364. * Add the stylesheet to the `$scripts_for_layout` layout var:
  365. *
  366. * `$this->Html->css('styles.css', array('inline' => false));`
  367. *
  368. * Add the stylesheet to a custom block:
  369. *
  370. * `$this->Html->css('styles.css', array('block' => 'layoutCss'));`
  371. *
  372. * ### Options
  373. *
  374. * - `inline` If set to false, the generated tag will be appended to the 'css' block,
  375. * and included in the `$scripts_for_layout` layout variable. Defaults to true.
  376. * - `block` Set the name of the block link/style tag will be appended to.
  377. * This overrides the `inline` option.
  378. * - `plugin` False value will prevent parsing path as a plugin
  379. * - `rel` Defaults to 'stylesheet'. If equal to 'import' the stylesheet will be imported.
  380. * - `fullBase` If true the url will get a full address for the css file.
  381. *
  382. * @param string|array $path The name of a CSS style sheet or an array containing names of
  383. * CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
  384. * of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
  385. * @param array $options Array of options and HTML arguments.
  386. * @return string CSS <link /> or <style /> tag, depending on the type of link.
  387. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::css
  388. */
  389. public function css($path, $options = array()) {
  390. if (!is_array($options)) {
  391. $rel = $options;
  392. $options = array();
  393. if ($rel) {
  394. $options['rel'] = $rel;
  395. }
  396. if (func_num_args() > 2) {
  397. $options = func_get_arg(2) + $options;
  398. }
  399. unset($rel);
  400. }
  401. $options += array('block' => null, 'inline' => true, 'rel' => 'stylesheet');
  402. if (!$options['inline'] && empty($options['block'])) {
  403. $options['block'] = __FUNCTION__;
  404. }
  405. unset($options['inline']);
  406. if (is_array($path)) {
  407. $out = '';
  408. foreach ($path as $i) {
  409. $out .= "\n\t" . $this->css($i, $options);
  410. }
  411. if (empty($options['block'])) {
  412. return $out . "\n";
  413. }
  414. return;
  415. }
  416. if (strpos($path, '//') !== false) {
  417. $url = $path;
  418. } else {
  419. $url = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.cssBaseUrl'), 'ext' => '.css'));
  420. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  421. if (Configure::read('Asset.filter.css')) {
  422. $pos = strpos($url, Configure::read('App.cssBaseUrl'));
  423. if ($pos !== false) {
  424. $url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(Configure::read('App.cssBaseUrl')));
  425. }
  426. }
  427. }
  428. if ($options['rel'] == 'import') {
  429. $out = sprintf(
  430. $this->_tags['style'],
  431. $this->_parseAttributes($options, array('rel', 'block'), '', ' '),
  432. '@import url(' . $url . ');'
  433. );
  434. } else {
  435. $out = sprintf(
  436. $this->_tags['css'],
  437. $options['rel'],
  438. $url,
  439. $this->_parseAttributes($options, array('rel', 'block'), '', ' ')
  440. );
  441. }
  442. if (empty($options['block'])) {
  443. return $out;
  444. }
  445. $this->_View->append($options['block'], $out);
  446. }
  447. /**
  448. * Returns one or many `<script>` tags depending on the number of scripts given.
  449. *
  450. * If the filename is prefixed with "/", the path will be relative to the base path of your
  451. * application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
  452. *
  453. *
  454. * ### Usage
  455. *
  456. * Include one script file:
  457. *
  458. * `echo $this->Html->script('styles.js');`
  459. *
  460. * Include multiple script files:
  461. *
  462. * `echo $this->Html->script(array('one.js', 'two.js'));`
  463. *
  464. * Add the script file to the `$scripts_for_layout` layout var:
  465. *
  466. * `$this->Html->script('styles.js', array('inline' => false));`
  467. *
  468. * Add the script file to a custom block:
  469. *
  470. * `$this->Html->script('styles.js', null, array('block' => 'bodyScript'));`
  471. *
  472. * ### Options
  473. *
  474. * - `inline` Whether script should be output inline or into `$scripts_for_layout`. When set to false,
  475. * the script tag will be appended to the 'script' view block as well as `$scripts_for_layout`.
  476. * - `block` The name of the block you want the script appended to. Leave undefined to output inline.
  477. * Using this option will override the inline option.
  478. * - `once` Whether or not the script should be checked for uniqueness. If true scripts will only be
  479. * included once, use false to allow the same script to be included more than once per request.
  480. * - `plugin` False value will prevent parsing path as a plugin
  481. * - `fullBase` If true the url will get a full address for the script file.
  482. *
  483. * @param string|array $url String or array of javascript files to include
  484. * @param array|boolean $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
  485. * @return mixed String of `<script />` tags or null if $inline is false or if $once is true and the file has been
  486. * included before.
  487. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::script
  488. */
  489. public function script($url, $options = array()) {
  490. if (is_bool($options)) {
  491. list($inline, $options) = array($options, array());
  492. $options['inline'] = $inline;
  493. }
  494. $options = array_merge(array('block' => null, 'inline' => true, 'once' => true), $options);
  495. if (!$options['inline'] && empty($options['block'])) {
  496. $options['block'] = __FUNCTION__;
  497. }
  498. unset($options['inline']);
  499. if (is_array($url)) {
  500. $out = '';
  501. foreach ($url as $i) {
  502. $out .= "\n\t" . $this->script($i, $options);
  503. }
  504. if (empty($options['block'])) {
  505. return $out . "\n";
  506. }
  507. return null;
  508. }
  509. if ($options['once'] && isset($this->_includedScripts[$url])) {
  510. return null;
  511. }
  512. $this->_includedScripts[$url] = true;
  513. if (strpos($url, '//') === false) {
  514. $url = $this->assetUrl($url, $options + array('pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js'));
  515. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  516. if (Configure::read('Asset.filter.js')) {
  517. $url = str_replace(Configure::read('App.jsBaseUrl'), 'cjs/', $url);
  518. }
  519. }
  520. $attributes = $this->_parseAttributes($options, array('block', 'once'), ' ');
  521. $out = sprintf($this->_tags['javascriptlink'], $url, $attributes);
  522. if (empty($options['block'])) {
  523. return $out;
  524. }
  525. $this->_View->append($options['block'], $out);
  526. }
  527. /**
  528. * Wrap $script in a script tag.
  529. *
  530. * ### Options
  531. *
  532. * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
  533. * - `inline` (boolean) Whether or not the $script should be added to
  534. * `$scripts_for_layout` / `script` block, or output inline. (Deprecated, use `block` instead)
  535. * - `block` Which block you want this script block appended to.
  536. * Defaults to `script`.
  537. *
  538. * @param string $script The script to wrap
  539. * @param array $options The options to use. Options not listed above will be
  540. * treated as HTML attributes.
  541. * @return mixed string or null depending on the value of `$options['block']`
  542. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock
  543. */
  544. public function scriptBlock($script, $options = array()) {
  545. $options += array('type' => 'text/javascript', 'safe' => true, 'inline' => true);
  546. if ($options['safe']) {
  547. $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
  548. }
  549. if (!$options['inline'] && empty($options['block'])) {
  550. $options['block'] = 'script';
  551. }
  552. unset($options['inline'], $options['safe']);
  553. $attributes = $this->_parseAttributes($options, array('block'), ' ');
  554. $out = sprintf($this->_tags['javascriptblock'], $attributes, $script);
  555. if (empty($options['block'])) {
  556. return $out;
  557. }
  558. $this->_View->append($options['block'], $out);
  559. }
  560. /**
  561. * Begin a script block that captures output until HtmlHelper::scriptEnd()
  562. * is called. This capturing block will capture all output between the methods
  563. * and create a scriptBlock from it.
  564. *
  565. * ### Options
  566. *
  567. * - `safe` Whether the code block should contain a CDATA
  568. * - `inline` Should the generated script tag be output inline or in `$scripts_for_layout`
  569. *
  570. * @param array $options Options for the code block.
  571. * @return void
  572. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptStart
  573. */
  574. public function scriptStart($options = array()) {
  575. $options += array('safe' => true, 'inline' => true);
  576. $this->_scriptBlockOptions = $options;
  577. ob_start();
  578. return null;
  579. }
  580. /**
  581. * End a Buffered section of Javascript capturing.
  582. * Generates a script tag inline or in `$scripts_for_layout` depending on the settings
  583. * used when the scriptBlock was started
  584. *
  585. * @return mixed depending on the settings of scriptStart() either a script tag or null
  586. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptEnd
  587. */
  588. public function scriptEnd() {
  589. $buffer = ob_get_clean();
  590. $options = $this->_scriptBlockOptions;
  591. $this->_scriptBlockOptions = array();
  592. return $this->scriptBlock($buffer, $options);
  593. }
  594. /**
  595. * Builds CSS style data from an array of CSS properties
  596. *
  597. * ### Usage:
  598. *
  599. * {{{
  600. * echo $this->Html->style(array('margin' => '10px', 'padding' => '10px'), true);
  601. *
  602. * // creates
  603. * 'margin:10px;padding:10px;'
  604. * }}}
  605. *
  606. * @param array $data Style data array, keys will be used as property names, values as property values.
  607. * @param boolean $oneline Whether or not the style block should be displayed on one line.
  608. * @return string CSS styling data
  609. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::style
  610. */
  611. public function style($data, $oneline = true) {
  612. if (!is_array($data)) {
  613. return $data;
  614. }
  615. $out = array();
  616. foreach ($data as $key => $value) {
  617. $out[] = $key . ':' . $value . ';';
  618. }
  619. if ($oneline) {
  620. return implode(' ', $out);
  621. }
  622. return implode("\n", $out);
  623. }
  624. /**
  625. * Returns the breadcrumb trail as a sequence of &raquo;-separated links.
  626. *
  627. * If `$startText` is an array, the accepted keys are:
  628. *
  629. * - `text` Define the text/content for the link.
  630. * - `url` Define the target of the created link.
  631. *
  632. * All other keys will be passed to HtmlHelper::link() as the `$options` parameter.
  633. *
  634. * @param string $separator Text to separate crumbs.
  635. * @param string|array|boolean $startText This will be the first crumb, if false it defaults to first crumb in array. Can
  636. * also be an array, see above for details.
  637. * @return string Composed bread crumbs
  638. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
  639. */
  640. public function getCrumbs($separator = '&raquo;', $startText = false) {
  641. $crumbs = $this->_prepareCrumbs($startText);
  642. if (!empty($crumbs)) {
  643. $out = array();
  644. foreach ($crumbs as $crumb) {
  645. if (!empty($crumb[1])) {
  646. $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
  647. } else {
  648. $out[] = $crumb[0];
  649. }
  650. }
  651. return implode($separator, $out);
  652. }
  653. return null;
  654. }
  655. /**
  656. * Returns breadcrumbs as a (x)html list
  657. *
  658. * This method uses HtmlHelper::tag() to generate list and its elements. Works
  659. * similar to HtmlHelper::getCrumbs(), so it uses options which every
  660. * crumb was added with.
  661. *
  662. * ### Options
  663. * - `separator` Separator content to insert in between breadcrumbs, defaults to ''
  664. * - `firstClass` Class for wrapper tag on the first breadcrumb, defaults to 'first'
  665. * - `lastClass` Class for wrapper tag on current active page, defaults to 'last'
  666. *
  667. * @param array $options Array of html attributes to apply to the generated list elements.
  668. * @param string|array|boolean $startText This will be the first crumb, if false it defaults to first crumb in array. Can
  669. * also be an array, see `HtmlHelper::getCrumbs` for details.
  670. * @return string breadcrumbs html list
  671. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
  672. */
  673. public function getCrumbList($options = array(), $startText = false) {
  674. $defaults = array('firstClass' => 'first', 'lastClass' => 'last', 'separator' => '');
  675. $options = array_merge($defaults, (array)$options);
  676. $firstClass = $options['firstClass'];
  677. $lastClass = $options['lastClass'];
  678. $separator = $options['separator'];
  679. unset($options['firstClass'], $options['lastClass'], $options['separator']);
  680. $crumbs = $this->_prepareCrumbs($startText);
  681. if (empty($crumbs)) {
  682. return null;
  683. }
  684. $result = '';
  685. $crumbCount = count($crumbs);
  686. $ulOptions = $options;
  687. foreach ($crumbs as $which => $crumb) {
  688. $options = array();
  689. if (empty($crumb[1])) {
  690. $elementContent = $crumb[0];
  691. } else {
  692. $elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
  693. }
  694. if (!$which && $firstClass !== false) {
  695. $options['class'] = $firstClass;
  696. } elseif ($which == $crumbCount - 1 && $lastClass !== false) {
  697. $options['class'] = $lastClass;
  698. }
  699. if (!empty($separator) && ($crumbCount - $which >= 2)) {
  700. $elementContent .= $separator;
  701. }
  702. $result .= $this->tag('li', $elementContent, $options);
  703. }
  704. return $this->tag('ul', $result, $ulOptions);
  705. }
  706. /**
  707. * Prepends startText to crumbs array if set
  708. *
  709. * @param string $startText Text to prepend
  710. * @return array Crumb list including startText (if provided)
  711. */
  712. protected function _prepareCrumbs($startText) {
  713. $crumbs = $this->_crumbs;
  714. if ($startText) {
  715. if (!is_array($startText)) {
  716. $startText = array(
  717. 'url' => '/',
  718. 'text' => $startText
  719. );
  720. }
  721. $startText += array('url' => '/', 'text' => __d('cake', 'Home'));
  722. list($url, $text) = array($startText['url'], $startText['text']);
  723. unset($startText['url'], $startText['text']);
  724. array_unshift($crumbs, array($text, $url, $startText));
  725. }
  726. return $crumbs;
  727. }
  728. /**
  729. * Creates a formatted IMG element.
  730. *
  731. * This method will set an empty alt attribute if one is not supplied.
  732. *
  733. * ### Usage:
  734. *
  735. * Create a regular image:
  736. *
  737. * `echo $this->Html->image('cake_icon.png', array('alt' => 'CakePHP'));`
  738. *
  739. * Create an image link:
  740. *
  741. * `echo $this->Html->image('cake_icon.png', array('alt' => 'CakePHP', 'url' => 'http://cakephp.org'));`
  742. *
  743. * ### Options:
  744. *
  745. * - `url` If provided an image link will be generated and the link will point at
  746. * `$options['url']`.
  747. * - `fullBase` If true the src attribute will get a full address for the image file.
  748. * - `plugin` False value will prevent parsing path as a plugin
  749. *
  750. * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
  751. * @param array $options Array of HTML attributes. See above for special options.
  752. * @return string completed img tag
  753. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::image
  754. */
  755. public function image($path, $options = array()) {
  756. $path = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.imageBaseUrl')));
  757. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  758. if (!isset($options['alt'])) {
  759. $options['alt'] = '';
  760. }
  761. $url = false;
  762. if (!empty($options['url'])) {
  763. $url = $options['url'];
  764. unset($options['url']);
  765. }
  766. $image = sprintf($this->_tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
  767. if ($url) {
  768. return sprintf($this->_tags['link'], $this->url($url), null, $image);
  769. }
  770. return $image;
  771. }
  772. /**
  773. * Returns a row of formatted and named TABLE headers.
  774. *
  775. * @param array $names Array of tablenames. Each tablename also can be a key that points to an array with a set
  776. * of attributes to its specific tag
  777. * @param array $trOptions HTML options for TR elements.
  778. * @param array $thOptions HTML options for TH elements.
  779. * @return string Completed table headers
  780. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tableHeaders
  781. */
  782. public function tableHeaders($names, $trOptions = null, $thOptions = null) {
  783. $out = array();
  784. foreach ($names as $arg) {
  785. if (!is_array($arg)) {
  786. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
  787. } else {
  788. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes(current($arg)), key($arg));
  789. }
  790. }
  791. return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), implode(' ', $out));
  792. }
  793. /**
  794. * Returns a formatted string of table rows (TR's with TD's in them).
  795. *
  796. * @param array $data Array of table data
  797. * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
  798. * @param array $evenTrOptions HTML options for even TR elements
  799. * @param boolean $useCount adds class "column-$i"
  800. * @param boolean $continueOddEven If false, will use a non-static $count variable,
  801. * so that the odd/even count is reset to zero just for that call.
  802. * @return string Formatted HTML
  803. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tableCells
  804. */
  805. public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
  806. if (empty($data[0]) || !is_array($data[0])) {
  807. $data = array($data);
  808. }
  809. if ($oddTrOptions === true) {
  810. $useCount = true;
  811. $oddTrOptions = null;
  812. }
  813. if ($evenTrOptions === false) {
  814. $continueOddEven = false;
  815. $evenTrOptions = null;
  816. }
  817. if ($continueOddEven) {
  818. static $count = 0;
  819. } else {
  820. $count = 0;
  821. }
  822. foreach ($data as $line) {
  823. $count++;
  824. $cellsOut = array();
  825. $i = 0;
  826. foreach ($line as $cell) {
  827. $cellOptions = array();
  828. if (is_array($cell)) {
  829. $cellOptions = $cell[1];
  830. $cell = $cell[0];
  831. } elseif ($useCount) {
  832. $cellOptions['class'] = 'column-' . ++$i;
  833. }
  834. $cellsOut[] = sprintf($this->_tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
  835. }
  836. $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
  837. $out[] = sprintf($this->_tags['tablerow'], $options, implode(' ', $cellsOut));
  838. }
  839. return implode("\n", $out);
  840. }
  841. /**
  842. * Returns a formatted block tag, i.e DIV, SPAN, P.
  843. *
  844. * ### Options
  845. *
  846. * - `escape` Whether or not the contents should be html_entity escaped.
  847. *
  848. * @param string $name Tag name.
  849. * @param string $text String content that will appear inside the div element.
  850. * If null, only a start tag will be printed
  851. * @param array $options Additional HTML attributes of the DIV tag, see above.
  852. * @return string The formatted tag element
  853. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tag
  854. */
  855. public function tag($name, $text = null, $options = array()) {
  856. if (empty($name)) {
  857. return $text;
  858. }
  859. if (isset($options['escape']) && $options['escape']) {
  860. $text = h($text);
  861. unset($options['escape']);
  862. }
  863. if ($text === null) {
  864. $tag = 'tagstart';
  865. } else {
  866. $tag = 'tag';
  867. }
  868. return sprintf($this->_tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
  869. }
  870. /**
  871. * Returns a formatted existent block of $tags
  872. *
  873. * @param string $tag Tag name
  874. * @return string Formatted block
  875. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::useTag
  876. */
  877. public function useTag($tag) {
  878. if (!isset($this->_tags[$tag])) {
  879. return '';
  880. }
  881. $args = func_get_args();
  882. array_shift($args);
  883. foreach ($args as &$arg) {
  884. if (is_array($arg)) {
  885. $arg = $this->_parseAttributes($arg, null, ' ', '');
  886. }
  887. }
  888. return vsprintf($this->_tags[$tag], $args);
  889. }
  890. /**
  891. * Returns a formatted DIV tag for HTML FORMs.
  892. *
  893. * ### Options
  894. *
  895. * - `escape` Whether or not the contents should be html_entity escaped.
  896. *
  897. * @param string $class CSS class name of the div element.
  898. * @param string $text String content that will appear inside the div element.
  899. * If null, only a start tag will be printed
  900. * @param array $options Additional HTML attributes of the DIV tag
  901. * @return string The formatted DIV element
  902. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::div
  903. */
  904. public function div($class = null, $text = null, $options = array()) {
  905. if (!empty($class)) {
  906. $options['class'] = $class;
  907. }
  908. return $this->tag('div', $text, $options);
  909. }
  910. /**
  911. * Returns a formatted P tag.
  912. *
  913. * ### Options
  914. *
  915. * - `escape` Whether or not the contents should be html_entity escaped.
  916. *
  917. * @param string $class CSS class name of the p element.
  918. * @param string $text String content that will appear inside the p element.
  919. * @param array $options Additional HTML attributes of the P tag
  920. * @return string The formatted P element
  921. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::para
  922. */
  923. public function para($class, $text, $options = array()) {
  924. if (isset($options['escape'])) {
  925. $text = h($text);
  926. }
  927. if ($class && !empty($class)) {
  928. $options['class'] = $class;
  929. }
  930. $tag = 'para';
  931. if ($text === null) {
  932. $tag = 'parastart';
  933. }
  934. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
  935. }
  936. /**
  937. * Returns an audio/video element
  938. *
  939. * ### Usage
  940. *
  941. * Using an audio file:
  942. *
  943. * `echo $this->Html->media('audio.mp3', array('fullBase' => true));`
  944. *
  945. * Outputs:
  946. *
  947. * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
  948. *
  949. * Using a video file:
  950. *
  951. * `echo $this->Html->media('video.mp4', array('text' => 'Fallback text'));`
  952. *
  953. * Outputs:
  954. *
  955. * `<video src="/files/video.mp4">Fallback text</video>`
  956. *
  957. * Using multiple video files:
  958. *
  959. * {{{
  960. * echo $this->Html->media(
  961. * array('video.mp4', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  962. * array('tag' => 'video', 'autoplay')
  963. * );
  964. * }}}
  965. *
  966. * Outputs:
  967. *
  968. * {{{
  969. * <video autoplay="autoplay">
  970. * <source src="/files/video.mp4" type="video/mp4"/>
  971. * <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
  972. * </video>
  973. * }}}
  974. *
  975. * ### Options
  976. *
  977. * - `tag` Type of media element to generate, either "audio" or "video".
  978. * If tag is not provided it's guessed based on file's mime type.
  979. * - `text` Text to include inside the audio/video tag
  980. * - `pathPrefix` Path prefix to use for relative URLs, defaults to 'files/'
  981. * - `fullBase` If provided the src attribute will get a full address including domain name
  982. *
  983. * @param string|array $path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory.
  984. * Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
  985. * @param array $options Array of HTML attributes, and special options above.
  986. * @return string Generated media element
  987. */
  988. public function media($path, $options = array()) {
  989. $options += array(
  990. 'tag' => null,
  991. 'pathPrefix' => 'files/',
  992. 'text' => ''
  993. );
  994. if (!empty($options['tag'])) {
  995. $tag = $options['tag'];
  996. } else {
  997. $tag = null;
  998. }
  999. if (is_array($path)) {
  1000. $sourceTags = '';
  1001. foreach ($path as &$source) {
  1002. if (is_string($source)) {
  1003. $source = array(
  1004. 'src' => $source,
  1005. );
  1006. }
  1007. if (!isset($source['type'])) {
  1008. $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
  1009. $source['type'] = $this->response->getMimeType($ext);
  1010. }
  1011. $source['src'] = $this->assetUrl($source['src'], $options);
  1012. $sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
  1013. }
  1014. unset($source);
  1015. $options['text'] = $sourceTags . $options['text'];
  1016. unset($options['fullBase']);
  1017. } else {
  1018. if (empty($path) && !empty($options['src'])) {
  1019. $path = $options['src'];
  1020. }
  1021. $options['src'] = $this->assetUrl($path, $options);
  1022. }
  1023. if ($tag === null) {
  1024. if (is_array($path)) {
  1025. $mimeType = $path[0]['type'];
  1026. } else {
  1027. $mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
  1028. }
  1029. if (preg_match('#^video/#', $mimeType)) {
  1030. $tag = 'video';
  1031. } else {
  1032. $tag = 'audio';
  1033. }
  1034. }
  1035. if (isset($options['poster'])) {
  1036. $options['poster'] = $this->assetUrl($options['poster'], array('pathPrefix' => Configure::read('App.imageBaseUrl')) + $options);
  1037. }
  1038. $text = $options['text'];
  1039. $options = array_diff_key($options, array(
  1040. 'tag' => null,
  1041. 'fullBase' => null,
  1042. 'pathPrefix' => null,
  1043. 'text' => null
  1044. ));
  1045. return $this->tag($tag, $text, $options);
  1046. }
  1047. /**
  1048. * Build a nested list (UL/OL) out of an associative array.
  1049. *
  1050. * @param array $list Set of elements to list
  1051. * @param array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
  1052. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  1053. * @param string $tag Type of list tag to use (ol/ul)
  1054. * @return string The nested list
  1055. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::nestedList
  1056. */
  1057. public function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
  1058. if (is_string($options)) {
  1059. $tag = $options;
  1060. $options = array();
  1061. }
  1062. $items = $this->_nestedListItem($list, $options, $itemOptions, $tag);
  1063. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
  1064. }
  1065. /**
  1066. * Internal function to build a nested list (UL/OL) out of an associative array.
  1067. *
  1068. * @param array $items Set of elements to list
  1069. * @param array $options Additional HTML attributes of the list (ol/ul) tag
  1070. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  1071. * @param string $tag Type of list tag to use (ol/ul)
  1072. * @return string The nested list element
  1073. * @see HtmlHelper::nestedList()
  1074. */
  1075. protected function _nestedListItem($items, $options, $itemOptions, $tag) {
  1076. $out = '';
  1077. $index = 1;
  1078. foreach ($items as $key => $item) {
  1079. if (is_array($item)) {
  1080. $item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
  1081. }
  1082. if (isset($itemOptions['even']) && $index % 2 === 0) {
  1083. $itemOptions['class'] = $itemOptions['even'];
  1084. } elseif (isset($itemOptions['odd']) && $index % 2 !== 0) {
  1085. $itemOptions['class'] = $itemOptions['odd'];
  1086. }
  1087. $out .= sprintf($this->_tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
  1088. $index++;
  1089. }
  1090. return $out;
  1091. }
  1092. /**
  1093. * Load Html tag configuration.
  1094. *
  1095. * Loads a file from APP/Config that contains tag data. By default the file is expected
  1096. * to be compatible with PhpReader:
  1097. *
  1098. * `$this->Html->loadConfig('tags.php');`
  1099. *
  1100. * tags.php could look like:
  1101. *
  1102. * {{{
  1103. * $tags = array(
  1104. * 'meta' => '<meta %s>'
  1105. * );
  1106. * }}}
  1107. *
  1108. * If you wish to store tag definitions in another format you can give an array
  1109. * containing the file name, and reader class name:
  1110. *
  1111. * `$this->Html->loadConfig(array('tags.ini', 'ini'));`
  1112. *
  1113. * Its expected that the `tags` index will exist from any configuration file that is read.
  1114. * You can also specify the path to read the configuration file from, if APP/Config is not
  1115. * where the file is.
  1116. *
  1117. * `$this->Html->loadConfig('tags.php', APP . 'Lib' . DS);`
  1118. *
  1119. * Configuration files can define the following sections:
  1120. *
  1121. * - `tags` The tags to replace.
  1122. * - `minimizedAttributes` The attributes that are represented like `disabled="disabled"`
  1123. * - `docTypes` Additional doctypes to use.
  1124. * - `attributeFormat` Format for long attributes e.g. `'%s="%s"'`
  1125. * - `minimizedAttributeFormat` Format for minimized attributes e.g. `'%s="%s"'`
  1126. *
  1127. * @param string|array $configFile String with the config file (load using PhpReader) or an array with file and reader name
  1128. * @param string $path Path with config file
  1129. * @return mixed False to error or loaded configs
  1130. * @throws ConfigureException
  1131. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#changing-the-tags-output-by-htmlhelper
  1132. */
  1133. public function loadConfig($configFile, $path = null) {
  1134. if (!$path) {
  1135. $path = APP . 'Config' . DS;
  1136. }
  1137. $file = null;
  1138. $reader = 'php';
  1139. if (!is_array($configFile)) {
  1140. $file = $configFile;
  1141. } elseif (isset($configFile[0])) {
  1142. $file = $configFile[0];
  1143. if (isset($configFile[1])) {
  1144. $reader = $configFile[1];
  1145. }
  1146. } else {
  1147. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Wrong "configFile" configuration.'));
  1148. }
  1149. $readerClass = Inflector::camelize($reader) . 'Reader';
  1150. App::uses($readerClass, 'Configure');
  1151. if (!class_exists($readerClass)) {
  1152. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Unknown reader.'));
  1153. }
  1154. $readerObj = new $readerClass($path);
  1155. $configs = $readerObj->read($file);
  1156. if (isset($configs['tags']) && is_array($configs['tags'])) {
  1157. $this->_tags = array_merge($this->_tags, $configs['tags']);
  1158. }
  1159. if (isset($configs['minimizedAttributes']) && is_array($configs['minimizedAttributes'])) {
  1160. $this->_minimizedAttributes = array_merge($this->_minimizedAttributes, $configs['minimizedAttributes']);
  1161. }
  1162. if (isset($configs['docTypes']) && is_array($configs['docTypes'])) {
  1163. $this->_docTypes = array_merge($this->_docTypes, $configs['docTypes']);
  1164. }
  1165. if (isset($configs['attributeFormat'])) {
  1166. $this->_attributeFormat = $configs['attributeFormat'];
  1167. }
  1168. if (isset($configs['minimizedAttributeFormat'])) {
  1169. $this->_minimizedAttributeFormat = $configs['minimizedAttributeFormat'];
  1170. }
  1171. return $configs;
  1172. }
  1173. }