<?php
define('DBFORM_FIELD_DEFAULT_SORT_ORDER',99);
define('DBF_INPUT_TEXT_SIZE',30);
define('DBF_INPUT_INT_SIZE',5);
define('DBF_INPUT_FLOAT_SIZE',15);
define('DBF_INPUT_DATEFORMAT_SIZE',19);
define('DBF_INPUT_TEXTAREA_WRAPMODE','virtual');
define('DBF_INPUT_TEXTAREA_COLS','40');
define('DBF_INPUT_TEXTAREA_ROWS','6');

class dbform_field
{
	var $input_field_size;
	var $input_field_class = 'dbforminput';
	var $value;
	var $value_array; // for multiple selects
	var $required = false;
	var $errors;
	var $visible;
	var $sort_order;
	var $db_field_type;
	var $input_field;
	var $name;
	var $max_length;
	var $min_length;
	var $index;
	var $title;
	var $default_value;
	var $validate_func;
	var $options;
	var $size; // for select options
	var $extern_table;
	var $style;
	var $comments;
	var $params;
	var $group_id; // um templates individuell anzuordnen
	var $array; // falls mehrere instanzen des formfelds vorkommen koennen
	var $array_allocation;
	var $show_field_relations; // felder in abhaengigkeit anzeigen/ausblenden
	var $view_hidden_field;
	var $ajax_function;
	var $ajax_multiselect;
	var $helper; // ausserhalb des input felds links anbieten
	

	/**
	  * create a new object instance for dbfield
	  *
	  * @author		mario aspeleiter
	  * @version	1.0
	  * @param Array $sql_field The Array for the new Field
	  * @param Boolean $extern_table Is the field a recordset of the source Table. Default false
	  * 
	  * */
	function dbform_field($sql_field,$extern_table = false){
		$this->name = $sql_field['Field'];
		$this->type = $this->get_type($sql_field['Type']);
		$this->max_length = $this->get_length($sql_field['Type'],$this->type);
		$this->sort_order = DBFORM_FIELD_DEFAULT_SORT_ORDER;
		$this->extern_table = $extern_table;
		if($sql_field['Key'] == 'PRI')
			$this->index = 1;
	}

	function get_type($value){
		if(!$this->type){
			$split = explode('(',$value);
			return current($split);
		}
	}

	function get_length($value,$type){
		switch($type){
			case 'datetime':
			case 'float':
			case 'decimal':
			case 'int':
			case 'char':
			case 'text':
			case 'hidden':
				return 0;
				break;
			
		}
		if(strstr($value,'(')){
			$split = explode('(',$value);
			$value = current(array_reverse($split));
			return substr($value,0,strlen($value)-1);
		}else{
			echo 'unknown length for '.$value;
		}
	}
	
	function set_title($value,$sort_order=DBFORM_FIELD_DEFAULT_SORT_ORDER){
		$this->title = $value;
		$this->visible = true;
		$this->sort_order = $sort_order;
	}
	
	function set_required($min_length,$validate_func=''){
		if($min_length > 0){
			$this->required = true;
			$this->min_length = $min_length;
		}
		$this->validate_func = $validate_func;
	}

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

	/**
	  * add option values for a select_box
	  *
	  * @author		mario aspeleiter
	  * @version	1.0
	  * @param Array $array The Option-Values Array
	  * @param Mixes $default_value Default Value
	  * @param Integer $size Size for the select-box. Default 1
	  * 
	  * */
	function set_options($array,$default_value,$size=1){
		$this->options = $array;
		if(is_array($default_value) and sizeOf($default_value)>0)
			$this->value_array = $default_value;
		else
			$this->set_default_value($default_value);
		$this->size = $size;
	}

	/**
	 * set input fields for formular
	 *
	 * @return unknown
	 */
	function set_input_field($counter=0 ){
		if($this->index == 1){
			$name = ($this->get_array() == true) ? $this->name.'[]' : $this->name;
			$this->input_field = xtc_draw_hidden_field($name,$this->value);
			return true;
		}
		if(!$this->visible){
			return false;
		}
		
		if($this->get_show_field_relations()){
			$this->params .= ' onchange="processDBF_DISPLAY();" ';		
		}
		
		if( is_array($this->options) ){
			$multiple = ($this->size > 1 or $this->get_ajax_multiselect()) ? 'multiple' : '';
			$name = ( ($this->size > 1 and !strstr($this->name,'[]') ) or $this->get_array() == true) ? $this->name.'[]' : $this->name;
			if(is_array($this->value_array) and sizeOf($this->value_array)>0){
				$this->value = $this->value_array;
			}
			$value = ($this->get_array() == true) ? $this->value[$counter] : $this->value;
			$params = $this->get_params();
			if($this->ajax_function && !$this->get_ajax_multiselect()){
				$onclick = ' onclick="yali_start(this,yali'.$this->ajax_function.');" ';
				$params .= $onclick;
			}
			if($this->get_ajax_multiselect()){
				$this->input_field_class = 'dbforminputselectmultiple';
				$name = $name.'[]';
				$this->name = $name;
			}
			$this->input_field = xtc_draw_pull_down_menu($name,$this->options,$value,'size="'.$this->size.'" class="'.$this->input_field_class.'" '.$params.' '.$multiple.$multistyle);
			return true;
		}
		$style = ($this->style) ? ' style="'.$this->style.'" ' : '';
		$name = ($this->get_array() == true) ? $this->name.'[]' : $this->name;
		$value = ($this->get_array() == true) ? $this->value[$counter] : $this->value;
		switch($this->type){
			case 'varchar':
				$this->input_field =  xtc_draw_input_field($name,$value,$style.'size="'.DBF_INPUT_TEXT_SIZE.'" class="'.$this->input_field_class.'"'.(($this->max_length > 0)?'maxlength="'.$this->max_length.'"':'').' '.$this->get_params());
				break;
			case 'char':
				$this->input_field =  xtc_draw_input_field($name,$value,$style.'size="'.$this->max_length.'" class="'.$this->input_field_class.'"'.(($this->max_length > 0)?'maxlength="'.$this->max_length.'"':'').' '.$this->get_params());
				break;
			case 'int':
				$this->input_field =  xtc_draw_input_field($name,$value,$style.'size="'.DBF_INPUT_INT_SIZE.'" class="'.$this->input_field_class.'"'.(($this->max_length > 0)?'maxlength="'.$this->max_length.'"':'').' '.$this->get_params());
				break;
			case 'float':
			case 'decimal':
				$this->input_field =  xtc_draw_input_field($name,$value,$style.'size="'.DBF_INPUT_FLOAT_SIZE.'" class="'.$this->input_field_class.'"'.(($this->max_length > 0)?'maxlength="'.$this->max_length.'"':'').' '.$this->get_params());
				break;
				case 'text':
				$this->input_field =  xtc_draw_textarea_field($name,DBF_INPUT_TEXTAREA_WRAPMODE,DBF_INPUT_TEXTAREA_COLS,DBF_INPUT_TEXTAREA_ROWS,$value,$style.'class="'.$this->input_field_class.'"'.' '.$this->get_params());
				break;
			case 'hidden':
				$this->visible = true;
				$this->input_field =  xtc_draw_hidden_field($name,$value);
				break;
			case 'datetime':
				$this->input_field = '';
				break;
		}
		return true;
	}
	
