Location: PHPKode > projects > interface preprocessor for php > ippfp-0.2.10/ippfp/element.php
<?php

    /**
     *	ippfp - interface preprocessor for php
     *
     *	Copyright 2004 Thomas Moenicke
     *
     *	This library is free software; you can redistribute it and/or
     *	modify it under the terms of the GNU Lesser General Public
     *	License as published by the Free Software Foundation; either
     *	version 2.1 of the License, or (at your option) any later
     *	version.
     *	This library 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
     *	Lesser General Public License for more details.
     *	You should have received a copy of the GNU Lesser General Public
     *	License along with this library; if not, write to the Free
     *	Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     *	02111-1307 USA
     *
     */
/**
*@package ippfp
*/
/**
*Base class to define an element.
*
*Abstract class ippfp_element. All elements are derived from it.
*/


class ippfp_element{

    /* name or id */
    var $name = 0;
    /* type */
    var $type;
    /* size, or length */
    var $size;
    /* height, lines */
    var $height;
    /* width, chars */
    var $width;
    /* list of elements included */
    var $elements = array();
    /* iterator of element list */
    var $e_it = 0;
    /* value of this element */
    var $value;
    /* disable */
    var $disable = false;

    /**
    *Constructor
    *
    *Creates an element.
    *
    *@param string $name Internal name of the element.
    *@param string $attr_1 First attribute.
    *@param string $attr_2 Second attribute.
    */

    function ippfp_element($name, $value, $attr){
	$this->name = $name;
	$this->value = $value;
    }

    /**
    *second Constructor
    *
    *auxiliary constructor related to output properties. internal use only.
    *
    */

    function ippfp_construct(){
	/* empty */
    }


    /**
    *Type
    *
    *Abstract function, returns the type.
    */

    function type(){ return $this->type; }

    /**
    *Displays the element
    *
    *Abstract. You do not need that, the ippfp calls this if it processes the interface.
    *
    */

    function display(){}

    /**
    *returns the name of this element
    *
    */

    function getName(){ return $this->name; }

    /**
    *sets the name of this element
    *
    *@param string $name
    */

    function setName($name){ $this->name = $name; }

    /**
    *returns the size of this elements value
    *
    */

    function size(){ return $this->size; }

    /**
    *returns the height of this element
    *
    */

    function height(){ return $this->height; }

    /**
    *returns the width of this element
    *
    */

    function width(){ return $this->width; }

    /**
    * sets a relation: this to $element
    *
    *@param element $element
    */

    function insert(&$element){ 
	if(is_array($element)) $this->elements = &$element;
	else if(is_string($element)) $this->elements[$this->e_it++]=&$element;
	else if(is_object($element)) $this->elements[$this->e_it++]=&$element;
	else {
	    echo "ippfp: element does not exist.";
	    exit(0);
	}
    }
	

    /**
    * copy, returns a copy of reference to the elements list
    *
    */

    function &copy(){ return $this->elements; }

    /**
    * transforms an element into another element with different output types
    *
    */

    function &transform(&$app){}

    /**
    * resets elements value
    *
    */

    function reset(){
	$this->value = null;
    }

    /**
    * sets the value of element
    *
    */

    function setValue($value){ $this->value = ippfp_application::verify(&$value); }

    /**
    * returns the value of element
    *
    */

    function &getValue(){ return $this->value; }

    /**
    * clears the value
    *
    */

    function &clear(){ $this->setValue(""); }


}

    /* element definitions */ 

    /* 	Notiz: in den Konstruktoren wird das $_POST array ausgelesen, das hat keine Auswirkungen
	solange es sich nicht um das HTML Paket handelt. Dort aber wird der Konstruktor bei jedem
	reload aufgerufen und der Wert entsprechend gesetzt.
	In Zukunft: dazu koennte eine extra Funktion dienen (z.B. der Hilfskonstruktor), die standardmaessig nichts zurueckgibt
	jedoch beim HTML Paket das $_POST array ausliest. (auch bei zukuenftigen Ideen wie XHTML sinnvoll)
    */
    /* Notiz:
	ncurses bekommen beim constructor einen key mitgegeben, dieser muss jetzt in einer extra Funktion uebergeben werden
    */

