HtmlHelper.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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 this HtmlHelper
  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. return $this;
  174. }
  175. /**
  176. * Returns a doctype string.
  177. *
  178. * Possible doctypes:
  179. *
  180. * - html4-strict: HTML4 Strict.
  181. * - html4-trans: HTML4 Transitional.
  182. * - html4-frame: HTML4 Frameset.
  183. * - html5: HTML5. Default value.
  184. * - xhtml-strict: XHTML1 Strict.
  185. * - xhtml-trans: XHTML1 Transitional.
  186. * - xhtml-frame: XHTML1 Frameset.
  187. * - xhtml11: XHTML1.1.
  188. *
  189. * @param string $type Doctype to use.
  190. * @return string Doctype string
  191. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::docType
  192. */
  193. public function docType($type = 'html5') {
  194. if (isset($this->_docTypes[$type])) {
  195. return $this->_docTypes[$type];
  196. }
  197. return null;
  198. }
  199. /**
  200. * Creates a link to an external resource and handles basic meta tags
  201. *
  202. * Create a meta tag that is output inline:
  203. *
  204. * `$this->Html->meta('icon', 'favicon.ico');
  205. *
  206. * Append the meta tag to `$scripts_for_layout`:
  207. *
  208. * `$this->Html->meta('description', 'A great page', array('inline' => false));`
  209. *
  210. * Append the meta tag to custom view block:
  211. *
  212. * `$this->Html->meta('description', 'A great page', array('block' => 'metaTags'));`
  213. *
  214. * ### Options
  215. *
  216. * - `inline` Whether or not the link element should be output inline. Set to false to
  217. * have the meta tag included in `$scripts_for_layout`, and appended to the 'meta' view block.
  218. * - `block` Choose a custom block to append the meta tag to. Using this option
  219. * will override the inline option.
  220. *
  221. * @param string $type The title of the external resource
  222. * @param string|array $url The address of the external resource or string for content attribute
  223. * @param array $options Other attributes for the generated tag. If the type attribute is html,
  224. * rss, atom, or icon, the mime-type is returned.
  225. * @return string A completed `<link />` element.
  226. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::meta
  227. */
  228. public function meta($type, $url = null, $options = array()) {
  229. $options += array('inline' => true, 'block' => null);
  230. if (!$options['inline'] && empty($options['block'])) {
  231. $options['block'] = __FUNCTION__;
  232. }
  233. unset($options['inline']);
  234. if (!is_array($type)) {
  235. $types = array(
  236. 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
  237. 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
  238. 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
  239. 'keywords' => array('name' => 'keywords', 'content' => $url),
  240. 'description' => array('name' => 'description', 'content' => $url),
  241. );
  242. if ($type === 'icon' && $url === null) {
  243. $types['icon']['link'] = 'favicon.ico';
  244. }
  245. if (isset($types[$type])) {
  246. $type = $types[$type];
  247. } elseif (!isset($options['type']) && $url !== null) {
  248. if (is_array($url) && isset($url['ext'])) {
  249. $type = $types[$url['ext']];
  250. } else {
  251. $type = $types['rss'];
  252. }
  253. } elseif (isset($options['type']) && isset($types[$options['type']])) {
  254. $type = $types[$options['type']];
  255. unset($options['type']);
  256. } else {
  257. $type = array();
  258. }
  259. }
  260. $options += $type;
  261. $out = null;
  262. if (isset($options['link'])) {
  263. $options['link'] = $this->assetUrl($options['link']);
  264. if (isset($options['rel']) && $options['rel'] === 'icon') {
  265. $out = sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('block', 'link'), ' ', ' '));
  266. $options['rel'] = 'shortcut icon';
  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('block' => null, 'inline' => true, 'once' => true);
  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' => '', 'escape' => true);
  675. $options = (array)$options + $defaults;
  676. $firstClass = $options['firstClass'];
  677. $lastClass = $options['lastClass'];
  678. $separator = $options['separator'];
  679. $escape = $options['escape'];
  680. unset($options['firstClass'], $options['lastClass'], $options['separator'], $options['escape']);
  681. $crumbs = $this->_prepareCrumbs($startText, $escape);
  682. if (empty($crumbs)) {
  683. return null;
  684. }
  685. $result = '';
  686. $crumbCount = count($crumbs);
  687. $ulOptions = $options;
  688. foreach ($crumbs as $which => $crumb) {
  689. $options = array();
  690. if (empty($crumb[1])) {
  691. $elementContent = $crumb[0];
  692. } else {
  693. $elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
  694. }
  695. if (!$which && $firstClass !== false) {
  696. $options['class'] = $firstClass;
  697. } elseif ($which == $crumbCount - 1 && $lastClass !== false) {
  698. $options['class'] = $lastClass;
  699. }
  700. if (!empty($separator) && ($crumbCount - $which >= 2)) {
  701. $elementContent .= $separator;
  702. }
  703. $result .= $this->tag('li', $elementContent, $options);
  704. }
  705. return $this->tag('ul', $result, $ulOptions);
  706. }
  707. /**
  708. * Prepends startText to crumbs array if set
  709. *
  710. * @param string $startText Text to prepend
  711. * @param boolean $escape If the output should be escaped or not
  712. * @return array Crumb list including startText (if provided)
  713. */
  714. protected function _prepareCrumbs($startText, $escape = true) {
  715. $crumbs = $this->_crumbs;
  716. if ($startText) {
  717. if (!is_array($startText)) {
  718. $startText = array(
  719. 'url' => '/',
  720. 'text' => $startText
  721. );
  722. }
  723. $startText += array('url' => '/', 'text' => __d('cake', 'Home'));
  724. list($url, $text) = array($startText['url'], $startText['text']);
  725. unset($startText['url'], $startText['text']);
  726. array_unshift($crumbs, array($text, $url, $startText + array('escape' => $escape)));
  727. }
  728. return $crumbs;
  729. }
  730. /**
  731. * Creates a formatted IMG element.
  732. *
  733. * This method will set an empty alt attribute if one is not supplied.
  734. *
  735. * ### Usage:
  736. *
  737. * Create a regular image:
  738. *
  739. * `echo $this->Html->image('cake_icon.png', array('alt' => 'CakePHP'));`
  740. *
  741. * Create an image link:
  742. *
  743. * `echo $this->Html->image('cake_icon.png', array('alt' => 'CakePHP', 'url' => 'http://cakephp.org'));`
  744. *
  745. * ### Options:
  746. *
  747. * - `url` If provided an image link will be generated and the link will point at
  748. * `$options['url']`.
  749. * - `fullBase` If true the src attribute will get a full address for the image file.
  750. * - `plugin` False value will prevent parsing path as a plugin
  751. *
  752. * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
  753. * @param array $options Array of HTML attributes. See above for special options.
  754. * @return string completed img tag
  755. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::image
  756. */
  757. public function image($path, $options = array()) {
  758. $path = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.imageBaseUrl')));
  759. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  760. if (!isset($options['alt'])) {
  761. $options['alt'] = '';
  762. }
  763. $url = false;
  764. if (!empty($options['url'])) {
  765. $url = $options['url'];
  766. unset($options['url']);
  767. }
  768. $image = sprintf($this->_tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
  769. if ($url) {
  770. return sprintf($this->_tags['link'], $this->url($url), null, $image);
  771. }
  772. return $image;
  773. }
  774. /**
  775. * Returns a row of formatted and named TABLE headers.
  776. *
  777. * @param array $names Array of tablenames. Each tablename also can be a key that points to an array with a set
  778. * of attributes to its specific tag
  779. * @param array $trOptions HTML options for TR elements.
  780. * @param array $thOptions HTML options for TH elements.
  781. * @return string Completed table headers
  782. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tableHeaders
  783. */
  784. public function tableHeaders($names, $trOptions = null, $thOptions = null) {
  785. $out = array();
  786. foreach ($names as $arg) {
  787. if (!is_array($arg)) {
  788. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
  789. } else {
  790. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes(current($arg)), key($arg));
  791. }
  792. }
  793. return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), implode(' ', $out));
  794. }
  795. /**
  796. * Returns a formatted string of table rows (TR's with TD's in them).
  797. *
  798. * @param array $data Array of table data
  799. * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
  800. * @param array $evenTrOptions HTML options for even TR elements
  801. * @param boolean $useCount adds class "column-$i"
  802. * @param boolean $continueOddEven If false, will use a non-static $count variable,
  803. * so that the odd/even count is reset to zero just for that call.
  804. * @return string Formatted HTML
  805. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tableCells
  806. */
  807. public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
  808. if (empty($data[0]) || !is_array($data[0])) {
  809. $data = array($data);
  810. }
  811. if ($oddTrOptions === true) {
  812. $useCount = true;
  813. $oddTrOptions = null;
  814. }
  815. if ($evenTrOptions === false) {
  816. $continueOddEven = false;
  817. $evenTrOptions = null;
  818. }
  819. if ($continueOddEven) {
  820. static $count = 0;
  821. } else {
  822. $count = 0;
  823. }
  824. foreach ($data as $line) {
  825. $count++;
  826. $cellsOut = array();
  827. $i = 0;
  828. foreach ($line as $cell) {
  829. $cellOptions = array();
  830. if (is_array($cell)) {
  831. $cellOptions = $cell[1];
  832. $cell = $cell[0];
  833. } elseif ($useCount) {
  834. $cellOptions['class'] = 'column-' . ++$i;
  835. }
  836. $cellsOut[] = sprintf($this->_tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
  837. }
  838. $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
  839. $out[] = sprintf($this->_tags['tablerow'], $options, implode(' ', $cellsOut));
  840. }
  841. return implode("\n", $out);
  842. }
  843. /**
  844. * Returns a formatted block tag, i.e DIV, SPAN, P.
  845. *
  846. * ### Options
  847. *
  848. * - `escape` Whether or not the contents should be html_entity escaped.
  849. *
  850. * @param string $name Tag name.
  851. * @param string $text String content that will appear inside the div element.
  852. * If null, only a start tag will be printed
  853. * @param array $options Additional HTML attributes of the DIV tag, see above.
  854. * @return string The formatted tag element
  855. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tag
  856. */
  857. public function tag($name, $text = null, $options = array()) {
  858. if (empty($name)) {
  859. return $text;
  860. }
  861. if (isset($options['escape']) && $options['escape']) {
  862. $text = h($text);
  863. unset($options['escape']);
  864. }
  865. if ($text === null) {
  866. $tag = 'tagstart';
  867. } else {
  868. $tag = 'tag';
  869. }
  870. return sprintf($this->_tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
  871. }
  872. /**
  873. * Returns a formatted existent block of $tags
  874. *
  875. * @param string $tag Tag name
  876. * @return string Formatted block
  877. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::useTag
  878. */
  879. public function useTag($tag) {
  880. if (!isset($this->_tags[$tag])) {
  881. return '';
  882. }
  883. $args = func_get_args();
  884. array_shift($args);
  885. foreach ($args as &$arg) {
  886. if (is_array($arg)) {
  887. $arg = $this->_parseAttributes($arg, null, ' ', '');
  888. }
  889. }
  890. return vsprintf($this->_tags[$tag], $args);
  891. }
  892. /**
  893. * Returns a formatted DIV tag for HTML FORMs.
  894. *
  895. * ### Options
  896. *
  897. * - `escape` Whether or not the contents should be html_entity escaped.
  898. *
  899. * @param string $class CSS class name of the div element.
  900. * @param string $text String content that will appear inside the div element.
  901. * If null, only a start tag will be printed
  902. * @param array $options Additional HTML attributes of the DIV tag
  903. * @return string The formatted DIV element
  904. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::div
  905. */
  906. public function div($class = null, $text = null, $options = array()) {
  907. if (!empty($class)) {
  908. $options['class'] = $class;
  909. }
  910. return $this->tag('div', $text, $options);
  911. }
  912. /**
  913. * Returns a formatted P tag.
  914. *
  915. * ### Options
  916. *
  917. * - `escape` Whether or not the contents should be html_entity escaped.
  918. *
  919. * @param string $class CSS class name of the p element.
  920. * @param string $text String content that will appear inside the p element.
  921. * @param array $options Additional HTML attributes of the P tag
  922. * @return string The formatted P element
  923. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::para
  924. */
  925. public function para($class, $text, $options = array()) {
  926. if (isset($options['escape'])) {
  927. $text = h($text);
  928. }
  929. if ($class && !empty($class)) {
  930. $options['class'] = $class;
  931. }
  932. $tag = 'para';
  933. if ($text === null) {
  934. $tag = 'parastart';
  935. }
  936. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
  937. }
  938. /**
  939. * Returns an audio/video element
  940. *
  941. * ### Usage
  942. *
  943. * Using an audio file:
  944. *
  945. * `echo $this->Html->media('audio.mp3', array('fullBase' => true));`
  946. *
  947. * Outputs:
  948. *
  949. * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
  950. *
  951. * Using a video file:
  952. *
  953. * `echo $this->Html->media('video.mp4', array('text' => 'Fallback text'));`
  954. *
  955. * Outputs:
  956. *
  957. * `<video src="/files/video.mp4">Fallback text</video>`
  958. *
  959. * Using multiple video files:
  960. *
  961. * {{{
  962. * echo $this->Html->media(
  963. * array('video.mp4', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  964. * array('tag' => 'video', 'autoplay')
  965. * );
  966. * }}}
  967. *
  968. * Outputs:
  969. *
  970. * {{{
  971. * <video autoplay="autoplay">
  972. * <source src="/files/video.mp4" type="video/mp4"/>
  973. * <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
  974. * </video>
  975. * }}}
  976. *
  977. * ### Options
  978. *
  979. * - `tag` Type of media element to generate, either "audio" or "video".
  980. * If tag is not provided it's guessed based on file's mime type.
  981. * - `text` Text to include inside the audio/video tag
  982. * - `pathPrefix` Path prefix to use for relative URLs, defaults to 'files/'
  983. * - `fullBase` If provided the src attribute will get a full address including domain name
  984. *
  985. * @param string|array $path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory.
  986. * Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
  987. * @param array $options Array of HTML attributes, and special options above.
  988. * @return string Generated media element
  989. */
  990. public function media($path, $options = array()) {
  991. $options += array(
  992. 'tag' => null,
  993. 'pathPrefix' => 'files/',
  994. 'text' => ''
  995. );
  996. if (!empty($options['tag'])) {
  997. $tag = $options['tag'];
  998. } else {
  999. $tag = null;
  1000. }
  1001. if (is_array($path)) {
  1002. $sourceTags = '';
  1003. foreach ($path as &$source) {
  1004. if (is_string($source)) {
  1005. $source = array(
  1006. 'src' => $source,
  1007. );
  1008. }
  1009. if (!isset($source['type'])) {
  1010. $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
  1011. $source['type'] = $this->response->getMimeType($ext);
  1012. }
  1013. $source['src'] = $this->assetUrl($source['src'], $options);
  1014. $sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
  1015. }
  1016. unset($source);
  1017. $options['text'] = $sourceTags . $options['text'];
  1018. unset($options['fullBase']);
  1019. } else {
  1020. if (empty($path) && !empty($options['src'])) {
  1021. $path = $options['src'];
  1022. }
  1023. $options['src'] = $this->assetUrl($path, $options);
  1024. }
  1025. if ($tag === null) {
  1026. if (is_array($path)) {
  1027. $mimeType = $path[0]['type'];
  1028. } else {
  1029. $mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
  1030. }
  1031. if (preg_match('#^video/#', $mimeType)) {
  1032. $tag = 'video';
  1033. } else {
  1034. $tag = 'audio';
  1035. }
  1036. }
  1037. if (isset($options['poster'])) {
  1038. $options['poster'] = $this->assetUrl($options['poster'], array('pathPrefix' => Configure::read('App.imageBaseUrl')) + $options);
  1039. }
  1040. $text = $options['text'];
  1041. $options = array_diff_key($options, array(
  1042. 'tag' => null,
  1043. 'fullBase' => null,
  1044. 'pathPrefix' => null,
  1045. 'text' => null
  1046. ));
  1047. return $this->tag($tag, $text, $options);
  1048. }
  1049. /**
  1050. * Build a nested list (UL/OL) out of an associative array.
  1051. *
  1052. * @param array $list Set of elements to list
  1053. * @param array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
  1054. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  1055. * @param string $tag Type of list tag to use (ol/ul)
  1056. * @return string The nested list
  1057. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::nestedList
  1058. */
  1059. public function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
  1060. if (is_string($options)) {
  1061. $tag = $options;
  1062. $options = array();
  1063. }
  1064. $items = $this->_nestedListItem($list, $options, $itemOptions, $tag);
  1065. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
  1066. }
  1067. /**
  1068. * Internal function to build a nested list (UL/OL) out of an associative array.
  1069. *
  1070. * @param array $items Set of elements to list
  1071. * @param array $options Additional HTML attributes of the list (ol/ul) tag
  1072. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  1073. * @param string $tag Type of list tag to use (ol/ul)
  1074. * @return string The nested list element
  1075. * @see HtmlHelper::nestedList()
  1076. */
  1077. protected function _nestedListItem($items, $options, $itemOptions, $tag) {
  1078. $out = '';
  1079. $index = 1;
  1080. foreach ($items as $key => $item) {
  1081. if (is_array($item)) {
  1082. $item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
  1083. }
  1084. if (isset($itemOptions['even']) && $index % 2 === 0) {
  1085. $itemOptions['class'] = $itemOptions['even'];
  1086. } elseif (isset($itemOptions['odd']) && $index % 2 !== 0) {
  1087. $itemOptions['class'] = $itemOptions['odd'];
  1088. }
  1089. $out .= sprintf($this->_tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
  1090. $index++;
  1091. }
  1092. return $out;
  1093. }
  1094. /**
  1095. * Load Html tag configuration.
  1096. *
  1097. * Loads a file from APP/Config that contains tag data. By default the file is expected
  1098. * to be compatible with PhpReader:
  1099. *
  1100. * `$this->Html->loadConfig('tags.php');`
  1101. *
  1102. * tags.php could look like:
  1103. *
  1104. * {{{
  1105. * $tags = array(
  1106. * 'meta' => '<meta %s>'
  1107. * );
  1108. * }}}
  1109. *
  1110. * If you wish to store tag definitions in another format you can give an array
  1111. * containing the file name, and reader class name:
  1112. *
  1113. * `$this->Html->loadConfig(array('tags.ini', 'ini'));`
  1114. *
  1115. * Its expected that the `tags` index will exist from any configuration file that is read.
  1116. * You can also specify the path to read the configuration file from, if APP/Config is not
  1117. * where the file is.
  1118. *
  1119. * `$this->Html->loadConfig('tags.php', APP . 'Lib' . DS);`
  1120. *
  1121. * Configuration files can define the following sections:
  1122. *
  1123. * - `tags` The tags to replace.
  1124. * - `minimizedAttributes` The attributes that are represented like `disabled="disabled"`
  1125. * - `docTypes` Additional doctypes to use.
  1126. * - `attributeFormat` Format for long attributes e.g. `'%s="%s"'`
  1127. * - `minimizedAttributeFormat` Format for minimized attributes e.g. `'%s="%s"'`
  1128. *
  1129. * @param string|array $configFile String with the config file (load using PhpReader) or an array with file and reader name
  1130. * @param string $path Path with config file
  1131. * @return mixed False to error or loaded configs
  1132. * @throws ConfigureException
  1133. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#changing-the-tags-output-by-htmlhelper
  1134. */
  1135. public function loadConfig($configFile, $path = null) {
  1136. if (!$path) {
  1137. $path = APP . 'Config' . DS;
  1138. }
  1139. $file = null;
  1140. $reader = 'php';
  1141. if (!is_array($configFile)) {
  1142. $file = $configFile;
  1143. } elseif (isset($configFile[0])) {
  1144. $file = $configFile[0];
  1145. if (isset($configFile[1])) {
  1146. $reader = $configFile[1];
  1147. }
  1148. } else {
  1149. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Wrong "configFile" configuration.'));
  1150. }
  1151. $readerClass = Inflector::camelize($reader) . 'Reader';
  1152. App::uses($readerClass, 'Configure');
  1153. if (!class_exists($readerClass)) {
  1154. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Unknown reader.'));
  1155. }
  1156. $readerObj = new $readerClass($path);
  1157. $configs = $readerObj->read($file);
  1158. if (isset($configs['tags']) && is_array($configs['tags'])) {
  1159. $this->_tags = $configs['tags'] + $this->_tags;
  1160. }
  1161. if (isset($configs['minimizedAttributes']) && is_array($configs['minimizedAttributes'])) {
  1162. $this->_minimizedAttributes = $configs['minimizedAttributes'] + $this->_minimizedAttributes;
  1163. }
  1164. if (isset($configs['docTypes']) && is_array($configs['docTypes'])) {
  1165. $this->_docTypes = $configs['docTypes'] + $this->_docTypes;
  1166. }
  1167. if (isset($configs['attributeFormat'])) {
  1168. $this->_attributeFormat = $configs['attributeFormat'];
  1169. }
  1170. if (isset($configs['minimizedAttributeFormat'])) {
  1171. $this->_minimizedAttributeFormat = $configs['minimizedAttributeFormat'];
  1172. }
  1173. return $configs;
  1174. }
  1175. }