<?
class geo{

function geo($type){
	switch($type) {
		case's':  case'street':  $this->item='street'; $this->group='city'; break;
		case'c':  case'city':    $this->item='city';   $this->group='county'; break;
		case'c2': case'city2':   $this->item='city';   $this->group='country'; break;
		case'co': case'county':  $this->item='county'; $this->group='country'; break;
	}
}

function getGroups(){
	global $dbi;
	$sql="select id,name from geo_$this->group order by name";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)){
		$data[]=$a;
	}
	return $data;
}

function getItems($groups='',$exclude=''){
	global $dbi;
	if($exclude) $sql_exclude=", case when id in ($exclude) then 1 else 0 end as exclude";
	if($groups) $where_groups=" where group_id in ($groups) ";
	$sql="select id,name$sql_exclude from geo_$this->item $where_groups order by name";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)){
		$data[]=$a;
	}
	return $data;
}

function getItemsName($groups='',$exclude=''){

	// csa kneveket, es majd a beadott irsz alapjan veti ossze, hogy jo-e a parositas. Ez csak ellenorzo lesz.

	global $dbi;
	if($exclude) $sql_exclude=", case when id in ($exclude) then 1 else 0 end as exclude";
	if($groups) $where_groups=" where group_id in ($groups) ";
	$sql="select distinct name$sql_exclude from geo_$this->item $where_groups order by name";
	$res=sql_query($sql,$dbi);
	$i==0;
	while($a=sql_fetch_assoc($res,$dbi)){
		$a[id]=$i++;
		$data[]=$a;
	}
	return $data;
}

function getGroupsWithItems() {
	$data=$this->getGroups();
	foreach($data as &$c)
		$c[cities]=$this->getItems($c[id]);
	return $data;
}

function getItemCount(){
	global $dbi;
	$sql="select max(id) from geo_$this->item";
	$res=sql_query($sql,$dbi);
	list($count)=sql_fetch_array($res,$dbi);
	return $count;
}

function getGeoDataByItem($item_id) {
	global $dbi;
	if(!$item_id) return false;

	if($this->item=='city') $extra=',zip';
	$sql="select gi.id,gi.name,gg.id as ".$this->group."_id,gg.name as ".$this->group."_name $extra from geo_$this->item gi ".
		"inner join geo_$this->group gg on group_id=gg.id ".
		"where gi.id='$item_id'";
	$res=sql_query($sql,$dbi);
	$a=sql_fetch_assoc($res,$dbi);

	global $gsf;
	if(is_object($gsf)) $a[shipmentfee]=$gsf->getItemShipmentfee($item_id);

	return $a;
}

function getGroupIdByName($name) {
	global $dbi;
	$sql="select id from geo_$this->group where name like '$name'";
	$res=sql_query($sql,$dbi);
	list($id)=sql_fetch_row($res,$dbi);
	return $id;
}
function getItemIdByName($name,$group_id) {
	global $dbi;
	$sql="select id from geo_$this->item where name like '$name' and group_id='$group_id'";
	$res=sql_query($sql,$dbi);
	list($id)=sql_fetch_row($res,$dbi);
	return $id;
}

function searchUsers($cities) {
	global $dbi;
	$sql="select distinct user_id from user_geo where geo_id in ($cities)";
	$res=sql_query($sql,$dbi);
	while(list($id)=sql_fetch_array($res,$dbi))
		$ids[]=$id;
	return $ids;
}

function getUserGeo($uid) {
	global $dbi;
	$sql="select gi.id, gi.name, gg.id as group_id from user_geo ".
		"left join geo_$this->item gi on geo_id=gi.id ".
		"left join geo_$this->group gg on gi.group_id=gg.id ".
		"where user_id='$uid'";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi))
		$data[]=$a;
	return $data;
}

function getUserItemIds($uid) {
	global $dbi;
	$sql="select geo_id from user_geo where user_id='$uid'";
	$res=sql_query($sql,$dbi);
	while(list($c)=sql_fetch_row($res,$dbi))
		$data[]=$c;
	return $data;
}


function saveUserGeo($uid,$cities) {
	global $dbi;
	$sql="delete from user_geo where user_id='$uid'";
	sql_query($sql,$dbi);

	if($cities) {
		foreach($cities as $c)
			$rows[]="('$uid','$c')";

		$sql="insert into user_geo (user_id,geo_id) values ".implode(',',$rows).";";
		sql_query($sql,$dbi);
	}
}

function getProductGeoData($product_id) {
	global $dbi;
	$sql="select gi.id, gi.name, gg.id as group_id, gg.name as group_name from webshop_product_geo ".
		"left join geo_$this->item gi on geo_id=gi.id ".
		"left join geo_$this->group gg on gi.group_id=gg.id ".
		"where product_id='$product_id'";
	$res=sql_query($sql,$dbi);
	$a=sql_fetch_assoc($res,$dbi);
	return $a;
}

function saveProductGeo($product_id,$item_id) {
	global $dbi;
	$sql="delete from webshop_product_geo where product_id='$product_id'";
	sql_query($sql,$dbi);

	$sql="insert into webshop_product_geo (product_id,geo_id) values ('$product_id','$item_id')";
	sql_query($sql,$dbi);
}

}
?>