HtmlHelper.php 41 KB

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