<?PHP
/**
 * Class to get and save values. Needed for the
 * config_vars.
 *
 * @author:     (c) Björn Brockmann 2003
 **/

class values_ct
{
	var $cached_values = array();

	/**
	* Konstruktur, holt Wertegruppen aus der Tabelle dedi_values, welche
	* immer gebraucht werden. Dies sind momentan die Gruppen config
	*
	*
	**/
	function values_ct()
	{
		//CHANGE STam
		global $dedi_db, $db, $dedi_lang, $lang, $client;
		$_lang = isset($lang) ? ", " . $lang : '';
		$_client = isset($client) ? ", " . $client : '';
		$sql = "SELECT 		*
				FROM		". $dedi_db['values'] ."
				WHERE		group_name IN ('cfg', 'lang')
				AND			idclient IN (0 " . $_client .")
				AND			idlang IN (0 " . $_lang . ")
				ORDER BY conf_sortindex";
		//CHANGE STam
		$db -> query($sql);

		while($db -> next_record())
		{
			//für globale langstrings (hauptsächlich für plugins gedacht)
			if($db->f('group_name') == 'lang'){
				$dedi_lang[$db->f('key1')] = $db->f('value');
			}
			//Systemvariablen
			else{
				if($db->f('key2') == '' && $db->f('key1') != ''){
					$temp[$db->f('key1')] = $db->f('value');
				}
				elseif($db->f('key3') == ''){
					$temp[$db->f('key1')][$db->f('key2')] = $db->f('value');
				}
				elseif($db->f('key4') == ''){
					$temp[$db->f('key1')][$db->f('key2')][$db->f('key3')] = $db->f('value');
				}
				elseif($db->f('key1') == ''){
					$temp[$db->f('key1')][$db->f('key2')][$db->f('key3')][$db->f('key4')] = $db->f('value');
				}
			}
		}

		$this -> cached_values['cfg'] = $temp;

		//die($sql);
	}

	function get_by_group($group, $client=0, $lang=0)
	{
		global $dedi_db, $db;

		$sql = "SELECT 		*
				FROM		". $dedi_db['values'] ."
				WHERE		group_name IN ('$group')
				AND			idclient IN ($client, 0)
				AND			idlang IN ($lang, 0)
				ORDER BY conf_sortindex";

		$db -> query($sql);

		return $this -> _make_array($db);
	}

 	function get_by_group_key($group, $key, $key_value, $client=0, $lang=0)
	{
		global $dedi_db, $db;

		$sql = "SELECT 		*
				FROM		". $dedi_db['values'] ."
				WHERE		group_name IN ('$group')
				AND			idclient IN ($client, 0)
				AND			idlang IN ($lang, 0)
        AND     $key IN ('$key_value')
				ORDER BY conf_sortindex";
		$db -> query($sql);

		return $this -> _make_array($db);
	}

  function _make_array($db) {
		while($db -> next_record())
		{
			if($db->f('key2') == '' && $db->f('key1') != ''){
				$temp[$db->f('key1')] = $db->f('value');
			}
			elseif($db->f('key3') == ''){
				$temp[$db->f('key1')][$db->f('key2')] = $db->f('value');
			}
			elseif($db->f('key4') == ''){
				$temp[$db->f('key1')][$db->f('key2')][$db->f('key3')] = $db->f('value');
			}
			elseif($db->f('key1') == ''){
				$temp[$db->f('key1')][$db->f('key2')][$db->f('key3')][$db->f('key4')] = $db->f('value');
			}
		}

		return $temp;
	}

	function get_cfg()
	{
		return $this -> cached_values['cfg'];
	}