class ippfp_button extends ippfp_element{

    var $status_;
    var $src;
    var $app;
	
    function ippfp_button($name, $status_, $value, $src, &$app){

	$this->name = $name; 
	$this->status_ = $status_;
	$this->app = &$app;
	$this->type = "button";
	$this->size = strlen($status_);
	$this->height = 1;
	$this->width = &$this->size;
	if($value) $this->value = $value;
	else $this->value = "empty";
	$this->src = $src;

	$this->status_ = $status_;

    }

    function setValue($value) {
	$this->status_ = $value;
    }
	    
    function &transform(&$app){
	$bt = &$app->createButton($this->status_, $this->value, $this->src);
	return $bt;
    }

}

class ippfp_checkbox extends ippfp_element{

    var $checked = false;
    var $text;
    
    function ippfp_checkbox($name, $value, $checked){

	$this->name = $name;

	if($checked) $this->checked = true;
	$this->type = "checkbox";

	$this->size = (strlen($value)+5);
	$this->height = 1;
	$this->width = &$this->size;
	$this->text = $value;
		    
	$checkedpost = ippfp_application::verify($_POST[$this->name],$value);
			
        if(!isset($_SESSION[$this->name])){
	    $_SESSION[$this->name] = $this->checked;
	} else {
	    $_SESSION[$this->name] = false;
	    $this->checked = false;

	    if($checkedpost == "true"){
		$_SESSION[$this->name] = true;
		$this->checked = true;
	    }
	}

	$this->value = &$this->checked;
	$this->ippfp_construct();
    }

    function setValue($checked){
	if($checked) {
	    $this->checked = true;
	    $_SESSION[$this->name] = true;
	} else {
	    $this->checked = false;
	    $_SESSION[$this->name] = false;	
	}
    }

    function getValue(){
	return $this->checked;
    }
					
    function &transform(&$app){
	return $app->createCheckbox($this->value, $this->checked);
    }

}

class ippfp_choice extends ippfp_element{

    var $array;
    var $reload = false;
    var $default_ = "---";
    var $status_;
	    
    function ippfp_choice($name, $default, $array, $status){

	$this->name = $name;
	if($default) {	
	    $default = array_search($default, $array);
	    $this->default_ = $default;
	}

	$this->array = $array;
	$this->type = "choice";
	/* aquire maxsize */
	$this->size = 16;
	if(is_array($array)) 
	    foreach($array as $a) 
		if(strlen($a) > $this->size) $this->size = strlen($a);
	$this->height = 1;
	$this->width = &$this->size;
	$this->status_ = $status;
	if($status) $this->reload = true;
	//	if($status) $_SESSION["stat_"] = $status;

	$checkedpost = ippfp_application::verify($_POST[$this->name], $default);
	$checkedsession = ippfp_application::verify($_SESSION[$this->name], $default);

	if(!isset($_SESSION[$this->name])) $_SESSION[$this->name] = $this->value;
	if($checkedpost){
	    $this->value = isset($this->array[$checkedpost]) ? $checkedpost : $default;
	    $_SESSION[$this->name] = $this->value;
	} else if($checkedsession) {
	    $this->setValue($checkedsession);
	}

	$this->ippfp_construct();

    }

    function &transform(&$app){
	return $app->createChoice($this->value, $this->array, $this->name, $this->status_);
    }
	
    function setValue($value){
	$this->value = $value;
	$_SESSION[$this->name] = $this->value;
    }

    function getValue(){
//	$_SESSION[$this->name] ? return $_SESSION[$this->name] : $return $this->default_;
	return $_SESSION[$this->name];
    }


