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 define('PAGE_TYPE_PREFS',
38 'text/html,application/xhtml+xml,'.
39 'application/xml;q=0.3,text/xml;q=0.2');
42 * Low-level generator for HTML
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.
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/
60 class HTMLOutputter extends XMLOutputter
65 * Just wraps the XMLOutputter constructor.
67 * @param string $output URI to output to, default = stdout
68 * @param boolean $indent Whether to indent output, default true
71 function __construct($output='php://output', $indent=true)
73 parent::__construct($output, $indent);
77 * Start an HTML document
79 * If $type isn't specified, will attempt to do content negotiation.
81 * Attempts to do content negotiation for language, also.
83 * @param string $type MIME type to use; default is to do negotation.
85 * @todo extract content negotiation code to an HTTP module or class.
90 function startHTML($type=null)
93 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
94 $_SERVER['HTTP_ACCEPT'] : null;
96 // XXX: allow content negotiation for RDF, RSS, or XRDS
98 $cp = common_accept_to_prefs($httpaccept);
99 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
101 $type = common_negotiate_type($cp, $sp);
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( ! 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');
116 $this->xw->writeDTD('html');
118 $language = $this->getLanguage();
120 $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
121 'xml:lang' => $language,
122 'lang' => $language));
125 function getLanguage()
127 // FIXME: correct language for interface
128 return common_language();
132 * Ends an HTML document
138 $this->elementEnd('html');
143 * To specify additional HTTP headers for the action
147 function extraHeaders()
149 // Needs to be overloaded
153 * Output an HTML text input element
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
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
165 * @todo add a $name parameter
166 * @todo add a $maxLength parameter
167 * @todo add a $size parameter
172 function input($id, $label, $value=null, $instructions=null)
174 $this->element('label', array('for' => $id), $label);
175 $attrs = array('name' => $id,
179 $attrs['value'] = $value;
181 $this->element('input', $attrs);
183 $this->element('p', 'form_guide', $instructions);
188 * output an HTML checkbox and associated elements
190 * Note that the value is default 'true' (the string), which can
191 * be used by Action::boolean()
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
202 * @todo add a $name parameter
205 function checkbox($id, $label, $checked=false, $instructions=null,
206 $value='true', $disabled=false)
208 $attrs = array('name' => $id,
209 'type' => 'checkbox',
210 'class' => 'checkbox',
213 $attrs['value'] = $value;
216 $attrs['checked'] = 'checked';
219 $attrs['disabled'] = 'true';
221 $this->element('input', $attrs);
223 $this->element('label', array('class' => 'checkbox',
228 $this->element('p', 'form_guide', $instructions);
233 * output an HTML combobox/select and associated elements
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.)
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
248 * @todo add a $name parameter
251 function dropdown($id, $label, $content, $instructions=null,
252 $blank_select=false, $selected=null)
254 $this->element('label', array('for' => $id), $label);
255 $this->elementStart('select', array('id' => $id, 'name' => $id));
257 $this->element('option', array('value' => ''));
259 foreach ($content as $value => $option) {
260 if ($value == $selected) {
261 $this->element('option', array('value' => $value,
262 'selected' => 'selected'),
265 $this->element('option', array('value' => $value), $option);
268 $this->elementEnd('select');
270 $this->element('p', 'form_guide', $instructions);
275 * output an HTML hidden element
277 * $id is re-used as name
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
286 function hidden($id, $value, $name=null)
288 $this->element('input', array('name' => ($name) ? $name : $id,
295 * output an HTML password input and associated elements
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
303 * @todo add a $name parameter
306 function password($id, $label, $instructions=null)
308 $this->element('label', array('for' => $id), $label);
309 $attrs = array('name' => $id,
310 'type' => 'password',
311 'class' => 'password',
313 $this->element('input', $attrs);
315 $this->element('p', 'form_guide', $instructions);
320 * output an HTML submit input and associated elements
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
329 * @todo add a $name parameter
332 function submit($id, $label, $cls='submit', $name=null, $title=null)
334 $this->element('input', array('type' => 'submit',
336 'name' => ($name) ? $name : $id,
343 * output a script (almost always javascript) tag
345 * @param string $src relative or absolute script path
346 * @param string $type 'type' attribute value of the tag
350 function script($src, $type='text/javascript')
352 $url = parse_url($src);
353 if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
355 $src = common_path($src) . '?version=' . STATUSNET_VERSION;
357 $this->element('script', array('type' => $type,
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
371 function cssLink($src,$theme=null,$media=null)
373 $url = parse_url($src);
374 if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
376 if(file_exists(theme_file($src,$theme))){
377 $src = theme_path($src, $theme) . '?version=' . STATUSNET_VERSION;
379 $src = common_path($src);
382 $this->element('link', array('rel' => 'stylesheet',
383 'type' => 'text/css',
389 * output an HTML textarea and associated elements
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
398 * @todo add a $name parameter
399 * @todo add a $cols parameter
400 * @todo add a $rows parameter
403 function textarea($id, $label, $content=null, $instructions=null)
405 $this->element('label', array('for' => $id), $label);
406 $this->element('textarea', array('rows' => 3,
410 ($content) ? $content : '');
412 $this->element('p', 'form_guide', $instructions);
418 * Internal script to autofocus the given element on page onload.
420 * @param string $id element ID, must refer to an existing element
425 function autofocus($id)
427 $this->elementStart('script', array('type' => 'text/javascript'));
430 $(document).ready(function() {
431 var el = $("#' . $id . '");
438 $this->elementEnd('script');