blob: b6bb402a877f420b5ccd94d84131e6c4bb2744c0 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter HTML Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/html_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Heading
44 *
Derek Jones4b9c6292011-07-01 17:40:48 -050045 * Generates an HTML heading tag. First param is the data.
Derek Allard2067d1a2008-11-13 22:59:24 +000046 * Second param is the size of the heading tag.
47 *
48 * @access public
49 * @param string
50 * @param integer
51 * @return string
52 */
53if ( ! function_exists('heading'))
54{
Greg Aker826429c2011-04-18 09:40:19 -050055 function heading($data = '', $h = '1', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
Greg Aker826429c2011-04-18 09:40:19 -050057 $attributes = ($attributes != '') ? ' '.$attributes : $attributes;
58 return "<h".$h.$attributes.">".$data."</h".$h.">";
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60}
61
62// ------------------------------------------------------------------------
63
64/**
65 * Unordered List
66 *
67 * Generates an HTML unordered list from an single or multi-dimensional array.
68 *
69 * @access public
70 * @param array
71 * @param mixed
72 * @return string
73 */
74if ( ! function_exists('ul'))
75{
76 function ul($list, $attributes = '')
77 {
78 return _list('ul', $list, $attributes);
79 }
80}
81
82// ------------------------------------------------------------------------
83
84/**
85 * Ordered List
86 *
87 * Generates an HTML ordered list from an single or multi-dimensional array.
88 *
89 * @access public
90 * @param array
91 * @param mixed
92 * @return string
93 */
94if ( ! function_exists('ol'))
95{
96 function ol($list, $attributes = '')
97 {
98 return _list('ol', $list, $attributes);
99 }
100}
101
102// ------------------------------------------------------------------------
103
104/**
105 * Generates the list
106 *
107 * Generates an HTML ordered list from an single or multi-dimensional array.
108 *
109 * @access private
110 * @param string
111 * @param mixed
112 * @param mixed
Derek Allard4e8d66a2010-03-15 12:23:27 -0400113 * @param integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return string
115 */
116if ( ! function_exists('_list'))
117{
118 function _list($type = 'ul', $list, $attributes = '', $depth = 0)
119 {
120 // If an array wasn't submitted there's nothing to do...
121 if ( ! is_array($list))
122 {
123 return $list;
124 }
125
126 // Set the indentation based on the depth
127 $out = str_repeat(" ", $depth);
128
Derek Jones4b9c6292011-07-01 17:40:48 -0500129 // Were any attributes submitted? If so generate a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if (is_array($attributes))
131 {
132 $atts = '';
133 foreach ($attributes as $key => $val)
134 {
135 $atts .= ' ' . $key . '="' . $val . '"';
136 }
137 $attributes = $atts;
138 }
Eric Barnesb13a0dd2011-07-18 00:13:07 -0400139 elseif (is_string($attributes) AND strlen($attributes) > 0)
140 {
141 $attributes = ' '. $attributes;
142 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000143
144 // Write the opening list tag
145 $out .= "<".$type.$attributes.">\n";
146
Derek Jones4b9c6292011-07-01 17:40:48 -0500147 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // encountered we will recursively call _list()
149
150 static $_last_list_item = '';
151 foreach ($list as $key => $val)
152 {
153 $_last_list_item = $key;
154
155 $out .= str_repeat(" ", $depth + 2);
156 $out .= "<li>";
157
158 if ( ! is_array($val))
159 {
160 $out .= $val;
161 }
162 else
163 {
164 $out .= $_last_list_item."\n";
165 $out .= _list($type, $val, '', $depth + 4);
166 $out .= str_repeat(" ", $depth + 2);
167 }
168
169 $out .= "</li>\n";
170 }
171
172 // Set the indentation for the closing tag
173 $out .= str_repeat(" ", $depth);
174
175 // Write the closing list tag
176 $out .= "</".$type.">\n";
177
178 return $out;
179 }
180}
181
182// ------------------------------------------------------------------------
183
184/**
185 * Generates HTML BR tags based on number supplied
186 *
187 * @access public
188 * @param integer
189 * @return string
190 */
191if ( ! function_exists('br'))
192{
193 function br($num = 1)
194 {
195 return str_repeat("<br />", $num);
196 }
197}
198
199// ------------------------------------------------------------------------
200
201/**
202 * Image
203 *
204 * Generates an <img /> element
205 *
206 * @access public
207 * @param mixed
208 * @return string
209 */
210if ( ! function_exists('img'))
211{
212 function img($src = '', $index_page = FALSE)
213 {
214 if ( ! is_array($src) )
215 {
216 $src = array('src' => $src);
217 }
218
Derek Allarda0905f32010-07-05 08:11:33 -0400219 // If there is no alt attribute defined, set it to an empty string
220 if ( ! isset($src['alt']))
221 {
222 $src['alt'] = '';
223 }
224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $img = '<img';
226
227 foreach ($src as $k=>$v)
228 {
229
230 if ($k == 'src' AND strpos($v, '://') === FALSE)
231 {
232 $CI =& get_instance();
233
234 if ($index_page === TRUE)
235 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600236 $img .= ' src="'.$CI->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238 else
239 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600240 $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242 }
243 else
244 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600245 $img .= " $k=\"$v\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247 }
248
249 $img .= '/>';
250
251 return $img;
252 }
253}
254
255// ------------------------------------------------------------------------
256
257/**
258 * Doctype
259 *
260 * Generates a page document type declaration
261 *
262 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
Derek Jones4b9c6292011-07-01 17:40:48 -0500263 * html4-strict, html4-trans, and html4-frame. Values are saved in the
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * doctypes config file.
265 *
266 * @access public
267 * @param string type The doctype to be generated
268 * @return string
269 */
270if ( ! function_exists('doctype'))
271{
Derek Allard93bddd12009-04-14 19:27:38 +0000272 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 global $_doctypes;
275
276 if ( ! is_array($_doctypes))
277 {
Greg Aker3a746652011-04-19 10:59:47 -0500278 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500279 {
Greg Aker3a746652011-04-19 10:59:47 -0500280 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500281 }
Greg Aker3a746652011-04-19 10:59:47 -0500282 elseif (is_file(APPPATH.'config/doctypes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500283 {
Greg Aker3a746652011-04-19 10:59:47 -0500284 include(APPPATH.'config/doctypes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500285 }
Eric Barnes92808342011-03-18 09:02:37 -0400286
bubbafoley0ea04142011-03-17 14:55:41 -0500287 if ( ! is_array($_doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 return FALSE;
290 }
291 }
292
293 if (isset($_doctypes[$type]))
294 {
295 return $_doctypes[$type];
296 }
297 else
298 {
299 return FALSE;
300 }
301 }
302}
303
304// ------------------------------------------------------------------------
305
306/**
307 * Link
308 *
309 * Generates link to a CSS file
310 *
311 * @access public
312 * @param mixed stylesheet hrefs or an array
313 * @param string rel
314 * @param string type
315 * @param string title
316 * @param string media
317 * @param boolean should index_page be added to the css path
318 * @return string
319 */
320if ( ! function_exists('link_tag'))
321{
322 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
323 {
324 $CI =& get_instance();
325
326 $link = '<link ';
327
328 if (is_array($href))
329 {
330 foreach ($href as $k=>$v)
331 {
332 if ($k == 'href' AND strpos($v, '://') === FALSE)
333 {
334 if ($index_page === TRUE)
335 {
Derek Allard76763bb2009-09-16 11:25:20 +0000336 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
338 else
339 {
Derek Allard76763bb2009-09-16 11:25:20 +0000340 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342 }
343 else
344 {
345 $link .= "$k=\"$v\" ";
346 }
347 }
348
349 $link .= "/>";
350 }
351 else
352 {
353 if ( strpos($href, '://') !== FALSE)
354 {
Derek Allard292dcd82009-09-16 11:26:32 +0000355 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357 elseif ($index_page === TRUE)
358 {
Derek Allard76763bb2009-09-16 11:25:20 +0000359 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361 else
362 {
Derek Allard76763bb2009-09-16 11:25:20 +0000363 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
367
368 if ($media != '')
369 {
370 $link .= 'media="'.$media.'" ';
371 }
372
373 if ($title != '')
374 {
375 $link .= 'title="'.$title.'" ';
376 }
377
378 $link .= '/>';
379 }
MrHyde365940a2011-10-05 13:17:26 +0300380 $link .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
382 return $link;
383 }
384}
385
386// ------------------------------------------------------------------------
387
388/**
389 * Generates meta tags from an array of key/values
390 *
391 * @access public
392 * @param array
393 * @return string
394 */
395if ( ! function_exists('meta'))
396{
397 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
398 {
399 // Since we allow the data to be passes as a string, a simple array
400 // or a multidimensional one, we need to do a little prepping.
401 if ( ! is_array($name))
402 {
403 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
404 }
405 else
406 {
407 // Turn single array into multidimensional
408 if (isset($name['name']))
409 {
410 $name = array($name);
411 }
412 }
413
414 $str = '';
415 foreach ($name as $meta)
416 {
Barry Mienydd671972010-10-04 16:33:58 +0200417 $type = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
418 $name = ( ! isset($meta['name'])) ? '' : $meta['name'];
419 $content = ( ! isset($meta['content'])) ? '' : $meta['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 $newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
421
422 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
423 }
424
425 return $str;
426 }
427}
428
429// ------------------------------------------------------------------------
430
431/**
432 * Generates non-breaking space entities based on number supplied
433 *
434 * @access public
435 * @param integer
436 * @return string
437 */
438if ( ! function_exists('nbs'))
439{
440 function nbs($num = 1)
441 {
442 return str_repeat("&nbsp;", $num);
443 }
444}
445
446
447/* End of file html_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000448/* Location: ./system/helpers/html_helper.php */