    function getValueAsString(){
	return $this->array[$this->value];
    }

    function reset(){
	$this->value = null;
	$_SESSION[$this->name] = null;
    }

}


class ippfp_fopen extends ippfp_element{

    var $status_;
    var $app;

    var $error;
    var $tmp_name;
    var $size;

    function ippfp_fopen($name, $status_, $value, &$app){

	$this->name = $name;
	$this->status_ = $status_;
	$this->app = &$app;
	$this->type = "fopen";
	$this->size = strlen($status_)+1;
	$this->height = 1;
	$this->width = &$this->size;
	$this->value = $value;

	$this->value = $_FILES[$this->name]["name"];
	$this->tmp_name = $_FILES[$this->name]["tmp_name"];
	$this->error = $_FILES[$this->name]["error"];
	if($_FILES[$this->name]["size"]) $this->size = $_FILES[$this->name]["size"];

        if(!isset($_SESSION[$this->name])) $_SESSION[$this->name] = $this->value;

    }

    function setValue($value) {
	$this->value = $value;
	$_SESSION[$this->name] = $this->value;
    }

    function getValue(){
	return $_SESSION[$this->name];
    }

    function &transform(&$app){
	$bt = &$app->createFopen($this->status_, $this->value, $this->name);
	return $bt;
    }

}

class ippfp_grid extends ippfp_element{

    var $array;
    var $app;
    
    function ippfp_grid($name, $text, &$array, &$app){
	$this->name = $name;
	$this->value = $text;
	$this->array = &$array;
	$this->app = &$app;
	$this->type = "grid";
	$this->size = 0;
	$this->height = 0;
	$this->width = 0;
    }

    function &transform(&$app){
	return $app->createGrid($this->value, &$this->array);
    }

}

class ippfp_image extends ippfp_element{

    /* path */
    var $src;

    function ippfp_image($name, $text, $src){ 
	$this->name = $name;
	$this->value = $text;
	$this->src = $src;
	$this->type = "image";
	$this->size = strlen($src);
	$this->height = 1;
	$this->width = &$this->size;
	$this->ippfp_construct();
    }

    function &transform(&$app){
	return $app->createImage($this->value, $this->src, $this->name);
    }

}

class ippfp_label extends ippfp_element {

    /* more than one columns for text */
    var $cols = 0;
    var $formatting = "";

    function ippfp_label($name, $text, $cols = null, $formatting = null){ 
	$this->name = $name;
	$this->value = $text;
	if($cols) $this->cols = $cols;
	$this->type = "label";
	$this->size = strlen($text);
	$this->height = 1;
	$this->width = &$this->size;
	$this->formatting = $formatting;
    }

    function display(){
	/* function is undefined here */
    }

    function &transform(&$app){
	return $app->createLabel($this->value, $this->cols,$this->name,$this->formatting);
    }

}

class ippfp_link extends ippfp_element {

    var $target;
    var $src;

    function ippfp_link($name, $value, $target, $src){

	$this->name = $name;
	$value ? $this->value = $value : $this->value = $target;
	$this->target = $target;
	$this->type = "link";
	if(!$value) $this->size = strlen($this->target);
	else $this->size = strlen($value);
	$this->height = 1;
	$this->width = &$this->size;
	$this->src = $src;
	$this->ippfp_construct();
    }

    function &transform(&$app){
	return $app->createLink($this->value, $this->target, $this->name);
    }

}

class ippfp_multiplechoice extends ippfp_element {

    var $array;
    var $reload = false;
    var $default_ = array("---");
    var $status_;
	    
