/system/helpers/html_helper.php

https://bitbucket.org/sarahman/mschool-project · PHP · 431 lines · 227 code · 57 blank · 147 comment · 35 complexity · ca35f30c66b4f6b5bb147ec81b67b1cb MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter HTML Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/helpers/html_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Heading
  28. *
  29. * Generates an HTML heading tag. First param is the data.
  30. * Second param is the size of the heading tag.
  31. *
  32. * @access public
  33. * @param string
  34. * @param integer
  35. * @return string
  36. */
  37. if ( ! function_exists('heading'))
  38. {
  39. function heading($data = '', $h = '1')
  40. {
  41. return "<h".$h.">".$data."</h".$h.">";
  42. }
  43. }
  44. // ------------------------------------------------------------------------
  45. /**
  46. * Unordered List
  47. *
  48. * Generates an HTML unordered list from an single or multi-dimensional array.
  49. *
  50. * @access public
  51. * @param array
  52. * @param mixed
  53. * @return string
  54. */
  55. if ( ! function_exists('ul'))
  56. {
  57. function ul($list, $attributes = '')
  58. {
  59. return _list('ul', $list, $attributes);
  60. }
  61. }
  62. // ------------------------------------------------------------------------
  63. /**
  64. * Ordered List
  65. *
  66. * Generates an HTML ordered list from an single or multi-dimensional array.
  67. *
  68. * @access public
  69. * @param array
  70. * @param mixed
  71. * @return string
  72. */
  73. if ( ! function_exists('ol'))
  74. {
  75. function ol($list, $attributes = '')
  76. {
  77. return _list('ol', $list, $attributes);
  78. }
  79. }
  80. // ------------------------------------------------------------------------
  81. /**
  82. * Generates the list
  83. *
  84. * Generates an HTML ordered list from an single or multi-dimensional array.
  85. *
  86. * @access private
  87. * @param string
  88. * @param mixed
  89. * @param mixed
  90. * @param integer
  91. * @return string
  92. */
  93. if ( ! function_exists('_list'))
  94. {
  95. function _list($type = 'ul', $list, $attributes = '', $depth = 0)
  96. {
  97. // If an array wasn't submitted there's nothing to do...
  98. if ( ! is_array($list))
  99. {
  100. return $list;
  101. }
  102. // Set the indentation based on the depth
  103. $out = str_repeat(" ", $depth);
  104. // Were any attributes submitted? If so generate a string
  105. if (is_array($attributes))
  106. {
  107. $atts = '';
  108. foreach ($attributes as $key => $val)
  109. {
  110. $atts .= ' ' . $key . '="' . $val . '"';
  111. }
  112. $attributes = $atts;
  113. }
  114. // Write the opening list tag
  115. $out .= "<".$type.$attributes.">\n";
  116. // Cycle through the list elements. If an array is
  117. // encountered we will recursively call _list()
  118. static $_last_list_item = '';
  119. foreach ($list as $key => $val)
  120. {
  121. $_last_list_item = $key;
  122. $out .= str_repeat(" ", $depth + 2);
  123. $out .= "<li>";
  124. if ( ! is_array($val))
  125. {
  126. $out .= $val;
  127. }
  128. else
  129. {
  130. $out .= $_last_list_item."\n";
  131. $out .= _list($type, $val, '', $depth + 4);
  132. $out .= str_repeat(" ", $depth + 2);
  133. }
  134. $out .= "</li>\n";
  135. }
  136. // Set the indentation for the closing tag
  137. $out .= str_repeat(" ", $depth);
  138. // Write the closing list tag
  139. $out .= "</".$type.">\n";
  140. return $out;
  141. }
  142. }
  143. // ------------------------------------------------------------------------
  144. /**
  145. * Generates HTML BR tags based on number supplied
  146. *
  147. * @access public
  148. * @param integer
  149. * @return string
  150. */
  151. if ( ! function_exists('br'))
  152. {
  153. function br($num = 1)
  154. {
  155. return str_repeat("<br />", $num);
  156. }
  157. }
  158. // ------------------------------------------------------------------------
  159. /**
  160. * Image
  161. *
  162. * Generates an <img /> element
  163. *
  164. * @access public
  165. * @param mixed
  166. * @return string
  167. */
  168. if ( ! function_exists('img'))
  169. {
  170. function img($src = '', $index_page = FALSE)
  171. {
  172. if ( ! is_array($src) )
  173. {
  174. $src = array('src' => $src);
  175. }
  176. // If there is no alt attribute defined, set it to an empty string
  177. if ( ! isset($src['alt']))
  178. {
  179. $src['alt'] = '';
  180. }
  181. $img = '<img';
  182. foreach ($src as $k=>$v)
  183. {
  184. if ($k == 'src' AND strpos($v, '://') === FALSE)
  185. {
  186. $CI =& get_instance();
  187. if ($index_page === TRUE)
  188. {
  189. $img .= ' src="'.$CI->config->site_url($v).'"';
  190. }
  191. else
  192. {
  193. $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
  194. }
  195. }
  196. else
  197. {
  198. $img .= " $k=\"$v\"";
  199. }
  200. }
  201. $img .= '/>';
  202. return $img;
  203. }
  204. }
  205. // ------------------------------------------------------------------------
  206. /**
  207. * Doctype
  208. *
  209. * Generates a page document type declaration
  210. *
  211. * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
  212. * html4-strict, html4-trans, and html4-frame. Values are saved in the
  213. * doctypes config file.
  214. *
  215. * @access public
  216. * @param string type The doctype to be generated
  217. * @return string
  218. */
  219. if ( ! function_exists('doctype'))
  220. {
  221. function doctype($type = 'xhtml1-strict')
  222. {
  223. global $_doctypes;
  224. if ( ! is_array($_doctypes))
  225. {
  226. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes'.EXT))
  227. {
  228. include(APPPATH.'config/'.ENVIRONMENT.'/doctypes'.EXT);
  229. }
  230. elseif (is_file(APPPATH.'config/doctypes'.EXT))
  231. {
  232. include(APPPATH.'config/doctypes'.EXT);
  233. }
  234. if ( ! is_array($_doctypes))
  235. {
  236. return FALSE;
  237. }
  238. }
  239. if (isset($_doctypes[$type]))
  240. {
  241. return $_doctypes[$type];
  242. }
  243. else
  244. {
  245. return FALSE;
  246. }
  247. }
  248. }
  249. // ------------------------------------------------------------------------
  250. /**
  251. * Link
  252. *
  253. * Generates link to a CSS file
  254. *
  255. * @access public
  256. * @param mixed stylesheet hrefs or an array
  257. * @param string rel
  258. * @param string type
  259. * @param string title
  260. * @param string media
  261. * @param boolean should index_page be added to the css path
  262. * @return string
  263. */
  264. if ( ! function_exists('link_tag'))
  265. {
  266. function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
  267. {
  268. $CI =& get_instance();
  269. $link = '<link ';
  270. if (is_array($href))
  271. {
  272. foreach ($href as $k=>$v)
  273. {
  274. if ($k == 'href' AND strpos($v, '://') === FALSE)
  275. {
  276. if ($index_page === TRUE)
  277. {
  278. $link .= 'href="'.$CI->config->site_url($v).'" ';
  279. }
  280. else
  281. {
  282. $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
  283. }
  284. }
  285. else
  286. {
  287. $link .= "$k=\"$v\" ";
  288. }
  289. }
  290. $link .= "/>";
  291. }
  292. else
  293. {
  294. if ( strpos($href, '://') !== FALSE)
  295. {
  296. $link .= 'href="'.$href.'" ';
  297. }
  298. elseif ($index_page === TRUE)
  299. {
  300. $link .= 'href="'.$CI->config->site_url($href).'" ';
  301. }
  302. else
  303. {
  304. $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
  305. }
  306. $link .= 'rel="'.$rel.'" type="'.$type.'" ';
  307. if ($media != '')
  308. {
  309. $link .= 'media="'.$media.'" ';
  310. }
  311. if ($title != '')
  312. {
  313. $link .= 'title="'.$title.'" ';
  314. }
  315. $link .= '/>';
  316. }
  317. return $link;
  318. }
  319. }
  320. // ------------------------------------------------------------------------
  321. /**
  322. * Generates meta tags from an array of key/values
  323. *
  324. * @access public
  325. * @param array
  326. * @return string
  327. */
  328. if ( ! function_exists('meta'))
  329. {
  330. function meta($name = '', $content = '', $type = 'name', $newline = "\n")
  331. {
  332. // Since we allow the data to be passes as a string, a simple array
  333. // or a multidimensional one, we need to do a little prepping.
  334. if ( ! is_array($name))
  335. {
  336. $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
  337. }
  338. else
  339. {
  340. // Turn single array into multidimensional
  341. if (isset($name['name']))
  342. {
  343. $name = array($name);
  344. }
  345. }
  346. $str = '';
  347. foreach ($name as $meta)
  348. {
  349. $type = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
  350. $name = ( ! isset($meta['name'])) ? '' : $meta['name'];
  351. $content = ( ! isset($meta['content'])) ? '' : $meta['content'];
  352. $newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
  353. $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
  354. }
  355. return $str;
  356. }
  357. }
  358. // ------------------------------------------------------------------------
  359. /**
  360. * Generates non-breaking space entities based on number supplied
  361. *
  362. * @access public
  363. * @param integer
  364. * @return string
  365. */
  366. if ( ! function_exists('nbs'))
  367. {
  368. function nbs($num = 1)
  369. {
  370. return str_repeat("&nbsp;", $num);
  371. }
  372. }
  373. /* End of file html_helper.php */
  374. /* Location: ./system/helpers/html_helper.php */