blob: 2372e8174e51a55ced2471f225eb6223d36a59d8 [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 {
Eric Barnesacedd2b2012-07-29 00:15:40 -040054 return '<h'.$h._stringify_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 Allard2067d1a2008-11-13 22:59:24 +0000122 // Write the opening list tag
Eric Barnesacedd2b2012-07-29 00:15:40 -0400123 $out .= '<'.$type._stringify_attributes($attributes).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000124
Derek Jones4b9c6292011-07-01 17:40:48 -0500125 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // encountered we will recursively call _list()
127
128 static $_last_list_item = '';
129 foreach ($list as $key => $val)
130 {
131 $_last_list_item = $key;
132
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200133 $out .= str_repeat(' ', $depth + 2).'<li>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000134
135 if ( ! is_array($val))
136 {
137 $out .= $val;
138 }
139 else
140 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200141 $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
144 $out .= "</li>\n";
145 }
146
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200147 // Set the indentation for the closing tag and apply it
148 return $out.str_repeat(' ', $depth).'</'.$type.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150}
151
152// ------------------------------------------------------------------------
153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154if ( ! function_exists('br'))
155{
Timothy Warren01b129a2012-04-27 11:36:50 -0400156 /**
157 * Generates HTML BR tags based on number supplied
158 *
159 * @param int
160 * @return string
161 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 function br($num = 1)
163 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200164 return str_repeat('<br />', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166}
167
168// ------------------------------------------------------------------------
169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170if ( ! function_exists('img'))
171{
Timothy Warren01b129a2012-04-27 11:36:50 -0400172 /**
173 * Image
174 *
175 * Generates an <img /> element
176 *
177 * @param mixed
178 * @param bool
Eric Barnes62ab8b22012-07-28 14:57:04 -0400179 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400180 * @return string
181 */
Eric Barnes62ab8b22012-07-28 14:57:04 -0400182 function img($src = '', $index_page = FALSE, $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 if ( ! is_array($src) )
185 {
186 $src = array('src' => $src);
187 }
188
Derek Allarda0905f32010-07-05 08:11:33 -0400189 // If there is no alt attribute defined, set it to an empty string
190 if ( ! isset($src['alt']))
191 {
192 $src['alt'] = '';
193 }
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 $img = '<img';
196
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200197 foreach ($src as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300199 if ($k === 'src' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 $CI =& get_instance();
202
203 if ($index_page === TRUE)
204 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600205 $img .= ' src="'.$CI->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207 else
208 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600209 $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211 }
212 else
213 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300214 $img .= ' '.$k.'="'.$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216 }
217
Andrey Andreev6a64f852012-08-15 11:16:47 +0300218 return $img._stringify_attributes($attributes).' />';
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220}
221
222// ------------------------------------------------------------------------
223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224if ( ! function_exists('doctype'))
225{
Timothy Warren01b129a2012-04-27 11:36:50 -0400226 /**
227 * Doctype
228 *
229 * Generates a page document type declaration
230 *
Przemyslaw Tomaszewski8711aeb2012-07-22 03:28:41 +0200231 * Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
232 * xhtml-frame, html4-strict, html4-trans, and html4-frame.
233 * All values are saved in the doctypes config file.
Timothy Warren01b129a2012-04-27 11:36:50 -0400234 *
235 * @param string type The doctype to be generated
236 * @return string
237 */
Derek Allard93bddd12009-04-14 19:27:38 +0000238 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 global $_doctypes;
241
242 if ( ! is_array($_doctypes))
243 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300244 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600245 {
246 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
247 }
248 elseif (is_file(APPPATH.'config/doctypes.php'))
249 {
250 include(APPPATH.'config/doctypes.php');
251 }
Eric Barnes92808342011-03-18 09:02:37 -0400252
bubbafoley0ea04142011-03-17 14:55:41 -0500253 if ( ! is_array($_doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return FALSE;
256 }
257 }
258
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300259 return isset($_doctypes[$type]) ? $_doctypes[$type] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261}
262
263// ------------------------------------------------------------------------
264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265if ( ! function_exists('link_tag'))
266{
Timothy Warren01b129a2012-04-27 11:36:50 -0400267 /**
268 * Link
269 *
270 * Generates link to a CSS file
271 *
272 * @param mixed stylesheet hrefs or an array
273 * @param string rel
274 * @param string type
275 * @param string title
276 * @param string media
277 * @param bool should index_page be added to the css path
278 * @return string
279 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
281 {
282 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 $link = '<link ';
284
285 if (is_array($href))
286 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200287 foreach ($href as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300289 if ($k === 'href' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 if ($index_page === TRUE)
292 {
Derek Allard76763bb2009-09-16 11:25:20 +0000293 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295 else
296 {
Derek Allard76763bb2009-09-16 11:25:20 +0000297 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299 }
300 else
301 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300302 $link .= $k.'="'.$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
306 else
307 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200308 if (strpos($href, '://') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Derek Allard292dcd82009-09-16 11:26:32 +0000310 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 elseif ($index_page === TRUE)
313 {
Derek Allard76763bb2009-09-16 11:25:20 +0000314 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316 else
317 {
Derek Allard76763bb2009-09-16 11:25:20 +0000318 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320
321 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
322
Alex Bilbie773ccc32012-06-02 11:11:08 +0100323 if ($media !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 $link .= 'media="'.$media.'" ';
326 }
327
Alex Bilbie773ccc32012-06-02 11:11:08 +0100328 if ($title !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $link .= 'title="'.$title.'" ';
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300334 return $link."/>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336}
337
338// ------------------------------------------------------------------------
339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340if ( ! function_exists('meta'))
341{
Timothy Warren01b129a2012-04-27 11:36:50 -0400342 /**
343 * Generates meta tags from an array of key/values
344 *
345 * @param array
346 * @param string
347 * @param string
348 * @param string
349 * @return string
350 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
352 {
353 // Since we allow the data to be passes as a string, a simple array
354 // or a multidimensional one, we need to do a little prepping.
355 if ( ! is_array($name))
356 {
357 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
358 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300359 elseif (isset($name['name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 // Turn single array into multidimensional
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300362 $name = array($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 $str = '';
366 foreach ($name as $meta)
367 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300368 $type = ( ! isset($meta['type']) OR $meta['type'] === 'name') ? 'name' : 'http-equiv';
369 $name = isset($meta['name']) ? $meta['name'] : '';
370 $content = isset($meta['content']) ? $meta['content'] : '';
371 $newline = isset($meta['newline']) ? $meta['newline'] : "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000372
373 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
374 }
375
376 return $str;
377 }
378}
379
380// ------------------------------------------------------------------------
381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382if ( ! function_exists('nbs'))
383{
Timothy Warren01b129a2012-04-27 11:36:50 -0400384 /**
385 * Generates non-breaking space entities based on number supplied
386 *
387 * @param int
388 * @return string
389 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 function nbs($num = 1)
391 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200392 return str_repeat('&nbsp;', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394}
395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396/* End of file html_helper.php */
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300397/* Location: ./system/helpers/html_helper.php */