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 // TRANS: Client exception 406
104 throw new ClientException(_('This page is not available in a '.
105 'media type you accept'), 406);
109 header('Content-Type: '.$type);
111 $this->extraHeaders();
112 if (preg_match("/.*\/.*xml/", $type)) {
113 // Required for XML documents
114 $this->xw->startDocument('1.0', 'UTF-8');
116 $this->xw->writeDTD('html',
117 '-//W3C//DTD XHTML 1.0 Strict//EN',
118 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
120 $language = $this->getLanguage();
123 'xmlns' => 'http://www.w3.org/1999/xhtml',
124 'xml:lang' => $language,
128 if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
129 $this->elementStart('html', $attrs);
130 Event::handle('EndHtmlElement', array($this, &$attrs));
134 function getLanguage()
136 // FIXME: correct language for interface
137 return common_language();
141 * Ends an HTML document
147 $this->elementEnd('html');
152 * To specify additional HTTP headers for the action
156 function extraHeaders()
158 // Needs to be overloaded
162 * Output an HTML text input element
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
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
174 * @todo add a $name parameter
175 * @todo add a $maxLength parameter
176 * @todo add a $size parameter
181 function input($id, $label, $value=null, $instructions=null)
183 $this->element('label', array('for' => $id), $label);
184 $attrs = array('name' => $id,
187 if (!is_null($value)) { // value can be 0 or ''
188 $attrs['value'] = $value;
190 $this->element('input', $attrs);
192 $this->element('p', 'form_guide', $instructions);
197 * output an HTML checkbox and associated elements
199 * Note that the value is default 'true' (the string), which can
200 * be used by Action::boolean()
202 * @param string $id element ID, must be unique on page
203 * @param string $label text of label for the element
204 * @param string $checked if the box is checked, default false
205 * @param string $instructions instructions for valid input
206 * @param string $value value of the checkbox, default 'true'
207 * @param string $disabled show the checkbox disabled, default false
211 * @todo add a $name parameter
214 function checkbox($id, $label, $checked=false, $instructions=null,
215 $value='true', $disabled=false)
217 $attrs = array('name' => $id,
218 'type' => 'checkbox',
219 'class' => 'checkbox',
222 $attrs['value'] = $value;
225 $attrs['checked'] = 'checked';
228 $attrs['disabled'] = 'true';
230 $this->element('input', $attrs);
232 $this->element('label', array('class' => 'checkbox',
237 $this->element('p', 'form_guide', $instructions);
242 * output an HTML combobox/select and associated elements
244 * $content is an array of key-value pairs for the dropdown, where
245 * the key is the option value attribute and the value is the option
246 * text. (Careful on the overuse of 'value' here.)
248 * @param string $id element ID, must be unique on page
249 * @param string $label text of label for the element
250 * @param array $content options array, value => text
251 * @param string $instructions instructions for valid input
252 * @param string $blank_select whether to have a blank entry, default false
253 * @param string $selected selected value, default null
257 * @todo add a $name parameter
260 function dropdown($id, $label, $content, $instructions=null,
261 $blank_select=false, $selected=null)
263 $this->element('label', array('for' => $id), $label);
264 $this->elementStart('select', array('id' => $id, 'name' => $id));
266 $this->element('option', array('value' => ''));
268 foreach ($content as $value => $option) {
269 if ($value == $selected) {
270 $this->element('option', array('value' => $value,
271 'selected' => 'selected'),
274 $this->element('option', array('value' => $value), $option);
277 $this->elementEnd('select');
279 $this->element('p', 'form_guide', $instructions);
284 * output an HTML hidden element
286 * $id is re-used as name
288 * @param string $id element ID, must be unique on page
289 * @param string $value hidden element value, default null
290 * @param string $name name, if different than ID
295 function hidden($id, $value, $name=null)
297 $this->element('input', array('name' => ($name) ? $name : $id,
304 * output an HTML password input and associated elements
306 * @param string $id element ID, must be unique on page
307 * @param string $label text of label for the element
308 * @param string $instructions instructions for valid input
312 * @todo add a $name parameter
315 function password($id, $label, $instructions=null)
317 $this->element('label', array('for' => $id), $label);
318 $attrs = array('name' => $id,
319 'type' => 'password',
320 'class' => 'password',
322 $this->element('input', $attrs);
324 $this->element('p', 'form_guide', $instructions);
329 * output an HTML submit input and associated elements
331 * @param string $id element ID, must be unique on page
332 * @param string $label text of the button
333 * @param string $cls class of the button, default 'submit'
334 * @param string $name name, if different than ID
335 * @param string $title title text for the submit button
339 * @todo add a $name parameter
342 function submit($id, $label, $cls='submit', $name=null, $title=null)
344 $this->element('input', array('type' => 'submit',
346 'name' => ($name) ? $name : $id,
353 * output a script (almost always javascript) tag
355 * @param string $src relative or absolute script path
356 * @param string $type 'type' attribute value of the tag
360 function script($src, $type='text/javascript')
362 if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
364 $url = parse_url($src);
366 if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
368 // XXX: this seems like a big assumption
370 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
372 $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . STATUSNET_VERSION;
376 if (StatusNet::isHTTPS()) {
378 $sslserver = common_config('javascript', 'sslserver');
380 if (empty($sslserver)) {
381 if (is_string(common_config('site', 'sslserver')) &&
382 mb_strlen(common_config('site', 'sslserver')) > 0) {
383 $server = common_config('site', 'sslserver');
384 } else if (common_config('site', 'server')) {
385 $server = common_config('site', 'server');
387 $path = common_config('site', 'path') . '/js/';
389 $server = $sslserver;
390 $path = common_config('javascript', 'sslpath');
392 $path = common_config('javascript', 'path');
400 $path = common_config('javascript', 'path');
403 $path = common_config('site', 'path') . '/js/';
406 $server = common_config('javascript', 'server');
408 if (empty($server)) {
409 $server = common_config('site', 'server');
415 if ($path[strlen($path)-1] != '/') {
419 if ($path[0] != '/') {
423 $src = $protocol.'://'.$server.$path.$src . '?version=' . STATUSNET_VERSION;
427 $this->element('script', array('type' => $type,
431 Event::handle('EndScriptElement', array($this,$src,$type));
436 * output a script (almost always javascript) tag with inline
439 * @param string $code code to put in the script tag
440 * @param string $type 'type' attribute value of the tag
445 function inlineScript($code, $type='text/javascript')
447 if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
448 $this->elementStart('script', array('type' => $type));
449 if($type == 'text/javascript') {
450 $this->raw('/*<![CDATA[*/ '); // XHTML compat
453 if($type == 'text/javascript') {
454 $this->raw(' /*]]>*/'); // XHTML compat
456 $this->elementEnd('script');
457 Event::handle('EndInlineScriptElement', array($this,$code,$type));
464 * @param string $src relative path within the theme directory, or an absolute path
465 * @param string $theme 'theme' that contains the stylesheet
466 * @param string media 'media' attribute of the tag
470 function cssLink($src,$theme=null,$media=null)
472 if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
473 $url = parse_url($src);
474 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
476 if(file_exists(Theme::file($src,$theme))){
477 $src = Theme::path($src, $theme);
479 $src = common_path($src, StatusNet::isHTTPS());
481 $src.= '?version=' . STATUSNET_VERSION;
483 $this->element('link', array('rel' => 'stylesheet',
484 'type' => 'text/css',
487 Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
492 * output a style (almost always css) tag with inline
495 * @param string $code code to put in the style tag
496 * @param string $type 'type' attribute value of the tag
497 * @param string $media 'media' attribute value of the tag
502 function style($code, $type = 'text/css', $media = null)
504 if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
505 $this->elementStart('style', array('type' => $type, 'media' => $media));
507 $this->elementEnd('style');
508 Event::handle('EndStyleElement', array($this,$code,$type,$media));
513 * output an HTML textarea and associated elements
515 * @param string $id element ID, must be unique on page
516 * @param string $label text of label for the element
517 * @param string $content content of the textarea, default none
518 * @param string $instructions instructions for valid input
522 * @todo add a $name parameter
523 * @todo add a $cols parameter
524 * @todo add a $rows parameter
527 function textarea($id, $label, $content=null, $instructions=null)
529 $this->element('label', array('for' => $id), $label);
530 $this->element('textarea', array('rows' => 3,
534 ($content) ? $content : '');
536 $this->element('p', 'form_guide', $instructions);
541 * Internal script to autofocus the given element on page onload.
543 * @param string $id element ID, must refer to an existing element
548 function autofocus($id)
551 ' $(document).ready(function() {'.
552 ' var el = $("#' . $id . '");'.
553 ' if (el.length) { el.focus(); }'.