]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
b3b2a5ae4205eace525e5fab4bfc319e5fe0fb6e
[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  * @see      Action
56  * @see      HTMLOutputter
57  */
58
59 class HTMLOutputter extends XMLOutputter
60 {
61     /**
62      * Constructor
63      *
64      * Just wraps the XMLOutputter constructor.
65      *
66      * @param string  $output URI to output to, default = stdout
67      * @param boolean $indent Whether to indent output, default true
68      */
69
70     function __construct($output='php://output', $indent=true)
71     {
72         parent::__construct($output, $indent);
73     }
74
75     /**
76      * Start an HTML document
77      *
78      * If $type isn't specified, will attempt to do content negotiation.
79      *
80      * Attempts to do content negotiation for language, also.
81      *
82      * @param string $type MIME type to use; default is to do negotation.
83      *
84      * @todo extract content negotiation code to an HTTP module or class.
85      *
86      * @return void
87      */
88
89     function startHTML($type=null)
90     {
91         if (!$type) {
92             $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
93               $_SERVER['HTTP_ACCEPT'] : null;
94
95             // XXX: allow content negotiation for RDF, RSS, or XRDS
96
97             $cp = common_accept_to_prefs($httpaccept);
98             $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
99
100             $type = common_negotiate_type($cp, $sp);
101
102             if (!$type) {
103                 common_user_error(_('This page is not available in a '.
104                                     'media type you accept'), 406);
105                 exit(0);
106             }
107         }
108
109         header('Content-Type: '.$type);
110
111         $this->startXML('html',
112                         '-//W3C//DTD XHTML 1.0 Strict//EN',
113                         'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
114
115         // FIXME: correct language for interface
116
117         $language = common_language();
118
119         $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
120                                           'xml:lang' => $language,
121                                           'lang' => $language));
122     }
123
124     /**
125      * Output an HTML text input element
126      *
127      * Despite the name, it is specifically for outputting a
128      * text input element, not other <input> elements. It outputs
129      * a cluster of elements, including a <label> and an associated
130      * instructions span.
131      *
132      * @param string $id           element ID, must be unique on page
133      * @param string $label        text of label for the element
134      * @param string $value        value of the element, default null
135      * @param string $instructions instructions for valid input
136      *
137      * @todo add a $name parameter
138      * @todo add a $maxLength parameter
139      * @todo add a $size parameter
140      *
141      * @return void
142      */
143
144     function input($id, $label, $value=null, $instructions=null)
145     {
146         $this->elementStart('p');
147         $this->element('label', array('for' => $id), $label);
148         $attrs = array('name' => $id,
149                        'type' => 'text',
150                        'class' => 'input_text',
151                        'id' => $id);
152         if ($value) {
153             $attrs['value'] = htmlspecialchars($value);
154         }
155         $this->element('input', $attrs);
156         if ($instructions) {
157             $this->element('span', 'input_instructions', $instructions);
158         }
159         $this->elementEnd('p');
160     }
161
162     /**
163      * output an HTML checkbox and associated elements
164      *
165      * Note that the value is default 'true' (the string), which can
166      * be used by Action::boolean()
167      *
168      * @param string $id           element ID, must be unique on page
169      * @param string $label        text of label for the element
170      * @param string $checked      if the box is checked, default false
171      * @param string $instructions instructions for valid input
172      * @param string $value        value of the checkbox, default 'true'
173      * @param string $disabled     show the checkbox disabled, default false
174      *
175      * @return void
176      *
177      * @todo add a $name parameter
178      */
179
180     function checkbox($id, $label, $checked=false, $instructions=null,
181                       $value='true', $disabled=false)
182     {
183         $this->elementStart('p');
184         $attrs = array('name' => $id,
185                        'type' => 'checkbox',
186                        'class' => 'checkbox',
187                        'id' => $id);
188         if ($value) {
189             $attrs['value'] = htmlspecialchars($value);
190         }
191         if ($checked) {
192             $attrs['checked'] = 'checked';
193         }
194         if ($disabled) {
195             $attrs['disabled'] = 'true';
196         }
197         $this->element('input', $attrs);
198         $this->text(' ');
199         $this->element('label', array('class' => 'checkbox_label',
200                                       'for' => $id),
201                        $label);
202         $this->text(' ');
203         if ($instructions) {
204             $this->element('span', 'input_instructions', $instructions);
205         }
206         $this->elementEnd('p');
207     }
208
209     /**
210      * output an HTML combobox/select and associated elements
211      *
212      * $content is an array of key-value pairs for the dropdown, where
213      * the key is the option value attribute and the value is the option
214      * text. (Careful on the overuse of 'value' here.)
215      *
216      * @param string $id           element ID, must be unique on page
217      * @param string $label        text of label for the element
218      * @param array  $content      options array, value => text
219      * @param string $instructions instructions for valid input
220      * @param string $blank_select whether to have a blank entry, default false
221      * @param string $selected     selected value, default null
222      *
223      * @return void
224      *
225      * @todo add a $name parameter
226      */
227
228     function dropdown($id, $label, $content, $instructions=null,
229                       $blank_select=false, $selected=null)
230     {
231         $this->elementStart('p');
232         $this->element('label', array('for' => $id), $label);
233         $this->elementStart('select', array('id' => $id, 'name' => $id));
234         if ($blank_select) {
235             $this->element('option', array('value' => ''));
236         }
237         foreach ($content as $value => $option) {
238             if ($value == $selected) {
239                 $this->element('option', array('value' => $value,
240                                                'selected' => $value),
241                                $option);
242             } else {
243                 $this->element('option', array('value' => $value), $option);
244             }
245         }
246         $this->elementEnd('select');
247         if ($instructions) {
248             $this->element('span', 'input_instructions', $instructions);
249         }
250         $this->elementEnd('p');
251     }
252
253     /**
254      * output an HTML hidden element
255      *
256      * $id is re-used as name
257      *
258      * @param string $id    element ID, must be unique on page
259      * @param string $value hidden element value, default null
260      *
261      * @return void
262      *
263      * @todo add a $name parameter
264      */
265
266     function hidden($id, $value)
267     {
268         $this->element('input', array('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      *
308      * @return void
309      *
310      * @todo add a $name parameter
311      */
312
313     function submit($id, $label, $cls='submit')
314     {
315         $this->elementStart('p');
316         $this->element('input', array('type' => 'submit',
317                                       'id' => $id,
318                                       'name' => $id,
319                                       'class' => $cls,
320                                       'value' => $label));
321         $this->elementEnd('p');
322     }
323
324     /**
325      * output an HTML textarea and associated elements
326      *
327      * @param string $id           element ID, must be unique on page
328      * @param string $label        text of label for the element
329      * @param string $content      content of the textarea, default none
330      * @param string $instructions instructions for valid input
331      *
332      * @return void
333      *
334      * @todo add a $name parameter
335      * @todo add a $cols parameter
336      * @todo add a $rows parameter
337      */
338
339     function textarea($id, $label, $content=null, $instructions=null)
340     {
341         $this->elementStart('p');
342         $this->element('label', array('for' => $id), $label);
343         $this->element('textarea', array('rows' => 3,
344                                          'cols' => 40,
345                                          'name' => $id,
346                                          'id' => $id),
347                        ($content) ? $content : '');
348         if ($instructions) {
349             $this->element('span', 'input_instructions', $instructions);
350         }
351         $this->elementEnd('p');
352     }
353 }