<?php # Copyright(C) 2004-2007 INL # Written by Victor Stinner <victor.stinner AT inl.fr> # # $Id: html.php 17927 2009-02-16 13:16:09Z haypo $ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see <http://www.gnu.org/licenses/>. function html_attributes($attr) { $html = ''; foreach ($attr as $key=>$value) { $html .= " $key=\"".htmlspecialchars($value).'"'; } return $html; } function html_open($name, $attr=Array(), $newline=false) { echo '<'.$name.html_attributes($attr).'>'; if ($newline) echo "\n"; } function html_close($name) { echo "</$name>\n"; } function html_tag($name, $attr, $content=null) { echo '<'.$name.html_attributes($attr); if (!is_null($content)) { echo '>'.htmlspecialchars($content); echo "</$name>\n"; } else { if (in_array($name, Array('img', 'hr'))) { /* Write <hr /> */ echo " />\n"; } else { /* Write <script (...)></script> */ echo "></$name>\n"; } } } function select_option($key, $value, $selected) { $attr = Array('value' => $key); if ($selected) { $attr['selected'] = "selected"; } html_tag('option', $attr, $value); } function select_array($name, $data, $selected=null, $default=null, $attr=Array()) { $attr['name'] = $name; html_open('select', $attr, true); if ($default) { list($key, $value) = $default; select_option($key, $value, is_null($selected)); } foreach ($data as $key=>$value) { $is_selected = ($selected and $key == $selected); select_option($key, $value, $is_selected); } echo " </select>\n"; } # Create '<a href="URL">TEXT</a>' string function createLink($url, $text, $attr=Array(), $raw_text=false) { if (!$raw_text) { $text = htmlspecialchars($text); } $attr['href'] = $url; return '<a'.html_attributes($attr).'>'.$text."</a>"; } function select_list($name, $data, $selected=null, $default=null) { $array = Array(); foreach ($data as $item) { $array[$item] = $item; } select_array($name, $array, $selected, $default); } function html_checkbox($name, $checked, $label=null, $attr=Array()) { if (!array_key_exists('id', $attr)) { $attr['id'] = "checkbox_$name"; } if ($checked) { $attr['checked'] = 'checked'; } $attr = array_merge(Array('name' => $name, 'type' => 'checkbox'), $attr); html_tag('input', $attr, null); if ($label) { html_tag('label', Array('for' => $attr['id']), $label); } } $_html_tail_javascript = Array(); $_html_tail_include_javascript = Array(); function html_include_javascript($url, $tail=false) { if ($tail) { global $_html_tail_include_javascript; $_html_tail_include_javascript[] = $url; return; } html_tag('script', Array( 'type' => 'text/javascript', 'src' => $url, )); } function html_javascript($code, $tail=false) { global $_html_tail_javascript; if (!is_array($code)) { $code = Array($code); } if (count($code) == 0) { return; } $code[0] = ltrim($code[0]); $last = count($code)-1; $code[$last] = rtrim($code[$last]); if (count($code) == 1 and !$code[0]) { return; } if ($tail) { $_html_tail_javascript = array_merge($_html_tail_javascript, $code); return; } html_open('script', Array('type' => 'text/javascript'), true); # echo "<!-- "; if (is_array($code)) { foreach ($code as $line) { echo $line."\n"; } } else { echo $code."\n"; } # echo "-->"; html_close('script'); } function _html_write_tail_javascript() { global $_html_tail_javascript, $_html_tail_include_javascript; foreach($_html_tail_include_javascript as $url) { html_include_javascript($url); } html_javascript($_html_tail_javascript); $_html_tail_javascript = Array(); $_html_tail_include_javascript = Array(); } function html_input_text($name, $value='', $attr=Array()) { $attr = array_merge(Array( 'name' => $name, 'value' => $value, 'type' => 'text' ), $attr); html_open('input', $attr, true); } function html_hidden($name, $value, $attr=Array()) { $attr = array_merge(Array( 'type' => 'hidden', 'name' => $name, 'value' => $value, ), $attr); html_open('input', $attr, true); } function html_submit($value, $attr=Array()) { $attr = array_merge(Array( 'type' => 'submit', 'value' => $value, ), $attr); html_open('input', $attr, true); } function html_textarea($name, $content, $cols=30, $rows=2, $attr=Array()) { $attr = array_merge(Array( 'name' => $name, 'rows' => $rows, 'cols' => $cols, ), $attr); html_open('textarea', $attr); echo htmlspecialchars($content); html_close('textarea'); } function html_img($src, $alt, $attr=Array()) { $attr['src'] = $src; $attr['alt'] = $alt; $attr['title'] = $alt; html_open('img', $attr, true); } register_shutdown_function('_html_write_tail_javascript'); ?>