blob: 8186d4ccdff9d84195add3767d1dbbc6e3be1e32 [file] [log] [blame]
Derek Allard17f4d732007-01-27 17:23:13 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Form Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @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
45 $form = '<form action="'.$CI->config->site_url($action).'"';
46
47 if ( ! isset($attributes['method']))
48 {
49 $form .= ' method="post"';
50 }
51
52 if (is_array($attributes) AND count($attributes) > 0)
53 {
54 foreach ($attributes as $key => $val)
55 {
56 $form .= ' '.$key.'="'.$val.'"';
57 }
58 }
59
60 $form .= '>';
61
62 if (is_array($hidden) AND count($hidden > 0))
63 {
64 $form .= form_hidden($hidden);
65 }
66
67 return $form;
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Form Declaration - Multipart type
74 *
75 * Creates the opening portion of the form, but with "multipart/form-data".
76 *
77 * @access public
78 * @param string the URI segments of the form destination
79 * @param array a key/value pair of attributes
80 * @param array a key/value pair hidden data
81 * @return string
82 */
83function form_open_multipart($action, $attributes = array(), $hidden = array())
84{
85 $attributes['enctype'] = 'multipart/form-data';
86 return form_open($action, $attributes, $hidden);
87}
88
89// ------------------------------------------------------------------------
90
91/**
92 * Hidden Input Field
93 *
94 * Generates hidden fields. You can pass a simple key/value string or an associative
95 * array with multiple values.
96 *
97 * @access public
98 * @param mixed
99 * @param string
100 * @return string
101 */
102function form_hidden($name, $value = '')
103{
104 if ( ! is_array($name))
105 {
106 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
107 }
108
109 $form = '';
110 foreach ($name as $name => $value)
111 {
112 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
113 }
114
115 return $form;
116}
117
118// ------------------------------------------------------------------------
119
120/**
121 * Text Input Field
122 *
123 * @access public
124 * @param mixed
125 * @param string
126 * @param string
127 * @return string
128 */
129function form_input($data = '', $value = '', $extra = '')
130{
131 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
132
133 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
134}
135
136// ------------------------------------------------------------------------
137
138/**
139 * Password Field
140 *
141 * Identical to the input function but adds the "password" type
142 *
143 * @access public
144 * @param mixed
145 * @param string
146 * @param string
147 * @return string
148 */
149function form_password($data = '', $value = '', $extra = '')
150{
151 if ( ! is_array($data))
152 {
153 $data = array('name' => $data);
154 }
155
156 $data['type'] = 'password';
157 return form_input($data, $value, $extra);
158}
159
160// ------------------------------------------------------------------------
161
162/**
163 * Upload Field
164 *
165 * Identical to the input function but adds the "file" type
166 *
167 * @access public
168 * @param mixed
169 * @param string
170 * @param string
171 * @return string
172 */
173function form_upload($data = '', $value = '', $extra = '')
174{
175 if ( ! is_array($data))
176 {
177 $data = array('name' => $data);
178 }
179
180 $data['type'] = 'file';
181 return form_input($data, $value, $extra);
182}
183
184// ------------------------------------------------------------------------
185
186/**
187 * Textarea field
188 *
189 * @access public
190 * @param mixed
191 * @param string
192 * @param string
193 * @return string
194 */
195function form_textarea($data = '', $value = '', $extra = '')
196{
197 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
198
199 $val = (( ! is_array($data) OR ! isset($data['value'])) ? $value : $data['value']);
200
201 unset ($data['value']); // textareas don't use the value attribute
202
203 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
204}
205
206// ------------------------------------------------------------------------
207
208/**
209 * Drop-down Menu
210 *
211 * @access public
212 * @param string
213 * @param array
214 * @param string
215 * @param string
216 * @return string
217 */
218function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
219{
220 if ($extra != '') $extra = ' '.$extra;
221
222 $form = '<select name="'.$name.'"'.$extra.">\n";
223
224 foreach ($options as $key => $val)
225 {
226 $sel = ($selected != $key) ? '' : ' selected="selected"';
227
228 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
229 }
230
231 $form .= '</select>';
232
233 return $form;
234}
235
236// ------------------------------------------------------------------------
237
238/**
239 * Checkbox Field
240 *
241 * @access public
242 * @param mixed
243 * @param string
244 * @param bool
245 * @param string
246 * @return string
247 */
248function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
249{
250 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
251
252 if (is_array($data) AND array_key_exists('checked', $data))
253 {
254 $checked = $data['checked'];
255
256 if ($checked == FALSE)
257 unset($data['checked']);
258 }
259
260 if ($checked == TRUE)
261 $defaults['checked'] = 'checked';
262 else
263 unset($defaults['checked']);
264
265 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
266}
267
268// ------------------------------------------------------------------------
269
270/**
271 * Radio Button
272 *
273 * @access public
274 * @param mixed
275 * @param string
276 * @param bool
277 * @param string
278 * @return string
279 */
280function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
281{
282 if ( ! is_array($data))
283 {
284 $data = array('name' => $data);
285 }
286
287 $data['type'] = 'radio';
288 return form_checkbox($data, $value, $checked, $extra);
289}
290
291// ------------------------------------------------------------------------
292
293/**
294 * Submit Button
295 *
296 * @access public
297 * @param mixed
298 * @param string
299 * @param string
300 * @return string
301 */
302function form_submit($data = '', $value = '', $extra = '')
303{
304 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
305
306 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
307}
308
309// ------------------------------------------------------------------------
310
311/**
312 * Form Close Tag
313 *
314 * @access public
315 * @param string
316 * @return string
317 */
318function form_close($extra = '')
319{
320 return "</form>\n".$extra;
321}
322
323// ------------------------------------------------------------------------
324
325/**
326 * Form Prep
327 *
328 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
329 *
330 * @access public
331 * @param string
332 * @return string
333 */
334function form_prep($str = '')
335{
336 if ($str === '')
337 {
338 return '';
339 }
340
341 $temp = '__TEMP_AMPERSANDS__';
342
343 // Replace entities to temporary markers so that
344 // htmlspecialchars won't mess them up
345 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
346 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
347
348 $str = htmlspecialchars($str);
349
350 // In case htmlspecialchars misses these.
351 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
352
353 // Decode the temp markers back to entities
354 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
355 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
356
357 return $str;
358}
359
360// ------------------------------------------------------------------------
361
362/**
363 * Parse the form attributes
364 *
365 * Helper function used by some of the form helpers
366 *
367 * @access private
368 * @param array
369 * @param array
370 * @return string
371 */
372function parse_form_attributes($attributes, $default)
373{
374 if (is_array($attributes))
375 {
376 foreach ($default as $key => $val)
377 {
378 if (isset($attributes[$key]))
379 {
380 $default[$key] = $attributes[$key];
381 unset($attributes[$key]);
382 }
383 }
384
385 if (count($attributes) > 0)
386 {
387 $default = array_merge($default, $attributes);
388 }
389 }
390
391 $att = '';
392 foreach ($default as $key => $val)
393 {
394 if ($key == 'value')
395 {
396 $val = form_prep($val);
397 }
398
399 $att .= $key . '="' . $val . '" ';
400 }
401
402 return $att;
403}
404
adminb0dd10f2006-08-25 17:25:49 +0000405?>