]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/form.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[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         $this->out->hidden('token', common_session_token());
95     }
96
97     /**
98      * Name of the form
99      *
100      * Sub-classes should overload this with the name of their form.
101      *
102      * @return void
103      */
104
105     function formLegend()
106     {
107     }
108
109     /**
110      * Visible or invisible data elements
111      *
112      * Display the form fields that make up the data of the form.
113      * Sub-classes should overload this to show their data.
114      *
115      * @return void
116      */
117
118     function formData()
119     {
120     }
121
122     /**
123      * HTTP method used to submit the form
124      *
125      * Defaults to post. Subclasses can override if they need to.
126      *
127      * @return string the method to use for submitting
128      */
129      function method()
130      {
131          return 'post';
132      }
133
134     /**
135      * Buttons for form actions
136      *
137      * Submit and cancel buttons (or whatever)
138      * Sub-classes should overload this to show their own buttons.
139      *
140      * @return void
141      */
142
143     function formActions()
144     {
145     }
146
147     /**
148      * ID of the form
149      *
150      * Should be unique on the page. Sub-classes should overload this
151      * to show their own IDs.
152      *
153      * @return int ID of the form
154      */
155
156     function id()
157     {
158         return null;
159     }
160
161     /**
162      * Action of the form.
163      *
164      * URL to post to. Should be overloaded by subclasses to give
165      * somewhere to post to.
166      *
167      * @return string URL to post to
168      */
169
170     function action()
171     {
172     }
173
174     /**
175      * Class of the form. May include space-separated list of multiple classes.
176      *
177      * If 'ajax' is included, the form will automatically be submitted with
178      * an 'ajax=1' parameter added, and the resulting form or error message
179      * will replace the form after submission.
180      *
181      * It's up to you to make sure that the target action supports this!
182      *
183      * @return string the form's class
184      */
185
186     function formClass()
187     {
188         return 'form';
189     }
190
191     function li()
192     {
193         $this->out->elementStart('li');
194     }
195
196     function unli()
197     {
198         $this->out->elementEnd('li');
199     }
200 }