Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1280 → Rev 1281

/asterisk-stats/trunk/includes/display.php
0,0 → 1,236
<?php
if ( !defined('IN_ASTWEB') ) {
die("Hacking attempt");
}
 
class template_handler {
private $data = array();
private $files = array();
private $vars = array();
private $root;
 
function __construct($root_dir) {
$this->root = $root_dir;
}
function add_template($file) {
array_push($this->files, array('FILE' => $file));
}
 
function add_text($text) {
array_push($this->files, array('TEXT' => $text));
}
 
function __toString() {
foreach($this->files as $f) {
if(array_key_exists('FILE', $f)) {
$this->data = array_merge($this->data, file($this->root.'/'.$f['FILE'].'.tpl'));
}
if(array_key_exists('TEXT', $f)) {
array_push($this->data, $f['TEXT']."\n");
}
}
foreach($this->data as $l) {
$form = $form.$this->replace_variables($l);
}
 
return $form;
}
 
function replace_variables($data) {
$varrefs = array();
preg_match_all('/\$\{(\w+)\}/', $data, $varrefs);
$varcount = sizeof($varrefs[1]);
for ($i = 0; $i < $varcount; $i++) {
if(array_key_exists($varrefs[1][$i], $this->vars)) {
$data = str_replace($varrefs[0][$i], $this->vars[$varrefs[1][$i]], $data);
}
}
 
return $data;
}
 
function add_variables($args) {
if(is_array($args)) {
$this->vars = array_merge($this->vars, $args);
}
}
}
 
function display_link($url, $label) {
if(!$label) {
$label = $url;
}
 
return '<A HREF="'.$url.'">'.$label.'</a>';
}
 
function s_text($text) {
if(is_array($text)) {
for($i=0;$i<sizeof($text);$i++) {
$text[$i] = ereg_replace('<', '&lt;', $text[$i]);
$text[$i] = ereg_replace('>', '&gt;', $text[$i]);
}
} else {
$text = ereg_replace('<', '&lt;', $text);
}
 
return $text;
}
 
class Table {
#Define somewhere to store the rows
protected $rowid = 0;
protected $rows = array();
protected $row_styles = array();
protected $hdr = array();
 
#Define an array to store the stylesheet classes for each row
protected $table_class = '';
protected $header_class = '';
 
function __construct($header = array()) {
$this->hdr = $header;
}
 
public function destroy() {
$this->hdr = array();
$this->sc = array();
$this->rows = array();
}
 
public function set_table_class($class) {
$this->table_class = $class;
}
 
public function newrow() {
$this->rows[] = array();
}
 
public function addcell($data) {
$key = count($this->rows) - 1;
if($key < 0) {
$this->newrow();
$key = 0;
}
 
if(!is_array($data)) {
$data = array('text' => $data);
}
 
$this->rows[$key][] = $data;
}
 
public function __toString() {
return $this->build();
}
 
public function build() {
$tag = "table";
if($this->table_class) $tag .= " class=\"$this->table_class\"";
 
$output = "<$tag>\n";
 
#Display the header
if($this->hdr) {
$output .= "\t<thead>\n\t\t<tr>\n";
 
foreach($this->hdr as $cell) {
if(!is_array($cell)) $cell = array('text' => $cell);
 
$tag = $cell['tag'] ? $cell['tag'] : 'th';
 
$output .= "\t\t\t<" . $tag;
if($cell['class']) {
$output .= " class=\"$class\"";
}
 
if($cell['text']) {
$html = s_text($cell['text']);
}
else {
$html = $cell['html'];
}
 
$output .= ">" . $html . "</" . $tag . ">\n";
}
 
$output .= "\t\t</tr>\n\t</thead>\n";
}
 
$output .= "\t<tbody>\n";
 
foreach($this->rows as $row) {
$output .= "\t\t<tr>\n";
 
foreach($row as $cell) {
$tag = $cell['tag'] ? $cell['tag'] : 'td';
 
$output .= "\t\t\t<" . $tag;
if($cell['class']) {
$output .= ' class="' . $cell['class'] . '"';
}
 
if($cell['text']) {
$html = s_text($cell['text']);
}
else {
$html = $cell['html'];
}
if(!$html) $html = '&nbsp;';
 
$output .= ">" . $html . "</" . $tag . ">\n";
}
 
$output .= "\t\t</tr>\n";
}
 
$output .= "\t</tbody>\n</table>\n";
 
return $output;
}
}
 
function Form($method, $action, $text) {
$form = '<form method="'.$method.'" action="'.$action.'">'."\n";
$form .= $text;
$form .= "</form>\n";
return $form;
}
 
function Input($type,$name,$value = '') {
return '<input type="'.$type.'" name="'.$name.'" value="'.$value.'">'."\n";
}
 
function Combo($name, $data, $default) {
if(is_array($data)) {
$output = '<SELECT NAME="'.$name."\">\n";
foreach($data as $options) {
$output .= "\t<OPTION ";
if(is_array($options)) {
$op = array_values($options);
if($default == $op[0]) {
$output .= 'SELECTED ';
}
$output .= 'value="'.$op[0].'">';
if($op[1] == '') {
$output .= $op[0];
} else {
$output .= $op[1];
}
$output .= '</OPTION>'."\n";
} else {
if($options == $default) {
$output .= 'SELECTED ';
}
$output .= 'value="'.$options.'">1'.$options;
}
$output .= "</option>\n";
}
$output .= '</SELECT>'."\n";
}
 
return $output;
}
 
?>