Source for file form.php

Documentation is available at form.php

  1. <?php
  2. /**
  3.  *  Base include file for SimpleTest.
  4.  *  @package    SimpleTest
  5.  *  @subpackage WebTester
  6.  *  @version    $Id: form.php 1672 2008-03-02 04:47:34Z edwardzyang $
  7.  */
  8.     
  9. /**#@+
  10.  * include SimpleTest files
  11.  */
  12. require_once(dirname(__FILE__'/tag.php');
  13. require_once(dirname(__FILE__'/encoding.php');
  14. require_once(dirname(__FILE__'/selector.php');
  15. /**#@-*/
  16.  
  17.  *    Form tag class to hold widget values.
  18.  *    @package SimpleTest
  19.  *    @subpackage WebTester
  20.  */
  21. class SimpleForm {
  22.     var $_method;
  23.     var $_action;
  24.     var $_encoding;
  25.     var $_default_target;
  26.     var $_id;
  27.     var $_buttons;
  28.     var $_images;
  29.     var $_widgets;
  30.     var $_radios;
  31.     var $_checkboxes;
  32.     
  33.     /**
  34.      *    Starts with no held controls/widgets.
  35.      *    @param SimpleTag $tag        Form tag to read.
  36.      *    @param SimplePage $page      Holding page.
  37.      */
  38.     function SimpleForm($tag&$page{
  39.         $this->_method = $tag->getAttribute('method');
  40.         $this->_action = $this->_createAction($tag->getAttribute('action')$page);
  41.         $this->_encoding = $this->_setEncodingClass($tag);
  42.         $this->_default_target = false;
  43.         $this->_id = $tag->getAttribute('id');
  44.         $this->_buttons = array();
  45.         $this->_images = array();
  46.         $this->_widgets = array();
  47.         $this->_radios = array();
  48.         $this->_checkboxes = array();
  49.     }
  50.     
  51.     /**
  52.      *    Creates the request packet to be sent by the form.
  53.      *    @param SimpleTag $tag        Form tag to read.
  54.      *    @return string               Packet class.
  55.      *    @access private
  56.      */
  57.     function _setEncodingClass($tag{
  58.         if (strtolower($tag->getAttribute('method')) == 'post'{
  59.             if (strtolower($tag->getAttribute('enctype')) == 'multipart/form-data'{
  60.                 return 'SimpleMultipartEncoding';
  61.             }
  62.             return 'SimplePostEncoding';
  63.         }
  64.         return 'SimpleGetEncoding';
  65.     }
  66.     
  67.     /**
  68.      *    Sets the frame target within a frameset.
  69.      *    @param string $frame        Name of frame.
  70.      *    @access public
  71.      */
  72.     function setDefaultTarget($frame{
  73.         $this->_default_target = $frame;
  74.     }
  75.     
  76.     /**
  77.      *    Accessor for method of form submission.
  78.      *    @return string           Either get or post.
  79.      *    @access public
  80.      */
  81.     function getMethod({
  82.         return ($this->_method ? strtolower($this->_method'get');
  83.     }
  84.     
  85.     /**
  86.      *    Combined action attribute with current location
  87.      *    to get an absolute form target.
  88.      *    @param string $action    Action attribute from form tag.
  89.      *    @param SimpleUrl $base   Page location.
  90.      *    @return SimpleUrl        Absolute form target.
  91.      */
  92.     function _createAction($action&$page{
  93.         if (($action === ''|| ($action === false)) {
  94.             return $page->expandUrl($page->getUrl());
  95.         }
  96.         return $page->expandUrl(new SimpleUrl($action));;
  97.     }
  98.     
  99.     /**
  100.      *    Absolute URL of the target.
  101.      *    @return SimpleUrl           URL target.
  102.      *    @access public
  103.      */
  104.     function getAction({
  105.         $url $this->_action;
  106.         if ($this->_default_target && $url->getTarget()) {
  107.             $url->setTarget($this->_default_target);
  108.         }
  109.         return $url;
  110.     }
  111.     
  112.     /**
  113.      *    Creates the encoding for the current values in the
  114.      *    form.
  115.      *    @return SimpleFormEncoding    Request to submit.
  116.      *    @access private
  117.      */
  118.     function _encode({
  119.         $class $this->_encoding;
  120.         $encoding new $class();
  121.         for ($i 0$count count($this->_widgets)$i $count$i++{
  122.             $this->_widgets[$i]->write($encoding);
  123.         }
  124.         return $encoding;
  125.     }
  126.             
  127.     /**
  128.      *    ID field of form for unique identification.
  129.      *    @return string           Unique tag ID.
  130.      *    @access public
  131.      */
  132.     function getId({
  133.         return $this->_id;
  134.     }
  135.     
  136.     /**
  137.      *    Adds a tag contents to the form.
  138.      *    @param SimpleWidget $tag        Input tag to add.
  139.      *    @access public
  140.      */
  141.     function addWidget(&$tag{
  142.         if (strtolower($tag->getAttribute('type')) == 'submit'{
  143.             $this->_buttons[&$tag;
  144.         elseif (strtolower($tag->getAttribute('type')) == 'image'{
  145.             $this->_images[&$tag;
  146.         elseif ($tag->getName()) {
  147.             $this->_setWidget($tag);
  148.         }
  149.     }
  150.     
  151.     /**
  152.      *    Sets the widget into the form, grouping radio
  153.      *    buttons if any.
  154.      *    @param SimpleWidget $tag   Incoming form control.
  155.      *    @access private
  156.      */
  157.     function _setWidget(&$tag{
  158.         if (strtolower($tag->getAttribute('type')) == 'radio'{
  159.             $this->_addRadioButton($tag);
  160.         elseif (strtolower($tag->getAttribute('type')) == 'checkbox'{
  161.             $this->_addCheckbox($tag);
  162.         else {
  163.             $this->_widgets[&$tag;
  164.         }
  165.     }
  166.     
  167.     /**
  168.      *    Adds a radio button, building a group if necessary.
  169.      *    @param SimpleRadioButtonTag $tag   Incoming form control.
  170.      *    @access private
  171.      */
  172.     function _addRadioButton(&$tag{
  173.         if (isset($this->_radios[$tag->getName()])) {
  174.             $this->_widgets[&new SimpleRadioGroup();
  175.             $this->_radios[$tag->getName()count($this->_widgets1;
  176.         }
  177.         $this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
  178.     }
  179.     
  180.     /**
  181.      *    Adds a checkbox, making it a group on a repeated name.
  182.      *    @param SimpleCheckboxTag $tag   Incoming form control.
  183.      *    @access private
  184.      */
  185.     function _addCheckbox(&$tag{
  186.         if (isset($this->_checkboxes[$tag->getName()])) {
  187.             $this->_widgets[&$tag;
  188.             $this->_checkboxes[$tag->getName()count($this->_widgets1;
  189.         else {
  190.             $index $this->_checkboxes[$tag->getName()];
  191.             if (SimpleTestCompatibility::isA($this->_widgets[$index]'SimpleCheckboxGroup')) {
  192.                 $previous &$this->_widgets[$index];
  193.                 $this->_widgets[$index&new SimpleCheckboxGroup();
  194.                 $this->_widgets[$index]->addWidget($previous);
  195.             }
  196.             $this->_widgets[$index]->addWidget($tag);
  197.         }
  198.     }
  199.     
  200.     /**
  201.      *    Extracts current value from form.
  202.      *    @param SimpleSelector $selector   Criteria to apply.
  203.      *    @return string/array              Value(s) as string or null
  204.      *                                       if not set.
  205.      *    @access public
  206.      */
  207.     function getValue($selector{
  208.         for ($i 0$count count($this->_widgets)$i $count$i++{
  209.             if ($selector->isMatch($this->_widgets[$i])) {
  210.                 return $this->_widgets[$i]->getValue();
  211.             }
  212.         }
  213.         foreach ($this->_buttons as $button{
  214.             if ($selector->isMatch($button)) {
  215.                 return $button->getValue();
  216.             }
  217.         }
  218.         return null;
  219.     }
  220.     
  221.     /**
  222.      *    Sets a widget value within the form.
  223.      *    @param SimpleSelector $selector   Criteria to apply.
  224.      *    @param string $value              Value to input into the widget.
  225.      *    @return boolean                   True if value is legal, false
  226.      *                                       otherwise. If the field is not
  227.      *                                       present, nothing will be set.
  228.      *    @access public
  229.      */
  230.     function setField($selector$value$position=false{
  231.         $success false;
  232.         $_position 0;
  233.         for ($i 0$count count($this->_widgets)$i $count$i++{
  234.             if ($selector->isMatch($this->_widgets[$i])) {
  235.                 $_position++;
  236.                 if ($position === false or $_position === (int)$position{
  237.                     if ($this->_widgets[$i]->setValue($value)) {
  238.                         $success true;
  239.                     }
  240.                 }
  241.             }
  242.         }
  243.         return $success;
  244.     }
  245.     
  246.     /**
  247.      *    Used by the page object to set widgets labels to
  248.      *    external label tags.
  249.      *    @param SimpleSelector $selector   Criteria to apply.
  250.      *    @access public
  251.      */
  252.     function attachLabelBySelector($selector$label{
  253.         for ($i 0$count count($this->_widgets)$i $count$i++{
  254.             if ($selector->isMatch($this->_widgets[$i])) {
  255.                 if (method_exists($this->_widgets[$i]'setLabel')) {
  256.                     $this->_widgets[$i]->setLabel($label);
  257.                     return;
  258.                 }
  259.             }
  260.         }
  261.     }
  262.     
  263.     /**
  264.      *    Test to see if a form has a submit button.
  265.      *    @param SimpleSelector $selector   Criteria to apply.
  266.      *    @return boolean                   True if present.
  267.      *    @access public
  268.      */
  269.     function hasSubmit($selector{
  270.         foreach ($this->_buttons as $button{
  271.             if ($selector->isMatch($button)) {
  272.                 return true;
  273.             }
  274.         }
  275.         return false;
  276.     }
  277.     
  278.     /**
  279.      *    Test to see if a form has an image control.
  280.      *    @param SimpleSelector $selector   Criteria to apply.
  281.      *    @return boolean                   True if present.
  282.      *    @access public
  283.      */
  284.     function hasImage($selector{
  285.         foreach ($this->_images as $image{
  286.             if ($selector->isMatch($image)) {
  287.                 return true;
  288.             }
  289.         }
  290.         return false;
  291.     }
  292.     
  293.     /**
  294.      *    Gets the submit values for a selected button.
  295.      *    @param SimpleSelector $selector   Criteria to apply.
  296.      *    @param hash $additional           Additional data for the form.
  297.      *    @return SimpleEncoding            Submitted values or false
  298.      *                                       if there is no such button
  299.      *                                       in the form.
  300.      *    @access public
  301.      */
  302.     function submitButton($selector$additional false{
  303.         $additional $additional $additional array();
  304.         foreach ($this->_buttons as $button{
  305.             if ($selector->isMatch($button)) {
  306.                 $encoding $this->_encode();
  307.                 $button->write($encoding);
  308.                 if ($additional{
  309.                     $encoding->merge($additional);
  310.                 }
  311.                 return $encoding;           
  312.             }
  313.         }
  314.         return false;
  315.     }
  316.         
  317.     /**
  318.      *    Gets the submit values for an image.
  319.      *    @param SimpleSelector $selector   Criteria to apply.
  320.      *    @param integer $x                 X-coordinate of click.
  321.      *    @param integer $y                 Y-coordinate of click.
  322.      *    @param hash $additional           Additional data for the form.
  323.      *    @return SimpleEncoding            Submitted values or false
  324.      *                                       if there is no such button in the
  325.      *                                       form.
  326.      *    @access public
  327.      */
  328.     function submitImage($selector$x$y$additional false{
  329.         $additional $additional $additional array();
  330.         foreach ($this->_images as $image{
  331.             if ($selector->isMatch($image)) {
  332.                 $encoding $this->_encode();
  333.                 $image->write($encoding$x$y);
  334.                 if ($additional{
  335.                     $encoding->merge($additional);
  336.                 }
  337.                 return $encoding;           
  338.             }
  339.         }
  340.         return false;
  341.     }
  342.     
  343.     /**
  344.      *    Simply submits the form without the submit button
  345.      *    value. Used when there is only one button or it
  346.      *    is unimportant.
  347.      *    @return hash           Submitted values.
  348.      *    @access public
  349.      */
  350.     function submit({
  351.         return $this->_encode();
  352.     }
  353. }
  354. ?>

Documentation generated on Sun, 04 May 2008 09:21:37 -0500 by phpDocumentor 1.3.0