]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Allow setting separate name and id on form input elements
[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 // Can include XHTML options but these are too fragile in practice.
38 define('PAGE_TYPE_PREFS', 'text/html');
39
40 /**
41  * Low-level generator for HTML
42  *
43  * Abstracts some of the code necessary for HTML generation. Especially
44  * has methods for generating HTML form elements. Note that these have
45  * been created kind of haphazardly, not with an eye to making a general
46  * HTML-creation class.
47  *
48  * @category Output
49  * @package  StatusNet
50  * @author   Evan Prodromou <evan@status.net>
51  * @author   Sarven Capadisli <csarven@status.net>
52  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
53  * @link     http://status.net/
54  *
55  * @see      Action
56  * @see      XMLOutputter
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=null)
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                 // TRANS: Client exception 406
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 (preg_match("/.*\/.*xml/", $type)) {
113             // Required for XML documents
114             $this->xw->startDocument('1.0', 'UTF-8');
115         }
116         $this->xw->writeDTD('html',
117                             '-//W3C//DTD XHTML 1.0 Strict//EN',
118                             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
119
120         $language = $this->getLanguage();
121
122         $attrs = array(
123             'xmlns' => 'http://www.w3.org/1999/xhtml',
124             'xml:lang' => $language,
125             'lang' => $language
126         );
127
128         if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
129             $this->elementStart('html', $attrs);
130             Event::handle('EndHtmlElement', array($this, &$attrs));
131         }
132     }
133
134     function getLanguage()
135     {
136         // FIXME: correct language for interface
137         return common_language();
138     }
139
140     /**
141     *  Ends an HTML document
142     *
143     *  @return void
144     */
145     function endHTML()
146     {
147         $this->elementEnd('html');
148         $this->endXML();
149     }
150
151     /**
152     *  To specify additional HTTP headers for the action
153     *
154     *  @return void
155     */
156     function extraHeaders()
157     {
158         // Needs to be overloaded
159     }
160
161     /**
162      * Output an HTML text input element
163      *
164      * Despite the name, it is specifically for outputting a
165      * text input element, not other <input> elements. It outputs
166      * a cluster of elements, including a <label> and an associated
167      * instructions span.
168      *
169      * @param string $id           element ID, must be unique on page
170      * @param string $label        text of label for the element
171      * @param string $value        value of the element, default null
172      * @param string $instructions instructions for valid input
173      * @param string $name         name of the element; if null, the id will
174      *                             be used
175      *
176      * @todo add a $maxLength parameter
177      * @todo add a $size parameter
178      *
179      * @return void
180      */
181
182     function input($id, $label, $value=null, $instructions=null, $name=null)
183     {
184         $this->element('label', array('for' => $id), $label);
185         $attrs = array('type' => 'text',
186                        'id'   => $id);
187         $attrs['name'] = is_null($name) ? $id : $name;
188         if (!is_null($value)) { // value can be 0 or ''
189             $attrs['value'] = $value;
190         }
191         $this->element('input', $attrs);
192         if ($instructions) {
193             $this->element('p', 'form_guide', $instructions);
194         }
195     }
196
197     /**
198      * output an HTML checkbox and associated elements
199      *
200      * Note that the value is default 'true' (the string), which can
201      * be used by Action::boolean()
202      *
203      * @param string $id           element ID, must be unique on page
204      * @param string $label        text of label for the element
205      * @param string $checked      if the box is checked, default false
206      * @param string $instructions instructions for valid input
207      * @param string $value        value of the checkbox, default 'true'
208      * @param string $disabled     show the checkbox disabled, default false
209      *
210      * @return void
211      *
212      * @todo add a $name parameter
213      */
214
215     function checkbox($id, $label, $checked=false, $instructions=null,
216                       $value='true', $disabled=false)
217     {
218         $attrs = array('name' => $id,
219                        'type' => 'checkbox',
220                        'class' => 'checkbox',
221                        'id' => $id);
222         if ($value) {
223             $attrs['value'] = $value;
224         }
225         if ($checked) {
226             $attrs['checked'] = 'checked';
227         }
228         if ($disabled) {
229             $attrs['disabled'] = 'true';
230         }
231         $this->element('input', $attrs);
232         $this->text(' ');
233         $this->element('label', array('class' => 'checkbox',
234                                       'for' => $id),
235                        $label);
236         $this->text(' ');
237         if ($instructions) {
238             $this->element('p', 'form_guide', $instructions);
239         }
240     }
241
242     /**
243      * output an HTML combobox/select and associated elements
244      *
245      * $content is an array of key-value pairs for the dropdown, where
246      * the key is the option value attribute and the value is the option
247      * text. (Careful on the overuse of 'value' here.)
248      *
249      * @param string $id           element ID, must be unique on page
250      * @param string $label        text of label for the element
251      * @param array  $content      options array, value => text
252      * @param string $instructions instructions for valid input
253      * @param string $blank_select whether to have a blank entry, default false
254      * @param string $selected     selected value, default null
255      *
256      * @return void
257      *
258      * @todo add a $name parameter
259      */
260
261     function dropdown($id, $label, $content, $instructions=null,
262                       $blank_select=false, $selected=null)
263     {
264         $this->element('label', array('for' => $id), $label);
265         $this->elementStart('select', array('id' => $id, 'name' => $id));
266         if ($blank_select) {
267             $this->element('option', array('value' => ''));
268         }
269         foreach ($content as $value => $option) {
270             if ($value == $selected) {
271                 $this->element('option', array('value' => $value,
272                                                'selected' => 'selected'),
273                                $option);
274             } else {
275                 $this->element('option', array('value' => $value), $option);
276             }
277         }
278         $this->elementEnd('select');
279         if ($instructions) {
280             $this->element('p', 'form_guide', $instructions);
281         }
282     }
283
284     /**
285      * output an HTML hidden element
286      *
287      * $id is re-used as name
288      *
289      * @param string $id    element ID, must be unique on page
290      * @param string $value hidden element value, default null
291      * @param string $name  name, if different than ID
292      *
293      * @return void
294      */
295
296     function hidden($id, $value, $name=null)
297     {
298         $this->element('input', array('name' => ($name) ? $name : $id,
299                                       'type' => 'hidden',
300                                       'id' => $id,
301                                       'value' => $value));
302     }
303
304     /**
305      * output an HTML password input and associated elements
306      *
307      * @param string $id           element ID, must be unique on page
308      * @param string $label        text of label for the element
309      * @param string $instructions instructions for valid input
310      *
311      * @return void
312      *
313      * @todo add a $name parameter
314      */
315
316     function password($id, $label, $instructions=null)
317     {
318         $this->element('label', array('for' => $id), $label);
319         $attrs = array('name' => $id,
320                        'type' => 'password',
321                        'class' => 'password',
322                        'id' => $id);
323         $this->element('input', $attrs);
324         if ($instructions) {
325             $this->element('p', 'form_guide', $instructions);
326         }
327     }
328
329     /**
330      * output an HTML submit input and associated elements
331      *
332      * @param string $id    element ID, must be unique on page
333      * @param string $label text of the button
334      * @param string $cls   class of the button, default 'submit'
335      * @param string $name  name, if different than ID
336      * @param string $title  title text for the submit button
337      *
338      * @return void
339      *
340      * @todo add a $name parameter
341      */
342
343     function submit($id, $label, $cls='submit', $name=null, $title=null)
344     {
345         $this->element('input', array('type' => 'submit',
346                                       'id' => $id,
347                                       'name' => ($name) ? $name : $id,
348                                       'class' => $cls,
349                                       'value' => $label,
350                                       'title' => $title));
351     }
352
353     /**
354      * output a script (almost always javascript) tag
355      *
356      * @param string $src          relative or absolute script path
357      * @param string $type         'type' attribute value of the tag
358      *
359      * @return void
360      */
361     function script($src, $type='text/javascript')
362     {
363         if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
364
365             $url = parse_url($src);
366
367             if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
368
369                 // XXX: this seems like a big assumption
370
371                 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
372
373                     $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . STATUSNET_VERSION;
374
375                 } else {
376
377                     if (StatusNet::isHTTPS()) {
378
379                         $sslserver = common_config('javascript', 'sslserver');
380
381                         if (empty($sslserver)) {
382                             if (is_string(common_config('site', 'sslserver')) &&
383                                 mb_strlen(common_config('site', 'sslserver')) > 0) {
384                                 $server = common_config('site', 'sslserver');
385                             } else if (common_config('site', 'server')) {
386                                 $server = common_config('site', 'server');
387                             }
388                             $path   = common_config('site', 'path') . '/js/';
389                         } else {
390                             $server = $sslserver;
391                             $path   = common_config('javascript', 'sslpath');
392                             if (empty($path)) {
393                                 $path = common_config('javascript', 'path');
394                             }
395                         }
396
397                         $protocol = 'https';
398
399                     } else {
400
401                         $path = common_config('javascript', 'path');
402
403                         if (empty($path)) {
404                             $path = common_config('site', 'path') . '/js/';
405                         }
406
407                         $server = common_config('javascript', 'server');
408
409                         if (empty($server)) {
410                             $server = common_config('site', 'server');
411                         }
412
413                         $protocol = 'http';
414                     }
415
416                     if ($path[strlen($path)-1] != '/') {
417                         $path .= '/';
418                     }
419
420                     if ($path[0] != '/') {
421                         $path = '/'.$path;
422                     }
423
424                     $src = $protocol.'://'.$server.$path.$src . '?version=' . STATUSNET_VERSION;
425                 }
426             }
427
428             $this->element('script', array('type' => $type,
429                                            'src' => $src),
430                            ' ');
431
432             Event::handle('EndScriptElement', array($this,$src,$type));
433         }
434     }
435
436     /**
437      * output a script (almost always javascript) tag with inline
438      * code.
439      *
440      * @param string $code         code to put in the script tag
441      * @param string $type         'type' attribute value of the tag
442      *
443      * @return void
444      */
445
446     function inlineScript($code, $type='text/javascript')
447     {
448         if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
449             $this->elementStart('script', array('type' => $type));
450             if($type == 'text/javascript') {
451                 $this->raw('/*<![CDATA[*/ '); // XHTML compat
452             }
453             $this->raw($code);
454             if($type == 'text/javascript') {
455                 $this->raw(' /*]]>*/'); // XHTML compat
456             }
457             $this->elementEnd('script');
458             Event::handle('EndInlineScriptElement', array($this,$code,$type));
459         }
460     }
461
462     /**
463      * output a css link
464      *
465      * @param string $src     relative path within the theme directory, or an absolute path
466      * @param string $theme        'theme' that contains the stylesheet
467      * @param string media         'media' attribute of the tag
468      *
469      * @return void
470      */
471     function cssLink($src,$theme=null,$media=null)
472     {
473         if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
474             $url = parse_url($src);
475             if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
476             {
477                 if(file_exists(Theme::file($src,$theme))){
478                    $src = Theme::path($src, $theme);
479                 }else{
480                     $src = common_path($src, StatusNet::isHTTPS());
481                 }
482                 $src.= '?version=' . STATUSNET_VERSION;
483             }
484             $this->element('link', array('rel' => 'stylesheet',
485                                     'type' => 'text/css',
486                                     'href' => $src,
487                                     'media' => $media));
488             Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
489         }
490     }
491
492     /**
493      * output a style (almost always css) tag with inline
494      * code.
495      *
496      * @param string $code         code to put in the style tag
497      * @param string $type         'type' attribute value of the tag
498      * @param string $media        'media' attribute value of the tag
499      *
500      * @return void
501      */
502
503     function style($code, $type = 'text/css', $media = null)
504     {
505         if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
506             $this->elementStart('style', array('type' => $type, 'media' => $media));
507             $this->raw($code);
508             $this->elementEnd('style');
509             Event::handle('EndStyleElement', array($this,$code,$type,$media));
510         }
511     }
512
513     /**
514      * output an HTML textarea and associated elements
515      *
516      * @param string $id           element ID, must be unique on page
517      * @param string $label        text of label for the element
518      * @param string $content      content of the textarea, default none
519      * @param string $instructions instructions for valid input
520      *
521      * @return void
522      *
523      * @todo add a $name parameter
524      * @todo add a $cols parameter
525      * @todo add a $rows parameter
526      */
527
528     function textarea($id, $label, $content=null, $instructions=null)
529     {
530         $this->element('label', array('for' => $id), $label);
531         $this->element('textarea', array('rows' => 3,
532                                          'cols' => 40,
533                                          'name' => $id,
534                                          'id' => $id),
535                        ($content) ? $content : '');
536         if ($instructions) {
537             $this->element('p', 'form_guide', $instructions);
538         }
539     }
540
541     /**
542     * Internal script to autofocus the given element on page onload.
543     *
544     * @param string $id element ID, must refer to an existing element
545     *
546     * @return void
547     *
548     */
549     function autofocus($id)
550     {
551         $this->inlineScript(
552                    ' $(document).ready(function() {'.
553                    ' var el = $("#' . $id . '");'.
554                    ' if (el.length) { el.focus(); }'.
555                    ' });');
556     }
557 }