/halogy/helpers/html_helper.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 416 lines · 215 code · 55 blank · 146 comment · 33 complexity · dbeee57daaf0a7ffa6dfb0629e3792aa 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 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, 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 intiger
  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. $img = '<img';
  177. foreach ($src as $k=>$v)
  178. {
  179. if ($k == 'src' AND strpos($v, '://') === FALSE)
  180. {
  181. $CI =& get_instance();
  182. if ($index_page === TRUE)
  183. {
  184. $img .= ' src="'.$CI->config->site_url($v).'" ';
  185. }
  186. else
  187. {
  188. $img .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';
  189. }
  190. }
  191. else
  192. {
  193. $img .= " $k=\"$v\" ";
  194. }
  195. }
  196. $img .= '/>';
  197. return $img;
  198. }
  199. }
  200. // ------------------------------------------------------------------------
  201. /**
  202. * Doctype
  203. *
  204. * Generates a page document type declaration
  205. *
  206. * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
  207. * html4-strict, html4-trans, and html4-frame. Values are saved in the
  208. * doctypes config file.
  209. *
  210. * @access public
  211. * @param string type The doctype to be generated
  212. * @return string
  213. */
  214. if ( ! function_exists('doctype'))
  215. {
  216. function doctype($type = 'xhtml1-strict')
  217. {
  218. global $_doctypes;
  219. if ( ! is_array($_doctypes))
  220. {
  221. if ( ! require_once(APPPATH.'config/doctypes.php'))
  222. {
  223. return FALSE;
  224. }
  225. }
  226. if (isset($_doctypes[$type]))
  227. {
  228. return $_doctypes[$type];
  229. }
  230. else
  231. {
  232. return FALSE;
  233. }
  234. }
  235. }
  236. // ------------------------------------------------------------------------
  237. /**
  238. * Link
  239. *
  240. * Generates link to a CSS file
  241. *
  242. * @access public
  243. * @param mixed stylesheet hrefs or an array
  244. * @param string rel
  245. * @param string type
  246. * @param string title
  247. * @param string media
  248. * @param boolean should index_page be added to the css path
  249. * @return string
  250. */
  251. if ( ! function_exists('link_tag'))
  252. {
  253. function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
  254. {
  255. $CI =& get_instance();
  256. $link = '<link ';
  257. if (is_array($href))
  258. {
  259. foreach ($href as $k=>$v)
  260. {
  261. if ($k == 'href' AND strpos($v, '://') === FALSE)
  262. {
  263. if ($index_page === TRUE)
  264. {
  265. $link .= ' href="'.$CI->config->site_url($v).'" ';
  266. }
  267. else
  268. {
  269. $link .= ' href="'.$CI->config->slash_item('base_url').$v.'" ';
  270. }
  271. }
  272. else
  273. {
  274. $link .= "$k=\"$v\" ";
  275. }
  276. }
  277. $link .= "/>";
  278. }
  279. else
  280. {
  281. if ( strpos($href, '://') !== FALSE)
  282. {
  283. $link .= ' href="'.$href.'" ';
  284. }
  285. elseif ($index_page === TRUE)
  286. {
  287. $link .= ' href="'.$CI->config->site_url($href).'" ';
  288. }
  289. else
  290. {
  291. $link .= ' href="'.$CI->config->slash_item('base_url').$href.'" ';
  292. }
  293. $link .= 'rel="'.$rel.'" type="'.$type.'" ';
  294. if ($media != '')
  295. {
  296. $link .= 'media="'.$media.'" ';
  297. }
  298. if ($title != '')
  299. {
  300. $link .= 'title="'.$title.'" ';
  301. }
  302. $link .= '/>';
  303. }
  304. return $link;
  305. }
  306. }
  307. // ------------------------------------------------------------------------
  308. /**
  309. * Generates meta tags from an array of key/values
  310. *
  311. * @access public
  312. * @param array
  313. * @return string
  314. */
  315. if ( ! function_exists('meta'))
  316. {
  317. function meta($name = '', $content = '', $type = 'name', $newline = "\n")
  318. {
  319. // Since we allow the data to be passes as a string, a simple array
  320. // or a multidimensional one, we need to do a little prepping.
  321. if ( ! is_array($name))
  322. {
  323. $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
  324. }
  325. else
  326. {
  327. // Turn single array into multidimensional
  328. if (isset($name['name']))
  329. {
  330. $name = array($name);
  331. }
  332. }
  333. $str = '';
  334. foreach ($name as $meta)
  335. {
  336. $type = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
  337. $name = ( ! isset($meta['name'])) ? '' : $meta['name'];
  338. $content = ( ! isset($meta['content'])) ? '' : $meta['content'];
  339. $newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
  340. $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
  341. }
  342. return $str;
  343. }
  344. }
  345. // ------------------------------------------------------------------------
  346. /**
  347. * Generates non-breaking space entities based on number supplied
  348. *
  349. * @access public
  350. * @param integer
  351. * @return string
  352. */
  353. if ( ! function_exists('nbs'))
  354. {
  355. function nbs($num = 1)
  356. {
  357. return str_repeat("&nbsp;", $num);
  358. }
  359. }
  360. /* End of file html_helper.php */
  361. /* Location: ./system/helpers/html_helper.php */