]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Add a name parameter to a couple of the form elements
[quix0rs-gnu-social.git] / lib / htmloutputter.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Low-level generator for HTML
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  Output
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Sarven Capadisli <csarven@controlyourself.ca>
26  * @copyright 2008 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/xmloutputter.php';
36
37 define('PAGE_TYPE_PREFS',
38        'text/html,application/xhtml+xml,'.
39        'application/xml;q=0.3,text/xml;q=0.2');
40
41 /**
42  * Low-level generator for HTML
43  *
44  * Abstracts some of the code necessary for HTML generation. Especially
45  * has methods for generating HTML form elements. Note that these have
46  * been created kind of haphazardly, not with an eye to making a general
47  * HTML-creation class.
48  *
49  * @category Output
50  * @package  Laconica
51  * @author   Evan Prodromou <evan@controlyourself.ca>
52  * @author   Sarven Capadisli <csarven@controlyourself.ca>
53  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
54  * @link     http://laconi.ca/
55  *
56  * @see      Action
57  * @see      XMLOutputter
58  */
59
60 class HTMLOutputter extends XMLOutputter
61 {
62     /**
63      * Constructor
64      *
65      * Just wraps the XMLOutputter constructor.
66      *
67      * @param string  $output URI to output to, default = stdout
68      * @param boolean $indent Whether to indent output, default true
69      */
70
71     function __construct($output='php://output', $indent=true)
72     {
73         parent::__construct($output, $indent);
74     }
75
76     /**
77      * Start an HTML document
78      *
79      * If $type isn't specified, will attempt to do content negotiation.
80      *
81      * Attempts to do content negotiation for language, also.
82      *
83      * @param string $type MIME type to use; default is to do negotation.
84      *
85      * @todo extract content negotiation code to an HTTP module or class.
86      *
87      * @return void
88      */
89
90     function startHTML($type=null)
91     {
92         if (!$type) {
93             $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
94               $_SERVER['HTTP_ACCEPT'] : null;
95
96             // XXX: allow content negotiation for RDF, RSS, or XRDS
97
98             $cp = common_accept_to_prefs($httpaccept);
99             $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
100
101             $type = common_negotiate_type($cp, $sp);
102
103             if (!$type) {
104                 common_user_error(_('This page is not available in a '.
105                                     'media type you accept'), 406);
106                 exit(0);
107             }
108         }
109
110         header('Content-Type: '.$type);
111
112         $this->startXML('html',
113                         '-//W3C//DTD XHTML 1.0 Strict//EN',
114                         'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
115
116         // FIXME: correct language for interface
117
118         $language = common_language();
119
120         $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
121                                           'xml:lang' => $language,
122                                           'lang' => $language));
123     }
124
125     /**
126      * Output an HTML text input element
127      *
128      * Despite the name, it is specifically for outputting a
129      * text input element, not other <input> elements. It outputs
130      * a cluster of elements, including a <label> and an associated
131      * instructions span.
132      *
133      * @param string $id           element ID, must be unique on page
134      * @param string $label        text of label for the element
135      * @param string $value        value of the element, default null
136      * @param string $instructions instructions for valid input
137      *
138      * @todo add a $name parameter
139      * @todo add a $maxLength parameter
140      * @todo add a $size parameter
141      *
142      * @return void
143      */
144
145     function input($id, $label, $value=null, $instructions=null)
146     {
147         $this->elementStart('p');
148         $this->element('label', array('for' => $id), $label);
149         $attrs = array('name' => $id,
150                        'type' => 'text',
151                        'class' => 'input_text',
152                        'id' => $id);
153         if ($value) {
154             $attrs['value'] = htmlspecialchars($value);
155         }
156         $this->element('input', $attrs);
157         if ($instructions) {
158             $this->element('span', 'input_instructions', $instructions);
159         }
160         $this->elementEnd('p');
161     }
162
163     /**
164      * output an HTML checkbox and associated elements
165      *
166      * Note that the value is default 'true' (the string), which can
167      * be used by Action::boolean()
168      *
169      * @param string $id           element ID, must be unique on page
170      * @param string $label        text of label for the element
171      * @param string $checked      if the box is checked, default false
172      * @param string $instructions instructions for valid input
173      * @param string $value        value of the checkbox, default 'true'
174      * @param string $disabled     show the checkbox disabled, default false
175      *
176      * @return void
177      *
178      * @todo add a $name parameter
179      */
180
181     function checkbox($id, $label, $checked=false, $instructions=null,
182                       $value='true', $disabled=false)
183     {
184         $this->elementStart('p');
185         $attrs = array('name' => $id,
186                        'type' => 'checkbox',
187                        'class' => 'checkbox',
188                        'id' => $id);
189         if ($value) {
190             $attrs['value'] = htmlspecialchars($value);
191         }
192         if ($checked) {
193             $attrs['checked'] = 'checked';
194         }
195         if ($disabled) {
196             $attrs['disabled'] = 'true';
197         }
198         $this->element('input', $attrs);
199         $this->text(' ');
200         $this->element('label', array('class' => 'checkbox_label',
201                                       'for' => $id),
202                        $label);
203         $this->text(' ');
204         if ($instructions) {
205             $this->element('span', 'input_instructions', $instructions);
206         }
207         $this->elementEnd('p');
208     }
209
210     /**
211      * output an HTML combobox/select and associated elements
212      *
213      * $content is an array of key-value pairs for the dropdown, where
214      * the key is the option value attribute and the value is the option
215      * text. (Careful on the overuse of 'value' here.)
216      *
217      * @param string $id           element ID, must be unique on page
218      * @param string $label        text of label for the element
219      * @param array  $content      options array, value => text
220      * @param string $instructions instructions for valid input
221      * @param string $blank_select whether to have a blank entry, default false
222      * @param string $selected     selected value, default null
223      *
224      * @return void
225      *
226      * @todo add a $name parameter
227      */
228
229     function dropdown($id, $label, $content, $instructions=null,
230                       $blank_select=false, $selected=null)
231     {
232         $this->elementStart('p');
233         $this->element('label', array('for' => $id), $label);
234         $this->elementStart('select', array('id' => $id, 'name' => $id));
235         if ($blank_select) {
236             $this->element('option', array('value' => ''));
237         }
238         foreach ($content as $value => $option) {
239             if ($value == $selected) {
240                 $this->element('option', array('value' => $value,
241                                                'selected' => $value),
242                                $option);
243             } else {
244                 $this->element('option', array('value' => $value), $option);
245             }
246         }
247         $this->elementEnd('select');
248         if ($instructions) {
249             $this->element('span', 'input_instructions', $instructions);
250         }
251         $this->elementEnd('p');
252     }
253
254     /**
255      * output an HTML hidden element
256      *
257      * $id is re-used as name
258      *
259      * @param string $id    element ID, must be unique on page
260      * @param string $value hidden element value, default null
261      * @param string $name  name, if different than ID
262      *
263      * @return void
264      */
265
266     function hidden($id, $value, $name=null)
267     {
268         $this->element('input', array('name' => ($name) ? $name : $id,
269                                       'type' => 'hidden',
270                                       'id' => $id,
271                                       'value' => $value));
272     }
273
274     /**
275      * output an HTML password input and associated elements
276      *
277      * @param string $id           element ID, must be unique on page
278      * @param string $label        text of label for the element
279      * @param string $instructions instructions for valid input
280      *
281      * @return void
282      *
283      * @todo add a $name parameter
284      */
285
286     function password($id, $label, $instructions=null)
287     {
288         $this->elementStart('p');
289         $this->element('label', array('for' => $id), $label);
290         $attrs = array('name' => $id,
291                        'type' => 'password',
292                        'class' => 'password',
293                        'id' => $id);
294         $this->element('input', $attrs);
295         if ($instructions) {
296             $this->element('span', 'input_instructions', $instructions);
297         }
298         $this->elementEnd('p');
299     }
300
301     /**
302      * output an HTML submit input and associated elements
303      *
304      * @param string $id    element ID, must be unique on page
305      * @param string $label text of the button
306      * @param string $cls   class of the button, default 'submit'
307      * @param string $name  name, if different than ID
308      *
309      * @return void
310      *
311      * @todo add a $name parameter
312      */
313
314     function submit($id, $label, $cls='submit', $name=null)
315     {
316         $this->elementStart('p');
317         $this->element('input', array('type' => 'submit',
318                                       'id' => $id,
319                                       'name' => ($name) ? $name : $id,
320                                       'class' => $cls,
321                                       'value' => $label));
322         $this->elementEnd('p');
323     }
324
325     /**
326      * output an HTML textarea and associated elements
327      *
328      * @param string $id           element ID, must be unique on page
329      * @param string $label        text of label for the element
330      * @param string $content      content of the textarea, default none
331      * @param string $instructions instructions for valid input
332      *
333      * @return void
334      *
335      * @todo add a $name parameter
336      * @todo add a $cols parameter
337      * @todo add a $rows parameter
338      */
339
340     function textarea($id, $label, $content=null, $instructions=null)
341     {
342         $this->elementStart('p');
343         $this->element('label', array('for' => $id), $label);
344         $this->element('textarea', array('rows' => 3,
345                                          'cols' => 40,
346                                          'name' => $id,
347                                          'id' => $id),
348                        ($content) ? $content : '');
349         if ($instructions) {
350             $this->element('span', 'input_instructions', $instructions);
351         }
352         $this->elementEnd('p');
353     }
354 }