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',
114 '-//W3C//DTD XHTML 1.0 Strict//EN',
115 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
117 $language = $this->getLanguage();
119 $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
120 'xml:lang' => $language,
121 'lang' => $language));
124 function getLanguage()
126 // FIXME: correct language for interface
127 return common_language();
131 * Ends an HTML document
137 $this->elementEnd('html');
142 * To specify additional HTTP headers for the action
146 function extraHeaders()
148 // Needs to be overloaded
152 * Output an HTML text input element
154 * Despite the name, it is specifically for outputting a
155 * text input element, not other <input> elements. It outputs
156 * a cluster of elements, including a <label> and an associated
159 * @param string $id element ID, must be unique on page
160 * @param string $label text of label for the element
161 * @param string $value value of the element, default null
162 * @param string $instructions instructions for valid input
164 * @todo add a $name parameter
165 * @todo add a $maxLength parameter
166 * @todo add a $size parameter
171 function input($id, $label, $value=null, $instructions=null)
173 $this->element('label', array('for' => $id), $label);
174 $attrs = array('name' => $id,
178 $attrs['value'] = $value;
180 $this->element('input', $attrs);
182 $this->element('p', 'form_guide', $instructions);
187 * output an HTML checkbox and associated elements
189 * Note that the value is default 'true' (the string), which can
190 * be used by Action::boolean()
192 * @param string $id element ID, must be unique on page
193 * @param string $label text of label for the element
194 * @param string $checked if the box is checked, default false
195 * @param string $instructions instructions for valid input
196 * @param string $value value of the checkbox, default 'true'
197 * @param string $disabled show the checkbox disabled, default false
201 * @todo add a $name parameter
204 function checkbox($id, $label, $checked=false, $instructions=null,
205 $value='true', $disabled=false)
207 $attrs = array('name' => $id,
208 'type' => 'checkbox',
209 'class' => 'checkbox',
212 $attrs['value'] = $value;
215 $attrs['checked'] = 'checked';
218 $attrs['disabled'] = 'true';
220 $this->element('input', $attrs);
222 $this->element('label', array('class' => 'checkbox',
227 $this->element('p', 'form_guide', $instructions);
232 * output an HTML combobox/select and associated elements
234 * $content is an array of key-value pairs for the dropdown, where
235 * the key is the option value attribute and the value is the option
236 * text. (Careful on the overuse of 'value' here.)
238 * @param string $id element ID, must be unique on page
239 * @param string $label text of label for the element
240 * @param array $content options array, value => text
241 * @param string $instructions instructions for valid input
242 * @param string $blank_select whether to have a blank entry, default false
243 * @param string $selected selected value, default null
247 * @todo add a $name parameter
250 function dropdown($id, $label, $content, $instructions=null,
251 $blank_select=false, $selected=null)
253 $this->element('label', array('for' => $id), $label);
254 $this->elementStart('select', array('id' => $id, 'name' => $id));
256 $this->element('option', array('value' => ''));
258 foreach ($content as $value => $option) {
259 if ($value == $selected) {
260 $this->element('option', array('value' => $value,
261 'selected' => 'selected'),
264 $this->element('option', array('value' => $value), $option);
267 $this->elementEnd('select');
269 $this->element('p', 'form_guide', $instructions);
274 * output an HTML hidden element
276 * $id is re-used as name
278 * @param string $id element ID, must be unique on page
279 * @param string $value hidden element value, default null
280 * @param string $name name, if different than ID
285 function hidden($id, $value, $name=null)
287 $this->element('input', array('name' => ($name) ? $name : $id,
294 * output an HTML password input and associated elements
296 * @param string $id element ID, must be unique on page
297 * @param string $label text of label for the element
298 * @param string $instructions instructions for valid input
302 * @todo add a $name parameter
305 function password($id, $label, $instructions=null)
307 $this->element('label', array('for' => $id), $label);
308 $attrs = array('name' => $id,
309 'type' => 'password',
310 'class' => 'password',
312 $this->element('input', $attrs);
314 $this->element('p', 'form_guide', $instructions);
319 * output an HTML submit input and associated elements
321 * @param string $id element ID, must be unique on page
322 * @param string $label text of the button
323 * @param string $cls class of the button, default 'submit'
324 * @param string $name name, if different than ID
328 * @todo add a $name parameter
331 function submit($id, $label, $cls='submit', $name=null, $title=null)
333 $this->element('input', array('type' => 'submit',
335 'name' => ($name) ? $name : $id,
342 * output an HTML textarea and associated elements
344 * @param string $id element ID, must be unique on page
345 * @param string $label text of label for the element
346 * @param string $content content of the textarea, default none
347 * @param string $instructions instructions for valid input
351 * @todo add a $name parameter
352 * @todo add a $cols parameter
353 * @todo add a $rows parameter
356 function textarea($id, $label, $content=null, $instructions=null)
358 $this->element('label', array('for' => $id), $label);
359 $this->element('textarea', array('rows' => 3,
363 ($content) ? $content : '');
365 $this->element('p', 'form_guide', $instructions);