HtmlHelper.php 39 KB

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