[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/helpers/ -> form_tag_helper.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionView
  13   * @subpackage Helpers
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  
  20  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php');
  21  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'tag_helper.php');
  22  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'asset_tag_helper.php');
  23  
  24  /**
  25  * Provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like
  26  * FormHelper does. With the FormTagHelper, you provide the names and values yourself.
  27  *
  28  * NOTE: The html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>'disabled' => true</tt>
  29  * will give <tt>disabled="disabled"</tt>.
  30  */
  31  
  32  class FormTagHelper  extends AkActionViewHelper
  33  {
  34      
  35      /**
  36        * Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
  37        * $controller->urlFor. The method for the form defaults to POST.
  38        *
  39        * Options:
  40        * * <tt>'multipart'</tt> - If set to true, the enctype is set to "multipart/form-data".
  41        * * <tt>'method'</tt> - The method to use when submitting the form, usually either "get" or "post".
  42        */
  43      function form_tag($url_for_options = array(), $options = array())
  44      {
  45          $html_options = array_merge(array('method'=>'post'), $options);
  46          if(!empty($html_options['multipart'])){
  47              $html_options['enctype'] = 'multipart/form-data';
  48              unset($html_options['multipart']);
  49          }
  50          
  51          // we need to avoid double ampersand scaping when calling TagHelper::tag method
  52          $html_options['action'] = str_replace('&amp;', '&', $this->_controller->urlFor($url_for_options)); 
  53          
  54  
  55          return TagHelper::tag('form', $html_options, true);
  56      }
  57  
  58      function start_form_tag($url_for_options = array(), $options = array())
  59      {
  60          return $this->form_tag($url_for_options, $options);
  61      }
  62  
  63  
  64      /**
  65        * Outputs '</form>'
  66        */
  67      function end_form_tag()
  68      {
  69          return '</form>';
  70      }
  71  
  72      /**
  73        * Creates a dropdown selection box, or if the <tt>'multiple'</tt> option is set to true, a multiple
  74        * choice selection box.
  75        *
  76        * Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or
  77        * associated records.
  78        *
  79        * <tt>option_tags</tt> is a string containing the option tags for the select box:
  80        *   # Outputs <select id="people" name="people"><option>David</option></select>
  81        *  $form_tag_helper->select_tag('people', '<option>David</option>');
  82        *
  83        * Options:
  84        * * <tt>'multiple'</tt> - If set to true the selection will allow multiple choices.
  85        */
  86      function select_tag($name, $option_tags = null, $options = array())
  87      {
  88          return TagHelper::content_tag('select', $option_tags, array_merge(array('name'=> $name, 'id' => trim(AkInflector::underscore($name),'_')), $options));
  89      }
  90  
  91      /**
  92        * Creates a standard text field.
  93        *
  94        * Options:
  95        * * <tt>'disabled'</tt> - If set to true, the user will not be able to use this input.
  96        * * <tt>'size'</tt> - The number of visible characters that will fit in the input.
  97        * * <tt>'maxlength'</tt> - The maximum number of characters that the browser will allow the user to enter.
  98        * 
  99        * An array of standard HTML options for the tag.
 100        */
 101      function text_field_tag($name, $value = null, $options = array())
 102      {
 103          return TagHelper::tag('input', array_merge(array('type'=>'text','name'=>$name,'id'=>trim(AkInflector::underscore($name),'_'),'value'=>$value), $options));
 104      }
 105  
 106      /**
 107        * Creates a hidden field.
 108        *
 109        * Takes the same options as text_field_tag
 110        */
 111      function hidden_field_tag($name, $value = null, $options = array())
 112      {
 113          return $this->text_field_tag($name, $value, array_merge($options,array('type'=>'hidden')));
 114      }
 115  
 116      /**
 117        * Creates a file upload field.
 118        *
 119        * If you are using file uploads then you will also need to set the multipart option for the form:
 120        *   <?= $form_tag_helper->form_tag(array('action'=>'post'),array('multipart'=>true)); ?>
 121        *     <label for="file">File to Upload</label> <?= $form_tag_helper->file_field_tag('file'); ?>
 122        *     <?= $form_tag_helper->submit_tag(); ?>
 123        *   <?= $form_tag_helper->end_form_tag(); ?>
 124        */
 125      function file_field_tag($name, $options = array())
 126      {
 127          return $this->text_field_tag($name, null, array_merge($options,array('type'=>'file')));
 128      }
 129  
 130      /**
 131        * Creates a password field.
 132        *
 133        * Takes the same options as text_field_tag
 134        */
 135      function password_field_tag($name = 'password', $value = null, $options = array())
 136      {
 137          return $this->text_field_tag($name, $value, array_merge($options,array('type'=>'password')));
 138      }
 139  
 140      /**
 141        * Creates a text input area.
 142        *
 143        * Options:
 144        * * <tt>'size'</tt> - A string specifying the dimensions of the textarea.
 145        *     # Outputs <textarea name="body" id="body" cols="25" rows="10"></textarea>
 146        *     <?= $form_tag_helper->text_area_tag('body', null, array('size'=>'25x10')); ?>
 147        */
 148      function text_area_tag($name, $content = null, $options = array())
 149      {
 150          if(!empty($options['size'])){
 151              list($options['cols'], $options['rows']) = split('x|X| ',trim(str_replace(' ','',$options['size'])));
 152              unset($options['size']);
 153          }
 154          return TagHelper::content_tag('textarea', $content, array_merge(array('name'=>$name,'id'=>$name),$options));
 155      }
 156  
 157      /**
 158        * Creates a check box.
 159        */
 160      function check_box_tag($name, $value = '1', $checked = false, $options = array())
 161      {
 162          $html_options = array_merge(array('type'=>'checkbox','name'=>$name,'id'=>$name,'value'=>$value),$options);
 163          if(!empty($html_options['checked']) || !empty($checked)){
 164              $html_options['checked'] = 'checked';
 165          }
 166          return TagHelper::tag('input', $html_options);
 167      }
 168  
 169      /**
 170        * Creates a radio button.
 171        */
 172      function radio_button_tag($name, $value, $checked = false, $options = array())
 173      {
 174          $html_options = array_merge(array('type'=>'radio','name'=>$name,'id'=>$name,'value'=>$value),$options);
 175          if(!empty($html_options['checked']) || !empty($checked)){
 176              $html_options['checked'] = 'checked';
 177          }
 178          return TagHelper::tag('input', $html_options);
 179      }
 180  
 181      /**
 182        * Creates a submit button with the text <tt>value</tt> as the caption. If options contains a pair with the key of "disable_with",
 183        * then the value will be used to rename a disabled version of the submit button.
 184        */
 185      function submit_tag($value = null, $options = array())
 186      {
 187          $value = empty($value) ? Ak::t('Save changes',array(),'helpers/form') : $value;
 188          if(!empty($options['disable_with'])){
 189              $disable_with = $options['disable_with'];
 190              unset($options['disable_with']);
 191              $options['onclick'] = "this.disabled=true;this.value='".addslashes($disable_with)."';this.form.submit();".@$options["onclick"];
 192          }
 193          return TagHelper::tag('input', array_merge(array('type'=>'submit','name'=>'commit','value'=>$value),$options));
 194      }
 195  
 196      /**
 197        * Displays an image which when clicked will submit the form.
 198        *
 199        * <tt>source</tt> is passed to AssetTagHelper#image_path
 200        */
 201      function image_submit_tag($source, $options = array())
 202      {
 203          return TagHelper::tag('input',array_merge(array('type'=>'image','src'=>$this->_controller->asset_tag_helper->image_path($source)),$options));
 204      }
 205  }
 206  
 207  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6