    function ippfp_multiplechoice($name, $default, $array, $status){

        $this->name = $name."";
	if($default) if(!is_array($default)) echo "ippfp warning: default is not an array in ".$name."<br>";
			
	//	else $this->value = ""; 
			
	if($array) if(!is_array($array)) echo "ippfp warning: array is not an array in ".$name."<br>";
	$this->array = $array;
	$this->type = "multiplechoice";
			    
	/* aquire maxsize */
	$this->size = 16;
	if(is_array($array)) foreach($array as $a) if(strlen($a) > $this->size) $this->size = strlen($a);
	$this->height = 1;
	$this->width = &$this->size;

	$this->status_ = $status;
	if($status) $this->reload = true;

	if($_POST[$this->name]){

	    $this->value = $_POST[$this->name];
	    $_SESSION[$this->name] = serialize($this->value);

	} else if($_SESSION[$this->name]){ 

	    $this->value = unserialize($_SESSION[$this->name]);

	} else if($default){

	    $this->value = $default; 

	}

	if(!isset($_SESSION[$this->name])) $_SESSION[$this->name] = serialize($this->value);
	$this->ippfp_construct();
    }

    function &transform(&$app){
	return $app->createMultipleChoice($this->value, $this->array, $this->name, $this->status_);
    }
	
    function setValue($id){
	$this->value = $id;
        $_SESSION[$this->name] = serialize($this->value);
    }
			
    function getValue(){
	return $this->value;
    }

    function getValueAsString(){

	$ret="";	
        foreach($this->array as $value => $item) {
	    if(is_array($this->value)){
		if(in_array($value, $this->value)){
		    $ret .= $item.", ";
		}
	    }
	}

	return $ret;

    }
										    
    function reset(){
	$this->value = null;
	$_SESSION[$this->name] = null;
    }

}

class ippfp_password extends ippfp_element{

    function ippfp_password($name, $value = null, $size){ 

        $this->name = $name;
        $value ? $this->value = $value : $this->value = "";
        $this->type = "password";
        $this->size = $size;
        $this->height = 1;
        $this->width = &$this->size;
			    
        $checkedpost = ippfp_application::verify($_POST[$this->name], $this->value);
        $checkedsession = ippfp_application::verify($_SESSION[$this->name], $this->value);
				    
        if(!isset($_SESSION[$this->name])) $_SESSION[$this->name] = $this->value;
        else if($checkedpost){
    	    $this->value = $checkedpost;
	    $_SESSION[$this->name] = $checkedpost;
        } else $this->value = $checkedsession;

    }

    function &transform(&$app){
	return $app->createPassword($this->value, $this->size, $this->name);
    }

    function setValue($value){
	$this->value = $value;
	$_SESSION[$this->name] = &$value;
    }

    function &getValue(){
        return $_SESSION[$this->name];
    }

    function reset(){
	$this->value = null;
	$_SESSION[$this->name] = null;
    }

}

class ippfp_radio extends ippfp_element {

    var $checked = false;
    var $group;
    var $text;
	    
    function ippfp_radio($name, $value, $checked, $group = null){

	$this->name = $name;
	$group ? $this->group = $group : $this->group = "radio";
	$this->value = $_POST[$this->group];
	$this->text = $value;
	$this->size = strlen($value);
	$this->height = 1;
	$this->width = &$this->size;
	$this->type = "radio";
				    
//	$checkedpost_radio = ippfp_application::verify($_POST[$this->group],"");
	$checkedpost_radio = ippfp_application::verify($this->value,"");
	$this->value = $checkedpost_radio;

	$checkedpost = ippfp_application::verify($_POST[$this->group],"");

	if(!isset($_SESSION[$this->name])) $_SESSION[$this->name] = $value;
	else {
	    if($this->text == $checkedpost) $this->checked = true;
	    else $this->checked = false;
	}

	/* first run */
	if(!$this->value) { 
	    if($checked) {
		$this->checked = true;
		$_SESSION[$this->name] = $this->text;
	    }
	}

	$this->ippfp_construct();

    }

    function setValue($checked){
	if($checked) $this->checked = true;
	else $this->checked = false;
    }

