Take two:
[www-register-wizard.git] / helpers / html_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * CodeIgniter HTML Helpers\r
20  *\r
21  * @package             CodeIgniter\r
22  * @subpackage  Helpers\r
23  * @category    Helpers\r
24  * @author              ExpressionEngine Dev Team\r
25  * @link                http://codeigniter.com/user_guide/helpers/html_helper.html\r
26  */\r
27 \r
28 // ------------------------------------------------------------------------\r
29 \r
30 /**\r
31  * Heading\r
32  *\r
33  * Generates an HTML heading tag.  First param is the data.\r
34  * Second param is the size of the heading tag.\r
35  *\r
36  * @access      public\r
37  * @param       string\r
38  * @param       integer\r
39  * @return      string\r
40  */     \r
41 if ( ! function_exists('heading'))\r
42 {\r
43         function heading($data = '', $h = '1')\r
44         {\r
45                 return "<h".$h.">".$data."</h".$h.">";\r
46         }\r
47 }\r
48 \r
49 // ------------------------------------------------------------------------\r
50 \r
51 /**\r
52  * Unordered List\r
53  *\r
54  * Generates an HTML unordered list from an single or multi-dimensional array.\r
55  *\r
56  * @access      public\r
57  * @param       array\r
58  * @param       mixed\r
59  * @return      string\r
60  */     \r
61 if ( ! function_exists('ul'))\r
62 {\r
63         function ul($list, $attributes = '')\r
64         {\r
65                 return _list('ul', $list, $attributes);\r
66         }\r
67 }\r
68 \r
69 // ------------------------------------------------------------------------\r
70 \r
71 /**\r
72  * Ordered List\r
73  *\r
74  * Generates an HTML ordered list from an single or multi-dimensional array.\r
75  *\r
76  * @access      public\r
77  * @param       array\r
78  * @param       mixed\r
79  * @return      string\r
80  */     \r
81 if ( ! function_exists('ol'))\r
82 {\r
83         function ol($list, $attributes = '')\r
84         {\r
85                 return _list('ol', $list, $attributes);\r
86         }\r
87 }\r
88 \r
89 // ------------------------------------------------------------------------\r
90 \r
91 /**\r
92  * Generates the list\r
93  *\r
94  * Generates an HTML ordered list from an single or multi-dimensional array.\r
95  *\r
96  * @access      private\r
97  * @param       string\r
98  * @param       mixed           \r
99  * @param       mixed           \r
100  * @param       intiger         \r
101  * @return      string\r
102  */     \r
103 if ( ! function_exists('_list'))\r
104 {\r
105         function _list($type = 'ul', $list, $attributes = '', $depth = 0)\r
106         {\r
107                 // If an array wasn't submitted there's nothing to do...\r
108                 if ( ! is_array($list))\r
109                 {\r
110                         return $list;\r
111                 }\r
112         \r
113                 // Set the indentation based on the depth\r
114                 $out = str_repeat(" ", $depth);\r
115         \r
116                 // Were any attributes submitted?  If so generate a string\r
117                 if (is_array($attributes))\r
118                 {\r
119                         $atts = '';\r
120                         foreach ($attributes as $key => $val)\r
121                         {\r
122                                 $atts .= ' ' . $key . '="' . $val . '"';\r
123                         }\r
124                         $attributes = $atts;\r
125                 }\r
126         \r
127                 // Write the opening list tag\r
128                 $out .= "<".$type.$attributes.">\n";\r
129 \r
130                 // Cycle through the list elements.  If an array is \r
131                 // encountered we will recursively call _list()\r
132 \r
133                 static $_last_list_item = '';\r
134                 foreach ($list as $key => $val)\r
135                 {       \r
136                         $_last_list_item = $key;\r
137 \r
138                         $out .= str_repeat(" ", $depth + 2);\r
139                         $out .= "<li>";\r
140                 \r
141                         if ( ! is_array($val))\r
142                         {\r
143                                 $out .= $val;\r
144                         }\r
145                         else\r
146                         {\r
147                                 $out .= $_last_list_item."\n";\r
148                                 $out .= _list($type, $val, '', $depth + 4);\r
149                                 $out .= str_repeat(" ", $depth + 2);\r
150                         }\r
151 \r
152                         $out .= "</li>\n";              \r
153                 }\r
154 \r
155                 // Set the indentation for the closing tag\r
156                 $out .= str_repeat(" ", $depth);\r
157         \r
158                 // Write the closing list tag\r
159                 $out .= "</".$type.">\n";\r
160 \r
161                 return $out;\r
162         }\r
163 }\r
164         \r
165 // ------------------------------------------------------------------------\r
166 \r
167 /**\r
168  * Generates HTML BR tags based on number supplied\r
169  *\r
170  * @access      public\r
171  * @param       integer\r
172  * @return      string\r
173  */     \r
174 if ( ! function_exists('br'))\r
175 {\r
176         function br($num = 1)\r
177         {\r
178                 return str_repeat("<br />", $num);\r
179         }\r
180 }\r
181         \r
182 // ------------------------------------------------------------------------\r
183 \r
184 /**\r
185  * Image\r
186  *\r
187  * Generates an <img /> element\r
188  *\r
189  * @access      public\r
190  * @param       mixed\r
191  * @return      string\r
192  */     \r
193 if ( ! function_exists('img'))\r
194 {\r
195         function img($src = '', $index_page = FALSE)\r
196         {\r
197                 if ( ! is_array($src) )\r
198                 {\r
199                         $src = array('src' => $src);\r
200                 }\r
201 \r
202                 $img = '<img';\r
203                 \r
204                 foreach ($src as $k=>$v)\r
205                 {\r
206 \r
207                         if ($k == 'src' AND strpos($v, '://') === FALSE)\r
208                         {\r
209                                 $CI =& get_instance();\r
210 \r
211                                 if ($index_page === TRUE)\r
212                                 {\r
213                                         $img .= ' src="'.$CI->config->site_url($v).'" ';\r
214                                 }\r
215                                 else\r
216                                 {\r
217                                         $img .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';\r
218                                 }\r
219                         }\r
220                         else\r
221                         {\r
222                                 $img .= " $k=\"$v\" ";\r
223                         }\r
224                 }\r
225 \r
226                 $img .= '/>';\r
227 \r
228                 return $img;\r
229         }\r
230 }\r
231 \r
232 // ------------------------------------------------------------------------\r
233 \r
234 /**\r
235  * Link\r
236  *\r
237  * Generates link to a CSS file\r
238  *\r
239  * @access      public\r
240  * @param       mixed   stylesheet hrefs or an array\r
241  * @param       string  rel\r
242  * @param       string  type\r
243  * @param       string  title\r
244  * @param       string  media\r
245  * @param       boolean should index_page be added to the css path \r
246  * @return      string\r
247  */     \r
248 if ( ! function_exists('link_tag'))\r
249 {\r
250         function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)\r
251         {\r
252                 $CI =& get_instance();\r
253 \r
254                 $link = '<link ';\r
255 \r
256                 if (is_array($href))\r
257                 {\r
258                         foreach ($href as $k=>$v)\r
259                         {\r
260                                 if ($k == 'href' AND strpos($v, '://') === FALSE)\r
261                                 {\r
262                                         if ($index_page === TRUE)\r
263                                         {\r
264                                                 $link .= ' href="'.$CI->config->site_url($v).'" ';\r
265                                         }\r
266                                         else\r
267                                         {\r
268                                                 $link .= ' href="'.$CI->config->slash_item('base_url').$v.'" ';\r
269                                         }\r
270                                 }\r
271                                 else\r
272                                 {\r
273                                         $link .= "$k=\"$v\" ";\r
274                                 }\r
275                         }\r
276                         \r
277                         $link .= "/>";\r
278                 }\r
279                 else\r
280                 {\r
281                         if ( strpos($href, '://') !== FALSE)\r
282                         {\r
283                                 $link .= ' href="'.$href.'" ';\r
284                         }\r
285                         elseif ($index_page === TRUE)\r
286                         {\r
287                                 $link .= ' href="'.$CI->config->site_url($href).'" ';\r
288                         }\r
289                         else\r
290                         {\r
291                                 $link .= ' href="'.$CI->config->slash_item('base_url').$href.'" ';\r
292                         }\r
293                                 \r
294                         $link .= 'rel="'.$rel.'" type="'.$type.'" ';\r
295                         \r
296                         if ($media      != '')\r
297                         {\r
298                                 $link .= 'media="'.$media.'" ';\r
299                         }\r
300 \r
301                         if ($title      != '')\r
302                         {\r
303                                 $link .= 'title="'.$title.'" ';\r
304                         }\r
305                         \r
306                         $link .= '/>';\r
307                 }\r
308 \r
309         \r
310                 return $link;\r
311         }\r
312 }\r
313 \r
314 // ------------------------------------------------------------------------\r
315 \r
316 /**\r
317  * Generates meta tags from an array of key/values\r
318  *\r
319  * @access      public\r
320  * @param       array\r
321  * @return      string\r
322  */     \r
323 if ( ! function_exists('meta'))\r
324 {\r
325         function meta($name = '', $content = '', $type = 'name', $newline = "\n")\r
326         {\r
327                 // Since we allow the data to be passes as a string, a simple array\r
328                 // or a multidimensional one, we need to do a little prepping.\r
329                 if ( ! is_array($name))\r
330                 {\r
331                         $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));\r
332                 }\r
333                 else\r
334                 {\r
335                         // Turn single array into multidimensional\r
336                         if (isset($name['name']))\r
337                         {\r
338                                 $name = array($name);\r
339                         }\r
340                 }\r
341         \r
342                 $str = '';\r
343                 foreach ($name as $meta)\r
344                 {\r
345                         $type           = ( ! isset($meta['type']) OR $meta['type'] == 'name') ? 'name' : 'http-equiv';\r
346                         $name           = ( ! isset($meta['name']))     ? ''    : $meta['name'];\r
347                         $content        = ( ! isset($meta['content']))  ? ''    : $meta['content'];\r
348                         $newline        = ( ! isset($meta['newline']))  ? "\n"  : $meta['newline'];\r
349                         \r
350                         $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;\r
351                 }\r
352 \r
353                 return $str;\r
354         }\r
355 }\r
356 \r
357 // ------------------------------------------------------------------------\r
358 \r
359 /**\r
360  * Generates non-breaking space entities based on number supplied\r
361  *\r
362  * @access      public\r
363  * @param       integer\r
364  * @return      string\r
365  */     \r
366 if ( ! function_exists('nbs'))\r
367 {\r
368         function nbs($num = 1)\r
369         {\r
370                 return str_repeat("&nbsp;", $num);\r
371         }\r
372 }\r
373 \r
374 \r
375 /* End of file html_helper.php */\r
376 /* Location: ./system/helpers/html_helper.php */