PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/html.php

https://bitbucket.org/codeyash/bootstrap
PHP | 287 lines | 150 code | 28 blank | 109 comment | 13 complexity | 93fd54f8db3c2c99d5139be03e3ca755 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. // ------------------------------------------------------------------------
  14. /**
  15. * Html Class
  16. *
  17. * @package Fuel
  18. * @subpackage Core
  19. * @category Core
  20. * @author Alfredo Rivera
  21. * @link http://docs.fuelphp.com/classes/html.html
  22. */
  23. class Html
  24. {
  25. public static $doctypes = null;
  26. public static $html5 = false;
  27. /**
  28. * Creates an html link
  29. *
  30. * @param string the url
  31. * @param string the text value
  32. * @param array the attributes array
  33. * @param bool true to force https, false to force http
  34. * @return string the html link
  35. */
  36. public static function anchor($href, $text = null, $attr = array(), $secure = null)
  37. {
  38. if ( ! preg_match('#^(\w+://|javascript:|\#)# i', $href))
  39. {
  40. $urlparts = explode('?', $href, 2);
  41. $href = \Uri::create($urlparts[0], array(), isset($urlparts[1])?$urlparts[1]:array(), $secure);
  42. }
  43. elseif ( ! preg_match('#^(javascript:|\#)# i', $href) and is_bool($secure))
  44. {
  45. $href = http_build_url($href, array('scheme' => $secure ? 'https' : 'http'));
  46. }
  47. // Create and display a URL hyperlink
  48. is_null($text) and $text = $href;
  49. $attr['href'] = $href;
  50. return html_tag('a', $attr, $text);
  51. }
  52. /**
  53. * Creates an html image tag
  54. *
  55. * Sets the alt atribute to filename of it is not supplied.
  56. *
  57. * @param string the source
  58. * @param array the attributes array
  59. * @return string the image tag
  60. */
  61. public static function img($src, $attr = array())
  62. {
  63. if ( ! preg_match('#^(\w+://)# i', $src))
  64. {
  65. $src = \Uri::base(false).$src;
  66. }
  67. $attr['src'] = $src;
  68. $attr['alt'] = (isset($attr['alt'])) ? $attr['alt'] : pathinfo($src, PATHINFO_FILENAME);
  69. return html_tag('img', $attr);
  70. }
  71. /**
  72. * Adds the given schema to the given URL if it is not already there.
  73. *
  74. * @param string the url
  75. * @param string the schema
  76. * @return string url with schema
  77. */
  78. public static function prep_url($url, $schema = 'http')
  79. {
  80. if ( ! preg_match('#^(\w+://|javascript:)# i', $url))
  81. {
  82. $url = $schema.'://'.$url;
  83. }
  84. return $url;
  85. }
  86. /**
  87. * Creates a mailto link.
  88. *
  89. * @param string The email address
  90. * @param string The text value
  91. * @param string The subject
  92. * @return string The mailto link
  93. */
  94. public static function mail_to($email, $text = null, $subject = null, $attr = array())
  95. {
  96. $text or $text = $email;
  97. $subject and $subject = '?subject='.$subject;
  98. return html_tag('a', array(
  99. 'href' => 'mailto:'.$email.$subject,
  100. ) + $attr, $text);
  101. }
  102. /**
  103. * Creates a mailto link with Javascript to prevent bots from picking up the
  104. * email address.
  105. *
  106. * @param string the email address
  107. * @param string the text value
  108. * @param string the subject
  109. * @param array attributes for the tag
  110. * @return string the javascript code containg email
  111. */
  112. public static function mail_to_safe($email, $text = null, $subject = null, $attr = array())
  113. {
  114. $text or $text = str_replace('@', '[at]', $email);
  115. $email = explode("@", $email);
  116. $subject and $subject = '?subject='.$subject;
  117. $attr = array_to_attr($attr);
  118. $attr = ($attr == '' ? '' : ' ').$attr;
  119. $output = '<script type="text/javascript">';
  120. $output .= '(function() {';
  121. $output .= 'var user = "'.$email[0].'";';
  122. $output .= 'var at = "@";';
  123. $output .= 'var server = "'.$email[1].'";';
  124. $output .= "document.write('<a href=\"' + 'mail' + 'to:' + user + at + server + '$subject\"$attr>$text</a>');";
  125. $output .= '})();';
  126. $output .= '</script>';
  127. return $output;
  128. }
  129. /**
  130. * Generates a html meta tag
  131. *
  132. * @param string|array multiple inputs or name/http-equiv value
  133. * @param string content value
  134. * @param string name or http-equiv
  135. * @return string
  136. */
  137. public static function meta($name = '', $content = '', $type = 'name')
  138. {
  139. if( ! is_array($name))
  140. {
  141. $result = html_tag('meta', array($type => $name, 'content' => $content));
  142. }
  143. elseif(is_array($name))
  144. {
  145. $result = "";
  146. foreach($name as $array)
  147. {
  148. $meta = $array;
  149. $result .= "\n" . html_tag('meta', $meta);
  150. }
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Generates a html doctype tag
  156. *
  157. * @param string doctype declaration key from doctypes config
  158. * @return string
  159. */
  160. public static function doctype($type = 'xhtml1-trans')
  161. {
  162. if(static::$doctypes === null)
  163. {
  164. \Config::load('doctypes', true);
  165. static::$doctypes = \Config::get('doctypes', array());
  166. }
  167. if(is_array(static::$doctypes) and isset(static::$doctypes[$type]))
  168. {
  169. if($type == "html5")
  170. {
  171. static::$html5 = true;
  172. }
  173. return static::$doctypes[$type];
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. }
  180. /**
  181. * Generates a html5 audio tag
  182. * It is required that you set html5 as the doctype to use this method
  183. *
  184. * @param string|array one or multiple audio sources
  185. * @param array tag attributes
  186. * @return string
  187. */
  188. public static function audio($src = '', $attr = false)
  189. {
  190. if(static::$html5)
  191. {
  192. if(is_array($src))
  193. {
  194. $source = '';
  195. foreach($src as $item)
  196. {
  197. $source .= html_tag('source', array('src' => $item));
  198. }
  199. }
  200. else
  201. {
  202. $source = html_tag('source', array('src' => $src));
  203. }
  204. return html_tag('audio', $attr, $source);
  205. }
  206. }
  207. /**
  208. * Generates a html un-ordered list tag
  209. *
  210. * @param array list items, may be nested
  211. * @param array|string outer list attributes
  212. * @return string
  213. */
  214. public static function ul(array $list = array(), $attr = false)
  215. {
  216. return static::build_list('ul', $list, $attr);
  217. }
  218. /**
  219. * Generates a html ordered list tag
  220. *
  221. * @param array list items, may be nested
  222. * @param array|string outer list attributes
  223. * @return string
  224. */
  225. public static function ol(array $list = array(), $attr = false)
  226. {
  227. return static::build_list('ol', $list, $attr);
  228. }
  229. /**
  230. * Generates the html for the list methods
  231. *
  232. * @param string list type (ol or ul)
  233. * @param array list items, may be nested
  234. * @param array tag attributes
  235. * @param string indentation
  236. * @return string
  237. */
  238. protected static function build_list($type = 'ul', array $list = array(), $attr = false, $indent = '')
  239. {
  240. if ( ! is_array($list))
  241. {
  242. $result = false;
  243. }
  244. $out = '';
  245. foreach ($list as $key => $val)
  246. {
  247. if ( ! is_array($val))
  248. {
  249. $out .= $indent."\t".html_tag('li', false, $val).PHP_EOL;
  250. }
  251. else
  252. {
  253. $out .= $indent."\t".html_tag('li', false, $key.PHP_EOL.static::build_list($type, $val, '', $indent."\t\t").$indent."\t").PHP_EOL;
  254. }
  255. }
  256. $result = $indent.html_tag($type, $attr, PHP_EOL.$out.$indent).PHP_EOL;
  257. return $result;
  258. }
  259. }