1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: 21: 22: 23: 24: 25: 26: 27: 28:
29: class HtmlHelper extends AppHelper {
30: 31: 32: 33: 34: 35:
36: var $tags = array(
37: 'meta' => '<meta%s/>',
38: 'metalink' => '<link href="%s"%s/>',
39: 'link' => '<a href="%s"%s>%s</a>',
40: 'mailto' => '<a href="mailto:%s" %s>%s</a>',
41: 'form' => '<form %s>',
42: 'formend' => '</form>',
43: 'input' => '<input name="%s" %s/>',
44: 'textarea' => '<textarea name="%s" %s>%s</textarea>',
45: 'hidden' => '<input type="hidden" name="%s" %s/>',
46: 'checkbox' => '<input type="checkbox" name="%s" %s/>',
47: 'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
48: 'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
49: 'selectstart' => '<select name="%s"%s>',
50: 'selectmultiplestart' => '<select name="%s[]"%s>',
51: 'selectempty' => '<option value=""%s> </option>',
52: 'selectoption' => '<option value="%s"%s>%s</option>',
53: 'selectend' => '</select>',
54: 'optiongroup' => '<optgroup label="%s"%s>',
55: 'optiongroupend' => '</optgroup>',
56: 'checkboxmultiplestart' => '',
57: 'checkboxmultipleend' => '',
58: 'password' => '<input type="password" name="%s" %s/>',
59: 'file' => '<input type="file" name="%s" %s/>',
60: 'file_no_model' => '<input type="file" name="%s" %s/>',
61: 'submit' => '<input %s/>',
62: 'submitimage' => '<input type="image" src="%s" %s/>',
63: 'button' => '<button type="%s"%s>%s</button>',
64: 'image' => '<img src="%s" %s/>',
65: 'tableheader' => '<th%s>%s</th>',
66: 'tableheaderrow' => '<tr%s>%s</tr>',
67: 'tablecell' => '<td%s>%s</td>',
68: 'tablerow' => '<tr%s>%s</tr>',
69: 'block' => '<div%s>%s</div>',
70: 'blockstart' => '<div%s>',
71: 'blockend' => '</div>',
72: 'tag' => '<%s%s>%s</%s>',
73: 'tagstart' => '<%s%s>',
74: 'tagend' => '</%s>',
75: 'para' => '<p%s>%s</p>',
76: 'parastart' => '<p%s>',
77: 'label' => '<label for="%s"%s>%s</label>',
78: 'fieldset' => '<fieldset%s>%s</fieldset>',
79: 'fieldsetstart' => '<fieldset><legend>%s</legend>',
80: 'fieldsetend' => '</fieldset>',
81: 'legend' => '<legend>%s</legend>',
82: 'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
83: 'style' => '<style type="text/css"%s>%s</style>',
84: 'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
85: 'ul' => '<ul%s>%s</ul>',
86: 'ol' => '<ol%s>%s</ol>',
87: 'li' => '<li%s>%s</li>',
88: 'error' => '<div%s>%s</div>',
89: 'javascriptblock' => '<script type="text/javascript"%s>%s</script>',
90: 'javascriptstart' => '<script type="text/javascript">',
91: 'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
92: 'javascriptend' => '</script>'
93: );
94:
95: 96: 97: 98: 99: 100:
101: var $_crumbs = array();
102:
103: 104: 105: 106: 107: 108:
109: var $__includedScripts = array();
110: 111: 112: 113: 114: 115:
116: var $_scriptBlockOptions = array();
117: 118: 119: 120: 121: 122:
123: var $__docTypes = array(
124: 'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
125: 'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
126: 'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
127: 'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
128: 'xhtml-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
129: 'xhtml-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
130: 'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
131: );
132:
133: 134: 135: 136: 137: 138: 139: 140: 141: 142:
143: function addCrumb($name, $link = null, $options = null) {
144: $this->_crumbs[] = array($name, $link, $options);
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164:
165: function docType($type = 'xhtml-strict') {
166: if (isset($this->__docTypes[$type])) {
167: return $this->__docTypes[$type];
168: }
169: return null;
170: }
171:
172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186:
187: function meta($type, $url = null, $options = array()) {
188: $inline = isset($options['inline']) ? $options['inline'] : true;
189: unset($options['inline']);
190:
191: if (!is_array($type)) {
192: $types = array(
193: 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
194: 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
195: 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
196: 'keywords' => array('name' => 'keywords', 'content' => $url),
197: 'description' => array('name' => 'description', 'content' => $url),
198: );
199:
200: if ($type === 'icon' && $url === null) {
201: $types['icon']['link'] = $this->webroot('favicon.ico');
202: }
203:
204: if (isset($types[$type])) {
205: $type = $types[$type];
206: } elseif (!isset($options['type']) && $url !== null) {
207: if (is_array($url) && isset($url['ext'])) {
208: $type = $types[$url['ext']];
209: } else {
210: $type = $types['rss'];
211: }
212: } elseif (isset($options['type']) && isset($types[$options['type']])) {
213: $type = $types[$options['type']];
214: unset($options['type']);
215: } else {
216: $type = array();
217: }
218: } elseif ($url !== null) {
219: $inline = $url;
220: }
221: $options = array_merge($type, $options);
222: $out = null;
223:
224: if (isset($options['link'])) {
225: if (isset($options['rel']) && $options['rel'] === 'icon') {
226: $out = sprintf($this->tags['metalink'], $options['link'], $this->_parseAttributes($options, array('link'), ' ', ' '));
227: $options['rel'] = 'shortcut icon';
228: } else {
229: $options['link'] = $this->url($options['link'], true);
230: }
231: $out .= sprintf($this->tags['metalink'], $options['link'], $this->_parseAttributes($options, array('link'), ' ', ' '));
232: } else {
233: $out = sprintf($this->tags['meta'], $this->_parseAttributes($options, array('type'), ' ', ' '));
234: }
235:
236: if ($inline) {
237: return $out;
238: } else {
239: $view =& ClassRegistry::getObject('view');
240: $view->addScript($out);
241: }
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251: 252:
253: function charset($charset = null) {
254: if (empty($charset)) {
255: $charset = strtolower(Configure::read('App.encoding'));
256: }
257: return sprintf($this->tags['charset'], (!empty($charset) ? $charset : 'utf-8'));
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280:
281: function link($title, $url = null, $options = array(), $confirmMessage = false) {
282: $escapeTitle = true;
283: if ($url !== null) {
284: $url = $this->url($url);
285: } else {
286: $url = $this->url($title);
287: $title = $url;
288: $escapeTitle = false;
289: }
290:
291: if (isset($options['escape'])) {
292: $escapeTitle = $options['escape'];
293: }
294:
295: if ($escapeTitle === true) {
296: $title = h($title);
297: } elseif (is_string($escapeTitle)) {
298: $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
299: }
300:
301: if (!empty($options['confirm'])) {
302: $confirmMessage = $options['confirm'];
303: unset($options['confirm']);
304: }
305: if ($confirmMessage) {
306: $confirmMessage = str_replace("'", "\'", $confirmMessage);
307: $confirmMessage = str_replace('"', '\"', $confirmMessage);
308: $options['onclick'] = "return confirm('{$confirmMessage}');";
309: } elseif (isset($options['default']) && $options['default'] == false) {
310: if (isset($options['onclick'])) {
311: $options['onclick'] .= ' event.returnValue = false; return false;';
312: } else {
313: $options['onclick'] = 'event.returnValue = false; return false;';
314: }
315: unset($options['default']);
316: }
317: return sprintf($this->tags['link'], $url, $this->_parseAttributes($options), $title);
318: }
319:
320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335:
336: function css($path, $rel = null, $options = array()) {
337: $options += array('inline' => true);
338: if (is_array($path)) {
339: $out = '';
340: foreach ($path as $i) {
341: $out .= "\n\t" . $this->css($i, $rel, $options);
342: }
343: if ($options['inline']) {
344: return $out . "\n";
345: }
346: return;
347: }
348:
349: if (strpos($path, '//') !== false) {
350: $url = $path;
351: } else {
352: if ($path[0] !== '/') {
353: $path = CSS_URL . $path;
354: }
355:
356: if (strpos($path, '?') === false) {
357: if (substr($path, -4) !== '.css') {
358: $path .= '.css';
359: }
360: }
361: $url = $this->assetTimestamp($this->webroot($path));
362:
363: if (Configure::read('Asset.filter.css')) {
364: $pos = strpos($url, CSS_URL);
365: if ($pos !== false) {
366: $url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(CSS_URL));
367: }
368: }
369: }
370:
371: if ($rel == 'import') {
372: $out = sprintf($this->tags['style'], $this->_parseAttributes($options, array('inline'), '', ' '), '@import url(' . $url . ');');
373: } else {
374: if ($rel == null) {
375: $rel = 'stylesheet';
376: }
377: $out = sprintf($this->tags['css'], $rel, $url, $this->_parseAttributes($options, array('inline'), '', ' '));
378: }
379:
380: if ($options['inline']) {
381: return $out;
382: } else {
383: $view =& ClassRegistry::getObject('view');
384: $view->addScript($out);
385: }
386: }
387:
388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408:
409: function script($url, $options = array()) {
410: if (is_bool($options)) {
411: list($inline, $options) = array($options, array());
412: $options['inline'] = $inline;
413: }
414: $options = array_merge(array('inline' => true, 'once' => true), $options);
415: if (is_array($url)) {
416: $out = '';
417: foreach ($url as $i) {
418: $out .= "\n\t" . $this->script($i, $options);
419: }
420: if ($options['inline']) {
421: return $out . "\n";
422: }
423: return null;
424: }
425: if ($options['once'] && isset($this->__includedScripts[$url])) {
426: return null;
427: }
428: $this->__includedScripts[$url] = true;
429:
430: if (strpos($url, '//') === false) {
431: if ($url[0] !== '/') {
432: $url = JS_URL . $url;
433: }
434: if (strpos($url, '?') === false && substr($url, -3) !== '.js') {
435: $url .= '.js';
436: }
437: $url = $this->assetTimestamp($this->webroot($url));
438:
439: if (Configure::read('Asset.filter.js')) {
440: $url = str_replace(JS_URL, 'cjs/', $url);
441: }
442: }
443: $attributes = $this->_parseAttributes($options, array('inline', 'once'), ' ');
444: $out = sprintf($this->tags['javascriptlink'], $url, $attributes);
445:
446: if ($options['inline']) {
447: return $out;
448: } else {
449: $view =& ClassRegistry::getObject('view');
450: $view->addScript($out);
451: }
452: }
453:
454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467:
468: function scriptBlock($script, $options = array()) {
469: $options += array('safe' => true, 'inline' => true);
470: if ($options['safe']) {
471: $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
472: }
473: $inline = $options['inline'];
474: unset($options['inline'], $options['safe']);
475: $attributes = $this->_parseAttributes($options, ' ', ' ');
476: if ($inline) {
477: return sprintf($this->tags['javascriptblock'], $attributes, $script);
478: } else {
479: $view =& ClassRegistry::getObject('view');
480: $view->addScript(sprintf($this->tags['javascriptblock'], $attributes, $script));
481: return null;
482: }
483: }
484:
485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499:
500: function scriptStart($options = array()) {
501: $options += array('safe' => true, 'inline' => true);
502: $this->_scriptBlockOptions = $options;
503: ob_start();
504: return null;
505: }
506:
507: 508: 509: 510: 511: 512: 513: 514: 515:
516: function scriptEnd() {
517: $buffer = ob_get_clean();
518: $options = $this->_scriptBlockOptions;
519: $this->_scriptBlockOptions = array();
520: return $this->scriptBlock($buffer, $options);
521: }
522:
523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540:
541: function style($data, $oneline = true) {
542: if (!is_array($data)) {
543: return $data;
544: }
545: $out = array();
546: foreach ($data as $key=> $value) {
547: $out[] = $key.':'.$value.';';
548: }
549: if ($oneline) {
550: return join(' ', $out);
551: }
552: return implode("\n", $out);
553: }
554:
555: 556: 557: 558: 559: 560: 561: 562:
563: function getCrumbs($separator = '»', $startText = false) {
564: if (!empty($this->_crumbs)) {
565: $out = array();
566: if ($startText) {
567: $out[] = $this->link($startText, '/');
568: }
569:
570: foreach ($this->_crumbs as $crumb) {
571: if (!empty($crumb[1])) {
572: $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
573: } else {
574: $out[] = $crumb[0];
575: }
576: }
577: return join($separator, $out);
578: } else {
579: return null;
580: }
581: }
582:
583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603:
604: function image($path, $options = array()) {
605: if (is_array($path)) {
606: $path = $this->url($path);
607: } elseif (strpos($path, '://') === false) {
608: if ($path[0] !== '/') {
609: $path = IMAGES_URL . $path;
610: }
611: $path = $this->assetTimestamp($this->webroot($path));
612: }
613:
614: if (!isset($options['alt'])) {
615: $options['alt'] = '';
616: }
617:
618: $url = false;
619: if (!empty($options['url'])) {
620: $url = $options['url'];
621: unset($options['url']);
622: }
623:
624: $image = sprintf($this->tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
625:
626: if ($url) {
627: return sprintf($this->tags['link'], $this->url($url), null, $image);
628: }
629: return $image;
630: }
631:
632: 633: 634: 635: 636: 637: 638: 639: 640: 641:
642: function tableHeaders($names, $trOptions = null, $thOptions = null) {
643: $out = array();
644: foreach ($names as $arg) {
645: $out[] = sprintf($this->tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
646: }
647: return sprintf($this->tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
648: }
649:
650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662:
663: function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
664: if (empty($data[0]) || !is_array($data[0])) {
665: $data = array($data);
666: }
667:
668: if ($oddTrOptions === true) {
669: $useCount = true;
670: $oddTrOptions = null;
671: }
672:
673: if ($evenTrOptions === false) {
674: $continueOddEven = false;
675: $evenTrOptions = null;
676: }
677:
678: if ($continueOddEven) {
679: static $count = 0;
680: } else {
681: $count = 0;
682: }
683:
684: foreach ($data as $line) {
685: $count++;
686: $cellsOut = array();
687: $i = 0;
688: foreach ($line as $cell) {
689: $cellOptions = array();
690:
691: if (is_array($cell)) {
692: $cellOptions = $cell[1];
693: $cell = $cell[0];
694: } elseif ($useCount) {
695: $cellOptions['class'] = 'column-' . ++$i;
696: }
697: $cellsOut[] = sprintf($this->tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
698: }
699: $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
700: $out[] = sprintf($this->tags['tablerow'], $options, implode(' ', $cellsOut));
701: }
702: return implode("\n", $out);
703: }
704:
705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719:
720: function tag($name, $text = null, $options = array()) {
721: if (is_array($options) && isset($options['escape']) && $options['escape']) {
722: $text = h($text);
723: unset($options['escape']);
724: }
725: if (!is_array($options)) {
726: $options = array('class' => $options);
727: }
728: if ($text === null) {
729: $tag = 'tagstart';
730: } else {
731: $tag = 'tag';
732: }
733: return sprintf($this->tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
734: }
735:
736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750:
751: function div($class = null, $text = null, $options = array()) {
752: if (!empty($class)) {
753: $options['class'] = $class;
754: }
755: return $this->tag('div', $text, $options);
756: }
757:
758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771:
772: function para($class, $text, $options = array()) {
773: if (isset($options['escape'])) {
774: $text = h($text);
775: }
776: if ($class != null && !empty($class)) {
777: $options['class'] = $class;
778: }
779: if ($text === null) {
780: $tag = 'parastart';
781: } else {
782: $tag = 'para';
783: }
784: return sprintf($this->tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $text);
785: }
786:
787: 788: 789: 790: 791: 792: 793: 794: 795: 796:
797: function nestedList($list, $options = array(), $itemOptions = array(), $tag = 'ul') {
798: if (is_string($options)) {
799: $tag = $options;
800: $options = array();
801: }
802: $items = $this->__nestedListItem($list, $options, $itemOptions, $tag);
803: return sprintf($this->tags[$tag], $this->_parseAttributes($options, null, ' ', ''), $items);
804: }
805:
806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816:
817: function __nestedListItem($items, $options, $itemOptions, $tag) {
818: $out = '';
819:
820: $index = 1;
821: foreach ($items as $key => $item) {
822: if (is_array($item)) {
823: $item = $key . $this->nestedList($item, $options, $itemOptions, $tag);
824: }
825: if (isset($itemOptions['even']) && $index % 2 == 0) {
826: $itemOptions['class'] = $itemOptions['even'];
827: } else if (isset($itemOptions['odd']) && $index % 2 != 0) {
828: $itemOptions['class'] = $itemOptions['odd'];
829: }
830: $out .= sprintf($this->tags['li'], $this->_parseAttributes($itemOptions, array('even', 'odd'), ' ', ''), $item);
831: $index++;
832: }
833: return $out;
834: }
835: }
836: