form.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Base class for forms
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. require_once INSTALLDIR . '/lib/ui/widget.php';
  34. /**
  35. * Base class for forms
  36. *
  37. * We have a lot of common forms (subscribe, fave, delete) and this superclass
  38. * lets us abstract out the basic features of the form.
  39. *
  40. * @category Widget
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Sarven Capadisli <csarven@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. *
  47. * @see HTMLOutputter
  48. */
  49. class Form extends Widget
  50. {
  51. var $enctype = null;
  52. /**
  53. * Show the form
  54. *
  55. * Uses a recipe to output the form.
  56. *
  57. * @return void
  58. * @see Widget::show()
  59. */
  60. function show()
  61. {
  62. $attributes = array('id' => $this->id(),
  63. 'class' => $this->formClass(),
  64. 'method' => $this->method(),
  65. 'action' => $this->action());
  66. if (!empty($this->enctype)) {
  67. $attributes['enctype'] = $this->enctype;
  68. }
  69. $this->out->elementStart('form', $attributes);
  70. $this->out->elementStart('fieldset');
  71. $this->formLegend();
  72. $this->sessionToken();
  73. $this->formData();
  74. $this->formActions();
  75. $this->out->elementEnd('fieldset');
  76. $this->out->elementEnd('form');
  77. }
  78. /**
  79. * Include a session token for CSRF protection
  80. *
  81. * @return void
  82. */
  83. function sessionToken()
  84. {
  85. if (strtolower($this->method()) == 'post') {
  86. $this->out->hidden('token-' . $this->id() ?: common_random_hexstr(3), common_session_token(), 'token');
  87. }
  88. }
  89. /**
  90. * Name of the form
  91. *
  92. * Sub-classes should overload this with the name of their form.
  93. *
  94. * @return void
  95. */
  96. function formLegend()
  97. {
  98. }
  99. /**
  100. * Visible or invisible data elements
  101. *
  102. * Display the form fields that make up the data of the form.
  103. * Sub-classes should overload this to show their data.
  104. *
  105. * @return void
  106. */
  107. function formData()
  108. {
  109. }
  110. /**
  111. * HTTP method used to submit the form
  112. *
  113. * Defaults to post. Subclasses can override if they need to.
  114. *
  115. * @return string the method to use for submitting
  116. */
  117. function method()
  118. {
  119. return 'post';
  120. }
  121. /**
  122. * Buttons for form actions
  123. *
  124. * Submit and cancel buttons (or whatever)
  125. * Sub-classes should overload this to show their own buttons.
  126. *
  127. * @return void
  128. */
  129. function formActions()
  130. {
  131. }
  132. /**
  133. * ID of the form
  134. *
  135. * Should be unique on the page. Sub-classes should overload this
  136. * to show their own IDs.
  137. *
  138. * @return int ID of the form
  139. */
  140. function id()
  141. {
  142. return null;
  143. }
  144. /**
  145. * Action of the form.
  146. *
  147. * URL to post to. Should be overloaded by subclasses to give
  148. * somewhere to post to.
  149. *
  150. * @return string URL to post to
  151. */
  152. function action()
  153. {
  154. }
  155. /**
  156. * Class of the form. May include space-separated list of multiple classes.
  157. *
  158. * If 'ajax' is included, the form will automatically be submitted with
  159. * an 'ajax=1' parameter added, and the resulting form or error message
  160. * will replace the form after submission.
  161. *
  162. * It's up to you to make sure that the target action supports this!
  163. *
  164. * @return string the form's class
  165. */
  166. function formClass()
  167. {
  168. return 'form';
  169. }
  170. function li($class=null)
  171. {
  172. $this->out->elementStart('li', $class);
  173. }
  174. function unli()
  175. {
  176. $this->out->elementEnd('li');
  177. }
  178. }