/recess/recess/framework/helpers/Html.class.php

https://github.com/wangmxf/recess · PHP · 192 lines · 93 code · 24 blank · 75 comment · 17 complexity · 44cbe8b374d9575cdc0764fdc6b6856b MD5 · raw file

  1. <?php
  2. Library::import('recess.framework.AbstractHelper');
  3. Library::import('recess.framework.helpers.Url');
  4. /**
  5. * HTML helper class.
  6. * @author Joshua Paine
  7. * @author Kris Jordan
  8. * @todo Add protocol parameters back in to anchor and url::..
  9. *
  10. * Based upon Kohana's HTML helper:
  11. * @author Kohana Team
  12. * @copyright (c) 2007-2009 Kohana Team
  13. * @license http://kohanaphp.com/license
  14. */
  15. class Html extends AbstractHelper {
  16. /**
  17. * Convert special characters to HTML entities
  18. *
  19. * @param string string to convert
  20. * @param boolean encode existing entities
  21. * @return string
  22. */
  23. public static function specialchars($str, $double_encode = true) {
  24. // Force the string to be a string
  25. $str = (string) $str;
  26. // Do encode existing HTML entities (default)
  27. if ($double_encode === true) {
  28. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  29. } else {
  30. // Do not encode existing HTML entities
  31. // From PHP 5.2.3 this functionality is built-in, otherwise use a regex
  32. if (version_compare(PHP_VERSION, '5.2.3', '>=')) {
  33. $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', false);
  34. } else {
  35. $str = preg_replace('/&(?!(?:#\d++|[a-z]++);)/ui', '&amp;', $str);
  36. $str = str_replace(array('<', '>', '\'', '"'), array('&lt;', '&gt;', '&#39;', '&quot;'), $str);
  37. }
  38. }
  39. return $str;
  40. }
  41. /**
  42. * Create HTML link anchors.
  43. *
  44. * @param string URL or URI string
  45. * @param string link text
  46. * @param array HTML anchor attributes
  47. * @return string
  48. */
  49. public static function anchor($uri, $title = NULL, $attributes = NULL) {
  50. if ($uri === '') {
  51. $siteUrl = url::base();
  52. } else {
  53. $siteUrl = $uri;
  54. }
  55. return
  56. // Parsed URL
  57. '<a href="'.html::specialchars($siteUrl, false).'"'
  58. // Attributes empty? Use an empty string
  59. .(is_array($attributes) ? html::attributes($attributes) : '').'>'
  60. // Title empty? Use the parsed URL
  61. .(($title === NULL) ? $siteUrl : $title).'</a>';
  62. }
  63. /**
  64. * Creates a stylesheet link.
  65. *
  66. * @param string|array filename, or array of filenames to match to array of medias
  67. * @param string|array media type of stylesheet, or array to match filenames
  68. * @return string
  69. */
  70. public static function css($style, $media = FALSE) {
  71. if(is_array($media)) {
  72. $media = implode(', ', $media);
  73. }
  74. return html::link($style, 'stylesheet', 'text/css', '.css', $media);
  75. }
  76. /**
  77. * Creates a link tag.
  78. *
  79. * @param string|array filename
  80. * @param string|array relationship
  81. * @param string|array mimetype
  82. * @param string specifies suffix of the file
  83. * @param string|array specifies on what device the document will be displayed
  84. * @return string
  85. */
  86. public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE) {
  87. $compiled = '';
  88. if (is_array($href)) {
  89. foreach ($href as $_href) {
  90. $_rel = is_array($rel) ? array_shift($rel) : $rel;
  91. $_type = is_array($type) ? array_shift($type) : $type;
  92. $_media = is_array($media) ? array_shift($media) : $media;
  93. $compiled .= html::link($_href, $_rel, $_type, $suffix, $_media);
  94. }
  95. } else {
  96. // Add the suffix only when it's not already present
  97. $suffix = ( ! empty($suffix) AND strpos($href, $suffix) === FALSE) ? $suffix : '';
  98. $media = empty($media) ? '' : ' media="'.$media.'"';
  99. $compiled = '<link rel="'.$rel.'" type="'.$type.'" href="'.url::asset(($type=="text/css" ? 'css/' : '').$href.$suffix).'"'.$media.' />';
  100. }
  101. return $compiled."\n";
  102. }
  103. /**
  104. * Creates a script link.
  105. *
  106. * @param string|array filename
  107. * @return string
  108. */
  109. public static function js($script) {
  110. $compiled = '';
  111. if (is_array($script)) {
  112. foreach ($script as $name) {
  113. $compiled .= html::js($name);
  114. }
  115. } else {
  116. // Do not touch full URLs
  117. if (strpos($script, '://') === FALSE) {
  118. // Add the suffix only when it's not already present
  119. $suffix = (substr($script, -3) !== '.js') ? '.js' : '';
  120. $script = url::asset('js/'.$script.$suffix);
  121. }
  122. $compiled = '<script type="text/javascript" src="'.$script.'"></script>';
  123. }
  124. return $compiled."\n";
  125. }
  126. /**
  127. * Creates a image link.
  128. *
  129. * @param string image source, or an array of attributes
  130. * @param string|array image alt attribute, or an array of attributes
  131. * @return string
  132. */
  133. public static function img($src = NULL, $alt = NULL) {
  134. // Create attribute list
  135. $attributes = is_array($src) ? $src : array('src' => $src);
  136. if (is_array($alt)) {
  137. $attributes += $alt;
  138. } elseif ( ! empty($alt)) {
  139. // Add alt to attributes
  140. $attributes['alt'] = $alt;
  141. }
  142. if(!isset($attributes['alt'])) $attributes['alt'] = '';
  143. if (strpos($attributes['src'], '://') === FALSE) {
  144. // Make the src attribute into an absolute URL
  145. $attributes['src'] = url::asset('img/'.$attributes['src']);
  146. }
  147. return '<img'.html::attributes($attributes).'>';
  148. }
  149. /**
  150. * Compiles an array of HTML attributes into an attribute string.
  151. *
  152. * @param string|array array of attributes
  153. * @return string
  154. */
  155. public static function attributes($attrs) {
  156. if (empty($attrs))
  157. return '';
  158. if (is_string($attrs))
  159. return ' '.$attrs;
  160. $compiled = '';
  161. foreach ($attrs as $key => $val) {
  162. $compiled .= ' '.$key.'="'.$val.'"';
  163. }
  164. return $compiled;
  165. }
  166. } // End html
  167. function h($var,$encode_entities=true){ return html::specialchars($var,$encode_entities); }
  168. ?>