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 throw new ClientException(_('This page is not available in a '.
105 'media type you accept'), 406);
109 header('Content-Type: '.$type);
111 $this->extraHeaders();
113 $this->startXML('html');
115 $language = $this->getLanguage();
117 $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
118 'xml:lang' => $language,
119 'lang' => $language));
122 function getLanguage()
124 // FIXME: correct language for interface
125 return common_language();
129 * Ends an HTML document
135 $this->elementEnd('html');
140 * To specify additional HTTP headers for the action
144 function extraHeaders()
146 // Needs to be overloaded
150 * Output an HTML text input element
152 * Despite the name, it is specifically for outputting a
153 * text input element, not other <input> elements. It outputs
154 * a cluster of elements, including a <label> and an associated
157 * @param string $id element ID, must be unique on page
158 * @param string $label text of label for the element
159 * @param string $value value of the element, default null
160 * @param string $instructions instructions for valid input
162 * @todo add a $name parameter
163 * @todo add a $maxLength parameter
164 * @todo add a $size parameter
169 function input($id, $label, $value=null, $instructions=null)
171 $this->element('label', array('for' => $id), $label);
172 $attrs = array('name' => $id,
176 $attrs['value'] = $value;
178 $this->element('input', $attrs);
180 $this->element('p', 'form_guide', $instructions);
185 * output an HTML checkbox and associated elements
187 * Note that the value is default 'true' (the string), which can
188 * be used by Action::boolean()
190 * @param string $id element ID, must be unique on page
191 * @param string $label text of label for the element
192 * @param string $checked if the box is checked, default false
193 * @param string $instructions instructions for valid input
194 * @param string $value value of the checkbox, default 'true'
195 * @param string $disabled show the checkbox disabled, default false
199 * @todo add a $name parameter
202 function checkbox($id, $label, $checked=false, $instructions=null,
203 $value='true', $disabled=false)
205 $attrs = array('name' => $id,
206 'type' => 'checkbox',
207 'class' => 'checkbox',
210 $attrs['value'] = $value;
213 $attrs['checked'] = 'checked';
216 $attrs['disabled'] = 'true';
218 $this->element('input', $attrs);
220 $this->element('label', array('class' => 'checkbox',
225 $this->element('p', 'form_guide', $instructions);
230 * output an HTML combobox/select and associated elements
232 * $content is an array of key-value pairs for the dropdown, where
233 * the key is the option value attribute and the value is the option
234 * text. (Careful on the overuse of 'value' here.)
236 * @param string $id element ID, must be unique on page
237 * @param string $label text of label for the element
238 * @param array $content options array, value => text
239 * @param string $instructions instructions for valid input
240 * @param string $blank_select whether to have a blank entry, default false
241 * @param string $selected selected value, default null
245 * @todo add a $name parameter
248 function dropdown($id, $label, $content, $instructions=null,
249 $blank_select=false, $selected=null)
251 $this->element('label', array('for' => $id), $label);
252 $this->elementStart('select', array('id' => $id, 'name' => $id));
254 $this->element('option', array('value' => ''));
256 foreach ($content as $value => $option) {
257 if ($value == $selected) {
258 $this->element('option', array('value' => $value,
259 'selected' => 'selected'),
262 $this->element('option', array('value' => $value), $option);
265 $this->elementEnd('select');
267 $this->element('p', 'form_guide', $instructions);
272 * output an HTML hidden element
274 * $id is re-used as name
276 * @param string $id element ID, must be unique on page
277 * @param string $value hidden element value, default null
278 * @param string $name name, if different than ID
283 function hidden($id, $value, $name=null)
285 $this->element('input', array('name' => ($name) ? $name : $id,
292 * output an HTML password input and associated elements
294 * @param string $id element ID, must be unique on page
295 * @param string $label text of label for the element
296 * @param string $instructions instructions for valid input
300 * @todo add a $name parameter
303 function password($id, $label, $instructions=null)
305 $this->element('label', array('for' => $id), $label);
306 $attrs = array('name' => $id,
307 'type' => 'password',
308 'class' => 'password',
310 $this->element('input', $attrs);
312 $this->element('p', 'form_guide', $instructions);
317 * output an HTML submit input and associated elements
319 * @param string $id element ID, must be unique on page
320 * @param string $label text of the button
321 * @param string $cls class of the button, default 'submit'
322 * @param string $name name, if different than ID
326 * @todo add a $name parameter
329 function submit($id, $label, $cls='submit', $name=null, $title=null)
331 $this->element('input', array('type' => 'submit',
333 'name' => ($name) ? $name : $id,
340 * output an HTML textarea and associated elements
342 * @param string $id element ID, must be unique on page
343 * @param string $label text of label for the element
344 * @param string $content content of the textarea, default none
345 * @param string $instructions instructions for valid input
349 * @todo add a $name parameter
350 * @todo add a $cols parameter
351 * @todo add a $rows parameter
354 function textarea($id, $label, $content=null, $instructions=null)
356 $this->element('label', array('for' => $id), $label);
357 $this->element('textarea', array('rows' => 3,
361 ($content) ? $content : '');
363 $this->element('p', 'form_guide', $instructions);