blob: 5c1457066885b4126e89f457104581b0d98cec24 [file] [log] [blame]
Derek Allardf3e8a352008-01-04 14:30:38 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardf3e8a352008-01-04 14:30:38 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @license http://www.codeigniter.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Form Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Allardf3e8a352008-01-04 14:30:38 +000025 * @link http://www.codeigniter.com/user_guide/helpers/form_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Form Declaration
32 *
33 * Creates the opening portion of the form.
34 *
35 * @access public
36 * @param string the URI segments of the form destination
37 * @param array a key/value pair of attributes
38 * @param array a key/value pair hidden data
39 * @return string
40 */
41function form_open($action = '', $attributes = array(), $hidden = array())
42{
43 $CI =& get_instance();
44
Derek Allard36731bc2008-01-10 18:00:13 +000045 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
46
47 $form = '<form action="'.$action.'"';
Derek Allardf3e8a352008-01-04 14:30:38 +000048
49 if ( ! isset($attributes['method']))
50 {
51 $form .= ' method="post"';
52 }
53
54 if (is_array($attributes) AND count($attributes) > 0)
55 {
56 foreach ($attributes as $key => $val)
57 {
58 $form .= ' '.$key.'="'.$val.'"';
59 }
60 }
61
62 $form .= '>';
63
64 if (is_array($hidden) AND count($hidden > 0))
65 {
66 $form .= form_hidden($hidden);
67 }
68
69 return $form;
70}
71
72// ------------------------------------------------------------------------
73
74/**
75 * Form Declaration - Multipart type
76 *
77 * Creates the opening portion of the form, but with "multipart/form-data".
78 *
79 * @access public
80 * @param string the URI segments of the form destination
81 * @param array a key/value pair of attributes
82 * @param array a key/value pair hidden data
83 * @return string
84 */
85function form_open_multipart($action, $attributes = array(), $hidden = array())
86{
87 $attributes['enctype'] = 'multipart/form-data';
88 return form_open($action, $attributes, $hidden);
89}
90
91// ------------------------------------------------------------------------
92
93/**
94 * Hidden Input Field
95 *
96 * Generates hidden fields. You can pass a simple key/value string or an associative
97 * array with multiple values.
98 *
99 * @access public
100 * @param mixed
101 * @param string
102 * @return string
103 */
104function form_hidden($name, $value = '')
105{
106 if ( ! is_array($name))
107 {
108 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
109 }
110
111 $form = '';
112 foreach ($name as $name => $value)
113 {
114 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
115 }
116
117 return $form;
118}
119
120// ------------------------------------------------------------------------
121
122/**
123 * Text Input Field
124 *
125 * @access public
126 * @param mixed
127 * @param string
128 * @param string
129 * @return string
130 */
131function form_input($data = '', $value = '', $extra = '')
132{
133 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
134
135 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
136}
137
138// ------------------------------------------------------------------------
139
140/**
141 * Password Field
142 *
143 * Identical to the input function but adds the "password" type
144 *
145 * @access public
146 * @param mixed
147 * @param string
148 * @param string
149 * @return string
150 */
151function form_password($data = '', $value = '', $extra = '')
152{
153 if ( ! is_array($data))
154 {
155 $data = array('name' => $data);
156 }
157
158 $data['type'] = 'password';
159 return form_input($data, $value, $extra);
160}
161
162// ------------------------------------------------------------------------
163
164/**
165 * Upload Field
166 *
167 * Identical to the input function but adds the "file" type
168 *
169 * @access public
170 * @param mixed
171 * @param string
172 * @param string
173 * @return string
174 */
175function form_upload($data = '', $value = '', $extra = '')
176{
177 if ( ! is_array($data))
178 {
179 $data = array('name' => $data);
180 }
181
182 $data['type'] = 'file';
183 return form_input($data, $value, $extra);
184}
185
186// ------------------------------------------------------------------------
187
188/**
189 * Textarea field
190 *
191 * @access public
192 * @param mixed
193 * @param string
194 * @param string
195 * @return string
196 */
197function form_textarea($data = '', $value = '', $extra = '')
198{
199 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
200
201 if ( ! is_array($data) OR ! isset($data['value']))
202 {
203 $val = $value;
204 }
205 else
206 {
207 $val = $data['value'];
208 unset($data['value']); // textareas don't use the value attribute
209 }
210
211 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
212}
213
214// ------------------------------------------------------------------------
215
216/**
217 * Drop-down Menu
218 *
219 * @access public
220 * @param string
221 * @param array
222 * @param string
223 * @param string
224 * @return string
225 */
Derek Allard4021b512008-01-04 22:26:12 +0000226function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000227{
Derek Allard4021b512008-01-04 22:26:12 +0000228 if ( ! is_array($selected))
229 {
230 $selected = array($selected);
231 }
232
Derek Allardf3e8a352008-01-04 14:30:38 +0000233 if ($extra != '') $extra = ' '.$extra;
Derek Allardf8f05702008-01-18 14:39:23 +0000234
235 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Derek Allard4021b512008-01-04 22:26:12 +0000236
237 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000238
239 foreach ($options as $key => $val)
240 {
241 $key = (string) $key;
242 $val = (string) $val;
243
Derek Allard4021b512008-01-04 22:26:12 +0000244 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000245
246 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
247 }
248
249 $form .= '</select>';
250
251 return $form;
252}
253
254// ------------------------------------------------------------------------
255
256/**
257 * Checkbox Field
258 *
259 * @access public
260 * @param mixed
261 * @param string
262 * @param bool
263 * @param string
264 * @return string
265 */
266function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
267{
268 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
269
270 if (is_array($data) AND array_key_exists('checked', $data))
271 {
272 $checked = $data['checked'];
273
274 if ($checked == FALSE)
275 {
276 unset($data['checked']);
277 }
278 else
279 {
280 $data['checked'] = 'checked';
281 }
282 }
283
284 if ($checked == TRUE)
285 $defaults['checked'] = 'checked';
286 else
287 unset($defaults['checked']);
288
289 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
290}
291
292// ------------------------------------------------------------------------
293
294/**
295 * Radio Button
296 *
297 * @access public
298 * @param mixed
299 * @param string
300 * @param bool
301 * @param string
302 * @return string
303 */
304function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
305{
306 if ( ! is_array($data))
307 {
308 $data = array('name' => $data);
309 }
310
311 $data['type'] = 'radio';
312 return form_checkbox($data, $value, $checked, $extra);
313}
314
315// ------------------------------------------------------------------------
316
317/**
318 * Submit Button
319 *
320 * @access public
321 * @param mixed
322 * @param string
323 * @param string
324 * @return string
325 */
326function form_submit($data = '', $value = '', $extra = '')
327{
328 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
329
330 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
331}
332
333// ------------------------------------------------------------------------
334
335/**
336 * Reset Button
337 *
338 * @access public
339 * @param mixed
340 * @param string
341 * @param string
342 * @return string
343 */
344function form_reset($data = '', $value = '', $extra = '')
345{
346 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
347
348 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
349}
350
351// ------------------------------------------------------------------------
352
353/**
354 * Form Label Tag
355 *
356 * @access public
357 * @param string The text to appear onscreen
358 * @param string The id the label applies to
359 * @param string Additional attributes
360 * @return string
361 */
362function form_label($label_text = '', $id = '', $attributes = array())
363{
364
365 $label = '<label';
366
367 if ($id != '')
368 {
369 $label .= " for=\"$id\"";
370 }
371
372 if (is_array($attributes) AND count($attributes) > 0)
373 {
374 foreach ($attributes as $key => $val)
375 {
376 $label .= ' '.$key.'="'.$val.'"';
377 }
378 }
379
380 $label .= ">$label_text</label>";
381
382 return $label;
383}
384
385// ------------------------------------------------------------------------
386/**
387 * Fieldset Tag
388 *
389 * Used to produce <fieldset><legend>text</legend>. To close fieldset
390 * use form_fieldset_close()
391 *
392 * @access public
393 * @param string The legend text
394 * @param string Additional attributes
395 * @return string
396 */
397function form_fieldset($legend_text = '', $attributes = array())
398{
399
400 $fieldset = "<fieldset";
401
402 if (is_array($attributes) AND count($attributes) > 0)
403 {
404 foreach ($attributes as $key => $val)
405 {
406 $fieldset .= ' '.$key.'="'.$val.'"';
407 }
408 }
409
410 $fieldset .= ">\n";
411
412 if ($legend_text != '')
413 {
414 $fieldset .= "<legend>$legend_text</legend>\n";
415 }
416
417
418
419 return $fieldset;
420}
421
422// ------------------------------------------------------------------------
423
424/**
425 * Fieldset Close Tag
426 *
427 * @access public
428 * @param string
429 * @return string
430 */
431function form_fieldset_close($extra = '')
432{
433 return "</fieldset>\n".$extra;
434}
435
436// ------------------------------------------------------------------------
437
438/**
439 * Form Close Tag
440 *
441 * @access public
442 * @param string
443 * @return string
444 */
445function form_close($extra = '')
446{
447 return "</form>\n".$extra;
448}
449
450// ------------------------------------------------------------------------
451
452/**
453 * Form Prep
454 *
455 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
456 *
457 * @access public
458 * @param string
459 * @return string
460 */
461function form_prep($str = '')
462{
463 if ($str === '')
464 {
465 return '';
466 }
467
468 $temp = '__TEMP_AMPERSANDS__';
469
470 // Replace entities to temporary markers so that
471 // htmlspecialchars won't mess them up
472 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
473 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
474
475 $str = htmlspecialchars($str);
476
477 // In case htmlspecialchars misses these.
478 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
479
480 // Decode the temp markers back to entities
481 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
482 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
483
484 return $str;
485}
486
487// ------------------------------------------------------------------------
488
489/**
490 * Parse the form attributes
491 *
492 * Helper function used by some of the form helpers
493 *
494 * @access private
495 * @param array
496 * @param array
497 * @return string
498 */
499function parse_form_attributes($attributes, $default)
500{
501 if (is_array($attributes))
502 {
503 foreach ($default as $key => $val)
504 {
505 if (isset($attributes[$key]))
506 {
507 $default[$key] = $attributes[$key];
508 unset($attributes[$key]);
509 }
510 }
511
512 if (count($attributes) > 0)
513 {
514 $default = array_merge($default, $attributes);
515 }
516 }
517
518 $att = '';
519 foreach ($default as $key => $val)
520 {
521 if ($key == 'value')
522 {
523 $val = form_prep($val);
524 }
525
526 $att .= $key . '="' . $val . '" ';
527 }
528
529 return $att;
530}
531
532?>