blob: 988eee715d2851c710a817dc22569ab5860bc203 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter HTML Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/html_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('heading'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Heading
45 *
46 * Generates an HTML heading tag.
47 *
48 * @param string content
49 * @param int heading level
50 * @param string
51 * @return string
52 */
Greg Aker826429c2011-04-18 09:40:19 -050053 function heading($data = '', $h = '1', $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Eric Barnesacedd2b2012-07-29 00:15:40 -040055 return '<h'.$h._stringify_attributes($attributes).'>'.$data.'</h'.$h.'>';
Derek Allard2067d1a2008-11-13 22:59:24 +000056 }
57}
58
59// ------------------------------------------------------------------------
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061if ( ! function_exists('ul'))
62{
Timothy Warren01b129a2012-04-27 11:36:50 -040063 /**
64 * Unordered List
65 *
66 * Generates an HTML unordered list from an single or multi-dimensional array.
67 *
68 * @param array
69 * @param mixed
70 * @return string
71 */
Derek Allard2067d1a2008-11-13 22:59:24 +000072 function ul($list, $attributes = '')
73 {
74 return _list('ul', $list, $attributes);
75 }
76}
77
78// ------------------------------------------------------------------------
79
Derek Allard2067d1a2008-11-13 22:59:24 +000080if ( ! function_exists('ol'))
81{
Timothy Warren01b129a2012-04-27 11:36:50 -040082 /**
83 * Ordered List
84 *
85 * Generates an HTML ordered list from an single or multi-dimensional array.
86 *
87 * @param array
88 * @param mixed
89 * @return string
90 */
Derek Allard2067d1a2008-11-13 22:59:24 +000091 function ol($list, $attributes = '')
92 {
93 return _list('ol', $list, $attributes);
94 }
95}
96
97// ------------------------------------------------------------------------
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099if ( ! function_exists('_list'))
100{
Timothy Warren01b129a2012-04-27 11:36:50 -0400101 /**
102 * Generates the list
103 *
104 * Generates an HTML ordered list from an single or multi-dimensional array.
105 *
106 * @param string
107 * @param mixed
108 * @param mixed
109 * @param int
110 * @return string
111 */
Rasmus Lerdorfe736f492013-05-18 10:25:03 -0400112 function _list($type = 'ul', $list = array(), $attributes = '', $depth = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
114 // If an array wasn't submitted there's nothing to do...
115 if ( ! is_array($list))
116 {
117 return $list;
118 }
119
120 // Set the indentation based on the depth
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200121 $out = str_repeat(' ', $depth);
Derek Allard2067d1a2008-11-13 22:59:24 +0000122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // Write the opening list tag
Eric Barnesacedd2b2012-07-29 00:15:40 -0400124 $out .= '<'.$type._stringify_attributes($attributes).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000125
Derek Jones4b9c6292011-07-01 17:40:48 -0500126 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // encountered we will recursively call _list()
128
129 static $_last_list_item = '';
130 foreach ($list as $key => $val)
131 {
132 $_last_list_item = $key;
133
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200134 $out .= str_repeat(' ', $depth + 2).'<li>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
136 if ( ! is_array($val))
137 {
138 $out .= $val;
139 }
140 else
141 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200142 $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 $out .= "</li>\n";
146 }
147
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200148 // Set the indentation for the closing tag and apply it
149 return $out.str_repeat(' ', $depth).'</'.$type.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151}
152
153// ------------------------------------------------------------------------
154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155if ( ! function_exists('br'))
156{
Timothy Warren01b129a2012-04-27 11:36:50 -0400157 /**
158 * Generates HTML BR tags based on number supplied
159 *
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200160 * @param int $count Number of times to repeat the tag
Timothy Warren01b129a2012-04-27 11:36:50 -0400161 * @return string
162 */
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200163 function br($count = 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200165 return str_repeat('<br />', $count);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167}
168
169// ------------------------------------------------------------------------
170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171if ( ! function_exists('img'))
172{
Timothy Warren01b129a2012-04-27 11:36:50 -0400173 /**
174 * Image
175 *
176 * Generates an <img /> element
177 *
178 * @param mixed
179 * @param bool
Eric Barnes62ab8b22012-07-28 14:57:04 -0400180 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400181 * @return string
182 */
Eric Barnes62ab8b22012-07-28 14:57:04 -0400183 function img($src = '', $index_page = FALSE, $attributes = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 if ( ! is_array($src) )
186 {
187 $src = array('src' => $src);
188 }
189
Derek Allarda0905f32010-07-05 08:11:33 -0400190 // If there is no alt attribute defined, set it to an empty string
191 if ( ! isset($src['alt']))
192 {
193 $src['alt'] = '';
194 }
195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 $img = '<img';
197
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200198 foreach ($src as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300200 if ($k === 'src' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 if ($index_page === TRUE)
203 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200204 $img .= ' src="'.get_instance()->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206 else
207 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200208 $img .= ' src="'.get_instance()->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210 }
211 else
212 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300213 $img .= ' '.$k.'="'.$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215 }
216
Andrey Andreev6a64f852012-08-15 11:16:47 +0300217 return $img._stringify_attributes($attributes).' />';
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219}
220
221// ------------------------------------------------------------------------
222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223if ( ! function_exists('doctype'))
224{
Timothy Warren01b129a2012-04-27 11:36:50 -0400225 /**
226 * Doctype
227 *
228 * Generates a page document type declaration
229 *
Przemyslaw Tomaszewski8711aeb2012-07-22 03:28:41 +0200230 * Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
231 * xhtml-frame, html4-strict, html4-trans, and html4-frame.
232 * All values are saved in the doctypes config file.
Timothy Warren01b129a2012-04-27 11:36:50 -0400233 *
234 * @param string type The doctype to be generated
235 * @return string
236 */
Derek Allard93bddd12009-04-14 19:27:38 +0000237 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev06879112013-01-29 15:05:02 +0200239 static $doctypes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240
Andrey Andreev06879112013-01-29 15:05:02 +0200241 if ( ! is_array($doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Andrey Andreev06879112013-01-29 15:05:02 +0200243 if (file_exists(APPPATH.'config/doctypes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600244 {
245 include(APPPATH.'config/doctypes.php');
246 }
Eric Barnes92808342011-03-18 09:02:37 -0400247
Andrey Andreev06879112013-01-29 15:05:02 +0200248 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreev06879112013-01-29 15:05:02 +0200250 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
251 }
252
253 if (empty($_doctypes) OR ! is_array($_doctypes))
254 {
255 $doctypes = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 return FALSE;
257 }
Andrey Andreev06879112013-01-29 15:05:02 +0200258
259 $doctypes = $_doctypes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261
Andrey Andreev06879112013-01-29 15:05:02 +0200262 return isset($doctypes[$type]) ? $doctypes[$type] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264}
265
266// ------------------------------------------------------------------------
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268if ( ! function_exists('link_tag'))
269{
Timothy Warren01b129a2012-04-27 11:36:50 -0400270 /**
271 * Link
272 *
273 * Generates link to a CSS file
274 *
275 * @param mixed stylesheet hrefs or an array
276 * @param string rel
277 * @param string type
278 * @param string title
279 * @param string media
280 * @param bool should index_page be added to the css path
281 * @return string
282 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
284 {
285 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 $link = '<link ';
287
288 if (is_array($href))
289 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200290 foreach ($href as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300292 if ($k === 'href' && strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 if ($index_page === TRUE)
295 {
Derek Allard76763bb2009-09-16 11:25:20 +0000296 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298 else
299 {
Derek Allard76763bb2009-09-16 11:25:20 +0000300 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 }
303 else
304 {
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300305 $link .= $k.'="'.$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 else
310 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200311 if (strpos($href, '://') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Derek Allard292dcd82009-09-16 11:26:32 +0000313 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
315 elseif ($index_page === TRUE)
316 {
Derek Allard76763bb2009-09-16 11:25:20 +0000317 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319 else
320 {
Derek Allard76763bb2009-09-16 11:25:20 +0000321 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323
324 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
325
Alex Bilbie773ccc32012-06-02 11:11:08 +0100326 if ($media !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $link .= 'media="'.$media.'" ';
329 }
330
Alex Bilbie773ccc32012-06-02 11:11:08 +0100331 if ($title !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 $link .= 'title="'.$title.'" ';
334 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300337 return $link."/>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339}
340
341// ------------------------------------------------------------------------
342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343if ( ! function_exists('meta'))
344{
Timothy Warren01b129a2012-04-27 11:36:50 -0400345 /**
346 * Generates meta tags from an array of key/values
347 *
348 * @param array
349 * @param string
350 * @param string
351 * @param string
352 * @return string
353 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
355 {
356 // Since we allow the data to be passes as a string, a simple array
357 // or a multidimensional one, we need to do a little prepping.
358 if ( ! is_array($name))
359 {
360 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
361 }
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300362 elseif (isset($name['name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 // Turn single array into multidimensional
Andrey Andreev2046b1a2012-03-26 21:07:04 +0300365 $name = array($name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
368 $str = '';
369 foreach ($name as $meta)
370 {
Andrey Andreevae31eb52012-05-17 14:54:15 +0300371 $type = ( ! isset($meta['type']) OR $meta['type'] === 'name') ? 'name' : 'http-equiv';
372 $name = isset($meta['name']) ? $meta['name'] : '';
373 $content = isset($meta['content']) ? $meta['content'] : '';
374 $newline = isset($meta['newline']) ? $meta['newline'] : "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
376 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
377 }
378
379 return $str;
380 }
381}
382
383// ------------------------------------------------------------------------
384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385if ( ! function_exists('nbs'))
386{
Timothy Warren01b129a2012-04-27 11:36:50 -0400387 /**
388 * Generates non-breaking space entities based on number supplied
389 *
390 * @param int
391 * @return string
392 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 function nbs($num = 1)
394 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200395 return str_repeat('&nbsp;', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397}
398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399/* End of file html_helper.php */
Andrey Andreevc5a7c5f2013-07-17 20:10:09 +0300400/* Location: ./system/helpers/html_helper.php */