PageRenderTime 35ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/library/helpers/html.helper.php

https://github.com/SeanJA/ShoestringPHP
PHP | 214 lines | 128 code | 11 blank | 75 comment | 21 complexity | 1cec4364970735f2601b18e55161e24c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Generate a url (<a href="$url" $attributes>$title</a>)
  4. * @param str $url the url being linked to
  5. * @param str $title will appear between the <a>[$title]</a>
  6. * @param array $attributes other attributes that will be added to the element
  7. * @param boolean $echo Whether or not to echo out the string at the end, default true
  8. * @return if the string is not echoed, return the string
  9. */
  10. function href($url, $title, $attributes=array(), $echo=true) {
  11. $config = sConfig::getInstance();
  12. $attributeString = '';
  13. foreach($attributes as $attribute=>$value) {
  14. if(strtolower($attribute) != 'href'){
  15. $attributeString .= h($attribute,false).'="'.h($value,false).'" ';
  16. }
  17. }
  18. $linkTypes = array('https?:\/\/','mailto:','git:\/\/','git@','ftps?:\/\/');
  19. if(!preg_match('/^('.implode('|', $linkTypes).')/', $url)) {
  20. $url = $config->base_url.$config->index_file.$url;
  21. }
  22. $return = '<a '.$attributeString.'href="'.h($url,false).'">'.h($title,false).'</a>';
  23. if($echo){
  24. echo $return;
  25. } else {
  26. return $return;
  27. }
  28. }
  29. /**
  30. * Generate a url for an html link (<a href="mailto:$email" $attributes>$email</a>)
  31. * @param str $email the email being linked to
  32. * @param array $attributes other attributes that will be added to the element
  33. * @param boolean $echo Whether or not to echo out the string at the end, default true
  34. * @return if the string is not echoed, return the string
  35. */
  36. function email_link($email, $attributes=array(), $echo=true) {
  37. return href('mailto:'.$email, $email, $attributes, $echo);
  38. }
  39. /**
  40. * Generate the doctype tag
  41. * xhtml1_strict
  42. * xhtml1_trans
  43. * html4_strict
  44. * html4_trans
  45. * html5
  46. * @param str $type
  47. * @param boolean $echo Whether or not to echo out the string at the end, default true
  48. * @return if the string is not echoed, return the string
  49. */
  50. function docType($type = 'html4_trans', $echo=true) {
  51. $docType = '';
  52. switch ($type) {
  53. case 'xhtml1_strict':
  54. $docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  55. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
  56. break;
  57. case 'xhtml1_trans':
  58. $docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  59. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  60. break;
  61. case 'html4_strict':
  62. $docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  63. "http://www.w3.org/TR/html4/strict.dtd">';
  64. break;
  65. case 'html4_trans':
  66. $docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  67. "http://www.w3.org/TR/html4/loose.dtd">';
  68. break;
  69. case 'html5':
  70. $docType = '<!DOCTYPE html>';
  71. break;
  72. default:
  73. throw new Exception('Unknown Doctype "'.$type.'".');
  74. }
  75. if($echo){
  76. echo $docType;
  77. } else {
  78. return $docType;
  79. }
  80. }
  81. /**
  82. * Generate a meta tag
  83. * @param array $attributes
  84. * @param boolean $echo Whether or not to echo out the string at the end, default true
  85. * @return if the string is not echoed, return the string
  86. */
  87. function metaTag(array $attributes, $echo=true) {
  88. $attributeList = ' ';
  89. foreach($attributes as $attribute=>$value) {
  90. $attributeList .= h($attribute, false) .'="'.h($value, false).'" ';
  91. }
  92. $meta = '<meta'.$attributeList.'/>';
  93. if($echo){
  94. echo $meta;
  95. } else {
  96. return $meta;
  97. }
  98. }
  99. /**
  100. * Generate the charset meta tag
  101. * @param boolean $echo Whether or not to echo out the string at the end, default true
  102. * @return if the string is not echoed, return the string
  103. */
  104. function charset($echo=true) {
  105. $config = sConfig::getInstance();
  106. $attributes = array(
  107. 'http-equiv'=>'Content-Type',
  108. 'content'=>'text/html; charset='.$config->char_encoding,
  109. );
  110. return metaTag($attributes, $echo);
  111. }
  112. /**
  113. * Generate the css include string
  114. * @param str $file The css file that you are linking to
  115. * @param array $media The media types that you want to use this css file for
  116. * @param boolean $echo Whether or not to echo out the string at the end, default true
  117. * @return if the string is not echoed, return the string
  118. */
  119. function css($file, array $media=array('all'), $echo) {
  120. if(preg_match('/^(https?:\/\//)', $file)) {
  121. $str = '<link rel="stylesheet" type="text/css" href="'.h($file, false).'"';
  122. } else {
  123. $config = sConfig::getInstance();
  124. $str = '<link rel="stylesheet" type="text/css" href="'.h($config->base_url.'public/css/'.$file, false).'"';
  125. }
  126. $str .= ' media = "'.h(implode(',', $media), false).'" />';
  127. if($echo){
  128. echo $str;
  129. } else {
  130. return $str;
  131. }
  132. }
  133. /**
  134. * Generate the js include string
  135. * @param string $file the js file being linked to
  136. * @param boolean $echo Whether or not to echo out the string at the end, default true
  137. * @return if the string is not echoed, return the string
  138. */
  139. function js($file) {
  140. if(preg_match('/^(https?:\/\//)', $file)) {
  141. $str = '<script type="text/javascript" src="'.h($file, false).'" ></script>';
  142. } else {
  143. $config = sConfig::getInstance();
  144. $str = '<script type="text/javascript" src="'.h($config->base_url."public/js/$file", false).'" ></script>';
  145. }
  146. if($echo){
  147. echo $str;
  148. } else {
  149. return $str;
  150. }
  151. }
  152. /**
  153. * Short code to escape a variable for html
  154. * @param string $var the string being escaped
  155. * @param boolean $echo Whether or not to print to the screen (default true)
  156. */
  157. function h($var, $echo=true){
  158. $config = sConfig::getInstance();
  159. if($echo){
  160. echo sEscape::html($var);
  161. } else {
  162. return sEscape::html($var);
  163. }
  164. }
  165. /**
  166. * Add an image to the page
  167. * @param string $image
  168. * @param string $alt
  169. * @param array $attributes
  170. */
  171. function image($image, $alt, $attributes=array()){
  172. $config = sConfig::getInstance();
  173. $attributes['alt']= $alt;
  174. $attributeString = '';
  175. foreach($attributes as $key=>$value){
  176. $attributeString .= ' '.h($key, false) .'="'.h($value, false).'" ';
  177. }
  178. echo '<img '.$attributeString.' src="'.$config->base_url.'/public/images/'.$image.'" />';
  179. }
  180. /**
  181. * Add a twitter link to the page
  182. * @param string $image
  183. * @param string $alt
  184. * @param array $attributes
  185. */
  186. function twitter_link($account, $attributes=array(), $echo = true){
  187. return href('http://twitter.com/'.$account, '@'.$account, $attributes);
  188. }
  189. /**
  190. * Process the text and return it marked down
  191. * @staticvar parser_class $parser
  192. * @param string $text
  193. * @return string
  194. */
  195. function markdown($text) {
  196. // Setup static parser variable.
  197. static $parser;
  198. if (!isset($parser)) {
  199. $parser = new Markdown();
  200. }
  201. //Transform text using parser.
  202. echo $parser->transform($text);
  203. }