<?php

class wbVariables{
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//		Configuration
	//
	
	
	/* deprecated */
		
	function saveConfigArray(&$array,$deleteOthers=false){
		return wbVariables::saveArray($array,$deleteOthers);
	}
	
	function getConfigArray(){
		return wbVariables::getArray();
	}
		
	/* end deprecated */
	
	
	function saveArray($array,$deleteOthers=false){
		global $wbPluginSpace,$wbTables;
		
		if( empty($wbPluginSpace) ){
			$space = '';
		}else{
			$space = $wbPluginSpace;
		}
		
		//just delete all of them before replacing
		if( $deleteOthers !== false ){
			$query = 'DELETE FROM '.$wbTables['config_vars'];
			$query .= ' WHERE ';
			$query .= ' `plugin_space` = "'.wbDB::escape($space).'" ';
			if( is_string($deleteOthers) ){
				$query .= ' AND `key` LIKE "'.wbDB::escape($deleteOthers).'%" ';
			}
			$result = wbDB::runQuery($query);
		}
		
		//insert/update these values
		foreach($array as $key => $value){
			wbVariables::set($key,$value);
		}
		
		return true;
	}
	
	//set a single $key => $value pair
	function set($key,$value){
		global $wbPluginSpace,$wbTables;
		
		if( empty($wbPluginSpace) ){
			$space = '';
		}else{
			$space = $wbPluginSpace;
		}
		
		if( strlen($key) > 40 ){
			trigger_error('Variable key was too long. '.$key.' was '.strlen($key).' where the maximum is 40.');
			return false;
		}
		if( strlen($value) > 255 ){
			trigger_error('Variable value was too long. ');
			message('A value was too long to be saved, please try again.');
			return false;
		}
		$query = 'REPLACE INTO '.$wbTables['config_vars'];
		$query .= ' SET ';
		$query .= ' `plugin_space` = "'.wbDB::escape($space).'" ';
		$query .= ' ,`key` = "'.wbDB::escape($key).'" ';
		$query .= ' ,`value` = "'.wbDB::escape($value).'" ';
		if( wbDB::runQuery($query) ){
			return true;
		}
		return false;
	}
	
	function get($key){
		global $wbPluginSpace,$wbTables;
		
		if( empty($wbPluginSpace) ){
			$space = '';
		}else{
			$space = $wbPluginSpace;
		}
		
		$query = 'SELECT `value` FROM '.$wbTables['config_vars'];
		$query .= ' WHERE ';
		$query .= ' `plugin_space` = "'.wbDB::escape($space).'" ';
		$query .= ' AND `key` = "'.wbDB::escape($key).'" ';
		$result = wbDB::runQuery($query);
		if( mysql_num_rows($result) < 1 ){
			return false;
		}
		$row = mysql_fetch_assoc($result);
		return $row['value'];
	}

	
	function getArray($like = false, $stripPrefix = false){
		global $wbPluginSpace,$wbTables;
		
		if( empty($wbPluginSpace) ){
			$space = '';
		}else{
			$space = $wbPluginSpace;
		}
		
		$query = 'SELECT `key`, `value` FROM '.$wbTables['config_vars'];
		$query .= ' WHERE ';
		$query .= ' `plugin_space` = "'.wbDB::escape($space).'" ';
		if( is_string($like) ){
			$query .= ' AND `key` LIKE "'.wbDB::escape($like).'%" ';
		}
		$result = wbDB::runQuery($query);
		
		$prefixLen = false;
		if( is_string($like) && $stripPrefix ){
			$prefixLen = strlen($like);
		}
		
		$array = array();
		while($row = mysql_fetch_assoc($result) ){
			if( $prefixLen ){
				$key = substr($row['key'],$prefixLen);
			}else{
				$key = $row['key'];
			}
			$array[$key] = $row['value'];
		}
		return $array;
	}
	
	function delete($key){
		global $wbPluginSpace,$wbTables;
		
		if( empty($wbPluginSpace) ){
			$space = '';
		}else{
			$space = $wbPluginSpace;
		}
		
		$query = 'DELETE  FROM '.$wbTables['config_vars'];
		$query .= ' WHERE ';
		$query .= ' `plugin_space` = "'.wbDB::escape($space).'" ';
		$query .= ' AND `key` = "'.wbDB::escape($key).'" ';
		return wbDB::runQuery($query);
	}
	
}
	