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