[ Index ]
 

Code source de PRADO 3.0.6

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

title

Body

[fermer]

/framework/Web/UI/WebControls/ -> TCheckBox.php (source)

   1  <?php
   2  /**
   3   * TCheckBox class file
   4   *
   5   * @author Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TCheckBox.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Web.UI.WebControls
  11   */
  12  
  13  /**
  14   * TCheckBox class
  15   *
  16   * TCheckBox displays a check box on the page.
  17   * You can specify the caption to display beside the check box by setting
  18   * the {@link setText Text} property.  The caption can appear either on the right
  19   * or left of the check box, which is determined by the {@link setTextAlign TextAlign}
  20   * property.
  21   *
  22   * To determine whether the TCheckBox component is checked, test the {@link getChecked Checked}
  23   * property. The {@link onCheckedChanged OnCheckedChanged} event is raised when
  24   * the {@link getChecked Checked} state of the TCheckBox component changes
  25   * between posts to the server. You can provide an event handler for
  26   * the {@link onCheckedChanged OnCheckedChanged} event to  to programmatically
  27   * control the actions performed when the state of the TCheckBox component changes
  28   * between posts to the server.
  29   *
  30   * If {@link setAutoPostBack AutoPostBack} is set true, changing the check box state
  31   * will cause postback action. And if {@link setCausesValidation CausesValidation}
  32   * is true, validation will also be processed, which can be further restricted within
  33   * a {@link setValidationGroup ValidationGroup}.
  34   *
  35   * Note, {@link setText Text} is rendered as is. Make sure it does not contain unwanted characters
  36   * that may bring security vulnerabilities.
  37   *
  38   * @author Qiang Xue <qiang.xue@gmail.com>
  39   * @version $Id: TCheckBox.php 1397 2006-09-07 07:55:53Z wei $
  40   * @package System.Web.UI.WebControls
  41   * @since 3.0
  42   */
  43  class TCheckBox extends TWebControl implements IPostBackDataHandler, IValidatable
  44  {
  45      /**
  46       * @return string tag name of the button
  47       */
  48  	protected function getTagName()
  49      {
  50          return 'input';
  51      }
  52  
  53      /**
  54       * Loads user input data.
  55       * This method is primarly used by framework developers.
  56       * @param string the key that can be used to retrieve data from the input data collection
  57       * @param array the input data collection
  58       * @return boolean whether the data of the control has been changed
  59       */
  60  	public function loadPostData($key,$values)
  61      {
  62          $checked=$this->getChecked();
  63          if($newChecked=isset($values[$key]))
  64              $this->setValue($values[$key]);
  65          $this->setChecked($newChecked);
  66          return $newChecked!==$checked;
  67      }
  68  
  69      /**
  70       * Raises postdata changed event.
  71       * This method raises {@link onCheckedChanged OnCheckedChanged} event.
  72       * This method is primarly used by framework developers.
  73       */
  74  	public function raisePostDataChangedEvent()
  75      {
  76          if($this->getAutoPostBack() && $this->getCausesValidation())
  77              $this->getPage()->validate($this->getValidationGroup());
  78          $this->onCheckedChanged(null);
  79      }
  80  
  81      /**
  82       * Raises <b>OnCheckedChanged</b> event when {@link getChecked Checked} changes value during postback.
  83       * If you override this method, be sure to call the parent implementation
  84       * so that the event delegates can be invoked.
  85       * @param TEventParameter event parameter to be passed to the event handlers
  86       */
  87  	public function onCheckedChanged($param)
  88      {
  89          $this->raiseEvent('OnCheckedChanged',$this,$param);
  90      }
  91  
  92      /**
  93       * Registers the checkbox to receive postback data during postback.
  94       * This is necessary because a checkbox if unchecked, when postback,
  95       * does not have direct mapping between post data and the checkbox name.
  96       *
  97       * This method overrides the parent implementation and is invoked before render.
  98       * @param mixed event parameter
  99       */
 100  	public function onPreRender($param)
 101      {
 102          parent::onPreRender($param);
 103          if($this->getEnabled(true))
 104              $this->getPage()->registerRequiresPostData($this);
 105      }
 106  
 107      /**
 108       * Returns the value of the property that needs validation.
 109       * @return mixed the property value to be validated
 110       */
 111  	public function getValidationPropertyValue()
 112      {
 113          return $this->getChecked();
 114      }
 115  
 116      /**
 117       * @return string the text caption of the checkbox
 118       */
 119  	public function getText()
 120      {
 121          return $this->getViewState('Text','');
 122      }
 123  
 124      /**
 125       * Sets the text caption of the checkbox.
 126       * @param string the text caption to be set
 127       */
 128  	public function setText($value)
 129      {
 130          $this->setViewState('Text',$value,'');
 131      }
 132  
 133      /**
 134       * @return string the value of the checkbox. Defaults to empty.
 135       */
 136  	public function getValue()
 137      {
 138          return $this->getViewState('Value','');
 139      }
 140  
 141      /**
 142       * @param string the value of the checkbox
 143       */
 144  	public function setValue($value)
 145      {
 146          $this->setViewState('Value',TPropertyValue::ensureString($value),'');
 147      }
 148  
 149      /**
 150       * @return TTextAlign the alignment (Left or Right) of the text caption, defaults to TTextAlign::Right.
 151       */
 152  	public function getTextAlign()
 153      {
 154          return $this->getViewState('TextAlign',TTextAlign::Right);
 155      }
 156  
 157      /**
 158       * @param TTextAlign the alignment of the text caption. Valid values include Left and Right.
 159       */
 160  	public function setTextAlign($value)
 161      {
 162          $this->setViewState('TextAlign',TPropertyValue::ensureEnum($value,'TTextAlign'),TTextAlign::Right);
 163      }
 164  
 165      /**
 166       * @return boolean whether the checkbox is checked
 167       */
 168  	public function getChecked()
 169      {
 170          return $this->getViewState('Checked',false);
 171      }
 172  
 173      /**
 174       * Sets a value indicating whether the checkbox is to be checked or not.
 175       * @param boolean whether the checkbox is to be checked or not.
 176       */
 177  	public function setChecked($value)
 178      {
 179          $this->setViewState('Checked',TPropertyValue::ensureBoolean($value),false);
 180      }
 181  
 182      /**
 183       * @return boolean whether clicking on the checkbox will post the page.
 184       */
 185  	public function getAutoPostBack()
 186      {
 187          return $this->getViewState('AutoPostBack',false);
 188      }
 189  
 190      /**
 191       * Sets a value indicating whether clicking on the checkbox will post the page.
 192       * @param boolean whether clicking on the checkbox will post the page.
 193       */
 194  	public function setAutoPostBack($value)
 195      {
 196          $this->setViewState('AutoPostBack',TPropertyValue::ensureBoolean($value),false);
 197      }
 198  
 199      /**
 200       * @return boolean whether postback event triggered by this checkbox will cause input validation, default is true.
 201       */
 202  	public function getCausesValidation()
 203      {
 204          return $this->getViewState('CausesValidation',true);
 205      }
 206  
 207      /**
 208       * Sets the value indicating whether postback event trigger by this checkbox will cause input validation.
 209       * @param boolean whether postback event trigger by this checkbox will cause input validation.
 210       */
 211  	public function setCausesValidation($value)
 212      {
 213          $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
 214      }
 215  
 216      /**
 217       * @return string the group of validators which the checkbox causes validation upon postback
 218       */
 219  	public function getValidationGroup()
 220      {
 221          return $this->getViewState('ValidationGroup','');
 222      }
 223  
 224      /**
 225       * @param string the group of validators which the checkbox causes validation upon postback
 226       */
 227  	public function setValidationGroup($value)
 228      {
 229          $this->setViewState('ValidationGroup',$value,'');
 230      }
 231  
 232      /**
 233       * Renders the checkbox control.
 234       * This method overrides the parent implementation by rendering a checkbox input element
 235       * and a span element if needed.
 236       * @param THtmlWriter the writer used for the rendering purpose
 237       */
 238  	public function render($writer)
 239      {
 240          $this->getPage()->ensureRenderInForm($this);
 241          $needSpan=false;
 242          if($this->getHasStyle())
 243          {
 244              $this->getStyle()->addAttributesToRender($writer);
 245              $needSpan=true;
 246          }
 247          if(($tooltip=$this->getToolTip())!=='')
 248          {
 249              $writer->addAttribute('title',$tooltip);
 250              $needSpan=true;
 251          }
 252          if($this->getHasAttributes())
 253          {
 254              $attributes=$this->getAttributes();
 255              $value=$attributes->remove('value');
 256              // onclick js should only be added to input tag
 257              $onclick=$attributes->remove('onclick');
 258              if($attributes->getCount())
 259              {
 260                  $writer->addAttributes($attributes);
 261                  $needSpan=true;
 262              }
 263              if($value!==null)
 264                  $attributes->add('value',$value);
 265          }
 266          else
 267              $onclick='';
 268          if($needSpan)
 269              $writer->renderBeginTag('span');
 270          $clientID=$this->getClientID();
 271          if(($text=$this->getText())!=='')
 272          {
 273              if($this->getTextAlign()===TTextAlign::Left)
 274              {
 275                  $this->renderLabel($writer,$clientID,$text);
 276                  $this->renderInputTag($writer,$clientID,$onclick);
 277              }
 278              else
 279              {
 280                  $this->renderInputTag($writer,$clientID,$onclick);
 281                  $this->renderLabel($writer,$clientID,$text);
 282              }
 283          }
 284          else
 285              $this->renderInputTag($writer,$clientID,$onclick);
 286          if($needSpan)
 287              $writer->renderEndTag();
 288      }
 289  
 290      /**
 291       * @return TMap list of attributes to be rendered for label beside the checkbox
 292       */
 293  	public function getLabelAttributes()
 294      {
 295          if($attributes=$this->getViewState('LabelAttributes',null))
 296              return $attributes;
 297          else
 298          {
 299              $attributes=new TAttributeCollection;
 300              $this->setViewState('LabelAttributes',$attributes,null);
 301              return $attributes;
 302          }
 303      }
 304  
 305      /**
 306       * @return TMap list of attributes to be rendered for the checkbox
 307       */
 308  	public function getInputAttributes()
 309      {
 310          if($attributes=$this->getViewState('InputAttributes',null))
 311              return $attributes;
 312          else
 313          {
 314              $attributes=new TAttributeCollection;
 315              $this->setViewState('InputAttributes',$attributes,null);
 316              return $attributes;
 317          }
 318      }
 319  
 320      /**
 321       * @return string the value attribute to be rendered
 322       */
 323  	protected function getValueAttribute()
 324      {
 325          if(($value=$this->getValue())!=='')
 326              return $value;
 327          else
 328          {
 329              $attributes=$this->getViewState('InputAttributes',null);
 330              if($attributes && $attributes->contains('value'))
 331                  return $attributes->itemAt('value');
 332              else if($this->hasAttribute('value'))
 333                  return $this->getAttribute('value');
 334              else
 335                  return '';
 336          }
 337      }
 338  
 339      /**
 340       * Renders a label beside the checkbox.
 341       * @param THtmlWriter the writer for the rendering purpose
 342       * @param string checkbox id
 343       * @param string label text
 344       */
 345  	protected function renderLabel($writer,$clientID,$text)
 346      {
 347          $writer->addAttribute('for',$clientID);
 348          if($attributes=$this->getViewState('LabelAttributes',null))
 349              $writer->addAttributes($attributes);
 350          $writer->renderBeginTag('label');
 351          $writer->write($text);
 352          $writer->renderEndTag();
 353      }
 354  
 355      /**
 356       * Renders a checkbox input element.
 357       * @param THtmlWriter the writer for the rendering purpose
 358       * @param string checkbox id
 359       * @param string onclick js
 360       */
 361  	protected function renderInputTag($writer,$clientID,$onclick)
 362      {
 363          if($clientID!=='')
 364              $writer->addAttribute('id',$clientID);
 365          $writer->addAttribute('type','checkbox');
 366          if(($value=$this->getValueAttribute())!=='')
 367              $writer->addAttribute('value',$value);
 368          if($onclick!=='')
 369              $writer->addAttribute('onclick',$onclick);
 370          if(($uniqueID=$this->getUniqueID())!=='')
 371              $writer->addAttribute('name',$uniqueID);
 372          if($this->getChecked())
 373              $writer->addAttribute('checked','checked');
 374          if(!$this->getEnabled(true))
 375              $writer->addAttribute('disabled','disabled');
 376  
 377          $page=$this->getPage();
 378          if($this->getEnabled(true) && $this->getAutoPostBack() && $page->getClientSupportsJavaScript())
 379              $page->getClientScript()->registerPostBackControl('Prado.WebUI.TCheckBox',$this->getPostBackOptions());
 380  
 381          if(($accesskey=$this->getAccessKey())!=='')
 382              $writer->addAttribute('accesskey',$accesskey);
 383          if(($tabindex=$this->getTabIndex())>0)
 384              $writer->addAttribute('tabindex',"$tabindex");
 385          if($attributes=$this->getViewState('InputAttributes',null))
 386              $writer->addAttributes($attributes);
 387          $writer->renderBeginTag('input');
 388          $writer->renderEndTag();
 389      }
 390  
 391      /**
 392       * Gets the post back options for this checkbox.
 393       * @return array
 394       */
 395  	protected function getPostBackOptions()
 396      {
 397          $options['ID'] = $this->getClientID();
 398          $options['ValidationGroup'] = $this->getValidationGroup();
 399          $options['CausesValidation'] = $this->getCausesValidation();
 400          $options['EventTarget'] = $this->getUniqueID();
 401          return $options;
 402      }
 403  
 404  }
 405  
 406  /**
 407   * TTextAlign class.
 408   * TTextAlign defines the enumerable type for the possible text alignments
 409   *
 410   * The following enumerable values are defined:
 411   * - Left: left aligned
 412   * - Right: right aligned
 413   *
 414   * @author Qiang Xue <qiang.xue@gmail.com>
 415   * @version $Id: TCheckBox.php 1397 2006-09-07 07:55:53Z wei $
 416   * @package System.Web.UI.WebControls
 417   * @since 3.0.4
 418   */
 419  class TTextAlign extends TEnumerable
 420  {
 421      const Left='Left';
 422      const Right='Right';
 423  }
 424  
 425  ?>


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