	/**
	 * get the default value for input field
	 *
	 * @return mixed
	 */
	function get_default_value(){
		if( $this->default_value )
			return $this->default_value;
		switch($this->type){
			case 'datetime':
				return date('Y-m-d H:i:s');
				break;
		}
		return '';
	}
	
	/**
	 * set the default value for element
	 *
	 * @param Mixed $value
	 */
	function set_default_value($value){
		$this->default_value = $value;
	}

	/**
	 * returns the field-object as array
	 *
	 * @param boolean $view_mode
	 * @return array
	 */
	function toArray($view_mode = false){
		if($this->visible or $view_mode){
			$array = array(
					'value'=> $this->value,
					'value_size'=>sizeOf($this->value),
					'required' => $this->required,
					'errors' => $this->errors,
					'sort_order' => $this->sort_order,
					'input'=>$this->input_field,
					'name'=>$this->name,
					'minlength'=>$this->min_length,
					'maxlength'=>$this->max_length,
					'title'=>$this->title,
					'validate_func'=>$this->validate_func,
					'type'=>$this->type,
					'comments'=>$this->get_comments(),
					'group_id'=>$this->get_group_id(),
					'array'=>$this->get_array(),
					'show_field_relations'=>$this->get_show_field_relations(),
					'view_hidden_field'=>$this->get_view_hidden_field(),
					'ajax_function'=>$this->get_ajax_function(),
					'ajax_multiselect'=>$this->get_ajax_multiselect(),
					'helper'=>$this->get_helper()
			);
			return $array;
		}
		return false;
	}
	
	/**
	 * Add a style="" Tag to input-field
	 *
	 * @param string $value
	 */
	function set_style($value){
		$this->style = $value;
	}
	
	/**
	 * add a comment
	 *
	 * @param string $value
	 */
	function set_comments($value){
		$this->comments = $value;
	}
		
	/**
	 * get the comment
	 *
	 * @return string
	 */
	function get_comments(){
		return $this->comments;
	}
	
	/**
	 * set additional params for input field
	 *
	 * @param string $value
	 */
	function set_params($value){
		$this->params = $value;
	}
	
	/**
	 * get additional params for input field
	 *
	 * @return string
	 */
	function get_params(){
		return $this->params;
	}
	
	/**
	 * set group id for input field
	 *
	 * @param string $value
	 */
	function set_group_id($value){
		$this->group_id = $value;
	}
	
	/**
	 * get group id for input field
	 *
	 * @return string
	 */
	function get_group_id(){
		return $this->group_id;
	}

	/**
	 * set is array for input field
	 *
	 * @param boolean $value
	 */
	function set_array($value){
		$this->array = $value;
	}
	
	/**
	 * get is array for input field
	 *
	 * @return boolean
	 */
	function get_array(){
		return $this->array;
	}
	
	/**
	 * set array allocation
	 *
	 * @param array $value
	 */
	function set_array_allocation($value){
		$this->array_allocation = $value;
	}
	
	/**
	 * get array allocation
	 *
	 * @return array
	 */
	function get_array_allocation(){
		return $this->array_allocation;
	}

	function set_show_field_relations($related_fields_array){
		$this->show_field_relations = $related_fields_array;
	}

	function get_show_field_relations(){
		return $this->show_field_relations;
	}

	function set_view_hidden_field($value){
		$this->view_hidden_field = $value;
	}
	
	function get_view_hidden_field(){
		return $this->view_hidden_field;
	}
		
	function set_ajax_function($value){
		$this->ajax_function = $value;
	}
	
	function get_ajax_function(){
		return $this->ajax_function;
	}

	function set_ajax_multiselect($value){
		$this->ajax_multiselect = $value;
	}

	function get_ajax_multiselect(){
		return $this->ajax_multiselect;
	}

	function get_helper() {
		if(strlen($this->helper)>0)
			return 'helper_'.$this->helper.'.html';
		else
			return '';
	}

	function set_helper($helper) {
		$this->helper = $helper;
	}


}
