PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/simpletest/form.php

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