blob: 72970d9cab067ede4abe9ad14c930023d2c0202a [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 *
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
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
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 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +020057 return '<h'.$h.($attributes != '' ? ' ' : '').$attributes.'>'.$data.'</h'.$h.'>';
Derek Allard2067d1a2008-11-13 22:59:24 +000058 }
59}
60
61// ------------------------------------------------------------------------
62
63/**
64 * Unordered List
65 *
66 * Generates an HTML unordered list from an single or multi-dimensional array.
67 *
68 * @access public
69 * @param array
70 * @param mixed
71 * @return string
72 */
73if ( ! function_exists('ul'))
74{
75 function ul($list, $attributes = '')
76 {
77 return _list('ul', $list, $attributes);
78 }
79}
80
81// ------------------------------------------------------------------------
82
83/**
84 * Ordered List
85 *
86 * Generates an HTML ordered list from an single or multi-dimensional array.
87 *
88 * @access public
89 * @param array
90 * @param mixed
91 * @return string
92 */
93if ( ! function_exists('ol'))
94{
95 function ol($list, $attributes = '')
96 {
97 return _list('ol', $list, $attributes);
98 }
99}
100
101// ------------------------------------------------------------------------
102
103/**
104 * Generates the list
105 *
106 * Generates an HTML ordered list from an single or multi-dimensional array.
107 *
108 * @access private
109 * @param string
110 * @param mixed
111 * @param mixed
Derek Allard4e8d66a2010-03-15 12:23:27 -0400112 * @param integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return string
114 */
115if ( ! function_exists('_list'))
116{
117 function _list($type = 'ul', $list, $attributes = '', $depth = 0)
118 {
119 // If an array wasn't submitted there's nothing to do...
120 if ( ! is_array($list))
121 {
122 return $list;
123 }
124
125 // Set the indentation based on the depth
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200126 $out = str_repeat(' ', $depth);
Derek Allard2067d1a2008-11-13 22:59:24 +0000127
Derek Jones4b9c6292011-07-01 17:40:48 -0500128 // Were any attributes submitted? If so generate a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if (is_array($attributes))
130 {
131 $atts = '';
132 foreach ($attributes as $key => $val)
133 {
134 $atts .= ' ' . $key . '="' . $val . '"';
135 }
136 $attributes = $atts;
137 }
Eric Barnesb13a0dd2011-07-18 00:13:07 -0400138 elseif (is_string($attributes) AND strlen($attributes) > 0)
139 {
140 $attributes = ' '. $attributes;
141 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 // Write the opening list tag
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200144 $out .= '<'.$type.$attributes.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000145
Derek Jones4b9c6292011-07-01 17:40:48 -0500146 // Cycle through the list elements. If an array is
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // encountered we will recursively call _list()
148
149 static $_last_list_item = '';
150 foreach ($list as $key => $val)
151 {
152 $_last_list_item = $key;
153
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200154 $out .= str_repeat(' ', $depth + 2).'<li>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000155
156 if ( ! is_array($val))
157 {
158 $out .= $val;
159 }
160 else
161 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200162 $out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 $out .= "</li>\n";
166 }
167
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200168 // Set the indentation for the closing tag and apply it
169 return $out.str_repeat(' ', $depth).'</'.$type.">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171}
172
173// ------------------------------------------------------------------------
174
175/**
176 * Generates HTML BR tags based on number supplied
177 *
178 * @access public
179 * @param integer
180 * @return string
181 */
182if ( ! function_exists('br'))
183{
184 function br($num = 1)
185 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200186 return str_repeat('<br />', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188}
189
190// ------------------------------------------------------------------------
191
192/**
193 * Image
194 *
195 * Generates an <img /> element
196 *
197 * @access public
198 * @param mixed
199 * @return string
200 */
201if ( ! function_exists('img'))
202{
203 function img($src = '', $index_page = FALSE)
204 {
205 if ( ! is_array($src) )
206 {
207 $src = array('src' => $src);
208 }
209
Derek Allarda0905f32010-07-05 08:11:33 -0400210 // If there is no alt attribute defined, set it to an empty string
211 if ( ! isset($src['alt']))
212 {
213 $src['alt'] = '';
214 }
215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 $img = '<img';
217
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200218 foreach ($src as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200220 if ($k === 'src' AND strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 $CI =& get_instance();
223
224 if ($index_page === TRUE)
225 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600226 $img .= ' src="'.$CI->config->site_url($v).'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228 else
229 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600230 $img .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232 }
233 else
234 {
Greg Akerf7d162a2010-11-09 14:52:28 -0600235 $img .= " $k=\"$v\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237 }
238
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200239 return $img.'/>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241}
242
243// ------------------------------------------------------------------------
244
245/**
246 * Doctype
247 *
248 * Generates a page document type declaration
249 *
250 * Valid options are xhtml-11, xhtml-strict, xhtml-trans, xhtml-frame,
Derek Jones4b9c6292011-07-01 17:40:48 -0500251 * html4-strict, html4-trans, and html4-frame. Values are saved in the
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * doctypes config file.
253 *
254 * @access public
255 * @param string type The doctype to be generated
256 * @return string
257 */
258if ( ! function_exists('doctype'))
259{
Derek Allard93bddd12009-04-14 19:27:38 +0000260 function doctype($type = 'xhtml1-strict')
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 global $_doctypes;
263
264 if ( ! is_array($_doctypes))
265 {
Greg Akerd96f8822011-12-27 16:23:47 -0600266 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
267 {
268 include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
269 }
270 elseif (is_file(APPPATH.'config/doctypes.php'))
271 {
272 include(APPPATH.'config/doctypes.php');
273 }
Eric Barnes92808342011-03-18 09:02:37 -0400274
bubbafoley0ea04142011-03-17 14:55:41 -0500275 if ( ! is_array($_doctypes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 return FALSE;
278 }
279 }
280
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200281 return (isset($_doctypes[$type])) ? $_doctypes[$type] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283}
284
285// ------------------------------------------------------------------------
286
287/**
288 * Link
289 *
290 * Generates link to a CSS file
291 *
292 * @access public
293 * @param mixed stylesheet hrefs or an array
294 * @param string rel
295 * @param string type
296 * @param string title
297 * @param string media
298 * @param boolean should index_page be added to the css path
299 * @return string
300 */
301if ( ! function_exists('link_tag'))
302{
303 function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
304 {
305 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 $link = '<link ';
307
308 if (is_array($href))
309 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200310 foreach ($href as $k => $v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200312 if ($k === 'href' AND strpos($v, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 if ($index_page === TRUE)
315 {
Derek Allard76763bb2009-09-16 11:25:20 +0000316 $link .= 'href="'.$CI->config->site_url($v).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 else
319 {
Derek Allard76763bb2009-09-16 11:25:20 +0000320 $link .= 'href="'.$CI->config->slash_item('base_url').$v.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322 }
323 else
324 {
325 $link .= "$k=\"$v\" ";
326 }
327 }
328
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200329 $link .= '/>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331 else
332 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200333 if (strpos($href, '://') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Derek Allard292dcd82009-09-16 11:26:32 +0000335 $link .= 'href="'.$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337 elseif ($index_page === TRUE)
338 {
Derek Allard76763bb2009-09-16 11:25:20 +0000339 $link .= 'href="'.$CI->config->site_url($href).'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341 else
342 {
Derek Allard76763bb2009-09-16 11:25:20 +0000343 $link .= 'href="'.$CI->config->slash_item('base_url').$href.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345
346 $link .= 'rel="'.$rel.'" type="'.$type.'" ';
347
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200348 if ($media != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $link .= 'media="'.$media.'" ';
351 }
352
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200353 if ($title != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $link .= 'title="'.$title.'" ';
356 }
357
358 $link .= '/>';
359 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000360
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200361 return $link."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
363}
364
365// ------------------------------------------------------------------------
366
367/**
368 * Generates meta tags from an array of key/values
369 *
370 * @access public
371 * @param array
372 * @return string
373 */
374if ( ! function_exists('meta'))
375{
376 function meta($name = '', $content = '', $type = 'name', $newline = "\n")
377 {
378 // Since we allow the data to be passes as a string, a simple array
379 // or a multidimensional one, we need to do a little prepping.
380 if ( ! is_array($name))
381 {
382 $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
383 }
384 else
385 {
386 // Turn single array into multidimensional
387 if (isset($name['name']))
388 {
389 $name = array($name);
390 }
391 }
392
393 $str = '';
394 foreach ($name as $meta)
395 {
Barry Mienydd671972010-10-04 16:33:58 +0200396 $type = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';
397 $name = ( ! isset($meta['name'])) ? '' : $meta['name'];
398 $content = ( ! isset($meta['content'])) ? '' : $meta['content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 $newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
400
401 $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
402 }
403
404 return $str;
405 }
406}
407
408// ------------------------------------------------------------------------
409
410/**
411 * Generates non-breaking space entities based on number supplied
412 *
413 * @access public
414 * @param integer
415 * @return string
416 */
417if ( ! function_exists('nbs'))
418{
419 function nbs($num = 1)
420 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200421 return str_repeat('&nbsp;', $num);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423}
424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425/* End of file html_helper.php */
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200426/* Location: ./system/helpers/html_helper.php */