blob: 080f622dd428e2756a3d8a2987e69b42d472fbad [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter HTML Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/html_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Heading
32 *
33 * Generates an HTML heading tag. First param is the data.
34 * Second param is the size of the heading tag.
35 *
36 * @access public
37 * @param string
38 * @param integer
39 * @return string
40 */
41if ( ! function_exists('heading'))
42{
Greg Aker826429c2011-04-18 09:40:19 -050043 function heading($data = '', $h = '1', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
Greg Aker826429c2011-04-18 09:40:19 -050045 $attributes = ($attributes != '') ? ' '.$attributes : $attributes;
46 return "<h".$h.$attributes.">".$data."</h".$h.">";
Derek Allard2067d1a2008-11-13 22:59:24 +000047 }
48}
49
50// ------------------------------------------------------------------------
51
52/**
53 * Unordered List
54 *
55 * Generates an HTML unordered list from an single or multi-dimensional array.
56 *
57 * @access public
58 * @param array
59 * @param mixed
60 * @return string
61 */
62if ( ! function_exists('ul'))
63{
64 function ul($list, $attributes = '')
65 {
66 return _list('ul', $list, $attributes);
67 }
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Ordered List
74 *
75 * Generates an HTML ordered list from an single or multi-dimensional array.
76 *
77 * @access public
78 * @param array
79 * @param mixed
80 * @return string
81 */
82if ( ! function_exists('ol'))
83{
84 function ol($list, $attributes = '')
85 {
86 return _list('ol', $list, $attributes);
87 }
88}
89
90// ------------------------------------------------------------------------
91
92/**
93 * Generates the list
94 *
95 * Generates an HTML ordered list from an single or multi-dimensional array.
96 *
97 * @access private
98 * @param string
99 * @param mixed
100 * @param mixed
Derek Allard4e8d66a2010-03-15 12:23:27 -0400101 * @param integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 * @return string
103 */
104if ( ! function_exists('_list'))
105{
106 function _list($type = 'ul', $list, $attributes = '', $depth = 0)
107 {
108 // If an array wasn't submitted there's nothing to do...
109 if ( ! is_array($list))
110 {
111 return $list;
112 }
113
114 // Set the indentation based on the depth
115 $out = str_repeat(" ", $depth);
116
117 // Were any attributes submitted? If so generate a string
118 if (is_array($attributes))
119 {
120 $atts = '';
121 foreach ($attributes as $key => $val)
122 {
123 $atts .= ' ' . $key . '="' . $val . '"';
124 }
125 $attributes = $atts;
126 }
127
128 // Write the opening list tag
129 $out .= "<".$type.$attributes.">\n";
130
131 // Cycle through the list elements. If an array is
132 // encountered we will recursively call _list()
133
134 static $_last_list_item = '';
135 foreach ($list as $key => $val)
136 {
137 $_last_list_item = $key;
138
139 $out .= str_repeat(" ", $depth + 2);
140 $out .= "<li>";
141
142 if ( ! is_array($val))
143 {
144 $out .= $val;
145 }
146 else
147 {
148 $out .= $_last_list_item."\n";
149 $out .= _list($type, $val, '', $depth + 4);
150 $out .= str_repeat(" ", $depth + 2);
151 }
152
153 $out .= "</li>\n";
154 }
155
156 // Set the indentation for the closing tag
157 $out .= str_repeat(" ", $depth);
158
159 // Write the closing list tag
160 $out .= "</".$type.">\n";
161
162 return $out;
163 }
164}
165
166// ------------------------------------------------------------------------
167
168/**
169 * Generates HTML BR tags based on number supplied
170 *
171 * @access public
172 * @param integer
173 * @return string
174 */
175if ( ! function_exists('br'))
176{
177 function br($num = 1)
178 {
179 return str_repeat("<br />", $num);
180 }
181}
182
183// ------------------------------------------------------------------------
184
185/**
186 * Image
187 *
188 * Generates an <img /> element
189 *
190 * @access public
191 * @param mixed
192 * @return string
193 */
194if ( ! function_exists('img'))
195{
196 function img($src = '', $index_page = FALSE)
197 {
198 if ( ! is_array($src) )
199 {
200 $src = array('src' => $src);
201 }
202
Derek Allarda0905f32010-07-05 08:11:33 -0400203 // If there is no alt attribute defined, set it to an empty string
204 if ( ! isset($src['alt']))
205 {
206 $src['alt'] = '';
207 }
208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 $img = '<img';
210
211 foreach ($src as $k=>$v)
212 {
213
214 if ($k == 'src' AND strpos($v, '://') === FALSE)
215 {
216 $CI =& get_instance();
217
218 if ($index_page === TRUE)
219 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600220 $img .= ' src="'.$CI->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222 else
223 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600224 $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226 }
227 else
228 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600229 $img .= " $k=\"$v\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231 }
232
233 $img .= '/>';
234
235 return $img;
236 }
237}
238
239// ------------------------------------------------------------------------
240
241/**
242 * Doctype
243 *
244 * Generates a page document type declaration
245 *
246 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
247 * html4-strict, html4-trans, and html4-frame. Values are saved in the
248 * doctypes config file.
249 *
250 * @access public
251 * @param string type The doctype to be generated
252 * @return string
253 */
254if ( ! function_exists('doctype'))
255{
Derek Allard93bddd12009-04-14 19:27:38 +0000256 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 global $_doctypes;
259
260 if ( ! is_array($_doctypes))
261 {
Greg Aker3a746652011-04-19 10:59:47 -0500262 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500263 {
Greg Aker3a746652011-04-19 10:59:47 -0500264 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500265 }
Greg Aker3a746652011-04-19 10:59:47 -0500266 elseif (is_file(APPPATH.'config/doctypes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500267 {
Greg Aker3a746652011-04-19 10:59:47 -0500268 include(APPPATH.'config/doctypes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500269 }
Eric Barnes92808342011-03-18 09:02:37 -0400270
bubbafoley0ea04142011-03-17 14:55:41 -0500271 if ( ! is_array($_doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 return FALSE;
274 }
275 }
276
277 if (isset($_doctypes[$type]))
278 {
279 return $_doctypes[$type];
280 }
281 else
282 {
283 return FALSE;
284 }
285 }
286}
287
288// ------------------------------------------------------------------------
289
290/**
291 * Link
292 *
293 * Generates link to a CSS file
294 *
295 * @access public
296 * @param mixed stylesheet hrefs or an array
297 * @param string rel
298 * @param string type
299 * @param string title
300 * @param string media
301 * @param boolean should index_page be added to the css path
302 * @return string
303 */
304if ( ! function_exists('link_tag'))
305{
306 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
307 {
308 $CI =& get_instance();
309
310 $link = '<link ';
311
312 if (is_array($href))
313 {
314 foreach ($href as $k=>$v)
315 {
316 if ($k == 'href' AND strpos($v, '://') === FALSE)
317 {
318 if ($index_page === TRUE)
319 {
Derek Allard76763bb2009-09-16 11:25:20 +0000320 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322 else
323 {
Derek Allard76763bb2009-09-16 11:25:20 +0000324 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326 }
327 else
328 {
329 $link .= "$k=\"$v\" ";
330 }
331 }
332
333 $link .= "/>";
334 }
335 else
336 {
337 if ( strpos($href, '://') !== FALSE)
338 {
Derek Allard292dcd82009-09-16 11:26:32 +0000339 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341 elseif ($index_page === TRUE)
342 {
Derek Allard76763bb2009-09-16 11:25:20 +0000343 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345 else
346 {
Derek Allard76763bb2009-09-16 11:25:20 +0000347 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349
350 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
351
352 if ($media != '')
353 {
354 $link .= 'media="'.$media.'" ';
355 }
356
357 if ($title != '')
358 {
359 $link .= 'title="'.$title.'" ';
360 }
361
362 $link .= '/>';
363 }
364
365
366 return $link;
367 }
368}
369
370// ------------------------------------------------------------------------
371
372/**
373 * Generates meta tags from an array of key/values
374 *
375 * @access public
376 * @param array
377 * @return string
378 */
379if ( ! function_exists('meta'))
380{
381 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
382 {
383 // Since we allow the data to be passes as a string, a simple array
384 // or a multidimensional one, we need to do a little prepping.
385 if ( ! is_array($name))
386 {
387 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
388 }
389 else
390 {
391 // Turn single array into multidimensional
392 if (isset($name['name']))
393 {
394 $name = array($name);
395 }
396 }
397
398 $str = '';
399 foreach ($name as $meta)
400 {
Barry Mienydd671972010-10-04 16:33:58 +0200401 $type = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
402 $name = ( ! isset($meta['name'])) ? '' : $meta['name'];
403 $content = ( ! isset($meta['content'])) ? '' : $meta['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 $newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
405
406 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
407 }
408
409 return $str;
410 }
411}
412
413// ------------------------------------------------------------------------
414
415/**
416 * Generates non-breaking space entities based on number supplied
417 *
418 * @access public
419 * @param integer
420 * @return string
421 */
422if ( ! function_exists('nbs'))
423{
424 function nbs($num = 1)
425 {
426 return str_repeat("&nbsp;", $num);
427 }
428}
429
430
431/* End of file html_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000432/* Location: ./system/helpers/html_helper.php */