phpDocumentor form
[ class tree: form ] [ index: form ] [ all elements ]

Source for file Form.php

Documentation is available at Form.php

  1. <?
  2. /**
  3. * @package form
  4. * @filesource
  5. * @see HTML_FORM_COMPONENT_PATH.'/Form.php'
  6. * @copyright (c) http://Finn-Rasmussen.com
  7. * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
  8. * @author http://Finn-Rasmussen.com
  9. * @version 1.9
  10. * @since 21-oct-2005
  11. */
  12.  
  13. /**
  14. * The required files
  15. */
  16. require_once(HTML_PATH.'/Html.php');
  17. require_once(HTML_FORM_COMPONENT_PATH.'/Label.php');
  18. require_once(HTML_FORM_COMPONENT_PATH.'/Input.php');
  19. require_once(HTML_FORM_COMPONENT_PATH.'/TextField.php');
  20. require_once(HTML_FORM_COMPONENT_PATH.'/Disabled.php');
  21. require_once(HTML_FORM_COMPONENT_PATH.'/Readonly.php');
  22. require_once(HTML_FORM_COMPONENT_PATH.'/Hidden.php');
  23. require_once(HTML_FORM_COMPONENT_PATH.'/Button.php');
  24. require_once(HTML_FORM_COMPONENT_PATH.'/Password.php');
  25. require_once(HTML_FORM_COMPONENT_PATH.'/ResetButton.php');
  26. require_once(HTML_FORM_COMPONENT_PATH.'/CancelButton.php');
  27. require_once(HTML_FORM_COMPONENT_PATH.'/SubmitButton.php');
  28. require_once(HTML_FORM_COMPONENT_PATH.'/ImageButton.php');
  29. require_once(HTML_FORM_COMPONENT_PATH.'/Radio.php');
  30. require_once(HTML_FORM_COMPONENT_PATH.'/Checkbox.php');
  31. require_once(HTML_FORM_COMPONENT_PATH.'/Select.php');
  32. require_once(HTML_FORM_COMPONENT_PATH.'/Option.php');
  33. require_once(HTML_FORM_COMPONENT_PATH.'/Textarea.php');
  34. require_once(HTML_FORM_COMPONENT_PATH.'/Zip.php');
  35. require_once(HTML_FORM_COMPONENT_PATH.'/Fileupload.php');
  36. if (defined('HTML_BASE_UTIL_PATH')) {
  37. require_once(HTML_BASE_UTIL_PATH.'/Fieldset.php');
  38. require_once(HTML_BASE_UTIL_PATH.'/Legend.php');
  39. }
  40.  
  41. /**
  42. * Generates a html form
  43. * <code>
  44. * Usage:
  45. * $form = new Form('goHere.php','get');
  46. * $form->add(new SubmitButton($action,$method,$name,$attr,$title,$onsubmit));
  47. * print $form->getHtml();
  48. * Or:
  49. * Form::start($action,$method,$name,$attr,$title,$onsubmit);
  50. * elements::display()
  51. * Form::end();
  52. * Or:
  53. * Form::start($action,$method,$name,$attr,$title,$onsubmit);
  54. * Form::textarea();
  55. * Form::submitbutton();
  56. * :
  57. * Form::end();
  58. * </code>
  59. * @package form
  60. */
  61.  
  62. class Form extends Html {
  63. var $action = ''; // Where to go
  64. var $method = ''; // How to send data POST/GET
  65. var $name = ''; // The name of the form (NOT xhtml 1.0 strict compliant)
  66. var $id = ''; // The id of the form (NOT netscape 4.x compliant)
  67. var $attr = ''; // Additional attributes for the form
  68. var $title = ''; // The title (tooltip) of the form
  69. var $onsubmit = ''; // Onsubmit event
  70.  
  71.  
  72. /**
  73. * Constructor
  74. * @param String $action The action where to go, default 'self'
  75. * @param String $method The method get/post
  76. * @param String $name The name if any (NOT xhtml 1.0 strict compliant)
  77. * @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"'
  78. * @param String $title The title (tooltip)
  79. * @param String $onsubmit The onsubmit event
  80. */
  81. function Form($action='',$method='',$name='',$attr='',$title='',$onsubmit='') {
  82. $this->Html();
  83. $this->action = $action!=''?$action:$_SERVER['PHP_SELF'];
  84. $this->method = $method!=''?$method:'get';
  85. $this->name = $name!=''?$name:'';
  86. $this->id = $name!=''?$name:'';
  87. $this->attr = $attr!=''?' '.$attr:'';
  88. $this->title = $title!=''?$title:'';
  89. $this->onsubmit = $onsubmit!=''?$onsubmit:'';
  90. }
  91.  
  92. /**
  93. * Returns the html for the hidden fields of a form
  94. * @return String The html
  95. */
  96. function getHidden() {
  97. $html = '';
  98. // GET
  99. if (defined('REQUEST_LANGUAGE') && !empty($_GET[REQUEST_LANGUAGE])) {
  100. $hidden = new Hidden(REQUEST_LANGUAGE,$_GET[REQUEST_LANGUAGE]);
  101. $html .= $hidden->getHtml();
  102. }
  103. if (defined('REQUEST_TAB') && !empty($_GET[REQUEST_TAB])) {
  104. $hidden = new Hidden(REQUEST_TAB,$_GET[REQUEST_TAB]);
  105. //$html .= $hidden->getHtml();
  106. }
  107. if (defined('REQUEST_SID') && !empty($_GET[REQUEST_SID])) {
  108. $hidden = new Hidden(REQUEST_SID,$_GET[REQUEST_SID]);
  109. $html .= $hidden->getHtml();
  110. }
  111.  
  112. // POST
  113. if (defined('REQUEST_LANGUAGE') && !empty($_POST[REQUEST_LANGUAGE])) {
  114. $hidden = new Hidden(REQUEST_LANGUAGE,$_POST[REQUEST_LANGUAGE]);
  115. $html .= $hidden->getHtml();
  116. }
  117. if (defined('REQUEST_TAB') && !empty($_POST[REQUEST_TAB])) {
  118. $hidden = new Hidden(REQUEST_TAB,$_POST[REQUEST_TAB]);
  119. //$html .= $hidden->getHtml();
  120. }
  121. if (defined('REQUEST_SID') && !empty($_POST[REQUEST_SID])) {
  122. $hidden = new Hidden(REQUEST_SID,$_POST[REQUEST_SID]);
  123. $html .= $hidden->getHtml();
  124. }
  125. if ($html!='') {
  126. $html = '<p>'.$html."</p>\r\n";
  127. }
  128. return $html;
  129. }
  130.  
  131. /**
  132. * Returns the html for the start of a form
  133. * @return String The html
  134. */
  135. function getStart() {
  136. $html = '';
  137. $html .= '<form';
  138. $html .= $this->getAttribute('action');
  139. $html .= $this->getAttribute('method');
  140. $html .= $this->getAttribute('name'); // NOT xhtml 1.0 strict compliant
  141. $html .= $this->getAttribute('id'); // NOT netscape 4.x compliant
  142. $html .= $this->getAttribute('title');
  143. $html .= $this->getAttribute('onsubmit');
  144. $html .= $this->attr; // more attributes, if required
  145. $html .= '>'."\r\n";
  146. $html .= $this->getHidden();
  147. return $html;
  148. }
  149.  
  150. /*
  151. * Returns the html for the end of a form
  152. * @return String The html
  153. */
  154. function getEnd() {
  155. return "</form>\r\n";
  156. }
  157.  
  158. /*
  159. * Get the complete html for a Form
  160. * @return String the html
  161. */
  162. function getHtml() {
  163. $html = $this->html;
  164. $html .= $this->getStart();
  165. $html .= $this->getElements(); // as html
  166. $html .= $this->getEnd();
  167. return $html;
  168. }
  169.  
  170. /**
  171. * Display start html
  172. * <code>
  173. * Usage:
  174. * Form::start($action,$method,$name,$attr,$title,$onsubmit);
  175. * </code>
  176. * @static
  177. * @param String $action The action where to go, default 'self'
  178. * @param String $method The method get/post
  179. * @param String $name The name if any (NOT xhtml 1.0 strict compliant)
  180. * @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"'
  181. * @param String $title The title (tooltip)
  182. * @param String $onsubmit The onsubmit event
  183. */
  184. function start($action='',$method='',$name='',$attr='',$title='',$onsubmit='') {
  185. switch ($action) {
  186. case 'fieldset':
  187. Fieldset::start($action,$method);
  188. break;
  189. default:
  190. $html = new Form($action,$method,$name,$attr,$title,$onsubmit);
  191. $html->addHtml($html->getStart());
  192. break;
  193. }
  194. }
  195.  
  196. /**
  197. * Display end html
  198. * <code>
  199. * Usage:
  200. * Form::end($element);
  201. * </code>
  202. * @static
  203. */
  204. function end($element='') {
  205. switch ($element) {
  206. case 'fieldset':
  207. Fieldset::end();
  208. break;
  209. default:
  210. $html = new Html();
  211. $html->addHtml(Form::getEnd());
  212. break;
  213. }
  214. }
  215.  
  216. /**
  217. * Display html
  218. * <code>
  219. * Usage:
  220. * Form::display($name,$class,$attr);
  221. * </code>
  222. * @static
  223. * @param String $action The action where to go, default 'self'
  224. * @param String $method The method get/post
  225. * @param String $name The name if any (NOT xhtml 1.0 strict compliant)
  226. * @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"'
  227. * @param String $title The title (tooltip)
  228. * @param String $onsubmit The onsubmit event
  229. */
  230. function display($action='',$method='',$name='',$attr='',$title='',$onsubmit='') {
  231. $html = new Form($action,$method,$name,$attr,$title,$onsubmit);
  232. $html->addHtml($html->getHtml());
  233. }
  234.  
  235. // *********** FORM ELEMENTS ***********
  236.  
  237.  
  238. /**
  239. * Display html
  240. * <code>
  241. * Usage:
  242. * Form::selectstart($name,$class,$attr);
  243. * </code>
  244. * @static
  245. * @param String $name The name of the control
  246. * @param String $class The class name
  247. * @param String $attr Additional atributes
  248. */
  249. function selectstart($name,$class='',$attr='') {
  250. Select::start($name,$class,$attr);
  251. }
  252.  
  253. /**
  254. * Display html
  255. * <code>
  256. * Usage:
  257. * Form::option($text,$value,$selected);
  258. * </code>
  259. * @static
  260. * @param String $text the text to show
  261. * @param String $value the value, if any
  262. * @param String $selected The option is selected
  263. */
  264. function option($text,$value='',$selected='') {
  265. Option::display($text,$value,$selected);
  266. }
  267.  
  268. /**
  269. * Display html
  270. * <code>
  271. * Usage:
  272. * Form::selectend();
  273. * </code>
  274. * @static
  275. */
  276. function selectend() {
  277. Select::end();
  278. }
  279.  
  280. /**
  281. * Display html
  282. * <code>
  283. * Usage:
  284. * Form::label($text,$id,$class);
  285. * </code>
  286. * @static
  287. * @param String $text the text to show
  288. * @param String $id the ID for the control to relate to
  289. * @param String $class the class name
  290. */
  291. function label($text,$id='',$class='') {
  292. Label::display($text,$id,$class);
  293. }
  294.  
  295. /**
  296. * Display html
  297. * <code>
  298. * Usage:
  299. * Form::legend($text,$class,$accesskey);
  300. * </code>
  301. * @static
  302. * @param String $text The text to show
  303. * @param String $class The class name
  304. * @param String $accesskey The access key
  305. */
  306. function legend($text,$class='',$accesskey='') {
  307. Legend::display($text,$class,$accesskey);
  308. }
  309.  
  310. /**
  311. * Display html
  312. * <code>
  313. * Usage:
  314. * Form::fieldsetstart($legend,$class);
  315. * </code>
  316. * @static
  317. * @param String $legend The legend object to use
  318. * @param String $class The css class of the link
  319. */
  320. function fieldsetstart($legend='',$class='') {
  321. Fieldset::start($legend,$class);
  322. }
  323.  
  324. /**
  325. * Display html
  326. * <code>
  327. * Usage:
  328. * Form::fieldsetend();
  329. * </code>
  330. * @static
  331. */
  332. function fieldsetend() {
  333. Fieldset::end();
  334. }
  335.  
  336. /**
  337. * Display html
  338. * <code>
  339. * Usage:
  340. * Form::textarea($name,$text='',$rows='',$cols='',$class='');
  341. * </code>
  342. * @static
  343. * @param String $name The name of the control
  344. * @param String $text The text value, if any
  345. * @param String $rows The number of rows
  346. * @param String $cols The number of columns
  347. * @param String $class The class name
  348. */
  349. function textarea($name,$text='',$rows='',$cols='',$class='') {
  350. Textarea::display($name,$text,$rows,$cols,$class);
  351. }
  352.  
  353. /**
  354. * Display html
  355. * <code>
  356. * Usage:
  357. * Form::input($type,$name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  358. * </code>
  359. * @static
  360. * @param String $type the type (text, radio, checkbox, file, button, hidden, submit, reset)
  361. * @param String $name the name
  362. * @param String $value the value, if any
  363. * @param String $class the class
  364. * @param String $size the size / or checked for radio/checkbox
  365. * @param String $maxlength the maxlength
  366. * @param String $disabled the disabled
  367. * @param String $readonly the readonly
  368. * @param String $onclick On click event for javascript
  369. */
  370. function input($type,$name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='') {
  371. Input::display($type,$name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  372. }
  373.  
  374. /**
  375. * Display html
  376. * <code>
  377. * Usage:
  378. * Form::textfield($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  379. * </code>
  380. * @static
  381. * @param String $name the name
  382. * @param String $value the value, if any
  383. * @param String $class the class
  384. * @param String $size the size
  385. * @param String $maxlength the maxlength
  386. * @param String $disabled the disabled
  387. * @param String $readonly the readonly
  388. * @param String $onclick On click event for javascript
  389. */
  390. function textfield($name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='') {
  391. TextField::display($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  392. }
  393.  
  394. /**
  395. * Display html
  396. * <code>
  397. * Usage:
  398. * Form::disabled($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  399. * </code>
  400. * @static
  401. * @param String $name the name
  402. * @param String $value the value, if any
  403. * @param String $class the class
  404. * @param String $size the size
  405. * @param String $maxlength the maxlength
  406. * @param String $disabled the disabled
  407. * @param String $readonly the readonly
  408. * @param String $onclick On click event for javascript
  409. */
  410. function disabled($name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='') {
  411. Disabled::display($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  412. }
  413.  
  414. /**
  415. * Display html
  416. * <code>
  417. * Usage:
  418. * Form::readonly($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  419. * </code>
  420. * @static
  421. * @param String $name the name
  422. * @param String $value the value, if any
  423. * @param String $class the class
  424. * @param String $size the size
  425. * @param String $maxlength the maxlength
  426. * @param String $disabled the disabled
  427. * @param String $readonly the readonly
  428. * @param String $onclick On click event for javascript
  429. */
  430. function readonly($name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='') {
  431. Readonly::display($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  432. }
  433.  
  434. /**
  435. * Display html
  436. * <code>
  437. * Usage:
  438. * Form::password($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  439. * </code>
  440. * @static
  441. * @param String $name the name
  442. * @param String $value the value, if any
  443. * @param String $class the class
  444. * @param String $size the size
  445. * @param String $maxlength the maxlength
  446. * @param String $disabled the disabled
  447. * @param String $readonly the readonly
  448. * @param String $onclick On click event for javascript
  449. */
  450. function password($name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='') {
  451. Password::display($name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick);
  452. }
  453.  
  454. /**
  455. * Display html
  456. * <code>
  457. * Usage:
  458. * Form::hidden($name,$value);
  459. * </code>
  460. * @static
  461. * @param String $name the name
  462. * @param String $value the value, if any
  463. */
  464. function hidden($name='',$value='') {
  465. Hidden::display($name,$value);
  466. }
  467.  
  468. /**
  469. * Display html
  470. * <code>
  471. * Usage:
  472. * Form::button($name,$value,$class,$onclick);
  473. * </code>
  474. * @static
  475. * @param String $name the name
  476. * @param String $value the value, if any
  477. * @param String $class the class
  478. * @param String $onclick On click event for javascript
  479. */
  480. function button($name='',$value='',$class='',$onclick='') {
  481. Button::display($name,$value,$class,$onclick);
  482. }
  483.  
  484. /**
  485. * Display html
  486. * <code>
  487. * Usage:
  488. * Form::resetbutton($name,$value,$class,$onclick);
  489. * </code>
  490. * @static
  491. * @param String $name the name
  492. * @param String $value the value, if any
  493. * @param String $class the class
  494. * @param String $onclick On click event for javascript
  495. */
  496. function resetbutton($name='',$value='',$class='',$onclick='') {
  497. ResetButton::display($name,$value,$class,$onclick);
  498. }
  499.  
  500. /**
  501. * Display html
  502. * <code>
  503. * Usage:
  504. * Form::submitbutton($name,$value,$class,$onclick);
  505. * </code>
  506. * @static
  507. * @param String $name the name
  508. * @param String $value the value, if any
  509. * @param String $class the class
  510. * @param String $onclick On click event for javascript
  511. */
  512. function submitbutton($name='',$value='',$class='',$onclick='') {
  513. SubmitButton::display($name,$value,$class,$onclick);
  514. }
  515.  
  516. /**
  517. * Display html
  518. * <code>
  519. * Usage:
  520. * Form::fileupload($name,$class);
  521. * </code>
  522. * @static
  523. * @param String $name the name
  524. * @param String $class the class
  525. */
  526. function fileupload($name='',$class='') {
  527. SubmitButton::display($name,$class);
  528. }
  529.  
  530. /**
  531. * Display html
  532. * <code>
  533. * Usage:
  534. * Form::cancelbuttonbutton($name,$value,$class,$onclick);
  535. * </code>
  536. * @static
  537. * @param String $name the name
  538. * @param String $value the value, if any
  539. * @param String $class the class
  540. * @param String $onclick On click event for javascript
  541. */
  542. function cancelbutton($name='',$value='',$class='',$onclick='') {
  543. CancelButton::display($name,$value,$class,$onclick);
  544. }
  545.  
  546. /**
  547. * Display html
  548. * <code>
  549. * Usage:
  550. * Form::radio($name,$value,$class,$checked);
  551. * </code>
  552. * @static
  553. * @param String $name the name
  554. * @param String $value the value, if any
  555. * @param String $class the class
  556. * @param String $checked if present
  557. * @param String $onclick On click event for javascript
  558. */
  559. function radio($name='',$value='',$class='',$checked='',$onclick='') {
  560. Radio::display($name,$value,$class,$checked,$onclick);
  561. }
  562.  
  563. /**
  564. * Display html
  565. * <code>
  566. * Usage:
  567. * Form::checkbox($name,$value,$class,$checked,$onclick);
  568. * </code>
  569. * @static
  570. * @param String $name the name
  571. * @param String $value the value, if any
  572. * @param String $class the class
  573. * @param String $checked if present
  574. * @param String $onclick On click event for javascript
  575. */
  576. function checkbox($name='',$value='',$class='',$checked='',$onclick='') {
  577. Checkbox::display($name,$value,$class,$checked,$onclick);
  578. }
  579. }
  580. ?>

Documentation generated on Thu, 22 Dec 2005 17:08:36 +0100 by phpDocumentor 1.3.0RC3