blob: a166198fb7ac6c1046751bfd32a19822ce05c3c5 [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
Derek Allardc4ac15f2007-02-15 17:22:31 +0000199 if ( ! is_array($data) OR ! isset($data['value']))
200 {
201 $val = $value;
202 }
203 else
204 {
205 $val = $data['value'];
206 unset($data['value']); // textareas don't use the value attribute
207 }
Derek Allard17f4d732007-01-27 17:23:13 +0000208
209 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
210}
211
212// ------------------------------------------------------------------------
213
214/**
215 * Drop-down Menu
216 *
217 * @access public
218 * @param string
219 * @param array
220 * @param string
221 * @param string
222 * @return string
223 */
224function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
225{
226 if ($extra != '') $extra = ' '.$extra;
227
228 $form = '<select name="'.$name.'"'.$extra.">\n";
229
230 foreach ($options as $key => $val)
231 {
232 $sel = ($selected != $key) ? '' : ' selected="selected"';
233
234 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
235 }
236
237 $form .= '</select>';
238
239 return $form;
240}
241
242// ------------------------------------------------------------------------
243
244/**
245 * Checkbox Field
246 *
247 * @access public
248 * @param mixed
249 * @param string
250 * @param bool
251 * @param string
252 * @return string
253 */
254function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
255{
256 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
257
258 if (is_array($data) AND array_key_exists('checked', $data))
259 {
260 $checked = $data['checked'];
261
262 if ($checked == FALSE)
263 unset($data['checked']);
264 }
265
266 if ($checked == TRUE)
267 $defaults['checked'] = 'checked';
268 else
269 unset($defaults['checked']);
270
271 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
272}
273
274// ------------------------------------------------------------------------
275
276/**
277 * Radio Button
278 *
279 * @access public
280 * @param mixed
281 * @param string
282 * @param bool
283 * @param string
284 * @return string
285 */
286function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
287{
288 if ( ! is_array($data))
289 {
290 $data = array('name' => $data);
291 }
292
293 $data['type'] = 'radio';
294 return form_checkbox($data, $value, $checked, $extra);
295}
296
297// ------------------------------------------------------------------------
298
299/**
300 * Submit Button
301 *
302 * @access public
303 * @param mixed
304 * @param string
305 * @param string
306 * @return string
307 */
308function form_submit($data = '', $value = '', $extra = '')
309{
310 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
311
312 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
313}
314
315// ------------------------------------------------------------------------
316
317/**
318 * Form Close Tag
319 *
320 * @access public
321 * @param string
322 * @return string
323 */
324function form_close($extra = '')
325{
326 return "</form>\n".$extra;
327}
328
329// ------------------------------------------------------------------------
330
331/**
332 * Form Prep
333 *
334 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
335 *
336 * @access public
337 * @param string
338 * @return string
339 */
340function form_prep($str = '')
341{
342 if ($str === '')
343 {
344 return '';
345 }
346
347 $temp = '__TEMP_AMPERSANDS__';
348
349 // Replace entities to temporary markers so that
350 // htmlspecialchars won't mess them up
351 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
352 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
353
354 $str = htmlspecialchars($str);
355
356 // In case htmlspecialchars misses these.
357 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
358
359 // Decode the temp markers back to entities
360 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
361 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
362
363 return $str;
364}
365
366// ------------------------------------------------------------------------
367
368/**
369 * Parse the form attributes
370 *
371 * Helper function used by some of the form helpers
372 *
373 * @access private
374 * @param array
375 * @param array
376 * @return string
377 */
378function parse_form_attributes($attributes, $default)
379{
380 if (is_array($attributes))
381 {
382 foreach ($default as $key => $val)
383 {
384 if (isset($attributes[$key]))
385 {
386 $default[$key] = $attributes[$key];
387 unset($attributes[$key]);
388 }
389 }
390
391 if (count($attributes) > 0)
392 {
393 $default = array_merge($default, $attributes);
394 }
395 }
396
397 $att = '';
398 foreach ($default as $key => $val)
399 {
400 if ($key == 'value')
401 {
402 $val = form_prep($val);
403 }
404
405 $att .= $key . '="' . $val . '" ';
406 }
407
408 return $att;
409}
410
adminb0dd10f2006-08-25 17:25:49 +0000411?>