	/**
	* Einen Wert in der Valuetabelle angeben. Existiert in der Datenbank
	* schon der entsprechende key- Kombination [1...4] zu einem Wert
	* wird dieser geupdatet. Ist die keykombination noch nicht vorhanden,
	* wird der entsprechende Eintrag neu erzeugt.
	*
	* Beispiel:
	*	$val_ct -> set_value(array(
	*						'group' 	=> 'test_gruppe',
	*						'key' 		=> 'key_des_arrays',
	*						'value'		=> 'wert_des_array'	))
	*
	* @args $mixed['client'] default '0'
	*             ['lang'] default '0'
	*             ['group'] default '0'
	*             ['key']
	*             ['key2']
	*             ['key3']
	*             ['key4']
	*             ['value']
	*             ['id']
	*
	* @return array
	*/
	function set_value($mixed)
	{
		global $dedi_db, $db;
		//build query

		$sql_group = (empty($mixed['group'])) ? 0: ''.$mixed['group'];
		$sql_client = (empty($mixed['client'])) ? '': 'AND idclient IN ('. $mixed['client'] .')';
		$sql_lang = (empty($mixed['lang'])) ? '': 'AND idlang IN ('. $mixed['lang'] .')';
		$sql_key = (empty($mixed['key'])) ? '': 'AND V.key1 = "'. $mixed['key'] . '" ';
		$sql_key2 = (empty($mixed['key2'])) ? '': 'AND V.key2 = "'. $mixed['key2'] . '" ';
		$sql_key3 = (empty($mixed['key3'])) ? '': 'AND V.key3 = "'. $mixed['key3'] . '" ';
		$sql_key4 = (empty($mixed['key4'])) ? '': 'AND V.key4 = "'. $mixed['key4'] . '" ';
		$sql_id = (empty($mixed['id'])) ? "": "AND V.idvalues = '". $mixed['id'] . "' ";


		$sql = "SELECT 		*
				FROM		". $dedi_db['values'] ."  AS V
				WHERE		V.group_name IN ('$sql_group')
				$sql_client $sql_lang
				$sql_key  $sql_key2  $sql_key3  $sql_key4 $sql_id";

		//die($sql);
		$db -> query($sql);

		$count_rows = $db ->num_rows();

		if($count_rows > 1){
			echo $sql .'<br> Fehler in Klasse "dedi_value_ct". Es wurde mehr als ein Ergebnis gefunden. Anfrage ist nicht eindeutig';
			exit;
		}
		elseif($count_rows == 1){
			$db -> next_record();
			$mixed['id'] = $db -> f('idvalues');
			//echo "update";
			$this -> _update_by_id($mixed);
		}
		else{
			$this -> insert($mixed);
		}

	}

	/**
	* Einen neuen Datensatz in die Tabelle einfügen.
	* Um eine gültige Eingabe zu haben, muss zumindest der Wert
	* key angegeben werden.
	*
	* @args $mixed['client'] default '0'
	*             ['lang'] default '0'
	*             ['group'] default '0'
	*             ['key']
	*             ['key2']
	*             ['key3']
	*             ['key4']
	*             ['value']
	*/
	function insert($mixed)
	{
		global $dedi_db, $db;

		if( empty($mixed['key']) ){return false;}

		//build query
		$mixed['value'] = addslashes($mixed['value']);
        //$mixed['value'] = make_string_dump ($mixed['value']);
		$sql_group = (empty($mixed['group'])) ? 0: '"'. $mixed['group'] .'"';
		$sql_client = (empty($mixed['client'])) ? 0: $mixed['client'];
		$sql_lang = (empty($mixed['lang'])) ? 0: $mixed['lang'];
		$sql_key = (empty($mixed['key'])) ? '""': '"'. $mixed['key'] . '" ';
		$sql_key2 = (empty($mixed['key2'])) ? '""': '"'. $mixed['key2'] . '" ';
		$sql_key3 = (empty($mixed['key3'])) ? '""': '"'. $mixed['key3'] . '" ';
		$sql_key4 = (empty($mixed['key4'])) ? '""': '"'. $mixed['key4'] . '" ';
		$sql_value = (empty($mixed['value'])) ? '""': "'". $mixed['value'] . "' ";
		$sql = "INSERT INTO `". $dedi_db['values'] ." ` (`idvalues`, `idclient`, `idlang`, `group_name`, `key1`, `key2`, `key3`, `key4`, `value`)
				VALUES		(\"\",$sql_client, $sql_lang, $sql_group, $sql_key, $sql_key2,
							 $sql_key3, $sql_key4, $sql_value)";
		$db -> query($sql);
	}
	/**
	* Einen Datensatz über die ID updaten.
	* Um eine gültige Eingabe zu haben, muss der Wert
	* id angegeben werden.
	*
	* @access private
	* @args $mixed['id']
	*             ['value']
	*/
	function _update_by_id($mixed)
	{
		global $dedi_db, $db;

		//build query
        $mixed['value'] = addslashes($mixed['value']);
        //$mixed['value'] = make_string_dump ($mixed['value']);
		$sql_value =  " value ='". $mixed['value'] . "' ";
		$sql = "UPDATE 		". $dedi_db['values'] ."
				SET			$sql_value
				WHERE		idvalues = " . $mixed['id'] ;
		//die($sql);

		$db -> query($sql);
	}
}

?>
