blob: 0417bd253d3298c9c4a72a4f0f4cc43276ef0b40 [file] [log] [blame]
Andrey Andreev1e6b72c2012-01-06 19:09:22 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1e6b72c2012-01-06 19:09:22 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1e6b72c2012-01-06 19:09:22 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter HTML Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/html_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
40/**
41 * Heading
42 *
Andrey Andreev2046b1a2012-03-26 21:07:04 +030043 * Generates an HTML heading tag.
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
Andrey Andreev2046b1a2012-03-26 21:07:04 +030045 * @param string content
46 * @param int heading level
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @return string
48 */
49if ( ! function_exists('heading'))
50{
Greg Aker826429c2011-04-18 09:40:19 -050051 function heading($data = '', $h = '1', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000052 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +020053 return '<h'.$h.($attributes != '' ? ' ' : '').$attributes.'>'.$data.'</h'.$h.'>';
Derek Allard2067d1a2008-11-13 22:59:24 +000054 }
55}
56
57// ------------------------------------------------------------------------
58
59/**
60 * Unordered List
61 *
62 * Generates an HTML unordered list from an single or multi-dimensional array.
63 *
Derek Allard2067d1a2008-11-13 22:59:24 +000064 * @param array
65 * @param mixed
66 * @return string
67 */
68if ( ! function_exists('ul'))
69{
70 function ul($list, $attributes = '')
71 {
72 return _list('ul', $list, $attributes);
73 }
74}
75
76// ------------------------------------------------------------------------
77
78/**
79 * Ordered List
80 *
81 * Generates an HTML ordered list from an single or multi-dimensional array.
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @param array
84 * @param mixed
85 * @return string
86 */
87if ( ! function_exists('ol'))
88{
89 function ol($list, $attributes = '')
90 {
91 return _list('ol', $list, $attributes);
92 }
93}
94
95// ------------------------------------------------------------------------
96
97/**
98 * Generates the list
99 *
100 * Generates an HTML ordered list from an single or multi-dimensional array.
101 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 * @param string
103 * @param mixed
104 * @param mixed
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300105 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return string
107 */
108if ( ! function_exists('_list'))
109{
110 function _list($type = 'ul', $list, $attributes = '', $depth = 0)
111 {
112 // If an array wasn't submitted there's nothing to do...
113 if ( ! is_array($list))
114 {
115 return $list;
116 }
117
118 // Set the indentation based on the depth
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200119 $out = str_repeat(' ', $depth);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120
Derek Jones4b9c6292011-07-01 17:40:48 -0500121 // Were any attributes submitted? If so generate a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 if (is_array($attributes))
123 {
124 $atts = '';
125 foreach ($attributes as $key => $val)
126 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300127 $atts .= ' '.$key.'="'.$val.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129 $attributes = $atts;
130 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300131 elseif (is_string($attributes) && strlen($attributes) > 0)
Eric Barnesb13a0dd2011-07-18 00:13:07 -0400132 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300133 $attributes = ' '.$attributes;
Eric Barnesb13a0dd2011-07-18 00:13:07 -0400134 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
136 // Write the opening list tag
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200137 $out .= '<'.$type.$attributes.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000138
Derek Jones4b9c6292011-07-01 17:40:48 -0500139 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // encountered we will recursively call _list()
141
142 static $_last_list_item = '';
143 foreach ($list as $key => $val)
144 {
145 $_last_list_item = $key;
146
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200147 $out .= str_repeat(' ', $depth + 2).'<li>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000148
149 if ( ! is_array($val))
150 {
151 $out .= $val;
152 }
153 else
154 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200155 $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 $out .= "</li>\n";
159 }
160
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200161 // Set the indentation for the closing tag and apply it
162 return $out.str_repeat(' ', $depth).'</'.$type.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164}
165
166// ------------------------------------------------------------------------
167
168/**
169 * Generates HTML BR tags based on number supplied
170 *
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300171 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @return string
173 */
174if ( ! function_exists('br'))
175{
176 function br($num = 1)
177 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200178 return str_repeat('<br />', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180}
181
182// ------------------------------------------------------------------------
183
184/**
185 * Image
186 *
187 * Generates an <img /> element
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param mixed
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300190 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @return string
192 */
193if ( ! function_exists('img'))
194{
195 function img($src = '', $index_page = FALSE)
196 {
197 if ( ! is_array($src) )
198 {
199 $src = array('src' => $src);
200 }
201
Derek Allarda0905f32010-07-05 08:11:33 -0400202 // If there is no alt attribute defined, set it to an empty string
203 if ( ! isset($src['alt']))
204 {
205 $src['alt'] = '';
206 }
207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 $img = '<img';
209
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200210 foreach ($src as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300212 if ($k === 'src' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $CI =& get_instance();
215
216 if ($index_page === TRUE)
217 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600218 $img .= ' src="'.$CI->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220 else
221 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600222 $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224 }
225 else
226 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300227 $img .= ' '.$k.'="'.$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229 }
230
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200231 return $img.'/>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233}
234
235// ------------------------------------------------------------------------
236
237/**
238 * Doctype
239 *
240 * Generates a page document type declaration
241 *
242 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300243 * html4-strict, html4-trans, and html4-frame. Values are saved in the
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * doctypes config file.
245 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @param string type The doctype to be generated
247 * @return string
248 */
249if ( ! function_exists('doctype'))
250{
Derek Allard93bddd12009-04-14 19:27:38 +0000251 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 global $_doctypes;
254
255 if ( ! is_array($_doctypes))
256 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300257 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600258 {
259 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
260 }
261 elseif (is_file(APPPATH.'config/doctypes.php'))
262 {
263 include(APPPATH.'config/doctypes.php');
264 }
Eric Barnes92808342011-03-18 09:02:37 -0400265
bubbafoley0ea04142011-03-17 14:55:41 -0500266 if ( ! is_array($_doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 return FALSE;
269 }
270 }
271
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300272 return isset($_doctypes[$type]) ? $_doctypes[$type] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274}
275
276// ------------------------------------------------------------------------
277
278/**
279 * Link
280 *
281 * Generates link to a CSS file
282 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @param mixed stylesheet hrefs or an array
284 * @param string rel
285 * @param string type
286 * @param string title
287 * @param string media
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300288 * @param bool should index_page be added to the css path
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @return string
290 */
291if ( ! function_exists('link_tag'))
292{
293 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
294 {
295 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $link = '<link ';
297
298 if (is_array($href))
299 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200300 foreach ($href as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300302 if ($k === 'href' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 if ($index_page === TRUE)
305 {
Derek Allard76763bb2009-09-16 11:25:20 +0000306 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 else
309 {
Derek Allard76763bb2009-09-16 11:25:20 +0000310 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 }
313 else
314 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300315 $link .= $k.'="'.$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319 else
320 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200321 if (strpos($href, '://') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Derek Allard292dcd82009-09-16 11:26:32 +0000323 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325 elseif ($index_page === TRUE)
326 {
Derek Allard76763bb2009-09-16 11:25:20 +0000327 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329 else
330 {
Derek Allard76763bb2009-09-16 11:25:20 +0000331 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333
334 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
335
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200336 if ($media != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $link .= 'media="'.$media.'" ';
339 }
340
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200341 if ($title != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 $link .= 'title="'.$title.'" ';
344 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300347 return $link."/>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349}
350
351// ------------------------------------------------------------------------
352
353/**
354 * Generates meta tags from an array of key/values
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param array
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300357 * @param string
358 * @param string
359 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @return string
361 */
362if ( ! function_exists('meta'))
363{
364 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
365 {
366 // Since we allow the data to be passes as a string, a simple array
367 // or a multidimensional one, we need to do a little prepping.
368 if ( ! is_array($name))
369 {
370 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
371 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300372 elseif (isset($name['name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 // Turn single array into multidimensional
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300375 $name = array($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377
378 $str = '';
379 foreach ($name as $meta)
380 {
Barry Mienydd671972010-10-04 16:33:58 +0200381 $type = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
382 $name = ( ! isset($meta['name'])) ? '' : $meta['name'];
383 $content = ( ! isset($meta['content'])) ? '' : $meta['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 $newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
385
386 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
387 }
388
389 return $str;
390 }
391}
392
393// ------------------------------------------------------------------------
394
395/**
396 * Generates non-breaking space entities based on number supplied
397 *
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300398 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @return string
400 */
401if ( ! function_exists('nbs'))
402{
403 function nbs($num = 1)
404 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200405 return str_repeat('&nbsp;', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407}
408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409/* End of file html_helper.php */
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300410/* Location: ./system/helpers/html_helper.php */