]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Merge branch '0.9.x' into pluginize-twitter-bridge
[quix0rs-gnu-social.git] / lib / htmloutputter.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !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  StatusNet
51  * @author   Evan Prodromou <evan@status.net>
52  * @author   Sarven Capadisli <csarven@status.net>
53  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
54  * @link     http://status.net/
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         if( ! substr($type,0,strlen('text/html'))=='text/html' ){
113             // Browsers don't like it when <?xml it output for non-xhtml documents
114             $this->xw->startDocument('1.0', 'UTF-8');
115         }
116         $this->xw->writeDTD('html');
117
118         $language = $this->getLanguage();
119
120         $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
121                                           'xml:lang' => $language,
122                                           'lang' => $language));
123     }
124
125     function getLanguage()
126     {
127         // FIXME: correct language for interface
128         return common_language();
129     }
130
131     /**
132     *  Ends an HTML document
133     *
134     *  @return void
135     */
136     function endHTML()
137     {
138         $this->elementEnd('html');
139         $this->endXML();
140     }
141
142     /**
143     *  To specify additional HTTP headers for the action
144     *
145     *  @return void
146     */
147     function extraHeaders()
148     {
149         // Needs to be overloaded
150     }
151
152     /**
153      * Output an HTML text input element
154      *
155      * Despite the name, it is specifically for outputting a
156      * text input element, not other <input> elements. It outputs
157      * a cluster of elements, including a <label> and an associated
158      * instructions span.
159      *
160      * @param string $id           element ID, must be unique on page
161      * @param string $label        text of label for the element
162      * @param string $value        value of the element, default null
163      * @param string $instructions instructions for valid input
164      *
165      * @todo add a $name parameter
166      * @todo add a $maxLength parameter
167      * @todo add a $size parameter
168      *
169      * @return void
170      */
171
172     function input($id, $label, $value=null, $instructions=null)
173     {
174         $this->element('label', array('for' => $id), $label);
175         $attrs = array('name' => $id,
176                        'type' => 'text',
177                        'id' => $id);
178         if ($value) {
179             $attrs['value'] = $value;
180         }
181         $this->element('input', $attrs);
182         if ($instructions) {
183             $this->element('p', 'form_guide', $instructions);
184         }
185     }
186
187     /**
188      * output an HTML checkbox and associated elements
189      *
190      * Note that the value is default 'true' (the string), which can
191      * be used by Action::boolean()
192      *
193      * @param string $id           element ID, must be unique on page
194      * @param string $label        text of label for the element
195      * @param string $checked      if the box is checked, default false
196      * @param string $instructions instructions for valid input
197      * @param string $value        value of the checkbox, default 'true'
198      * @param string $disabled     show the checkbox disabled, default false
199      *
200      * @return void
201      *
202      * @todo add a $name parameter
203      */
204
205     function checkbox($id, $label, $checked=false, $instructions=null,
206                       $value='true', $disabled=false)
207     {
208         $attrs = array('name' => $id,
209                        'type' => 'checkbox',
210                        'class' => 'checkbox',
211                        'id' => $id);
212         if ($value) {
213             $attrs['value'] = $value;
214         }
215         if ($checked) {
216             $attrs['checked'] = 'checked';
217         }
218         if ($disabled) {
219             $attrs['disabled'] = 'true';
220         }
221         $this->element('input', $attrs);
222         $this->text(' ');
223         $this->element('label', array('class' => 'checkbox',
224                                       'for' => $id),
225                        $label);
226         $this->text(' ');
227         if ($instructions) {
228             $this->element('p', 'form_guide', $instructions);
229         }
230     }
231
232     /**
233      * output an HTML combobox/select and associated elements
234      *
235      * $content is an array of key-value pairs for the dropdown, where
236      * the key is the option value attribute and the value is the option
237      * text. (Careful on the overuse of 'value' here.)
238      *
239      * @param string $id           element ID, must be unique on page
240      * @param string $label        text of label for the element
241      * @param array  $content      options array, value => text
242      * @param string $instructions instructions for valid input
243      * @param string $blank_select whether to have a blank entry, default false
244      * @param string $selected     selected value, default null
245      *
246      * @return void
247      *
248      * @todo add a $name parameter
249      */
250
251     function dropdown($id, $label, $content, $instructions=null,
252                       $blank_select=false, $selected=null)
253     {
254         $this->element('label', array('for' => $id), $label);
255         $this->elementStart('select', array('id' => $id, 'name' => $id));
256         if ($blank_select) {
257             $this->element('option', array('value' => ''));
258         }
259         foreach ($content as $value => $option) {
260             if ($value == $selected) {
261                 $this->element('option', array('value' => $value,
262                                                'selected' => 'selected'),
263                                $option);
264             } else {
265                 $this->element('option', array('value' => $value), $option);
266             }
267         }
268         $this->elementEnd('select');
269         if ($instructions) {
270             $this->element('p', 'form_guide', $instructions);
271         }
272     }
273
274     /**
275      * output an HTML hidden element
276      *
277      * $id is re-used as name
278      *
279      * @param string $id    element ID, must be unique on page
280      * @param string $value hidden element value, default null
281      * @param string $name  name, if different than ID
282      *
283      * @return void
284      */
285
286     function hidden($id, $value, $name=null)
287     {
288         $this->element('input', array('name' => ($name) ? $name : $id,
289                                       'type' => 'hidden',
290                                       'id' => $id,
291                                       'value' => $value));
292     }
293
294     /**
295      * output an HTML password input and associated elements
296      *
297      * @param string $id           element ID, must be unique on page
298      * @param string $label        text of label for the element
299      * @param string $instructions instructions for valid input
300      *
301      * @return void
302      *
303      * @todo add a $name parameter
304      */
305
306     function password($id, $label, $instructions=null)
307     {
308         $this->element('label', array('for' => $id), $label);
309         $attrs = array('name' => $id,
310                        'type' => 'password',
311                        'class' => 'password',
312                        'id' => $id);
313         $this->element('input', $attrs);
314         if ($instructions) {
315             $this->element('p', 'form_guide', $instructions);
316         }
317     }
318
319     /**
320      * output an HTML submit input and associated elements
321      *
322      * @param string $id    element ID, must be unique on page
323      * @param string $label text of the button
324      * @param string $cls   class of the button, default 'submit'
325      * @param string $name  name, if different than ID
326      *
327      * @return void
328      *
329      * @todo add a $name parameter
330      */
331
332     function submit($id, $label, $cls='submit', $name=null, $title=null)
333     {
334         $this->element('input', array('type' => 'submit',
335                                       'id' => $id,
336                                       'name' => ($name) ? $name : $id,
337                                       'class' => $cls,
338                                       'value' => $label,
339                                       'title' => $title));
340     }
341
342     /**
343      * output a script (almost always javascript) tag
344      *
345      * @param string $src          relative or absolute script path
346      * @param string $type         'type' attribute value of the tag
347      *
348      * @return void
349      */
350     function script($src, $type='text/javascript')
351     {
352         $url = parse_url($src);
353         if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
354         {
355             $src = common_path($src) . '?version=' . STATUSNET_VERSION;
356         }
357         $this->element('script', array('type' => $type,
358                                                'src' => $src),
359                                ' ');
360     }
361
362     /**
363      * output a css link
364      *
365      * @param string $src     relative path within the theme directory, or an absolute path
366      * @param string $theme        'theme' that contains the stylesheet
367      * @param string media         'media' attribute of the tag
368      *
369      * @return void
370      */
371     function cssLink($src,$theme=null,$media=null)
372     {
373         $url = parse_url($src);
374         if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
375         {
376             if(file_exists(theme_file($src,$theme))){
377                $src = theme_path($src, $theme) . '?version=' . STATUSNET_VERSION;
378             }else{
379                $src = common_path($src);
380             }
381         }
382         $this->element('link', array('rel' => 'stylesheet',
383                                 'type' => 'text/css',
384                                 'href' => $src,
385                                 'media' => $media));
386     }
387
388     /**
389      * output an HTML textarea and associated elements
390      *
391      * @param string $id           element ID, must be unique on page
392      * @param string $label        text of label for the element
393      * @param string $content      content of the textarea, default none
394      * @param string $instructions instructions for valid input
395      *
396      * @return void
397      *
398      * @todo add a $name parameter
399      * @todo add a $cols parameter
400      * @todo add a $rows parameter
401      */
402
403     function textarea($id, $label, $content=null, $instructions=null)
404     {
405         $this->element('label', array('for' => $id), $label);
406         $this->element('textarea', array('rows' => 3,
407                                          'cols' => 40,
408                                          'name' => $id,
409                                          'id' => $id),
410                        ($content) ? $content : '');
411         if ($instructions) {
412             $this->element('p', 'form_guide', $instructions);
413         }
414     }
415
416
417     /**
418     * Internal script to autofocus the given element on page onload.
419     *
420     * @param string $id element ID, must refer to an existing element
421     *
422     * @return void
423     *
424     */
425     function autofocus($id)
426     {
427         $this->elementStart('script', array('type' => 'text/javascript'));
428         $this->raw('
429         <!--
430         $(document).ready(function() {
431             var el = $("#' . $id . '");
432             if (el.length) {
433                 el.focus();
434             }
435         });
436         -->
437         ');
438         $this->elementEnd('script');
439     }
440 }