<?php
/*
Panglossa go!Johnny PHP library
version 1.0c
release 2007-01-26
Please see the readme.txt file that goes with this source.
Licensed under the GPL, please see:
http://www.gnu.org/licenses/gpl-3.0-standalone.html
Copyleft Panglossa Software
http://panglossa.com
software@panglossa.com
Araçatuba - SP - Brazil - 2007/2008
*/
//////////////////////////////////////////////////////
//function vname: get php variable's name.
//Used in member function getname() (to be deprecated?).
//Function got from lucas dot karisny at linuxmail dot org
//via http://lists.evolt.org/archive/Week-of-Mon-20060925/185326.html
function vname(&$var, $scope=false, $prefix='unique', $suffix='value'){
if($scope){
$vals = $scope;
}else{
$vals = $GLOBALS;
}
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
//////////////////////////////////////////////////////
//class TElement: base class for all HTML elements.
class TElement{
var $items = array();
var $properties = array();
var $events = array();
var $type;
var $printable;
////////////////////
function TElement(){
$this->type = 'element';
}
////////////////////
//Sets element's "id" and "name" properties, if void, based on the php variable's name.
function setID($anID=''){
if($anID!=''){
$this->properties['id']=$anID;
$this->properties['name']=$anID;
}else{
if ($this->properties['id']==''){
$instance = md5(serialize($this));
foreach( $GLOBALS as $variable => $value ){
if( md5(serialize($value)) == $instance ){
$this->properties['id']=$variable;
}
}
}
if ($this->properties['name']==''){
$this->properties['name']=$this->properties['id'];
}
}
}
////////////////////
//function p(): sets and gets element's properties.
//call it with the property name to get its value.
//call it with the property name and a value to set it.
//shortcut to using the properties[] member variable.
//returns the property's value, anyway.
function p($aproperty, $avalue=false){
if ($avalue){
$this->properties[$aproperty] = $avalue;
}
return $this->properties[$aproperty];
}
////////////////////
function getname($avar){
return vname($avar);
}
////////////////////
function clear(){//Seems not to work... why? :(
unset($this->items);
$this->items = array();
}
////////////////////
function add($anitem){
$this->items[] = $anitem;
//$newitem = '';
//if(empty($anitem->type)){
//array_push($this->items, $anitem);
//}else{
//foreach($anitem->properties as $key=>$value){
//$newitem->properties[$key] = $value;
//}
//foreach($anitem->events as $key=>$value){
//$newitem->events[$key] = $value;
//}
//$newitem->type = $anitem->type;
//array_push($this->items, $newitem);//in some cases, the simpler $this->items[] = $anitem doesn't work.
//$this->items[] = $anitem;
//}
}
////////////////////
function getprefix(){
$res = '<' . $this->type;
foreach($this->properties as $key=>$value){
$res .= " $key=\"$value\"";
}
foreach($this->events as $key=>$value){
$res .= " $key=\"$value\"";
}
$res .= '>';
return $res;
}
////////////////////
function getsuffix(){
$res = '</' . $this->type . ">\n";
return $res;
}
////////////////////
function content(){
$res = '';
foreach ($this->items as $item){
if ($item->printable==true){
$res .= $item->show();
}else{
$res .= "\n$item\n";
}
}
return $res;
}
function show(){
$this->setID();
$res = $this->getprefix() . $this->content() . $this->getsuffix();
return $res;
}
}
?>
|