<?php
class databasetable
{
	var $tablename;
	var $fields;
	var $index;
	var $related_tables = array();
	var $filename;
	
	function set_fields(){
	}

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

	function get_tablename(){
		return $this->tablename;
	}

	/**
	 * Default Filename for the class
	 * 
	 * Wird gesetzt wenn Tabellenname der Klasse ungleich dem Default FILENAME_
	 *
	 * @param string $value
	 */
	function set_filename($value){
		$this->filename = $value;
	}

	function get_filename(){
		return $this->filename;
	}

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

	function get_index(){
		return $this->index;
	}

	function get_row_by_index($id,$cacheObject = false){
		$row_query_raw = sprintf("SELECT * FROM %s WHERE %s='%s'",$this->get_tablename(),$this->get_index(),$id);
		return $this->query($row_query_raw,$cacheObject);
	}

	function query($sql,$cache=false){
		$execute_query = true;
		// CACHEN! 
		if( is_object($cache) ){
			$execute_query = $cache->process($sql);
		}

		if (!$execute_query) {

			// get cached resulst
			$result = unserialize(implode('',file($cache->cache_filename)));

		} else {

			// get result from DB and create new file
			$result = xtc_db_query($sql);
			// fetch data into array
			while ($record = xtc_db_fetch_array($result)){
			        $records[]=$record;
			}

			if(is_object($cache)){
				$result = $cache->save_cachefile($records);
			}else{
				$result = $records;
			}
		}
		return $result;
	}
	
	function delete_recursion($class,$key,$index,$id){
		require_once(DIR_WS_CLASSES.$class.'.php');
		$class_instance = new $class;
		$query_raw = sprintf("SELECT %s FROM %s WHERE %s='%s'",$class_instance->get_index(),$class_instance->get_tablename(),$key,$id);
		$query = $this->query($query_raw);
		$process_array = array();
		if($query){
			foreach($query as $result){
				$delete_query_raw = sprintf( "DELETE FROM %s WHERE %s='%s'", $class_instance->get_tablename(),$class_instance->get_index(),$result[$class_instance->get_index()]);
				$this->query($delete_query_raw);
				$process_array[] = $result[$class_instance->get_index()]; 
			}
		}
		if($class_instance->related_tables and sizeOf($class_instance->related_tables)>0){
			for($i=0;$i<sizeOf($class_instance->related_tables);$i++){
				for($j=0;$j<sizeOf($process_array);$j++){
					$this->delete_recursion($class_instance->related_tables[$i]['class'],$class_instance->related_tables[$i]['key'],$class_instance->get_index(),$process_array[$j]);
				}
			}
		}
	}
	
	function delete($id){
		$this->query(sprintf( "DELETE FROM %s WHERE %s='%s'", $this->get_tablename(),$this->get_index(),$id) );
		for($i=0;$i<sizeOf($this->related_tables);$i++){
			$this->delete_recursion($this->related_tables[$i]['class'],$this->related_tables[$i]['key'],$this->get_index(),$id);
		}
	}

	/**
	  * get a list of entries for select-box
	  *
	  * @author		mario aspeleiter
	  * @version	1.0
	  * @params		$fields_array	Array	The Fields for the Query
	  * @params		$order_by		String	Sort Order SQL
	  * @returns	Array
	  */
	function get_selectbox_list($fields_array,$order_by){
		$list = array();
		$fields = implode(',',$fields_array);
		$list_query = $this->query(sprintf("SELECT %s FROM %s ORDER BY %s",$fields,$this->get_tablename(),$order_by));
		if(sizeOf($list_query)>0){
			foreach($list_query as $k=>$v){
				$list[] = array('id'=>$v[$fields_array[0]],'text'=>$v[$fields_array[1]]);
			}
		}
		return $list;
	}
	
	function array2object($array){
		return (object) $array;
	}

	/**
	 * get action href link for classes
	 *
	 * @param int $id
	 * @param string $action
	 * @return string
	 */
	function get_href_data($id,$action='overview'){
		$key = $this->get_tablename();
		if($this->get_filename() == ''){
			$file = constant('FILENAME_'.strtoupper($key));
		}else{
			$file = $this->get_filename();
		}
		switch($action){
			case 'delete':
				$access = 'func_'.$key.'_delete';
				break;
			case 'overview':
				$access = $key;
				break;
			case 'edit':
				$access = $key.'_edit';
				$file = $file.'_EDIT';
				break;
			default:
				$access = $action;
				break;
		}
		$data = array(
			'href'=>xtc_href_link($file,'id='.$id.'&action='.$action),
			'access'=>$access
		);
		return $data;
	}
	
}

