form_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Form Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/form_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Form Declaration
00032  *
00033  * Creates the opening portion of the form.
00034  *
00035  * @access      public
00036  * @param       string  the URI segments of the form destination
00037  * @param       array   a key/value pair of attributes
00038  * @param       array   a key/value pair hidden data
00039  * @return      string
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  * Form Declaration - Multipart type
00073  *
00074  * Creates the opening portion of the form, but with "multipart/form-data".
00075  *
00076  * @access      public
00077  * @param       string  the URI segments of the form destination
00078  * @param       array   a key/value pair of attributes
00079  * @param       array   a key/value pair hidden data
00080  * @return      string
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  * Hidden Input Field
00095  *
00096  * Generates hidden fields.  You can pass a simple key/value string or an associative
00097  * array with multiple values.
00098  *
00099  * @access      public
00100  * @param       mixed
00101  * @param       string
00102  * @return      string
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  * Text Input Field
00127  *
00128  * @access      public
00129  * @param       mixed
00130  * @param       string
00131  * @param       string
00132  * @return      string
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  * Password Field
00148  *
00149  * Identical to the input function but adds the "password" type
00150  *
00151  * @access      public
00152  * @param       mixed
00153  * @param       string
00154  * @param       string
00155  * @return      string
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  * Upload Field
00175  *
00176  * Identical to the input function but adds the "file" type
00177  *
00178  * @access      public
00179  * @param       mixed
00180  * @param       string
00181  * @param       string
00182  * @return      string
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  * Textarea field
00202  *
00203  * @access      public
00204  * @param       mixed
00205  * @param       string
00206  * @param       string
00207  * @return      string
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']); // textareas don't use the value attribute
00223                 }
00224                 
00225                 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
00226         }
00227 }
00228         
00229 // ------------------------------------------------------------------------
00230 
00231 /**
00232  * Drop-down Menu
00233  *
00234  * @access      public
00235  * @param       string
00236  * @param       array
00237  * @param       string
00238  * @param       string
00239  * @return      string
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  * Checkbox Field
00276  *
00277  * @access      public
00278  * @param       mixed
00279  * @param       string
00280  * @param       bool
00281  * @param       string
00282  * @return      string
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  * Radio Button
00317  *
00318  * @access      public
00319  * @param       mixed
00320  * @param       string
00321  * @param       bool
00322  * @param       string
00323  * @return      string
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  * Submit Button
00343  *
00344  * @access      public
00345  * @param       mixed
00346  * @param       string
00347  * @param       string
00348  * @return      string
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  * Reset Button
00364  *
00365  * @access      public
00366  * @param       mixed
00367  * @param       string
00368  * @param       string
00369  * @return      string
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  * Form Button
00385  *
00386  * @access      public
00387  * @param       mixed
00388  * @param       string
00389  * @param       string
00390  * @return      string
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']); // content is not an attribute
00402                 }
00403                 
00404                 return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>\n";
00405         }
00406 }
00407 
00408 // ------------------------------------------------------------------------
00409 
00410 /**
00411  * Form Label Tag
00412  *
00413  * @access      public
00414  * @param       string  The text to appear onscreen
00415  * @param       string  The id the label applies to
00416  * @param       string  Additional attributes
00417  * @return      string
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  * Fieldset Tag
00448  *
00449  * Used to produce <fieldset><legend>text</legend>.  To close fieldset
00450  * use form_fieldset_close()
00451  *
00452  * @access      public
00453  * @param       string  The legend text
00454  * @param       string  Additional attributes
00455  * @return      string
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  * Fieldset Close Tag
00480  *
00481  * @access      public
00482  * @param       string
00483  * @return      string
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  * Form Close Tag
00497  *
00498  * @access      public
00499  * @param       string
00500  * @return      string
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  * Form Prep
00514  *
00515  * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
00516  *
00517  * @access      public
00518  * @param       string
00519  * @return      string
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                 // Replace entities to temporary markers so that 
00533                 // htmlspecialchars won't mess them up
00534                 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
00535                 $str = preg_replace("/&(\w+);/",  "$temp\\1;", $str);
00536 
00537                 $str = htmlspecialchars($str);
00538 
00539                 // In case htmlspecialchars misses these.
00540                 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);    
00541         
00542                 // Decode the temp markers back to entities
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  * Parse the form attributes
00554  *
00555  * Helper function used by some of the form helpers
00556  *
00557  * @access      private
00558  * @param       array
00559  * @param       array
00560  * @return      string
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  * Attributes To String
00602  *
00603  * Helper function used by some of the form helpers
00604  *
00605  * @access      private
00606  * @param       mixed
00607  * @param       bool
00608  * @return      string
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 /* End of file form_helper.php */
00649 /* Location: ./system/helpers/form_helper.php */