    function getValue(){
	return $this->checked;
    }

    function &transform(&$app){
	return $app->createRadio($this->value, $this->checked, $this->name, $this->group);
    }

    function getText(){
	return $_SESSION[$this->name];
    }

}

class ippfp_table extends ippfp_element{

    var $array;
    var $head;
    var $app;
		
    function ippfp_table($name, $head, &$array, &$app){

	$this->name = $name;
	$this->head = $head;
	$this->array = &$array;
	$this->type = "table";
	$this->size = 0;
	$this->height = 0;
	$this->width = 0;
	$this->app = &$app;

    }

    function &transform(&$app){
	return $app->createTable($this->head, &$this->array);
    }							    

}

class ippfp_text extends ippfp_element{

    var $inactive = false;
    var $maxlength;
    var $unlimited = false;

    
    function ippfp_text($name, $value, $size){ 

	$this->name = $name;
	$this->value = $value;
	$this->type = "text";
	$this->size = $size;
	$this->maxlength = $size;
	$this->height = 1;
	$this->width = &$this->size;

	$checkedsession = ippfp_application::verify($_SESSION[$this->name], $value);
	$checkedpost = ippfp_application::verify($_POST[$this->name], $value);

	if(!$_SESSION[$this->name]){
	    $_SESSION[$this->name] = $this->value;
	} else {
	    if($checkedpost){
		$this->value = $checkedpost;
		$_SESSION[$this->name] = $checkedpost;
	    } else {
		$this->value = $checkedsession;
	    }
	}
	if($_SESSION[$this->name."_sensitive"]) {
	    $_SESSION[$this->name."_sensitive"] = false;
	    /* workaround to delete fields by user */
	    /* delete post is if empty and element is sensitive */
	    if(!$_POST[$this->name]){
		$this->value = "";
		$_SESSION[$this->name] = "";
	    }
	}


    }

    function &transform(&$app){
	return $app->createText($this->value, $this->size, $this->name);
    }
	        
    function &getValue(){
	return $_SESSION[$this->name];
    }

    function setValue($value){
	$this->value = ippfp_application::verify($value);
	$_SESSION[$this->name] = &$this->value;
    }

    function reset(){
	$this->value = null;
	$_SESSION[$this->name] = null;
    }

}

class ippfp_textarea extends ippfp_element{

    var $cols = 0;
    var $rows = 5;

    function ippfp_textarea($name, $value, $cols){

	$this->name = $name;
	$this->value = $value;
	$this->cols = $cols;
	$this->type = "textarea";
	$this->size = $this->rows * $cols;
	$this->height = &$this->row;
	$this->width = &$this->cols;

	$checkedpost = ippfp_application::verify($_POST[$this->name], $value);
	$checkedsession = ippfp_application::verify($_SESSION[$this->name], $value);

        if(!isset($_SESSION[$this->name])) $_SESSION[$this->name] = $this->value;
	else {
	    if($checkedpost){
    		$this->value = $checkedpost;
		$_SESSION[$this->name] = $checkedpost;
    	    } else $this->value = $checkedsession;
	}

	if($_SESSION[$this->name."_sensitive"]) {
	    $_SESSION[$this->name."_sensitive"] = false;
	    /* workaround to delete fields by user */
	    /* delete post is if empty and element is sensitive */
	    if(!$_POST[$this->name]){
		$this->value = "";
		$_SESSION[$this->name] = "";
	    }
	}

    }

    function &transform(&$app){
	$ta = &$app->createTextarea($this->value, $this->cols);
	$ta->rows = $this->rows;
	return $ta;
    }
		        
    function setValue($value){
	$this->value = $value;
	$_SESSION[$this->name] = &$this->value;
    }

    function getValue(){
	return $_SESSION[$this->name];
    }

    function reset(){
	$this->value = "";
	$_SESSION[$this->name] = "";
    }

}




?>
Return current item: interface preprocessor for php