PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/simpletest/form.php

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