3 * Laconica, 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@controlyourself.ca>
25 * @author Sarven Capadisli <csarven@controlyourself.ca>
26 * @copyright 2008 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!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@controlyourself.ca>
52 * @author Sarven Capadisli <csarven@controlyourself.ca>
53 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
54 * @link http://laconi.ca/
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 common_user_error(_('This page is not available in a '.
105 'media type you accept'), 406);
110 header('Content-Type: '.$type);
112 $this->startXML('html',
113 '-//W3C//DTD XHTML 1.0 Strict//EN',
114 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
116 // FIXME: correct language for interface
118 $language = common_language();
120 $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
121 'xml:lang' => $language,
122 'lang' => $language));
126 * Output an HTML text input element
128 * Despite the name, it is specifically for outputting a
129 * text input element, not other <input> elements. It outputs
130 * a cluster of elements, including a <label> and an associated
133 * @param string $id element ID, must be unique on page
134 * @param string $label text of label for the element
135 * @param string $value value of the element, default null
136 * @param string $instructions instructions for valid input
138 * @todo add a $name parameter
139 * @todo add a $maxLength parameter
140 * @todo add a $size parameter
145 function input($id, $label, $value=null, $instructions=null)
147 $this->elementStart('p');
148 $this->element('label', array('for' => $id), $label);
149 $attrs = array('name' => $id,
151 'class' => 'input_text',
154 $attrs['value'] = htmlspecialchars($value);
156 $this->element('input', $attrs);
158 $this->element('span', 'input_instructions', $instructions);
160 $this->elementEnd('p');
164 * output an HTML checkbox and associated elements
166 * Note that the value is default 'true' (the string), which can
167 * be used by Action::boolean()
169 * @param string $id element ID, must be unique on page
170 * @param string $label text of label for the element
171 * @param string $checked if the box is checked, default false
172 * @param string $instructions instructions for valid input
173 * @param string $value value of the checkbox, default 'true'
174 * @param string $disabled show the checkbox disabled, default false
178 * @todo add a $name parameter
181 function checkbox($id, $label, $checked=false, $instructions=null,
182 $value='true', $disabled=false)
184 $this->elementStart('p');
185 $attrs = array('name' => $id,
186 'type' => 'checkbox',
187 'class' => 'checkbox',
190 $attrs['value'] = htmlspecialchars($value);
193 $attrs['checked'] = 'checked';
196 $attrs['disabled'] = 'true';
198 $this->element('input', $attrs);
200 $this->element('label', array('class' => 'checkbox_label',
205 $this->element('span', 'input_instructions', $instructions);
207 $this->elementEnd('p');
211 * output an HTML combobox/select and associated elements
213 * $content is an array of key-value pairs for the dropdown, where
214 * the key is the option value attribute and the value is the option
215 * text. (Careful on the overuse of 'value' here.)
217 * @param string $id element ID, must be unique on page
218 * @param string $label text of label for the element
219 * @param array $content options array, value => text
220 * @param string $instructions instructions for valid input
221 * @param string $blank_select whether to have a blank entry, default false
222 * @param string $selected selected value, default null
226 * @todo add a $name parameter
229 function dropdown($id, $label, $content, $instructions=null,
230 $blank_select=false, $selected=null)
232 $this->elementStart('p');
233 $this->element('label', array('for' => $id), $label);
234 $this->elementStart('select', array('id' => $id, 'name' => $id));
236 $this->element('option', array('value' => ''));
238 foreach ($content as $value => $option) {
239 if ($value == $selected) {
240 $this->element('option', array('value' => $value,
241 'selected' => $value),
244 $this->element('option', array('value' => $value), $option);
247 $this->elementEnd('select');
249 $this->element('span', 'input_instructions', $instructions);
251 $this->elementEnd('p');
255 * output an HTML hidden element
257 * $id is re-used as name
259 * @param string $id element ID, must be unique on page
260 * @param string $value hidden element value, default null
261 * @param string $name name, if different than ID
266 function hidden($id, $value, $name=null)
268 $this->element('input', array('name' => ($name) ? $name : $id,
275 * output an HTML password input and associated elements
277 * @param string $id element ID, must be unique on page
278 * @param string $label text of label for the element
279 * @param string $instructions instructions for valid input
283 * @todo add a $name parameter
286 function password($id, $label, $instructions=null)
288 $this->elementStart('p');
289 $this->element('label', array('for' => $id), $label);
290 $attrs = array('name' => $id,
291 'type' => 'password',
292 'class' => 'password',
294 $this->element('input', $attrs);
296 $this->element('span', 'input_instructions', $instructions);
298 $this->elementEnd('p');
302 * output an HTML submit input and associated elements
304 * @param string $id element ID, must be unique on page
305 * @param string $label text of the button
306 * @param string $cls class of the button, default 'submit'
307 * @param string $name name, if different than ID
311 * @todo add a $name parameter
314 function submit($id, $label, $cls='submit', $name=null)
316 $this->elementStart('p');
317 $this->element('input', array('type' => 'submit',
319 'name' => ($name) ? $name : $id,
322 $this->elementEnd('p');
326 * output an HTML textarea and associated elements
328 * @param string $id element ID, must be unique on page
329 * @param string $label text of label for the element
330 * @param string $content content of the textarea, default none
331 * @param string $instructions instructions for valid input
335 * @todo add a $name parameter
336 * @todo add a $cols parameter
337 * @todo add a $rows parameter
340 function textarea($id, $label, $content=null, $instructions=null)
342 $this->elementStart('p');
343 $this->element('label', array('for' => $id), $label);
344 $this->element('textarea', array('rows' => 3,
348 ($content) ? $content : '');
350 $this->element('span', 'input_instructions', $instructions);
352 $this->elementEnd('p');