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