blob: 92a6db47714125111b61e9c170d0c599142fcf73 [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
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('heading'))
41{
Timothy Warren01b129a2012-04-27 11:36:50 -040042 /**
43 * Heading
44 *
45 * Generates an HTML heading tag.
46 *
47 * @param string content
48 * @param int heading level
49 * @param string
50 * @return string
51 */
Greg Aker826429c2011-04-18 09:40:19 -050052 function heading($data = '', $h = '1', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +020054 return '<h'.$h.($attributes != '' ? ' ' : '').$attributes.'>'.$data.'</h'.$h.'>';
Derek Allard2067d1a2008-11-13 22:59:24 +000055 }
56}
57
58// ------------------------------------------------------------------------
59
Derek Allard2067d1a2008-11-13 22:59:24 +000060if ( ! function_exists('ul'))
61{
Timothy Warren01b129a2012-04-27 11:36:50 -040062 /**
63 * Unordered List
64 *
65 * Generates an HTML unordered list from an single or multi-dimensional array.
66 *
67 * @param array
68 * @param mixed
69 * @return string
70 */
Derek Allard2067d1a2008-11-13 22:59:24 +000071 function ul($list, $attributes = '')
72 {
73 return _list('ul', $list, $attributes);
74 }
75}
76
77// ------------------------------------------------------------------------
78
Derek Allard2067d1a2008-11-13 22:59:24 +000079if ( ! function_exists('ol'))
80{
Timothy Warren01b129a2012-04-27 11:36:50 -040081 /**
82 * Ordered List
83 *
84 * Generates an HTML ordered list from an single or multi-dimensional array.
85 *
86 * @param array
87 * @param mixed
88 * @return string
89 */
Derek Allard2067d1a2008-11-13 22:59:24 +000090 function ol($list, $attributes = '')
91 {
92 return _list('ol', $list, $attributes);
93 }
94}
95
96// ------------------------------------------------------------------------
97
Derek Allard2067d1a2008-11-13 22:59:24 +000098if ( ! function_exists('_list'))
99{
Timothy Warren01b129a2012-04-27 11:36:50 -0400100 /**
101 * Generates the list
102 *
103 * Generates an HTML ordered list from an single or multi-dimensional array.
104 *
105 * @param string
106 * @param mixed
107 * @param mixed
108 * @param int
109 * @return string
110 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 function _list($type = 'ul', $list, $attributes = '', $depth = 0)
112 {
113 // If an array wasn't submitted there's nothing to do...
114 if ( ! is_array($list))
115 {
116 return $list;
117 }
118
119 // Set the indentation based on the depth
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200120 $out = str_repeat(' ', $depth);
Derek Allard2067d1a2008-11-13 22:59:24 +0000121
Derek Jones4b9c6292011-07-01 17:40:48 -0500122 // Were any attributes submitted? If so generate a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 if (is_array($attributes))
124 {
125 $atts = '';
126 foreach ($attributes as $key => $val)
127 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300128 $atts .= ' '.$key.'="'.$val.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130 $attributes = $atts;
131 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300132 elseif (is_string($attributes) && strlen($attributes) > 0)
Eric Barnesb13a0dd2011-07-18 00:13:07 -0400133 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300134 $attributes = ' '.$attributes;
Eric Barnesb13a0dd2011-07-18 00:13:07 -0400135 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000136
137 // Write the opening list tag
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200138 $out .= '<'.$type.$attributes.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000139
Derek Jones4b9c6292011-07-01 17:40:48 -0500140 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // encountered we will recursively call _list()
142
143 static $_last_list_item = '';
144 foreach ($list as $key => $val)
145 {
146 $_last_list_item = $key;
147
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200148 $out .= str_repeat(' ', $depth + 2).'<li>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000149
150 if ( ! is_array($val))
151 {
152 $out .= $val;
153 }
154 else
155 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200156 $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 $out .= "</li>\n";
160 }
161
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200162 // Set the indentation for the closing tag and apply it
163 return $out.str_repeat(' ', $depth).'</'.$type.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165}
166
167// ------------------------------------------------------------------------
168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169if ( ! function_exists('br'))
170{
Timothy Warren01b129a2012-04-27 11:36:50 -0400171 /**
172 * Generates HTML BR tags based on number supplied
173 *
174 * @param int
175 * @return string
176 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 function br($num = 1)
178 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200179 return str_repeat('<br />', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181}
182
183// ------------------------------------------------------------------------
184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185if ( ! function_exists('img'))
186{
Timothy Warren01b129a2012-04-27 11:36:50 -0400187 /**
188 * Image
189 *
190 * Generates an <img /> element
191 *
192 * @param mixed
193 * @param bool
194 * @return string
195 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 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
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200211 foreach ($src as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300213 if ($k === 'src' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 $CI =& get_instance();
216
217 if ($index_page === TRUE)
218 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600219 $img .= ' src="'.$CI->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221 else
222 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600223 $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225 }
226 else
227 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300228 $img .= ' '.$k.'="'.$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230 }
231
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200232 return $img.'/>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234}
235
236// ------------------------------------------------------------------------
237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238if ( ! function_exists('doctype'))
239{
Timothy Warren01b129a2012-04-27 11:36:50 -0400240 /**
241 * Doctype
242 *
243 * Generates a page document type declaration
244 *
245 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
246 * html4-strict, html4-trans, and html4-frame. Values are saved in the
247 * doctypes config file.
248 *
249 * @param string type The doctype to be generated
250 * @return string
251 */
Derek Allard93bddd12009-04-14 19:27:38 +0000252 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 global $_doctypes;
255
256 if ( ! is_array($_doctypes))
257 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300258 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600259 {
260 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
261 }
262 elseif (is_file(APPPATH.'config/doctypes.php'))
263 {
264 include(APPPATH.'config/doctypes.php');
265 }
Eric Barnes92808342011-03-18 09:02:37 -0400266
bubbafoley0ea04142011-03-17 14:55:41 -0500267 if ( ! is_array($_doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 return FALSE;
270 }
271 }
272
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300273 return isset($_doctypes[$type]) ? $_doctypes[$type] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275}
276
277// ------------------------------------------------------------------------
278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279if ( ! function_exists('link_tag'))
280{
Timothy Warren01b129a2012-04-27 11:36:50 -0400281 /**
282 * Link
283 *
284 * Generates link to a CSS file
285 *
286 * @param mixed stylesheet hrefs or an array
287 * @param string rel
288 * @param string type
289 * @param string title
290 * @param string media
291 * @param bool should index_page be added to the css path
292 * @return string
293 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
295 {
296 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 $link = '<link ';
298
299 if (is_array($href))
300 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200301 foreach ($href as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300303 if ($k === 'href' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 if ($index_page === TRUE)
306 {
Derek Allard76763bb2009-09-16 11:25:20 +0000307 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 else
310 {
Derek Allard76763bb2009-09-16 11:25:20 +0000311 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313 }
314 else
315 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300316 $link .= $k.'="'.$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320 else
321 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200322 if (strpos($href, '://') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Derek Allard292dcd82009-09-16 11:26:32 +0000324 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326 elseif ($index_page === TRUE)
327 {
Derek Allard76763bb2009-09-16 11:25:20 +0000328 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330 else
331 {
Derek Allard76763bb2009-09-16 11:25:20 +0000332 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334
335 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
336
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200337 if ($media != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $link .= 'media="'.$media.'" ';
340 }
341
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200342 if ($title != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 $link .= 'title="'.$title.'" ';
345 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300348 return $link."/>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350}
351
352// ------------------------------------------------------------------------
353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354if ( ! function_exists('meta'))
355{
Timothy Warren01b129a2012-04-27 11:36:50 -0400356 /**
357 * Generates meta tags from an array of key/values
358 *
359 * @param array
360 * @param string
361 * @param string
362 * @param string
363 * @return string
364 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
366 {
367 // Since we allow the data to be passes as a string, a simple array
368 // or a multidimensional one, we need to do a little prepping.
369 if ( ! is_array($name))
370 {
371 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
372 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300373 elseif (isset($name['name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 // Turn single array into multidimensional
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300376 $name = array($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
379 $str = '';
380 foreach ($name as $meta)
381 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300382 $type = ( ! isset($meta['type']) OR $meta['type'] === 'name') ? 'name' : 'http-equiv';
383 $name = isset($meta['name']) ? $meta['name'] : '';
384 $content = isset($meta['content']) ? $meta['content'] : '';
385 $newline = isset($meta['newline']) ? $meta['newline'] : "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000386
387 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
388 }
389
390 return $str;
391 }
392}
393
394// ------------------------------------------------------------------------
395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396if ( ! function_exists('nbs'))
397{
Timothy Warren01b129a2012-04-27 11:36:50 -0400398 /**
399 * Generates non-breaking space entities based on number supplied
400 *
401 * @param int
402 * @return string
403 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 function nbs($num = 1)
405 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200406 return str_repeat('&nbsp;', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408}
409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410/* End of file html_helper.php */
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300411/* Location: ./system/helpers/html_helper.php */