Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/form.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/tag.php');
00013 require_once(dirname(__FILE__) . '/encoding.php');
00014 require_once(dirname(__FILE__) . '/selector.php');
00022 class SimpleForm {
00023     var $_method;
00024     var $_action;
00025     var $_encoding;
00026     var $_default_target;
00027     var $_id;
00028     var $_buttons;
00029     var $_images;
00030     var $_widgets;
00031     var $_radios;
00032     var $_checkboxes;
00033     
00039     function SimpleForm($tag, &$page) {
00040         $this->_method = $tag->getAttribute('method');
00041         $this->_action = $this->_createAction($tag->getAttribute('action'), $page);
00042         $this->_encoding = $this->_setEncodingClass($tag);
00043         $this->_default_target = false;
00044         $this->_id = $tag->getAttribute('id');
00045         $this->_buttons = array();
00046         $this->_images = array();
00047         $this->_widgets = array();
00048         $this->_radios = array();
00049         $this->_checkboxes = array();
00050     }
00051     
00058     function _setEncodingClass($tag) {
00059         if (strtolower($tag->getAttribute('method')) == 'post') {
00060             if (strtolower($tag->getAttribute('enctype')) == 'multipart/form-data') {
00061                 return 'SimpleMultipartEncoding';
00062             }
00063             return 'SimplePostEncoding';
00064         }
00065         return 'SimpleGetEncoding';
00066     }
00067     
00073     function setDefaultTarget($frame) {
00074         $this->_default_target = $frame;
00075     }
00076     
00082     function getMethod() {
00083         return ($this->_method ? strtolower($this->_method) : 'get');
00084     }
00085     
00093     function _createAction($action, &$page) {
00094         if (($action === '') || ($action === false)) {
00095             return $page->expandUrl($page->getUrl());
00096         }
00097         return $page->expandUrl(new SimpleUrl($action));;
00098     }
00099     
00105     function getAction() {
00106         $url = $this->_action;
00107         if ($this->_default_target && ! $url->getTarget()) {
00108             $url->setTarget($this->_default_target);
00109         }
00110         return $url;
00111     }
00112     
00119     function _encode() {
00120         $class = $this->_encoding;
00121         $encoding = new $class();
00122         for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
00123             $this->_widgets[$i]->write($encoding);
00124         }
00125         return $encoding;
00126     }
00127             
00133     function getId() {
00134         return $this->_id;
00135     }
00136     
00142     function addWidget(&$tag) {
00143         if (strtolower($tag->getAttribute('type')) == 'submit') {
00144             $this->_buttons[] = &$tag;
00145         } elseif (strtolower($tag->getAttribute('type')) == 'image') {
00146             $this->_images[] = &$tag;
00147         } elseif ($tag->getName()) {
00148             $this->_setWidget($tag);
00149         }
00150     }
00151     
00158     function _setWidget(&$tag) {
00159         if (strtolower($tag->getAttribute('type')) == 'radio') {
00160             $this->_addRadioButton($tag);
00161         } elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
00162             $this->_addCheckbox($tag);
00163         } else {
00164             $this->_widgets[] = &$tag;
00165         }
00166     }
00167     
00173     function _addRadioButton(&$tag) {
00174         if (! isset($this->_radios[$tag->getName()])) {
00175             $this->_widgets[] = new SimpleRadioGroup();
00176             $this->_radios[$tag->getName()] = count($this->_widgets) - 1;
00177         }
00178         $this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
00179     }
00180     
00186     function _addCheckbox(&$tag) {
00187         if (! isset($this->_checkboxes[$tag->getName()])) {
00188             $this->_widgets[] = &$tag;
00189             $this->_checkboxes[$tag->getName()] = count($this->_widgets) - 1;
00190         } else {
00191             $index = $this->_checkboxes[$tag->getName()];
00192             if (! SimpleTestCompatibility::isA($this->_widgets[$index], 'SimpleCheckboxGroup')) {
00193                 $previous = &$this->_widgets[$index];
00194                 $this->_widgets[$index] = new SimpleCheckboxGroup();
00195                 $this->_widgets[$index]->addWidget($previous);
00196             }
00197             $this->_widgets[$index]->addWidget($tag);
00198         }
00199     }
00200     
00208     function getValue($selector) {
00209         for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
00210             if ($selector->isMatch($this->_widgets[$i])) {
00211                 return $this->_widgets[$i]->getValue();
00212             }
00213         }
00214         foreach ($this->_buttons as $button) {
00215             if ($selector->isMatch($button)) {
00216                 return $button->getValue();
00217             }
00218         }
00219         return null;
00220     }
00221     
00231     function setField($selector, $value, $position=false) {
00232         $success = false;
00233         $_position = 0;
00234         for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
00235             if ($selector->isMatch($this->_widgets[$i])) {
00236                 $_position++;
00237                 if ($position === false or $_position === (int)$position) {
00238                     if ($this->_widgets[$i]->setValue($value)) {
00239                         $success = true;
00240                     }
00241                 }
00242             }
00243         }
00244         return $success;
00245     }
00246     
00253     function attachLabelBySelector($selector, $label) {
00254         for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
00255             if ($selector->isMatch($this->_widgets[$i])) {
00256                 if (method_exists($this->_widgets[$i], 'setLabel')) {
00257                     $this->_widgets[$i]->setLabel($label);
00258                     return;
00259                 }
00260             }
00261         }
00262     }
00263     
00270     function hasSubmit($selector) {
00271         foreach ($this->_buttons as $button) {
00272             if ($selector->isMatch($button)) {
00273                 return true;
00274             }
00275         }
00276         return false;
00277     }
00278     
00285     function hasImage($selector) {
00286         foreach ($this->_images as $image) {
00287             if ($selector->isMatch($image)) {
00288                 return true;
00289             }
00290         }
00291         return false;
00292     }
00293     
00303     function submitButton($selector, $additional = false) {
00304         $additional = $additional ? $additional : array();
00305         foreach ($this->_buttons as $button) {
00306             if ($selector->isMatch($button)) {
00307                 $encoding = $this->_encode();
00308                 $button->write($encoding);
00309                 if ($additional) {
00310                     $encoding->merge($additional);
00311                 }
00312                 return $encoding;           
00313             }
00314         }
00315         return false;
00316     }
00317         
00329     function submitImage($selector, $x, $y, $additional = false) {
00330         $additional = $additional ? $additional : array();
00331         foreach ($this->_images as $image) {
00332             if ($selector->isMatch($image)) {
00333                 $encoding = $this->_encode();
00334                 $image->write($encoding, $x, $y);
00335                 if ($additional) {
00336                     $encoding->merge($additional);
00337                 }
00338                 return $encoding;           
00339             }
00340         }
00341         return false;
00342     }
00343     
00351     function submit() {
00352         return $this->_encode();
00353     }
00354 }
00355 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations