HtmlHelper.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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'] = '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. $options['link'] = $this->assetUrl($options['link']);
  263. if (isset($options['rel']) && $options['rel'] === 'icon') {
  264. $out = sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('block', 'link'), ' ', ' '));
  265. $options['rel'] = 'shortcut icon';
  266. }
  267. $out .= sprintf($this->_tags['metalink'], $options['link'], $this->_parseAttributes($options, array('block', 'link'), ' ', ' '));
  268. } else {
  269. $out = sprintf($this->_tags['meta'], $this->_parseAttributes($options, array('block', 'type'), ' ', ' '));
  270. }
  271. if (empty($options['block'])) {
  272. return $out;
  273. }
  274. $this->_View->append($options['block'], $out);
  275. }
  276. /**
  277. * Returns a charset META-tag.
  278. *
  279. * @param string $charset The character set to be used in the meta tag. If empty,
  280. * The App.encoding value will be used. Example: "utf-8".
  281. * @return string A meta tag containing the specified character set.
  282. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::charset
  283. */
  284. public function charset($charset = null) {
  285. if (empty($charset)) {
  286. $charset = strtolower(Configure::read('App.encoding'));
  287. }
  288. return sprintf($this->_tags['charset'], (!empty($charset) ? $charset : 'utf-8'));
  289. }
  290. /**
  291. * Creates an HTML link.
  292. *
  293. * If $url starts with "http://" this is treated as an external link. Else,
  294. * it is treated as a path to controller/action and parsed with the
  295. * HtmlHelper::url() method.
  296. *
  297. * If the $url is empty, $title is used instead.
  298. *
  299. * ### Options
  300. *
  301. * - `escape` Set to false to disable escaping of title and attributes.
  302. * - `escapeTitle` Set to false to disable escaping of title. (Takes precedence over value of `escape`)
  303. * - `confirm` JavaScript confirmation message.
  304. *
  305. * @param string $title The content to be wrapped by <a> tags.
  306. * @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  307. * @param array $options Array of options and HTML attributes.
  308. * @param string $confirmMessage JavaScript confirmation message.
  309. * @return string An `<a />` element.
  310. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
  311. */
  312. public function link($title, $url = null, $options = array(), $confirmMessage = false) {
  313. $escapeTitle = true;
  314. if ($url !== null) {
  315. $url = $this->url($url);
  316. } else {
  317. $url = $this->url($title);
  318. $title = htmlspecialchars_decode($url, ENT_QUOTES);
  319. $title = h(urldecode($title));
  320. $escapeTitle = false;
  321. }
  322. if (isset($options['escapeTitle'])) {
  323. $escapeTitle = $options['escapeTitle'];
  324. unset($options['escapeTitle']);
  325. } elseif (isset($options['escape'])) {
  326. $escapeTitle = $options['escape'];
  327. }
  328. if ($escapeTitle === true) {
  329. $title = h($title);
  330. } elseif (is_string($escapeTitle)) {
  331. $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
  332. }
  333. if (!empty($options['confirm'])) {
  334. $confirmMessage = $options['confirm'];
  335. unset($options['confirm']);
  336. }
  337. if ($confirmMessage) {
  338. $options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
  339. } elseif (isset($options['default']) && !$options['default']) {
  340. if (isset($options['onclick'])) {
  341. $options['onclick'] .= ' ';
  342. } else {
  343. $options['onclick'] = '';
  344. }
  345. $options['onclick'] .= 'event.returnValue = false; return false;';
  346. unset($options['default']);
  347. }
  348. return sprintf($this->_tags['link'], $url, $this->_parseAttributes($options), $title);
  349. }
  350. /**
  351. * Creates a link element for CSS stylesheets.
  352. *
  353. * ### Usage
  354. *
  355. * Include one CSS file:
  356. *
  357. * `echo $this->Html->css('styles.css');`
  358. *
  359. * Include multiple CSS files:
  360. *
  361. * `echo $this->Html->css(array('one.css', 'two.css'));`
  362. *
  363. * Add the stylesheet to the `$scripts_for_layout` layout var:
  364. *
  365. * `$this->Html->css('styles.css', array('inline' => false));`
  366. *
  367. * Add the stylesheet to a custom block:
  368. *
  369. * `$this->Html->css('styles.css', array('block' => 'layoutCss'));`
  370. *
  371. * ### Options
  372. *
  373. * - `inline` If set to false, the generated tag will be appended to the 'css' block,
  374. * and included in the `$scripts_for_layout` layout variable. Defaults to true.
  375. * - `block` Set the name of the block link/style tag will be appended to.
  376. * This overrides the `inline` option.
  377. * - `plugin` False value will prevent parsing path as a plugin
  378. * - `rel` Defaults to 'stylesheet'. If equal to 'import' the stylesheet will be imported.
  379. * - `fullBase` If true the URL will get a full address for the css file.
  380. *
  381. * @param string|array $path The name of a CSS style sheet or an array containing names of
  382. * CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
  383. * of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
  384. * @param array $options Array of options and HTML arguments.
  385. * @return string CSS <link /> or <style /> tag, depending on the type of link.
  386. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::css
  387. */
  388. public function css($path, $options = array()) {
  389. if (!is_array($options)) {
  390. $rel = $options;
  391. $options = array();
  392. if ($rel) {
  393. $options['rel'] = $rel;
  394. }
  395. if (func_num_args() > 2) {
  396. $options = func_get_arg(2) + $options;
  397. }
  398. unset($rel);
  399. }
  400. $options += array('block' => null, 'inline' => true, 'rel' => 'stylesheet');
  401. if (!$options['inline'] && empty($options['block'])) {
  402. $options['block'] = __FUNCTION__;
  403. }
  404. unset($options['inline']);
  405. if (is_array($path)) {
  406. $out = '';
  407. foreach ($path as $i) {
  408. $out .= "\n\t" . $this->css($i, $options);
  409. }
  410. if (empty($options['block'])) {
  411. return $out . "\n";
  412. }
  413. return;
  414. }
  415. if (strpos($path, '//') !== false) {
  416. $url = $path;
  417. } else {
  418. $url = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.cssBaseUrl'), 'ext' => '.css'));
  419. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  420. if (Configure::read('Asset.filter.css')) {
  421. $pos = strpos($url, Configure::read('App.cssBaseUrl'));
  422. if ($pos !== false) {
  423. $url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(Configure::read('App.cssBaseUrl')));
  424. }
  425. }
  426. }
  427. if ($options['rel'] == 'import') {
  428. $out = sprintf(
  429. $this->_tags['style'],
  430. $this->_parseAttributes($options, array('rel', 'block'), '', ' '),
  431. '@import url(' . $url . ');'
  432. );
  433. } else {
  434. $out = sprintf(
  435. $this->_tags['css'],
  436. $options['rel'],
  437. $url,
  438. $this->_parseAttributes($options, array('rel', 'block'), '', ' ')
  439. );
  440. }
  441. if (empty($options['block'])) {
  442. return $out;
  443. }
  444. $this->_View->append($options['block'], $out);
  445. }
  446. /**
  447. * Returns one or many `<script>` tags depending on the number of scripts given.
  448. *
  449. * If the filename is prefixed with "/", the path will be relative to the base path of your
  450. * application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
  451. *
  452. *
  453. * ### Usage
  454. *
  455. * Include one script file:
  456. *
  457. * `echo $this->Html->script('styles.js');`
  458. *
  459. * Include multiple script files:
  460. *
  461. * `echo $this->Html->script(array('one.js', 'two.js'));`
  462. *
  463. * Add the script file to the `$scripts_for_layout` layout var:
  464. *
  465. * `$this->Html->script('styles.js', array('inline' => false));`
  466. *
  467. * Add the script file to a custom block:
  468. *
  469. * `$this->Html->script('styles.js', null, array('block' => 'bodyScript'));`
  470. *
  471. * ### Options
  472. *
  473. * - `inline` Whether script should be output inline or into `$scripts_for_layout`. When set to false,
  474. * the script tag will be appended to the 'script' view block as well as `$scripts_for_layout`.
  475. * - `block` The name of the block you want the script appended to. Leave undefined to output inline.
  476. * Using this option will override the inline option.
  477. * - `once` Whether or not the script should be checked for uniqueness. If true scripts will only be
  478. * included once, use false to allow the same script to be included more than once per request.
  479. * - `plugin` False value will prevent parsing path as a plugin
  480. * - `fullBase` If true the url will get a full address for the script file.
  481. *
  482. * @param string|array $url String or array of javascript files to include
  483. * @param array|boolean $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
  484. * @return mixed String of `<script />` tags or null if $inline is false or if $once is true and the file has been
  485. * included before.
  486. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::script
  487. */
  488. public function script($url, $options = array()) {
  489. if (is_bool($options)) {
  490. list($inline, $options) = array($options, array());
  491. $options['inline'] = $inline;
  492. }
  493. $options = array_merge(array('block' => null, 'inline' => true, 'once' => true), $options);
  494. if (!$options['inline'] && empty($options['block'])) {
  495. $options['block'] = __FUNCTION__;
  496. }
  497. unset($options['inline']);
  498. if (is_array($url)) {
  499. $out = '';
  500. foreach ($url as $i) {
  501. $out .= "\n\t" . $this->script($i, $options);
  502. }
  503. if (empty($options['block'])) {
  504. return $out . "\n";
  505. }
  506. return null;
  507. }
  508. if ($options['once'] && isset($this->_includedScripts[$url])) {
  509. return null;
  510. }
  511. $this->_includedScripts[$url] = true;
  512. if (strpos($url, '//') === false) {
  513. $url = $this->assetUrl($url, $options + array('pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js'));
  514. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  515. if (Configure::read('Asset.filter.js')) {
  516. $url = str_replace(Configure::read('App.jsBaseUrl'), 'cjs/', $url);
  517. }
  518. }
  519. $attributes = $this->_parseAttributes($options, array('block', 'once'), ' ');
  520. $out = sprintf($this->_tags['javascriptlink'], $url, $attributes);
  521. if (empty($options['block'])) {
  522. return $out;
  523. }
  524. $this->_View->append($options['block'], $out);
  525. }
  526. /**
  527. * Wrap $script in a script tag.
  528. *
  529. * ### Options
  530. *
  531. * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
  532. * - `inline` (boolean) Whether or not the $script should be added to
  533. * `$scripts_for_layout` / `script` block, or output inline. (Deprecated, use `block` instead)
  534. * - `block` Which block you want this script block appended to.
  535. * Defaults to `script`.
  536. *
  537. * @param string $script The script to wrap
  538. * @param array $options The options to use. Options not listed above will be
  539. * treated as HTML attributes.
  540. * @return mixed string or null depending on the value of `$options['block']`
  541. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock
  542. */
  543. public function scriptBlock($script, $options = array()) {
  544. $options += array('type' => 'text/javascript', 'safe' => true, 'inline' => true);
  545. if ($options['safe']) {
  546. $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
  547. }
  548. if (!$options['inline'] && empty($options['block'])) {
  549. $options['block'] = 'script';
  550. }
  551. unset($options['inline'], $options['safe']);
  552. $attributes = $this->_parseAttributes($options, array('block'), ' ');
  553. $out = sprintf($this->_tags['javascriptblock'], $attributes, $script);
  554. if (empty($options['block'])) {
  555. return $out;
  556. }
  557. $this->_View->append($options['block'], $out);
  558. }
  559. /**
  560. * Begin a script block that captures output until HtmlHelper::scriptEnd()
  561. * is called. This capturing block will capture all output between the methods
  562. * and create a scriptBlock from it.
  563. *
  564. * ### Options
  565. *
  566. * - `safe` Whether the code block should contain a CDATA
  567. * - `inline` Should the generated script tag be output inline or in `$scripts_for_layout`
  568. *
  569. * @param array $options Options for the code block.
  570. * @return void
  571. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptStart
  572. */
  573. public function scriptStart($options = array()) {
  574. $options += array('safe' => true, 'inline' => true);
  575. $this->_scriptBlockOptions = $options;
  576. ob_start();
  577. return null;
  578. }
  579. /**
  580. * End a Buffered section of JavaScript capturing.
  581. * Generates a script tag inline or in `$scripts_for_layout` depending on the settings
  582. * used when the scriptBlock was started
  583. *
  584. * @return mixed depending on the settings of scriptStart() either a script tag or null
  585. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptEnd
  586. */
  587. public function scriptEnd() {
  588. $buffer = ob_get_clean();
  589. $options = $this->_scriptBlockOptions;
  590. $this->_scriptBlockOptions = array();
  591. return $this->scriptBlock($buffer, $options);
  592. }
  593. /**
  594. * Builds CSS style data from an array of CSS properties
  595. *
  596. * ### Usage:
  597. *
  598. * {{{
  599. * echo $this->Html->style(array('margin' => '10px', 'padding' => '10px'), true);
  600. *
  601. * // creates
  602. * 'margin:10px;padding:10px;'
  603. * }}}
  604. *
  605. * @param array $data Style data array, keys will be used as property names, values as property values.
  606. * @param boolean $oneline Whether or not the style block should be displayed on one line.
  607. * @return string CSS styling data
  608. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::style
  609. */
  610. public function style($data, $oneline = true) {
  611. if (!is_array($data)) {
  612. return $data;
  613. }
  614. $out = array();
  615. foreach ($data as $key => $value) {
  616. $out[] = $key . ':' . $value . ';';
  617. }
  618. if ($oneline) {
  619. return implode(' ', $out);
  620. }
  621. return implode("\n", $out);
  622. }
  623. /**
  624. * Returns the breadcrumb trail as a sequence of &raquo;-separated links.
  625. *
  626. * If `$startText` is an array, the accepted keys are:
  627. *
  628. * - `text` Define the text/content for the link.
  629. * - `url` Define the target of the created link.
  630. *
  631. * All other keys will be passed to HtmlHelper::link() as the `$options` parameter.
  632. *
  633. * @param string $separator Text to separate crumbs.
  634. * @param string|array|boolean $startText This will be the first crumb, if false it defaults to first crumb in array. Can
  635. * also be an array, see above for details.
  636. * @return string Composed bread crumbs
  637. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
  638. */
  639. public function getCrumbs($separator = '&raquo;', $startText = false) {
  640. $crumbs = $this->_prepareCrumbs($startText);
  641. if (!empty($crumbs)) {
  642. $out = array();
  643. foreach ($crumbs as $crumb) {
  644. if (!empty($crumb[1])) {
  645. $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
  646. } else {
  647. $out[] = $crumb[0];
  648. }
  649. }
  650. return implode($separator, $out);
  651. }
  652. return null;
  653. }
  654. /**
  655. * Returns breadcrumbs as a (x)html list
  656. *
  657. * This method uses HtmlHelper::tag() to generate list and its elements. Works
  658. * similar to HtmlHelper::getCrumbs(), so it uses options which every
  659. * crumb was added with.
  660. *
  661. * ### Options
  662. * - `separator` Separator content to insert in between breadcrumbs, defaults to ''
  663. * - `firstClass` Class for wrapper tag on the first breadcrumb, defaults to 'first'
  664. * - `lastClass` Class for wrapper tag on current active page, defaults to 'last'
  665. *
  666. * @param array $options Array of html attributes to apply to the generated list elements.
  667. * @param string|array|boolean $startText This will be the first crumb, if false it defaults to first crumb in array. Can
  668. * also be an array, see `HtmlHelper::getCrumbs` for details.
  669. * @return string breadcrumbs html list
  670. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#creating-breadcrumb-trails-with-htmlhelper
  671. */
  672. public function getCrumbList($options = array(), $startText = false) {
  673. $defaults = array('firstClass' => 'first', 'lastClass' => 'last', 'separator' => '', 'escape' => true);
  674. $options = array_merge($defaults, (array)$options);
  675. $firstClass = $options['firstClass'];
  676. $lastClass = $options['lastClass'];
  677. $separator = $options['separator'];
  678. $escape = $options['escape'];
  679. unset($options['firstClass'], $options['lastClass'], $options['separator'], $options['escape']);
  680. $crumbs = $this->_prepareCrumbs($startText, $escape);
  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. * @param boolean $escape If the output should be escaped or not
  711. * @return array Crumb list including startText (if provided)
  712. */
  713. protected function _prepareCrumbs($startText, $escape = true) {
  714. $crumbs = $this->_crumbs;
  715. if ($startText) {
  716. if (!is_array($startText)) {
  717. $startText = array(
  718. 'url' => '/',
  719. 'text' => $startText
  720. );
  721. }
  722. $startText += array('url' => '/', 'text' => __d('cake', 'Home'));
  723. list($url, $text) = array($startText['url'], $startText['text']);
  724. unset($startText['url'], $startText['text']);
  725. array_unshift($crumbs, array($text, $url, $startText + array('escape' => $escape)));
  726. }
  727. return $crumbs;
  728. }
  729. /**
  730. * Creates a formatted IMG element.
  731. *
  732. * This method will set an empty alt attribute if one is not supplied.
  733. *
  734. * ### Usage:
  735. *
  736. * Create a regular image:
  737. *
  738. * `echo $this->Html->image('cake_icon.png', array('alt' => 'CakePHP'));`
  739. *
  740. * Create an image link:
  741. *
  742. * `echo $this->Html->image('cake_icon.png', array('alt' => 'CakePHP', 'url' => 'http://cakephp.org'));`
  743. *
  744. * ### Options:
  745. *
  746. * - `url` If provided an image link will be generated and the link will point at
  747. * `$options['url']`.
  748. * - `fullBase` If true the src attribute will get a full address for the image file.
  749. * - `plugin` False value will prevent parsing path as a plugin
  750. *
  751. * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
  752. * @param array $options Array of HTML attributes. See above for special options.
  753. * @return string completed img tag
  754. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::image
  755. */
  756. public function image($path, $options = array()) {
  757. $path = $this->assetUrl($path, $options + array('pathPrefix' => Configure::read('App.imageBaseUrl')));
  758. $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
  759. if (!isset($options['alt'])) {
  760. $options['alt'] = '';
  761. }
  762. $url = false;
  763. if (!empty($options['url'])) {
  764. $url = $options['url'];
  765. unset($options['url']);
  766. }
  767. $image = sprintf($this->_tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
  768. if ($url) {
  769. return sprintf($this->_tags['link'], $this->url($url), null, $image);
  770. }
  771. return $image;
  772. }
  773. /**
  774. * Returns a row of formatted and named TABLE headers.
  775. *
  776. * @param array $names Array of tablenames. Each tablename also can be a key that points to an array with a set
  777. * of attributes to its specific tag
  778. * @param array $trOptions HTML options for TR elements.
  779. * @param array $thOptions HTML options for TH elements.
  780. * @return string Completed table headers
  781. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tableHeaders
  782. */
  783. public function tableHeaders($names, $trOptions = null, $thOptions = null) {
  784. $out = array();
  785. foreach ($names as $arg) {
  786. if (!is_array($arg)) {
  787. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
  788. } else {
  789. $out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes(current($arg)), key($arg));
  790. }
  791. }
  792. return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), implode(' ', $out));
  793. }
  794. /**
  795. * Returns a formatted string of table rows (TR's with TD's in them).
  796. *
  797. * @param array $data Array of table data
  798. * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
  799. * @param array $evenTrOptions HTML options for even TR elements
  800. * @param boolean $useCount adds class "column-$i"
  801. * @param boolean $continueOddEven If false, will use a non-static $count variable,
  802. * so that the odd/even count is reset to zero just for that call.
  803. * @return string Formatted HTML
  804. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tableCells
  805. */
  806. public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
  807. if (empty($data[0]) || !is_array($data[0])) {
  808. $data = array($data);
  809. }
  810. if ($oddTrOptions === true) {
  811. $useCount = true;
  812. $oddTrOptions = null;
  813. }
  814. if ($evenTrOptions === false) {
  815. $continueOddEven = false;
  816. $evenTrOptions = null;
  817. }
  818. if ($continueOddEven) {
  819. static $count = 0;
  820. } else {
  821. $count = 0;
  822. }
  823. foreach ($data as $line) {
  824. $count++;
  825. $cellsOut = array();
  826. $i = 0;
  827. foreach ($line as $cell) {
  828. $cellOptions = array();
  829. if (is_array($cell)) {
  830. $cellOptions = $cell[1];
  831. $cell = $cell[0];
  832. } elseif ($useCount) {
  833. $cellOptions['class'] = 'column-' . ++$i;
  834. }
  835. $cellsOut[] = sprintf($this->_tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
  836. }
  837. $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
  838. $out[] = sprintf($this->_tags['tablerow'], $options, implode(' ', $cellsOut));
  839. }
  840. return implode("\n", $out);
  841. }
  842. /**
  843. * Returns a formatted block tag, i.e DIV, SPAN, P.
  844. *
  845. * ### Options
  846. *
  847. * - `escape` Whether or not the contents should be html_entity escaped.
  848. *
  849. * @param string $name Tag name.
  850. * @param string $text String content that will appear inside the div element.
  851. * If null, only a start tag will be printed
  852. * @param array $options Additional HTML attributes of the DIV tag, see above.
  853. * @return string The formatted tag element
  854. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::tag
  855. */
  856. public function tag($name, $text = null, $options = array()) {
  857. if (empty($name)) {
  858. return $text;
  859. }
  860. if (isset($options['escape']) && $options['escape']) {
  861. $text = h($text);
  862. unset($options['escape']);
  863. }
  864. if ($text === null) {
  865. $tag = 'tagstart';
  866. } else {
  867. $tag = 'tag';
  868. }
  869. return sprintf($this->_tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
  870. }
  871. /**
  872. * Returns a formatted existent block of $tags
  873. *
  874. * @param string $tag Tag name
  875. * @return string Formatted block
  876. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::useTag
  877. */
  878. public function useTag($tag) {
  879. if (!isset($this->_tags[$tag])) {
  880. return '';
  881. }
  882. $args = func_get_args();
  883. array_shift($args);
  884. foreach ($args as &$arg) {
  885. if (is_array($arg)) {
  886. $arg = $this->_parseAttributes($arg, null, ' ', '');
  887. }
  888. }
  889. return vsprintf($this->_tags[$tag], $args);
  890. }
  891. /**
  892. * Returns a formatted DIV tag for HTML FORMs.
  893. *
  894. * ### Options
  895. *
  896. * - `escape` Whether or not the contents should be html_entity escaped.
  897. *
  898. * @param string $class CSS class name of the div element.
  899. * @param string $text String content that will appear inside the div element.
  900. * If null, only a start tag will be printed
  901. * @param array $options Additional HTML attributes of the DIV tag
  902. * @return string The formatted DIV element
  903. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::div
  904. */
  905. public function div($class = null, $text = null, $options = array()) {
  906. if (!empty($class)) {
  907. $options['class'] = $class;
  908. }
  909. return $this->tag('div', $text, $options);
  910. }
  911. /**
  912. * Returns a formatted P tag.
  913. *
  914. * ### Options
  915. *
  916. * - `escape` Whether or not the contents should be html_entity escaped.
  917. *
  918. * @param string $class CSS class name of the p element.
  919. * @param string $text String content that will appear inside the p element.
  920. * @param array $options Additional HTML attributes of the P tag
  921. * @return string The formatted P element
  922. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::para
  923. */
  924. public function para($class, $text, $options = array()) {
  925. if (isset($options['escape'])) {
  926. $text = h($text);
  927. }
  928. if ($class && !empty($class)) {
  929. $options['class'] = $class;
  930. }
  931. $tag = 'para';
  932. if ($text === null) {
  933. $tag = 'parastart';
  934. }
  935. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
  936. }
  937. /**
  938. * Returns an audio/video element
  939. *
  940. * ### Usage
  941. *
  942. * Using an audio file:
  943. *
  944. * `echo $this->Html->media('audio.mp3', array('fullBase' => true));`
  945. *
  946. * Outputs:
  947. *
  948. * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
  949. *
  950. * Using a video file:
  951. *
  952. * `echo $this->Html->media('video.mp4', array('text' => 'Fallback text'));`
  953. *
  954. * Outputs:
  955. *
  956. * `<video src="/files/video.mp4">Fallback text</video>`
  957. *
  958. * Using multiple video files:
  959. *
  960. * {{{
  961. * echo $this->Html->media(
  962. * array('video.mp4', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
  963. * array('tag' => 'video', 'autoplay')
  964. * );
  965. * }}}
  966. *
  967. * Outputs:
  968. *
  969. * {{{
  970. * <video autoplay="autoplay">
  971. * <source src="/files/video.mp4" type="video/mp4"/>
  972. * <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
  973. * </video>
  974. * }}}
  975. *
  976. * ### Options
  977. *
  978. * - `tag` Type of media element to generate, either "audio" or "video".
  979. * If tag is not provided it's guessed based on file's mime type.
  980. * - `text` Text to include inside the audio/video tag
  981. * - `pathPrefix` Path prefix to use for relative URLs, defaults to 'files/'
  982. * - `fullBase` If provided the src attribute will get a full address including domain name
  983. *
  984. * @param string|array $path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory.
  985. * Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
  986. * @param array $options Array of HTML attributes, and special options above.
  987. * @return string Generated media element
  988. */
  989. public function media($path, $options = array()) {
  990. $options += array(
  991. 'tag' => null,
  992. 'pathPrefix' => 'files/',
  993. 'text' => ''
  994. );
  995. if (!empty($options['tag'])) {
  996. $tag = $options['tag'];
  997. } else {
  998. $tag = null;
  999. }
  1000. if (is_array($path)) {
  1001. $sourceTags = '';
  1002. foreach ($path as &$source) {
  1003. if (is_string($source)) {
  1004. $source = array(
  1005. 'src' => $source,
  1006. );
  1007. }
  1008. if (!isset($source['type'])) {
  1009. $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
  1010. $source['type'] = $this->response->getMimeType($ext);
  1011. }
  1012. $source['src'] = $this->assetUrl($source['src'], $options);
  1013. $sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
  1014. }
  1015. unset($source);
  1016. $options['text'] = $sourceTags . $options['text'];
  1017. unset($options['fullBase']);
  1018. } else {
  1019. if (empty($path) && !empty($options['src'])) {
  1020. $path = $options['src'];
  1021. }
  1022. $options['src'] = $this->assetUrl($path, $options);
  1023. }
  1024. if ($tag === null) {
  1025. if (is_array($path)) {
  1026. $mimeType = $path[0]['type'];
  1027. } else {
  1028. $mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
  1029. }
  1030. if (preg_match('#^video/#', $mimeType)) {
  1031. $tag = 'video';
  1032. } else {
  1033. $tag = 'audio';
  1034. }
  1035. }
  1036. if (isset($options['poster'])) {
  1037. $options['poster'] = $this->assetUrl($options['poster'], array('pathPrefix' => Configure::read('App.imageBaseUrl')) + $options);
  1038. }
  1039. $text = $options['text'];
  1040. $options = array_diff_key($options, array(
  1041. 'tag' => null,
  1042. 'fullBase' => null,
  1043. 'pathPrefix' => null,
  1044. 'text' => null
  1045. ));
  1046. return $this->tag($tag, $text, $options);
  1047. }
  1048. /**
  1049. * Build a nested list (UL/OL) out of an associative array.
  1050. *
  1051. * @param array $list Set of elements to list
  1052. * @param array $options Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
  1053. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  1054. * @param string $tag Type of list tag to use (ol/ul)
  1055. * @return string The nested list
  1056. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::nestedList
  1057. */
  1058. public function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
  1059. if (is_string($options)) {
  1060. $tag = $options;
  1061. $options = array();
  1062. }
  1063. $items = $this->_nestedListItem($list, $options, $itemOptions, $tag);
  1064. return sprintf($this->_tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
  1065. }
  1066. /**
  1067. * Internal function to build a nested list (UL/OL) out of an associative array.
  1068. *
  1069. * @param array $items Set of elements to list
  1070. * @param array $options Additional HTML attributes of the list (ol/ul) tag
  1071. * @param array $itemOptions Additional HTML attributes of the list item (LI) tag
  1072. * @param string $tag Type of list tag to use (ol/ul)
  1073. * @return string The nested list element
  1074. * @see HtmlHelper::nestedList()
  1075. */
  1076. protected function _nestedListItem($items, $options, $itemOptions, $tag) {
  1077. $out = '';
  1078. $index = 1;
  1079. foreach ($items as $key => $item) {
  1080. if (is_array($item)) {
  1081. $item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
  1082. }
  1083. if (isset($itemOptions['even']) && $index % 2 === 0) {
  1084. $itemOptions['class'] = $itemOptions['even'];
  1085. } elseif (isset($itemOptions['odd']) && $index % 2 !== 0) {
  1086. $itemOptions['class'] = $itemOptions['odd'];
  1087. }
  1088. $out .= sprintf($this->_tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
  1089. $index++;
  1090. }
  1091. return $out;
  1092. }
  1093. /**
  1094. * Load Html tag configuration.
  1095. *
  1096. * Loads a file from APP/Config that contains tag data. By default the file is expected
  1097. * to be compatible with PhpReader:
  1098. *
  1099. * `$this->Html->loadConfig('tags.php');`
  1100. *
  1101. * tags.php could look like:
  1102. *
  1103. * {{{
  1104. * $tags = array(
  1105. * 'meta' => '<meta %s>'
  1106. * );
  1107. * }}}
  1108. *
  1109. * If you wish to store tag definitions in another format you can give an array
  1110. * containing the file name, and reader class name:
  1111. *
  1112. * `$this->Html->loadConfig(array('tags.ini', 'ini'));`
  1113. *
  1114. * Its expected that the `tags` index will exist from any configuration file that is read.
  1115. * You can also specify the path to read the configuration file from, if APP/Config is not
  1116. * where the file is.
  1117. *
  1118. * `$this->Html->loadConfig('tags.php', APP . 'Lib' . DS);`
  1119. *
  1120. * Configuration files can define the following sections:
  1121. *
  1122. * - `tags` The tags to replace.
  1123. * - `minimizedAttributes` The attributes that are represented like `disabled="disabled"`
  1124. * - `docTypes` Additional doctypes to use.
  1125. * - `attributeFormat` Format for long attributes e.g. `'%s="%s"'`
  1126. * - `minimizedAttributeFormat` Format for minimized attributes e.g. `'%s="%s"'`
  1127. *
  1128. * @param string|array $configFile String with the config file (load using PhpReader) or an array with file and reader name
  1129. * @param string $path Path with config file
  1130. * @return mixed False to error or loaded configs
  1131. * @throws ConfigureException
  1132. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#changing-the-tags-output-by-htmlhelper
  1133. */
  1134. public function loadConfig($configFile, $path = null) {
  1135. if (!$path) {
  1136. $path = APP . 'Config' . DS;
  1137. }
  1138. $file = null;
  1139. $reader = 'php';
  1140. if (!is_array($configFile)) {
  1141. $file = $configFile;
  1142. } elseif (isset($configFile[0])) {
  1143. $file = $configFile[0];
  1144. if (isset($configFile[1])) {
  1145. $reader = $configFile[1];
  1146. }
  1147. } else {
  1148. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Wrong "configFile" configuration.'));
  1149. }
  1150. $readerClass = Inflector::camelize($reader) . 'Reader';
  1151. App::uses($readerClass, 'Configure');
  1152. if (!class_exists($readerClass)) {
  1153. throw new ConfigureException(__d('cake_dev', 'Cannot load the configuration file. Unknown reader.'));
  1154. }
  1155. $readerObj = new $readerClass($path);
  1156. $configs = $readerObj->read($file);
  1157. if (isset($configs['tags']) && is_array($configs['tags'])) {
  1158. $this->_tags = array_merge($this->_tags, $configs['tags']);
  1159. }
  1160. if (isset($configs['minimizedAttributes']) && is_array($configs['minimizedAttributes'])) {
  1161. $this->_minimizedAttributes = array_merge($this->_minimizedAttributes, $configs['minimizedAttributes']);
  1162. }
  1163. if (isset($configs['docTypes']) && is_array($configs['docTypes'])) {
  1164. $this->_docTypes = array_merge($this->_docTypes, $configs['docTypes']);
  1165. }
  1166. if (isset($configs['attributeFormat'])) {
  1167. $this->_attributeFormat = $configs['attributeFormat'];
  1168. }
  1169. if (isset($configs['minimizedAttributeFormat'])) {
  1170. $this->_minimizedAttributeFormat = $configs['minimizedAttributeFormat'];
  1171. }
  1172. return $configs;
  1173. }
  1174. }