[ Index ]
 

Code source de PHPonTrax 2.6.6-svn

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/vendor/trax/action_view/helpers/ -> form_helper.php (source)

   1  <?php
   2  /**
   3   *  File containing the FormHelper class
   4   *
   5   *  (PHP 5)
   6   *
   7   *  @package PHPonTrax
   8   *  @version $Id: form_helper.php 243 2006-08-23 05:25:24Z john $
   9   *  @copyright (c) 2005 John Peterson
  10   *
  11   *  Permission is hereby granted, free of charge, to any person obtaining
  12   *  a copy of this software and associated documentation files (the
  13   *  "Software"), to deal in the Software without restriction, including
  14   *  without limitation the rights to use, copy, modify, merge, publish,
  15   *  distribute, sublicense, and/or sell copies of the Software, and to
  16   *  permit persons to whom the Software is furnished to do so, subject to
  17   *  the following conditions:
  18   *
  19   *  The above copyright notice and this permission notice shall be
  20   *  included in all copies or substantial portions of the Software.
  21   *
  22   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23   *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24   *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25   *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26   *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27   *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28   *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29   */
  30  
  31  /**
  32   *  @todo Document this class
  33   */
  34  class FormHelper extends Helpers {
  35  
  36      /**
  37       *  Default attributes for input fields
  38       *  @var string[]
  39       */
  40      private $default_field_options = array();
  41  
  42      /**
  43       *  Default attributes for radio buttons
  44       *  @var string[]
  45       */
  46      private $default_radio_options = array();
  47  
  48      /**
  49       *  Default attributes for text areas
  50       *  @var string[]
  51       */
  52      private $default_text_area_options = array();
  53  
  54      /**
  55       *  Default attributes for dates
  56       *  @var string[]
  57       */
  58      private $default_date_options = array();
  59  
  60      /**
  61       *  @todo Document this method
  62       *  @uses default_date_options
  63       *  @uses default_field_options
  64       *  @uses default_radio_options
  65       *  @uses default_text_area_options
  66       */
  67      function __construct($object_name, $attribute_name) {
  68          parent::__construct($object_name, $attribute_name);
  69  
  70          //  Set default attributes for input fields
  71          $this->default_field_options = 
  72              array_key_exists('DEFAULT_FIELD_OPTIONS',$GLOBALS)
  73              ? $GLOBALS['DEFAULT_FIELD_OPTIONS']
  74              : array("size" => 30);
  75  
  76          //  Set default attributes for radio buttons
  77          $this->default_radio_options =
  78              array_key_exists('DEFAULT_RADIO_OPTIONS',$GLOBALS)
  79              ? $GLOBALS['DEFAULT_RADIO_OPTIONS']
  80              : array();
  81  
  82          //  Set default attributes for text areas
  83          $this->default_text_area_options =
  84              array_key_exists('DEFAULT_TEXT_AREA_OPTIONS',$GLOBALS)
  85              ? $GLOBALS['DEFAULT_TEXT_AREA_OPTIONS']
  86              : array("cols" => 40, "rows" => 20);
  87  
  88          //  Set default attributes for dates
  89          $this->default_date_options =
  90              array_key_exists('DEFAULT_Date_OPTIONS',$GLOBALS)
  91              ? $GLOBALS['DEFAULT_DATE_OPTIONS']
  92              : array(":discard_type" => true);
  93      }
  94  
  95      /**
  96       *  @todo Document this method
  97       */
  98      function tag_name() {
  99          return "{$this->object_name}[{$this->attribute_name}]";
 100      }
 101  
 102      /**
 103       *  @todo Document this method
 104       */
 105      function tag_name_with_index($index) {
 106          return "{$this->object_name}[{$index}][{$this->attribute_name}]";
 107      }
 108  
 109      /**
 110       *  @todo Document this method
 111       */
 112      function tag_id() {
 113          return "{$this->object_name}_{$this->attribute_name}";
 114      }
 115  
 116      /**
 117       *  @todo Document this method
 118       */
 119      function tag_id_with_index($index) {
 120          return "{$this->object_name}_{$index}_{$this->attribute_name}";
 121      }
 122  
 123      /**
 124       *  @todo Document this method
 125       *  @param string[]
 126       *  @uses auto_index
 127       *  @uses tag_id
 128       *  @uses tag_name
 129       *  @uses tag_id_with_index()
 130       *  @uses tag_name_with_index()
 131       */
 132      function add_default_name_and_id($options) {  
 133          $name_option_exists = array_key_exists('name', $options);    
 134             if(array_key_exists("index", $options)) {
 135              $options['name'] = $name_option_exists
 136                  ? $options['name']
 137                  : $this->tag_name_with_index($options['index']);
 138              $options['id'] = array_key_exists('id', $options)
 139                  ? $options['id']
 140                  : $this->tag_id_with_index($options['index']);
 141              unset($options['index']);
 142          } elseif($this->auto_index) {
 143              $options['name'] = $name_option_exists
 144                  ? $options['name']
 145                  : $this->tag_name_with_index($this->auto_index);
 146              $options['id'] = array_key_exists('id', $options)
 147                  ? $options['id']
 148                  : $this->tag_id_with_index($this->auto_index);
 149          } else {
 150              $options['name'] = $name_option_exists
 151                  ? $options['name']
 152                  : $this->tag_name();
 153              $options['id'] = array_key_exists('id', $options)
 154                  ? $options['id']
 155                  : $this->tag_id();
 156          }
 157          if(array_key_exists('multiple', $options) && !$name_option_exists) {
 158              $options['name'] .= "[]";           
 159          }
 160          return $options;
 161      }
 162  
 163      /**
 164       *  Generate an HTML or XML input tag with optional attributes
 165       *
 166       *  @param string  Type of input field (<samp>'text'</samp>,
 167       *                 <samp>'password'</samp>, <samp>'hidden'</samp>
 168       *                 <i>etc.</i>)
 169       *  @param string[] Attributes to apply to the input tag:<br>
 170       *    <samp>array('attr1' => 'value1'[, 'attr2' => 'value2']...)</samp>
 171       *  @return string
 172       *   <samp><input type="</samp><i>type</i>
 173       *   <samp>" maxlength="</samp><i>maxlength</i><samp>" .../>\n</samp>
 174       *  @uses add_default_name_and_id()
 175       *  @uses attribute_name
 176       *  @uses error_wrapping
 177       *  @uses default_field_options
 178       *  @uses object()
 179       *  @uses tag()
 180       *  @uses value()
 181       */
 182      function to_input_field_tag($field_type, $options = array()) {
 183          $default_size = array_key_exists("maxlength", $options)
 184              ? $options["maxlength"] : $this->default_field_options['size'];
 185          $options["size"] = array_key_exists("size", $options)
 186              ? $options["size"]: $default_size;
 187          $options = array_merge($this->default_field_options, $options);
 188          if($field_type == "hidden") {
 189              unset($options["size"]);
 190          }
 191          $options["type"] = $field_type;
 192          if($field_type != "file") {
 193              $options["value"] = array_key_exists("value", $options)
 194                  ? $options["value"] : $this->value();
 195          }
 196          $options = $this->add_default_name_and_id($options);
 197          return $this->error_wrapping(
 198                       $this->tag("input", $options),
 199                       @array_key_exists($this->attribute_name,
 200                                        $this->object()->errors)
 201                       ? true : false);
 202      }
 203  
 204      /**
 205       *  @todo Document this method
 206       *  @uses add_default_name_and_id()
 207       */
 208      function to_radio_button_tag($tag_value, $options = array()) {
 209          $options = array_merge($this->default_radio_options, $options);
 210          $options["type"] = "radio";
 211          $options["value"] = $tag_value;
 212          if($this->value() == $tag_value) {
 213              $options["checked"] = "checked";
 214          }
 215          $pretty_tag_value = preg_replace('/\s/', "_", preg_replace('/\W/', "", strtolower($tag_value)));
 216          $options["id"] = $this->auto_index ?
 217              "{$this->object_name}_{$this->auto_index}_{$this->attribute_name}_{$pretty_tag_value}" :
 218              "{$this->object_name}_{$this->attribute_name}_{$pretty_tag_value}";
 219          $options = $this->add_default_name_and_id($options);
 220          return $this->error_wrapping($this->tag("input", $options),$this->object()->errors[$this->attribute_name]);
 221      }
 222  
 223      /**
 224       *  @todo Document this method
 225       *  @uses add_default_name_and_id()
 226       */
 227      function to_text_area_tag($options = array()) {
 228          if (array_key_exists("size", $options)) {
 229              $size = explode('x', $options["size"]);
 230              $options["cols"] = reset($size);
 231              $options["rows"] = end($size);
 232              unset($options["size"]);
 233          }
 234          $options = array_merge($this->default_text_area_options, $options);
 235          $options = $this->add_default_name_and_id($options);
 236          return $this->error_wrapping(
 237             $this->content_tag("textarea",
 238                                htmlspecialchars($this->value(), ENT_COMPAT),
 239                                $options),
 240             array_key_exists($this->attribute_name,$this->object()->errors)
 241             ? $this->object()->errors[$this->attribute_name] : false);
 242      }
 243  
 244      /**
 245       *  @todo Document this method
 246       *  @uses add_default_name_and_id()
 247       */
 248      function to_check_box_tag($options = array(), $checked_value = "1", $unchecked_value = "0") {
 249          $options["type"] = "checkbox";
 250          $options["value"] = $checked_value;
 251          switch(gettype($this->value())) {
 252              case 'boolean':
 253                  $checked = $this->value();
 254                  break;
 255              case 'NULL':
 256                  $checked = false;
 257                  break;
 258              case 'integer':
 259                  $checked = ($this->value() != 0);
 260                  break;
 261              case 'string':
 262                  $checked = ($this->value() == $checked_value);
 263                  break;
 264              default:
 265                  $checked = ($this->value() != 0);
 266          }
 267  
 268          if ($checked || $options["checked"] == "checked") {
 269              $options["checked"] = "checked";
 270          } else {
 271              unset($options["checked"]);
 272          }
 273  
 274          $options = $this->add_default_name_and_id($options);
 275          return $this->error_wrapping($this->tag("input", array("name" => $options["name"], "type" => "hidden", "value" => $unchecked_value)) . $this->tag("input", $options),$this->object()->errors[$this->attribute_name]);
 276      }
 277  
 278      /**
 279       *  @todo Document this method
 280       *  @uses add_default_name_and_id()
 281       */
 282      function to_boolean_select_tag($options = array()) {
 283          $options = $this->add_default_name_and_id($options);
 284          $tag_text = "<select ";
 285          $tag_text .= $this->tag_options($options);
 286          $tag_text .= ">\n";
 287          $tag_text .= "<option value=\"0\"";
 288          if($this->value() == false) {
 289              $tag_text .= " selected";
 290          }
 291          $tag_text .= ">False</option>\n";
 292          $tag_text .= "<option value=\"1\"";
 293          if($this->value()) {
 294              $tag_text .= " selected";
 295          }
 296          $tag_text .= ">True</option>\n";
 297          $tag_text .= "</select>\n";
 298          return $this->error_wrapping($tag_text,$this->object()->errors[$this->attribute_name]);
 299      }
 300      
 301  }
 302  
 303  
 304  /**
 305   *  Generate HTML/XML for <input type="text" /> in a view file
 306   *
 307   *  Example: In the view file, code
 308   *           <code><?= text_field("Person", "fname"); ?></code>
 309   *  Result: <input id="Person_fname" name="Person[fname]" size="30" type="text" value="$Person->fname" />
 310   *  @param string  Class name of the object being processed
 311   *  @param string  Name of attribute in the object being processed
 312   *  @param string[]  Attributes to apply to the generated input tag as:<br>
 313   *    <samp>array('attr1' => 'value1'[, 'attr2' => 'value2']...)</samp>
 314   *  @uses FormHelper::to_input_field_tag()
 315   */
 316  function text_field($object, $field, $options = array()) {
 317      $form = new FormHelper($object, $field);
 318      return $form->to_input_field_tag("text", $options);
 319  }
 320  
 321  /**
 322   *  Works just like text_field, but returns a input tag of the "password" type instead.
 323   * Example: password_field("user", "password");
 324   *  Result: <input type="password" id="user_password" name="user[password]" value="$user->password" />
 325   *  @uses FormHelper::to_input_field_tag()
 326   */
 327  function password_field($object, $field, $options = array()) {
 328      $form = new FormHelper($object, $field);
 329      return $form->to_input_field_tag("password", $options);
 330  }
 331  
 332  /**
 333   *  Works just like text_field, but returns a input tag of the "hidden" type instead.
 334   *  Example: hidden_field("post", "title");
 335   *  Result: <input type="hidden" id="post_title" name="post[title]" value="$post->title" />
 336   *  @uses FormHelper::to_input_field_tag()
 337   */
 338  function hidden_field($object, $field, $options = array()) {
 339      $form = new FormHelper($object, $field);
 340      return $form->to_input_field_tag("hidden", $options);
 341  }
 342  
 343  /**
 344   * Works just like text_field, but returns a input tag of the "file" type instead, which won't have any default value.
 345   *  @uses FormHelper::to_input_field_tag()
 346   */
 347  function file_field($object, $field, $options = array()) {
 348      $form = new FormHelper($object, $field);
 349      return $form->to_input_field_tag("file", $options);
 350  }
 351  
 352  /**
 353   *  Example: text_area("post", "body", array("cols" => 20, "rows" => 40));
 354   *  Result: <textarea cols="20" rows="40" id="post_body" name="post[body]">$post->body</textarea>
 355   *  @uses FormHelper::to_text_area_tag()
 356   */
 357  function text_area($object, $field, $options = array()) {
 358      $form = new FormHelper($object, $field);
 359      return $form->to_text_area_tag($options);
 360  }
 361  
 362  /**
 363   * Returns a checkbox tag tailored for accessing a specified attribute (identified by $field) on an object
 364   * assigned to the template (identified by $object). It's intended that $field returns an integer and if that
 365   * integer is above zero, then the checkbox is checked. Additional $options on the input tag can be passed as an
 366   * array with $options. The $checked_value defaults to 1 while the default $unchecked_value
 367   * is set to 0 which is convenient for boolean values. Usually unchecked checkboxes don't post anything.
 368   * We work around this problem by adding a hidden value with the same name as the checkbox.
 369  #
 370   * Example: Imagine that $post->validated is 1:
 371   *   check_box("post", "validated");
 372   * Result:
 373   *   <input type="checkbox" id="post_validate" name="post[validated] value="1" checked="checked" />
 374   *   <input name="post[validated]" type="hidden" value="0" />
 375  #
 376   * Example: Imagine that $puppy->gooddog is no:
 377   *   check_box("puppy", "gooddog", array(), "yes", "no");
 378   * Result:
 379   *     <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog] value="yes" />
 380   *     <input name="puppy[gooddog]" type="hidden" value="no" />
 381     *  @uses FormHelper::to_check_box_tag()
 382   */
 383  function check_box($object, $field, $options = array(), $checked_value = "1", $unchecked_value = "0") {
 384      $form = new FormHelper($object, $field);
 385      return $form->to_check_box_tag($options, $checked_value, $unchecked_value);
 386  }
 387  
 388  /**
 389   * Returns a radio button tag for accessing a specified attribute (identified by $field) on an object
 390   * assigned to the template (identified by $object). If the current value of $field is $tag_value the
 391   * radio button will be checked. Additional $options on the input tag can be passed as a
 392   * hash with $options.
 393   * Example: Imagine that $post->category is "trax":
 394   *   radio_button("post", "category", "trax");
 395   *   radio_button("post", "category", "java");
 396   * Result:
 397   *     <input type="radio" id="post_category" name="post[category] value="trax" checked="checked" />
 398   *     <input type="radio" id="post_category" name="post[category] value="java" />
 399   *  @uses FormHelper::to_radio_button_tag()
 400   */
 401  function radio_button($object, $field, $tag_value, $options = array()) {
 402      $form = new FormHelper($object, $field);
 403      return $form->to_radio_button_tag($tag_value, $options);
 404  }
 405  
 406  /**
 407   *  Make a new FormHelper object and call its to_boolean_select_tag method
 408   *  @uses FormHelper::to_boolean_select_tag()
 409   */
 410  function boolean_select($object, $field, $options = array()) {
 411      $form = new FormHelper($object, $field);
 412      return $form->to_boolean_select_tag($options);        
 413  }
 414  
 415  // -- set Emacs parameters --
 416  // Local variables:
 417  // tab-width: 4
 418  // c-basic-offset: 4
 419  // c-hanging-comment-ender-p: nil
 420  // indent-tabs-mode: nil
 421  // End:
 422  ?>


Généré le : Sun Feb 25 20:04:38 2007 par Balluche grâce à PHPXref 0.7