00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 if ( ! function_exists('form_open'))
00042 {
00043 function form_open($action = '', $attributes = '', $hidden = array())
00044 {
00045 $CI =& get_instance();
00046
00047 if ($attributes == '')
00048 {
00049 $attributes = 'method="post"';
00050 }
00051
00052 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
00053
00054 $form = '<form action="'.$action.'"';
00055
00056 $form .= _attributes_to_string($attributes, TRUE);
00057
00058 $form .= '>';
00059
00060 if (is_array($hidden) AND count($hidden > 0))
00061 {
00062 $form .= form_hidden($hidden);
00063 }
00064
00065 return $form;
00066 }
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 if ( ! function_exists('form_open_multipart'))
00083 {
00084 function form_open_multipart($action, $attributes = array(), $hidden = array())
00085 {
00086 $attributes['enctype'] = 'multipart/form-data';
00087 return form_open($action, $attributes, $hidden);
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 if ( ! function_exists('form_hidden'))
00105 {
00106 function form_hidden($name, $value = '')
00107 {
00108 if ( ! is_array($name))
00109 {
00110 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
00111 }
00112
00113 $form = '';
00114 foreach ($name as $name => $value)
00115 {
00116 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
00117 }
00118
00119 return $form;
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 if ( ! function_exists('form_input'))
00135 {
00136 function form_input($data = '', $value = '', $extra = '')
00137 {
00138 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
00139
00140 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 if ( ! function_exists('form_password'))
00158 {
00159 function form_password($data = '', $value = '', $extra = '')
00160 {
00161 if ( ! is_array($data))
00162 {
00163 $data = array('name' => $data);
00164 }
00165
00166 $data['type'] = 'password';
00167 return form_input($data, $value, $extra);
00168 }
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 if ( ! function_exists('form_upload'))
00185 {
00186 function form_upload($data = '', $value = '', $extra = '')
00187 {
00188 if ( ! is_array($data))
00189 {
00190 $data = array('name' => $data);
00191 }
00192
00193 $data['type'] = 'file';
00194 return form_input($data, $value, $extra);
00195 }
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 if ( ! function_exists('form_textarea'))
00210 {
00211 function form_textarea($data = '', $value = '', $extra = '')
00212 {
00213 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
00214
00215 if ( ! is_array($data) OR ! isset($data['value']))
00216 {
00217 $val = $value;
00218 }
00219 else
00220 {
00221 $val = $data['value'];
00222 unset($data['value']);
00223 }
00224
00225 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 if ( ! function_exists('form_dropdown'))
00242 {
00243 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
00244 {
00245 if ( ! is_array($selected))
00246 {
00247 $selected = array($selected);
00248 }
00249
00250 if ($extra != '') $extra = ' '.$extra;
00251
00252 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
00253
00254 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
00255
00256 foreach ($options as $key => $val)
00257 {
00258 $key = (string) $key;
00259 $val = (string) $val;
00260
00261 $sel = (in_array($key, $selected))?' selected="selected"':'';
00262
00263 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
00264 }
00265
00266 $form .= '</select>';
00267
00268 return $form;
00269 }
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 if ( ! function_exists('form_checkbox'))
00285 {
00286 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
00287 {
00288 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
00289
00290 if (is_array($data) AND array_key_exists('checked', $data))
00291 {
00292 $checked = $data['checked'];
00293
00294 if ($checked == FALSE)
00295 {
00296 unset($data['checked']);
00297 }
00298 else
00299 {
00300 $data['checked'] = 'checked';
00301 }
00302 }
00303
00304 if ($checked == TRUE)
00305 $defaults['checked'] = 'checked';
00306 else
00307 unset($defaults['checked']);
00308
00309 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
00310 }
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 if ( ! function_exists('form_radio'))
00326 {
00327 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
00328 {
00329 if ( ! is_array($data))
00330 {
00331 $data = array('name' => $data);
00332 }
00333
00334 $data['type'] = 'radio';
00335 return form_checkbox($data, $value, $checked, $extra);
00336 }
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 if ( ! function_exists('form_submit'))
00351 {
00352 function form_submit($data = '', $value = '', $extra = '')
00353 {
00354 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
00355
00356 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
00357 }
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 if ( ! function_exists('form_reset'))
00372 {
00373 function form_reset($data = '', $value = '', $extra = '')
00374 {
00375 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
00376
00377 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
00378 }
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 if ( ! function_exists('form_button'))
00393 {
00394 function form_button($data = '', $content = '', $extra = '')
00395 {
00396 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
00397
00398 if ( is_array($data) AND isset($data['content']))
00399 {
00400 $content = $data['content'];
00401 unset($data['content']);
00402 }
00403
00404 return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>\n";
00405 }
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419 if ( ! function_exists('form_label'))
00420 {
00421 function form_label($label_text = '', $id = '', $attributes = array())
00422 {
00423
00424 $label = '<label';
00425
00426 if ($id != '')
00427 {
00428 $label .= " for=\"$id\"";
00429 }
00430
00431 if (is_array($attributes) AND count($attributes) > 0)
00432 {
00433 foreach ($attributes as $key => $val)
00434 {
00435 $label .= ' '.$key.'="'.$val.'"';
00436 }
00437 }
00438
00439 $label .= ">$label_text</label>";
00440
00441 return $label;
00442 }
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 if ( ! function_exists('form_fieldset'))
00458 {
00459 function form_fieldset($legend_text = '', $attributes = array())
00460 {
00461 $fieldset = "<fieldset";
00462
00463 $fieldset .= _attributes_to_string($attributes, FALSE);
00464
00465 $fieldset .= ">\n";
00466
00467 if ($legend_text != '')
00468 {
00469 $fieldset .= "<legend>$legend_text</legend>\n";
00470 }
00471
00472 return $fieldset;
00473 }
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 if ( ! function_exists('form_fieldset_close'))
00486 {
00487 function form_fieldset_close($extra = '')
00488 {
00489 return "</fieldset>\n".$extra;
00490 }
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 if ( ! function_exists('form_close'))
00503 {
00504 function form_close($extra = '')
00505 {
00506 return "</form>\n".$extra;
00507 }
00508 }
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 if ( ! function_exists('form_prep'))
00522 {
00523 function form_prep($str = '')
00524 {
00525 if ($str === '')
00526 {
00527 return '';
00528 }
00529
00530 $temp = '__TEMP_AMPERSANDS__';
00531
00532
00533
00534 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
00535 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
00536
00537 $str = htmlspecialchars($str);
00538
00539
00540 $str = str_replace(array("'", '"'), array("'", """), $str);
00541
00542
00543 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
00544 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
00545
00546 return $str;
00547 }
00548 }
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 if ( ! function_exists('parse_form_attributes'))
00563 {
00564 function parse_form_attributes($attributes, $default)
00565 {
00566 if (is_array($attributes))
00567 {
00568 foreach ($default as $key => $val)
00569 {
00570 if (isset($attributes[$key]))
00571 {
00572 $default[$key] = $attributes[$key];
00573 unset($attributes[$key]);
00574 }
00575 }
00576
00577 if (count($attributes) > 0)
00578 {
00579 $default = array_merge($default, $attributes);
00580 }
00581 }
00582
00583 $att = '';
00584 foreach ($default as $key => $val)
00585 {
00586 if ($key == 'value')
00587 {
00588 $val = form_prep($val);
00589 }
00590
00591 $att .= $key . '="' . $val . '" ';
00592 }
00593
00594 return $att;
00595 }
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610 if ( ! function_exists('_attributes_to_string'))
00611 {
00612 function _attributes_to_string($attributes, $formtag = FALSE)
00613 {
00614 if (is_string($attributes) AND strlen($attributes) > 0)
00615 {
00616 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
00617 {
00618 $attributes .= ' method="post"';
00619 }
00620
00621 return ' '.$attributes;
00622 }
00623
00624 if (is_object($attributes) AND count($attributes) > 0)
00625 {
00626 $attributes = (array)$attributes;
00627 }
00628
00629 if (is_array($attributes) AND count($attributes) > 0)
00630 {
00631 $atts = '';
00632
00633 if ( ! isset($attributes['method']) AND $formtag === TRUE)
00634 {
00635 $atts .= ' method="post"';
00636 }
00637
00638 foreach ($attributes as $key => $val)
00639 {
00640 $atts .= ' '.$key.'="'.$val.'"';
00641 }
00642
00643 return $atts;
00644 }
00645 }
00646 }
00647
00648
00649