3 * StatusNet, the distributed open-source microblogging tool
5 * Low-level generator for HTML
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.
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.
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/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/xmloutputter.php';
37 // Can include XHTML options but these are too fragile in practice.
38 define('PAGE_TYPE_PREFS', 'text/html');
41 * Low-level generator for HTML
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.
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/
59 class HTMLOutputter extends XMLOutputter
64 * Just wraps the XMLOutputter constructor.
66 * @param string $output URI to output to, default = stdout
67 * @param boolean $indent Whether to indent output, default true
70 function __construct($output='php://output', $indent=null)
72 parent::__construct($output, $indent);
76 * Start an HTML document
78 * If $type isn't specified, will attempt to do content negotiation.
80 * Attempts to do content negotiation for language, also.
82 * @param string $type MIME type to use; default is to do negotation.
84 * @todo extract content negotiation code to an HTTP module or class.
89 function startHTML($type=null)
92 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
93 $_SERVER['HTTP_ACCEPT'] : null;
95 // XXX: allow content negotiation for RDF, RSS, or XRDS
97 $cp = common_accept_to_prefs($httpaccept);
98 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
100 $type = common_negotiate_type($cp, $sp);
103 throw new ClientException(_('This page is not available in a '.
104 'media type you accept'), 406);
108 header('Content-Type: '.$type);
110 $this->extraHeaders();
111 if (preg_match("/.*\/.*xml/", $type)) {
112 // Required for XML documents
113 $this->xw->startDocument('1.0', 'UTF-8');
115 $this->xw->writeDTD('html',
116 '-//W3C//DTD XHTML 1.0 Strict//EN',
117 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
119 $language = $this->getLanguage();
121 $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
122 'xml:lang' => $language,
123 'lang' => $language));
126 function getLanguage()
128 // FIXME: correct language for interface
129 return common_language();
133 * Ends an HTML document
139 $this->elementEnd('html');
144 * To specify additional HTTP headers for the action
148 function extraHeaders()
150 // Needs to be overloaded
154 * Output an HTML text input element
156 * Despite the name, it is specifically for outputting a
157 * text input element, not other <input> elements. It outputs
158 * a cluster of elements, including a <label> and an associated
161 * @param string $id element ID, must be unique on page
162 * @param string $label text of label for the element
163 * @param string $value value of the element, default null
164 * @param string $instructions instructions for valid input
166 * @todo add a $name parameter
167 * @todo add a $maxLength parameter
168 * @todo add a $size parameter
173 function input($id, $label, $value=null, $instructions=null)
175 $this->element('label', array('for' => $id), $label);
176 $attrs = array('name' => $id,
180 $attrs['value'] = $value;
182 $this->element('input', $attrs);
184 $this->element('p', 'form_guide', $instructions);
189 * output an HTML checkbox and associated elements
191 * Note that the value is default 'true' (the string), which can
192 * be used by Action::boolean()
194 * @param string $id element ID, must be unique on page
195 * @param string $label text of label for the element
196 * @param string $checked if the box is checked, default false
197 * @param string $instructions instructions for valid input
198 * @param string $value value of the checkbox, default 'true'
199 * @param string $disabled show the checkbox disabled, default false
203 * @todo add a $name parameter
206 function checkbox($id, $label, $checked=false, $instructions=null,
207 $value='true', $disabled=false)
209 $attrs = array('name' => $id,
210 'type' => 'checkbox',
211 'class' => 'checkbox',
214 $attrs['value'] = $value;
217 $attrs['checked'] = 'checked';
220 $attrs['disabled'] = 'true';
222 $this->element('input', $attrs);
224 $this->element('label', array('class' => 'checkbox',
229 $this->element('p', 'form_guide', $instructions);
234 * output an HTML combobox/select and associated elements
236 * $content is an array of key-value pairs for the dropdown, where
237 * the key is the option value attribute and the value is the option
238 * text. (Careful on the overuse of 'value' here.)
240 * @param string $id element ID, must be unique on page
241 * @param string $label text of label for the element
242 * @param array $content options array, value => text
243 * @param string $instructions instructions for valid input
244 * @param string $blank_select whether to have a blank entry, default false
245 * @param string $selected selected value, default null
249 * @todo add a $name parameter
252 function dropdown($id, $label, $content, $instructions=null,
253 $blank_select=false, $selected=null)
255 $this->element('label', array('for' => $id), $label);
256 $this->elementStart('select', array('id' => $id, 'name' => $id));
258 $this->element('option', array('value' => ''));
260 foreach ($content as $value => $option) {
261 if ($value == $selected) {
262 $this->element('option', array('value' => $value,
263 'selected' => 'selected'),
266 $this->element('option', array('value' => $value), $option);
269 $this->elementEnd('select');
271 $this->element('p', 'form_guide', $instructions);
276 * output an HTML hidden element
278 * $id is re-used as name
280 * @param string $id element ID, must be unique on page
281 * @param string $value hidden element value, default null
282 * @param string $name name, if different than ID
287 function hidden($id, $value, $name=null)
289 $this->element('input', array('name' => ($name) ? $name : $id,
296 * output an HTML password input and associated elements
298 * @param string $id element ID, must be unique on page
299 * @param string $label text of label for the element
300 * @param string $instructions instructions for valid input
304 * @todo add a $name parameter
307 function password($id, $label, $instructions=null)
309 $this->element('label', array('for' => $id), $label);
310 $attrs = array('name' => $id,
311 'type' => 'password',
312 'class' => 'password',
314 $this->element('input', $attrs);
316 $this->element('p', 'form_guide', $instructions);
321 * output an HTML submit input and associated elements
323 * @param string $id element ID, must be unique on page
324 * @param string $label text of the button
325 * @param string $cls class of the button, default 'submit'
326 * @param string $name name, if different than ID
330 * @todo add a $name parameter
333 function submit($id, $label, $cls='submit', $name=null, $title=null)
335 $this->element('input', array('type' => 'submit',
337 'name' => ($name) ? $name : $id,
344 * output a script (almost always javascript) tag
346 * @param string $src relative or absolute script path
347 * @param string $type 'type' attribute value of the tag
351 function script($src, $type='text/javascript')
353 if(Event::handle('StartScriptElement', array($this,&$src,&$type))) {
355 $url = parse_url($src);
357 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
359 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
361 $src = common_path($src) . '?version=' . STATUSNET_VERSION;
365 $path = common_config('javascript', 'path');
368 $path = common_config('site', 'path') . '/js/';
371 if ($path[strlen($path)-1] != '/') {
375 if ($path[0] != '/') {
379 $server = common_config('javascript', 'server');
381 if (empty($server)) {
382 $server = common_config('site', 'server');
385 $ssl = common_config('javascript', 'ssl');
387 if (is_null($ssl)) { // null -> guess
388 if (common_config('site', 'ssl') == 'always' &&
389 !common_config('javascript', 'server')) {
396 $protocol = ($ssl) ? 'https' : 'http';
398 $src = $protocol.'://'.$server.$path.$src . '?version=' . STATUSNET_VERSION;
402 $this->element('script', array('type' => $type,
406 Event::handle('EndScriptElement', array($this,$src,$type));
411 * output a script (almost always javascript) tag with inline
414 * @param string $code code to put in the script tag
415 * @param string $type 'type' attribute value of the tag
420 function inlineScript($code, $type='text/javascript')
422 if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
423 $this->elementStart('script', array('type' => $type));
424 if($type == 'text/javascript') {
425 $this->raw('/*<![CDATA[*/ '); // XHTML compat
428 if($type == 'text/javascript') {
429 $this->raw(' /*]]>*/'); // XHTML compat
431 $this->elementEnd('script');
432 Event::handle('EndInlineScriptElement', array($this,$code,$type));
439 * @param string $src relative path within the theme directory, or an absolute path
440 * @param string $theme 'theme' that contains the stylesheet
441 * @param string media 'media' attribute of the tag
445 function cssLink($src,$theme=null,$media=null)
447 if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
448 $url = parse_url($src);
449 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
451 if(file_exists(Theme::file($src,$theme))){
452 $src = Theme::path($src, $theme);
454 $src = common_path($src);
456 $src.= '?version=' . STATUSNET_VERSION;
458 $this->element('link', array('rel' => 'stylesheet',
459 'type' => 'text/css',
462 Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
467 * output a style (almost always css) tag with inline
470 * @param string $code code to put in the style tag
471 * @param string $type 'type' attribute value of the tag
472 * @param string $media 'media' attribute value of the tag
477 function style($code, $type = 'text/css', $media = null)
479 if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
480 $this->elementStart('style', array('type' => $type, 'media' => $media));
482 $this->elementEnd('style');
483 Event::handle('EndStyleElement', array($this,$code,$type,$media));
488 * output an HTML textarea and associated elements
490 * @param string $id element ID, must be unique on page
491 * @param string $label text of label for the element
492 * @param string $content content of the textarea, default none
493 * @param string $instructions instructions for valid input
497 * @todo add a $name parameter
498 * @todo add a $cols parameter
499 * @todo add a $rows parameter
502 function textarea($id, $label, $content=null, $instructions=null)
504 $this->element('label', array('for' => $id), $label);
505 $this->element('textarea', array('rows' => 3,
509 ($content) ? $content : '');
511 $this->element('p', 'form_guide', $instructions);
516 * Internal script to autofocus the given element on page onload.
518 * @param string $id element ID, must refer to an existing element
523 function autofocus($id)
526 ' $(document).ready(function() {'.
527 ' var el = $("#' . $id . '");'.
528 ' if (el.length) { el.focus(); }'.