html_helper.php 8.7 KB

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