]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/form.php
Always naming it 'plugin' is not good, it can easily confuse. So better name it
[quix0rs-gnu-social.git] / lib / form.php
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
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/widget.php';
36
37 /**
38  * Base class for forms
39  *
40  * We have a lot of common forms (subscribe, fave, delete) and this superclass
41  * lets us abstract out the basic features of the form.
42  *
43  * @category Widget
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @author   Sarven Capadisli <csarven@status.net>
47  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48  * @link     http://status.net/
49  *
50  * @see      HTMLOutputter
51  */
52
53 class Form extends Widget
54 {
55     var $enctype = null;
56
57     /**
58      * Show the form
59      *
60      * Uses a recipe to output the form.
61      *
62      * @return void
63      * @see Widget::show()
64      */
65
66     function show()
67     {
68         $attributes = array('id' => $this->id(),
69             'class' => $this->formClass(),
70             'method' => $this->method(),
71             'action' => $this->action());
72
73         if (!empty($this->enctype)) {
74             $attributes['enctype'] = $this->enctype;
75         }
76         $this->out->elementStart('form', $attributes);
77         $this->out->elementStart('fieldset');
78         $this->formLegend();
79         $this->sessionToken();
80         $this->formData();
81         $this->formActions();
82         $this->out->elementEnd('fieldset');
83         $this->out->elementEnd('form');
84     }
85
86     /**
87      * Include a session token for CSRF protection
88      *
89      * @return void
90      */
91
92     function sessionToken()
93     {
94         if (strtolower($this->method()) == 'post') {
95             $this->out->hidden('token-' . $this->id() ?: common_random_hexstr(3), common_session_token(), 'token');
96         }
97     }
98
99     /**
100      * Name of the form
101      *
102      * Sub-classes should overload this with the name of their form.
103      *
104      * @return void
105      */
106
107     function formLegend()
108     {
109     }
110
111     /**
112      * Visible or invisible data elements
113      *
114      * Display the form fields that make up the data of the form.
115      * Sub-classes should overload this to show their data.
116      *
117      * @return void
118      */
119
120     function formData()
121     {
122     }
123
124     /**
125      * HTTP method used to submit the form
126      *
127      * Defaults to post. Subclasses can override if they need to.
128      *
129      * @return string the method to use for submitting
130      */
131      function method()
132      {
133          return 'post';
134      }
135
136     /**
137      * Buttons for form actions
138      *
139      * Submit and cancel buttons (or whatever)
140      * Sub-classes should overload this to show their own buttons.
141      *
142      * @return void
143      */
144
145     function formActions()
146     {
147     }
148
149     /**
150      * ID of the form
151      *
152      * Should be unique on the page. Sub-classes should overload this
153      * to show their own IDs.
154      *
155      * @return int ID of the form
156      */
157
158     function id()
159     {
160         return null;
161     }
162
163     /**
164      * Action of the form.
165      *
166      * URL to post to. Should be overloaded by subclasses to give
167      * somewhere to post to.
168      *
169      * @return string URL to post to
170      */
171
172     function action()
173     {
174     }
175
176     /**
177      * Class of the form. May include space-separated list of multiple classes.
178      *
179      * If 'ajax' is included, the form will automatically be submitted with
180      * an 'ajax=1' parameter added, and the resulting form or error message
181      * will replace the form after submission.
182      *
183      * It's up to you to make sure that the target action supports this!
184      *
185      * @return string the form's class
186      */
187
188     function formClass()
189     {
190         return 'form';
191     }
192
193     function li()
194     {
195         $this->out->elementStart('li');
196     }
197
198     function unli()
199     {
200         $this->out->elementEnd('li');
201     }
202 }