blob: eb97913f1213cb80c83e43ae279deca16163b369 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +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 */
admin141808a2006-08-27 01:52:51 +000041function form_open($action = '', $attributes = array(), $hidden = array())
adminb0dd10f2006-08-25 17:25:49 +000042{
43 $obj =& get_instance();
44
admin141808a2006-08-27 01:52:51 +000045 $form = '<form action="'.$obj->config->site_url($action).'"';
46
47 if ( ! isset($attributes['method']))
48 {
49 $form .= ' method="post"';
50 }
adminb0dd10f2006-08-25 17:25:49 +000051
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 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
202}
203
204// ------------------------------------------------------------------------
205
206/**
207 * Dropdown Menu
208 *
209 * @access public
210 * @param string
211 * @param array
212 * @param string
213 * @param string
214 * @return string
215 */
216function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
217{
218 if ($extra != '') $extra = ' '.$extra;
219
220 $form = '<select name="'.$name.'"'.$extra.">\n";
221
222 foreach ($options as $key => $val)
223 {
224 $sel = ($selected != $key) ? '' : ' selected="selected"';
225
226 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
227 }
228
229 $form .= '</select>';
230
231 return $form;
232}
233
234// ------------------------------------------------------------------------
235
236/**
237 * Checkbox Field
238 *
239 * @access public
240 * @param mixed
241 * @param string
242 * @param bool
243 * @param string
244 * @return string
245 */
246function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
247{
248 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
249
admin57b3d392006-08-27 15:28:31 +0000250 if (is_array($data) AND array_key_exists('checked', $data))
adminb0dd10f2006-08-25 17:25:49 +0000251 {
252 $checked = $data['checked'];
253
254 if ($checked == FALSE)
255 unset($data['checked']);
256 }
257
258 if ($checked == TRUE)
admin57b3d392006-08-27 15:28:31 +0000259 $defaults['checked'] = 'checked';
adminb0dd10f2006-08-25 17:25:49 +0000260 else
261 unset($defaults['checked']);
262
263 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
264}
265
266// ------------------------------------------------------------------------
267
268/**
269 * Radio Button
270 *
271 * @access public
272 * @param mixed
273 * @param string
274 * @param bool
275 * @param string
276 * @return string
277 */
278function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
279{
280 if ( ! is_array($data))
281 {
282 $data = array('name' => $data);
283 }
284
285 $data['type'] = 'radio';
286 return form_checkbox($data, $value, $checked, $extra);
287}
288
289// ------------------------------------------------------------------------
290
291/**
292 * Submit Button
293 *
294 * @access public
295 * @param mixed
296 * @param string
297 * @param string
298 * @return string
299 */
300function form_submit($data = '', $value = '', $extra = '')
301{
302 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
303
304 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
305}
306
307// ------------------------------------------------------------------------
308
309/**
310 * Form Close Tag
311 *
312 * @access public
313 * @param string
314 * @return string
315 */
316function form_close($extra = '')
317{
318 return "</form>\n".$extra;
319}
320
321// ------------------------------------------------------------------------
322
323/**
324 * Form Prep
325 *
326 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
327 *
328 * @access public
329 * @param string
330 * @return string
331 */
332function form_prep($str = '')
333{
adminb071bb52006-08-26 19:28:37 +0000334 if ($str === '')
adminb0dd10f2006-08-25 17:25:49 +0000335 {
336 return '';
337 }
338
339 return str_replace(array("'", '"'), array("&#39;", "&quot;"), htmlspecialchars($str));
340}
341
342// ------------------------------------------------------------------------
343
344/**
345 * Parse the form attributes
346 *
347 * Helper function used by some of the form helpers
348 *
349 * @access private
350 * @param array
351 * @parm array
352 * @return string
353 */
354function parse_form_attributes($attributes, $default)
355{
356 if (is_array($attributes))
357 {
358 foreach ($default as $key => $val)
359 {
360 if (isset($attributes[$key]))
361 {
362 $default[$key] = $attributes[$key];
363 unset($attributes[$key]);
364 }
365 }
366
367 if (count($attributes) > 0)
368 {
369 $default = array_merge($default, $attributes);
370 }
371 }
372
373 $att = '';
374 foreach ($default as $key => $val)
375 {
376 if ($key == 'value')
377 {
378 $val = form_prep($val);
379 }
380
381 $att .= $key . '="' . $val . '" ';
382 }
383
384 return $att;
385}
386
387?>