]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
showBody() -- revisit to add @id and @class to <body>
[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     /**
127     *  Ends an HTML document
128     *
129     *  @return void
130     */
131     function endHTML()
132     {
133         $this->elementEnd('html');
134         $this->endXML();
135     }
136
137
138     /**
139      * Output an HTML text input element
140      *
141      * Despite the name, it is specifically for outputting a
142      * text input element, not other <input> elements. It outputs
143      * a cluster of elements, including a <label> and an associated
144      * instructions span.
145      *
146      * @param string $id           element ID, must be unique on page
147      * @param string $label        text of label for the element
148      * @param string $value        value of the element, default null
149      * @param string $instructions instructions for valid input
150      *
151      * @todo add a $name parameter
152      * @todo add a $maxLength parameter
153      * @todo add a $size parameter
154      *
155      * @return void
156      */
157
158     function input($id, $label, $value=null, $instructions=null)
159     {
160         $this->elementStart('p');
161         $this->element('label', array('for' => $id), $label);
162         $attrs = array('name' => $id,
163                        'type' => 'text',
164                        'class' => 'input_text',
165                        'id' => $id);
166         if ($value) {
167             $attrs['value'] = htmlspecialchars($value);
168         }
169         $this->element('input', $attrs);
170         if ($instructions) {
171             $this->element('span', 'input_instructions', $instructions);
172         }
173         $this->elementEnd('p');
174     }
175
176     /**
177      * output an HTML checkbox and associated elements
178      *
179      * Note that the value is default 'true' (the string), which can
180      * be used by Action::boolean()
181      *
182      * @param string $id           element ID, must be unique on page
183      * @param string $label        text of label for the element
184      * @param string $checked      if the box is checked, default false
185      * @param string $instructions instructions for valid input
186      * @param string $value        value of the checkbox, default 'true'
187      * @param string $disabled     show the checkbox disabled, default false
188      *
189      * @return void
190      *
191      * @todo add a $name parameter
192      */
193
194     function checkbox($id, $label, $checked=false, $instructions=null,
195                       $value='true', $disabled=false)
196     {
197         $this->elementStart('p');
198         $attrs = array('name' => $id,
199                        'type' => 'checkbox',
200                        'class' => 'checkbox',
201                        'id' => $id);
202         if ($value) {
203             $attrs['value'] = htmlspecialchars($value);
204         }
205         if ($checked) {
206             $attrs['checked'] = 'checked';
207         }
208         if ($disabled) {
209             $attrs['disabled'] = 'true';
210         }
211         $this->element('input', $attrs);
212         $this->text(' ');
213         $this->element('label', array('class' => 'checkbox_label',
214                                       'for' => $id),
215                        $label);
216         $this->text(' ');
217         if ($instructions) {
218             $this->element('span', 'input_instructions', $instructions);
219         }
220         $this->elementEnd('p');
221     }
222
223     /**
224      * output an HTML combobox/select and associated elements
225      *
226      * $content is an array of key-value pairs for the dropdown, where
227      * the key is the option value attribute and the value is the option
228      * text. (Careful on the overuse of 'value' here.)
229      *
230      * @param string $id           element ID, must be unique on page
231      * @param string $label        text of label for the element
232      * @param array  $content      options array, value => text
233      * @param string $instructions instructions for valid input
234      * @param string $blank_select whether to have a blank entry, default false
235      * @param string $selected     selected value, default null
236      *
237      * @return void
238      *
239      * @todo add a $name parameter
240      */
241
242     function dropdown($id, $label, $content, $instructions=null,
243                       $blank_select=false, $selected=null)
244     {
245         $this->elementStart('p');
246         $this->element('label', array('for' => $id), $label);
247         $this->elementStart('select', array('id' => $id, 'name' => $id));
248         if ($blank_select) {
249             $this->element('option', array('value' => ''));
250         }
251         foreach ($content as $value => $option) {
252             if ($value == $selected) {
253                 $this->element('option', array('value' => $value,
254                                                'selected' => $value),
255                                $option);
256             } else {
257                 $this->element('option', array('value' => $value), $option);
258             }
259         }
260         $this->elementEnd('select');
261         if ($instructions) {
262             $this->element('span', 'input_instructions', $instructions);
263         }
264         $this->elementEnd('p');
265     }
266
267     /**
268      * output an HTML hidden element
269      *
270      * $id is re-used as name
271      *
272      * @param string $id    element ID, must be unique on page
273      * @param string $value hidden element value, default null
274      * @param string $name  name, if different than ID
275      *
276      * @return void
277      */
278
279     function hidden($id, $value, $name=null)
280     {
281         $this->element('input', array('name' => ($name) ? $name : $id,
282                                       'type' => 'hidden',
283                                       'id' => $id,
284                                       'value' => $value));
285     }
286
287     /**
288      * output an HTML password input and associated elements
289      *
290      * @param string $id           element ID, must be unique on page
291      * @param string $label        text of label for the element
292      * @param string $instructions instructions for valid input
293      *
294      * @return void
295      *
296      * @todo add a $name parameter
297      */
298
299     function password($id, $label, $instructions=null)
300     {
301         $this->elementStart('p');
302         $this->element('label', array('for' => $id), $label);
303         $attrs = array('name' => $id,
304                        'type' => 'password',
305                        'class' => 'password',
306                        'id' => $id);
307         $this->element('input', $attrs);
308         if ($instructions) {
309             $this->element('span', 'input_instructions', $instructions);
310         }
311         $this->elementEnd('p');
312     }
313
314     /**
315      * output an HTML submit input and associated elements
316      *
317      * @param string $id    element ID, must be unique on page
318      * @param string $label text of the button
319      * @param string $cls   class of the button, default 'submit'
320      * @param string $name  name, if different than ID
321      *
322      * @return void
323      *
324      * @todo add a $name parameter
325      */
326
327     function submit($id, $label, $cls='submit', $name=null)
328     {
329         $this->elementStart('p');
330         $this->element('input', array('type' => 'submit',
331                                       'id' => $id,
332                                       'name' => ($name) ? $name : $id,
333                                       'class' => $cls,
334                                       'value' => $label));
335         $this->elementEnd('p');
336     }
337
338     /**
339      * output an HTML textarea and associated elements
340      *
341      * @param string $id           element ID, must be unique on page
342      * @param string $label        text of label for the element
343      * @param string $content      content of the textarea, default none
344      * @param string $instructions instructions for valid input
345      *
346      * @return void
347      *
348      * @todo add a $name parameter
349      * @todo add a $cols parameter
350      * @todo add a $rows parameter
351      */
352
353     function textarea($id, $label, $content=null, $instructions=null)
354     {
355         $this->elementStart('p');
356         $this->element('label', array('for' => $id), $label);
357         $this->element('textarea', array('rows' => 3,
358                                          'cols' => 40,
359                                          'name' => $id,
360                                          'id' => $id),
361                        ($content) ? $content : '');
362         if ($instructions) {
363             $this->element('span', 'input_instructions', $instructions);
364         }
365         $this->elementEnd('p');
366     }
367 }