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
173 * @param string $name name of the element; if null, the id will
176 * @todo add a $maxLength parameter
177 * @todo add a $size parameter
182 function input($id, $label, $value=null, $instructions=null, $name=null)
184 $this->element('label', array('for' => $id), $label);
185 $attrs = array('type' => 'text',
187 $attrs['name'] = is_null($name) ? $id : $name;
188 if (!is_null($value)) { // value can be 0 or ''
189 $attrs['value'] = $value;
191 $this->element('input', $attrs);
193 $this->element('p', 'form_guide', $instructions);
198 * output an HTML checkbox and associated elements
200 * Note that the value is default 'true' (the string), which can
201 * be used by Action::boolean()
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
212 * @todo add a $name parameter
215 function checkbox($id, $label, $checked=false, $instructions=null,
216 $value='true', $disabled=false)
218 $attrs = array('name' => $id,
219 'type' => 'checkbox',
220 'class' => 'checkbox',
223 $attrs['value'] = $value;
226 $attrs['checked'] = 'checked';
229 $attrs['disabled'] = 'true';
231 $this->element('input', $attrs);
233 $this->element('label', array('class' => 'checkbox',
238 $this->element('p', 'form_guide', $instructions);
243 * output an HTML combobox/select and associated elements
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.)
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
258 * @todo add a $name parameter
261 function dropdown($id, $label, $content, $instructions=null,
262 $blank_select=false, $selected=null)
264 $this->element('label', array('for' => $id), $label);
265 $this->elementStart('select', array('id' => $id, 'name' => $id));
267 $this->element('option', array('value' => ''));
269 foreach ($content as $value => $option) {
270 if ($value == $selected) {
271 $this->element('option', array('value' => $value,
272 'selected' => 'selected'),
275 $this->element('option', array('value' => $value), $option);
278 $this->elementEnd('select');
280 $this->element('p', 'form_guide', $instructions);
285 * output an HTML hidden element
287 * $id is re-used as name
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
296 function hidden($id, $value, $name=null)
298 $this->element('input', array('name' => ($name) ? $name : $id,
305 * output an HTML password input and associated elements
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
313 * @todo add a $name parameter
316 function password($id, $label, $instructions=null)
318 $this->element('label', array('for' => $id), $label);
319 $attrs = array('name' => $id,
320 'type' => 'password',
321 'class' => 'password',
323 $this->element('input', $attrs);
325 $this->element('p', 'form_guide', $instructions);
330 * output an HTML submit input and associated elements
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
340 * @todo add a $name parameter
343 function submit($id, $label, $cls='submit', $name=null, $title=null)
345 $this->element('input', array('type' => 'submit',
347 'name' => ($name) ? $name : $id,
354 * output a script (almost always javascript) tag
356 * @param string $src relative or absolute script path
357 * @param string $type 'type' attribute value of the tag
361 function script($src, $type='text/javascript')
363 if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
365 $url = parse_url($src);
367 if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
369 // XXX: this seems like a big assumption
371 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
373 $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . STATUSNET_VERSION;
377 if (StatusNet::isHTTPS()) {
379 $sslserver = common_config('javascript', 'sslserver');
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');
388 $path = common_config('site', 'path') . '/js/';
390 $server = $sslserver;
391 $path = common_config('javascript', 'sslpath');
393 $path = common_config('javascript', 'path');
401 $path = common_config('javascript', 'path');
404 $path = common_config('site', 'path') . '/js/';
407 $server = common_config('javascript', 'server');
409 if (empty($server)) {
410 $server = common_config('site', 'server');
416 if ($path[strlen($path)-1] != '/') {
420 if ($path[0] != '/') {
424 $src = $protocol.'://'.$server.$path.$src . '?version=' . STATUSNET_VERSION;
428 $this->element('script', array('type' => $type,
432 Event::handle('EndScriptElement', array($this,$src,$type));
437 * output a script (almost always javascript) tag with inline
440 * @param string $code code to put in the script tag
441 * @param string $type 'type' attribute value of the tag
446 function inlineScript($code, $type='text/javascript')
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
454 if($type == 'text/javascript') {
455 $this->raw(' /*]]>*/'); // XHTML compat
457 $this->elementEnd('script');
458 Event::handle('EndInlineScriptElement', array($this,$code,$type));
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
471 function cssLink($src,$theme=null,$media=null)
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']))
477 if(file_exists(Theme::file($src,$theme))){
478 $src = Theme::path($src, $theme);
480 $src = common_path($src, StatusNet::isHTTPS());
482 $src.= '?version=' . STATUSNET_VERSION;
484 $this->element('link', array('rel' => 'stylesheet',
485 'type' => 'text/css',
488 Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
493 * output a style (almost always css) tag with inline
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
503 function style($code, $type = 'text/css', $media = null)
505 if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
506 $this->elementStart('style', array('type' => $type, 'media' => $media));
508 $this->elementEnd('style');
509 Event::handle('EndStyleElement', array($this,$code,$type,$media));
514 * output an HTML textarea and associated elements
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 * @param string $name name of textarea; if null, $id will be used
521 * @param int $cols number of columns
522 * @param int $rows number of rows
531 $instructions = null,
536 $this->element('label', array('for' => $id), $label);
542 $attrs['name'] = is_null($name) ? $id : $name;
545 $attrs['cols'] = $cols;
549 $attrs['rows'] = $rows;
554 is_null($content) ? '' : $content
557 $this->element('p', 'form_guide', $instructions);
562 * Internal script to autofocus the given element on page onload.
564 * @param string $id element ID, must refer to an existing element
569 function autofocus($id)
572 ' $(document).ready(function() {'.
573 ' var el = $("#' . $id . '");'.
574 ' if (el.length) { el.focus(); }'.