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