HtmlHelper.php 41 KB

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