blob: f82ad01a067eb4c1dba1076d4cdab58f3156dc64 [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 */
41function form_open($action, $attributes = array(), $hidden = array())
42{
43 $obj =& get_instance();
44
45 $form = '<form method="post" action="'.$obj->config->site_url($action).'"';
46
47 if (is_array($attributes) AND count($attributes) > 0)
48 {
49 foreach ($attributes as $key => $val)
50 {
51 $form .= ' '.$key.'="'.$val.'"';
52 }
53 }
54
55 $form .= '>';
56
57 if (is_array($hidden) AND count($hidden > 0))
58 {
59 $form .= form_hidden($hidden);
60 }
61
62 return $form;
63}
64
65// ------------------------------------------------------------------------
66
67/**
68 * Form Declaration - Multipart type
69 *
70 * Creates the opening portion of the form, but with "multipart/form-data".
71 *
72 * @access public
73 * @param string the URI segments of the form destination
74 * @param array a key/value pair of attributes
75 * @param array a key/value pair hidden data
76 * @return string
77 */
78function form_open_multipart($action, $attributes = array(), $hidden = array())
79{
80 $attributes['enctype'] = 'multipart/form-data';
81 return form_open($action, $attributes, $hidden);
82}
83
84// ------------------------------------------------------------------------
85
86/**
87 * Hidden Input Field
88 *
89 * Generates hidden fields. You can pass a simple key/value string or an associative
90 * array with multiple values.
91 *
92 * @access public
93 * @param mixed
94 * @param string
95 * @return string
96 */
97function form_hidden($name, $value = '')
98{
99 if ( ! is_array($name))
100 {
101 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
102 }
103
104 $form = '';
105 foreach ($name as $name => $value)
106 {
107 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
108 }
109
110 return $form;
111}
112
113// ------------------------------------------------------------------------
114
115/**
116 * Text Input Field
117 *
118 * @access public
119 * @param mixed
120 * @param string
121 * @param string
122 * @return string
123 */
124function form_input($data = '', $value = '', $extra = '')
125{
126 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
127
128 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
129}
130
131// ------------------------------------------------------------------------
132
133/**
134 * Password Field
135 *
136 * Identical to the input function but adds the "password" type
137 *
138 * @access public
139 * @param mixed
140 * @param string
141 * @param string
142 * @return string
143 */
144function form_password($data = '', $value = '', $extra = '')
145{
146 if ( ! is_array($data))
147 {
148 $data = array('name' => $data);
149 }
150
151 $data['type'] = 'password';
152 return form_input($data, $value, $extra);
153}
154
155// ------------------------------------------------------------------------
156
157/**
158 * Upload Field
159 *
160 * Identical to the input function but adds the "file" type
161 *
162 * @access public
163 * @param mixed
164 * @param string
165 * @param string
166 * @return string
167 */
168function form_upload($data = '', $value = '', $extra = '')
169{
170 if ( ! is_array($data))
171 {
172 $data = array('name' => $data);
173 }
174
175 $data['type'] = 'file';
176 return form_input($data, $value, $extra);
177}
178
179// ------------------------------------------------------------------------
180
181/**
182 * Textarea field
183 *
184 * @access public
185 * @param mixed
186 * @param string
187 * @param string
188 * @return string
189 */
190function form_textarea($data = '', $value = '', $extra = '')
191{
192 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
193
194 $val = (( ! is_array($data) OR ! isset($data['value'])) ? $value : $data['value']);
195
196 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
197}
198
199// ------------------------------------------------------------------------
200
201/**
202 * Dropdown Menu
203 *
204 * @access public
205 * @param string
206 * @param array
207 * @param string
208 * @param string
209 * @return string
210 */
211function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
212{
213 if ($extra != '') $extra = ' '.$extra;
214
215 $form = '<select name="'.$name.'"'.$extra.">\n";
216
217 foreach ($options as $key => $val)
218 {
219 $sel = ($selected != $key) ? '' : ' selected="selected"';
220
221 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
222 }
223
224 $form .= '</select>';
225
226 return $form;
227}
228
229// ------------------------------------------------------------------------
230
231/**
232 * Checkbox Field
233 *
234 * @access public
235 * @param mixed
236 * @param string
237 * @param bool
238 * @param string
239 * @return string
240 */
241function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
242{
243 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
244
245 if (isset($data['checked']))
246 {
247 $checked = $data['checked'];
248
249 if ($checked == FALSE)
250 unset($data['checked']);
251 }
252
253 if ($checked == TRUE)
254 $defaults['checked'] = ' checked="checked"';
255 else
256 unset($defaults['checked']);
257
258 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
259}
260
261// ------------------------------------------------------------------------
262
263/**
264 * Radio Button
265 *
266 * @access public
267 * @param mixed
268 * @param string
269 * @param bool
270 * @param string
271 * @return string
272 */
273function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
274{
275 if ( ! is_array($data))
276 {
277 $data = array('name' => $data);
278 }
279
280 $data['type'] = 'radio';
281 return form_checkbox($data, $value, $checked, $extra);
282}
283
284// ------------------------------------------------------------------------
285
286/**
287 * Submit Button
288 *
289 * @access public
290 * @param mixed
291 * @param string
292 * @param string
293 * @return string
294 */
295function form_submit($data = '', $value = '', $extra = '')
296{
297 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
298
299 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
300}
301
302// ------------------------------------------------------------------------
303
304/**
305 * Form Close Tag
306 *
307 * @access public
308 * @param string
309 * @return string
310 */
311function form_close($extra = '')
312{
313 return "</form>\n".$extra;
314}
315
316// ------------------------------------------------------------------------
317
318/**
319 * Form Prep
320 *
321 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
322 *
323 * @access public
324 * @param string
325 * @return string
326 */
327function form_prep($str = '')
328{
329 if ($str == '')
330 {
331 return '';
332 }
333
334 return str_replace(array("'", '"'), array("&#39;", "&quot;"), htmlspecialchars($str));
335}
336
337// ------------------------------------------------------------------------
338
339/**
340 * Parse the form attributes
341 *
342 * Helper function used by some of the form helpers
343 *
344 * @access private
345 * @param array
346 * @parm array
347 * @return string
348 */
349function parse_form_attributes($attributes, $default)
350{
351 if (is_array($attributes))
352 {
353 foreach ($default as $key => $val)
354 {
355 if (isset($attributes[$key]))
356 {
357 $default[$key] = $attributes[$key];
358 unset($attributes[$key]);
359 }
360 }
361
362 if (count($attributes) > 0)
363 {
364 $default = array_merge($default, $attributes);
365 }
366 }
367
368 $att = '';
369 foreach ($default as $key => $val)
370 {
371 if ($key == 'value')
372 {
373 $val = form_prep($val);
374 }
375
376 $att .= $key . '="' . $val . '" ';
377 }
378
379 return $att;
380}
381
382?>