<?
/*** Property type-ok:
 *
 * G: Tulajdons�gcsoport
 * N: Tulajdons�g: norm�l (igen,nem,N/A)
 * U: Tulajdons�g: egyedi
 * I: Tulajdons�g: egyedi, intervallum (haszn�lja a value_extra mez�t)
*/
class property {

	var $webshop_category_parents; // webshop csoportokat n�z term�k eseten, vessz�s lista
	var $usertype_filter;
	var $conn_webshop_category;
	var $conn_user_type;

function property(){

	global $auth;

	if(sql_check_table('property_user_type')){
		$this->conn_user_type=1;
	}

	if(sql_check_table('property_webshop_category')){
		$this->conn_webshop_category=1;
	}

	// alapbol a belepett tulajdonsagai
	$this->usertype_filter=$auth->user_type_id;

}

function getTree($mode='',$parent=0,$target_type='',$target=0, $ordering=false) {

	/*
	$target_type='p':
		- $target term�k f�kateg�ri�j�hoz hozz�rendelt f��gakra sz�r
		- $target term�k �rt�k�t hozz�f�zi a lek�rdez�shez
	$target_type='u':
		- a felhaszn�l�khoz �ltal�nosan hozz�rendelt f��gakra sz�r
		- $target lehet 0 is, ha csak ez az �ltal�nos sz�r�s kell
		- $target user �rt�k�t hozz�f�zi a lek�rdez�shez
	*/
	global $dbi;

	if($this->search_void){
		 $where[]=" p.search_void=0 ";
	}

	if($this->exclude_ids) $where[]=" p.id not in ($this->exclude_ids) ";
	unset($this->exclude_ids);

	if($target_type=='p' && !$this->cat_filter && ($maincats=$this->getWebshopProductAllParentCategories($target)))
		$this->cat_filter=implode(',',$maincats);

	if(!$parent) $where[]=" sub is null";
	else{
		if(is_numeric($parent)) $where[]=" main=$parent";
		elseif(is_array($parent)) $where[]=" main in (".implode(',',$parent).") ";
	}

	if($target_type=='u' || $target_type=='c'  || ($target_type=='p' && $this->cat_filter)) {
		if(!$parent){
			switch($target_type) {
				case 'p': $join_filter="inner join (select * from property_webshop_category where category_id in ($this->cat_filter)) pc on p.id=pc.property_id "; break;
				case 'u':
						$join_filter=" inner join (select * from property_user_type where user_type_id in ($this->usertype_filter)) pc on p.id=pc.property_id ";
					break;
				case 'c': $join_filter=' ';break;
			}
		}
		if($target) {
			$join_value="left outer join (select * from property_value where target_type='$target_type' and target_id='$target') pv on p.id=pv.property_id ";
			$field_value=',value,value_extra';
		}
	}

	if ($ordering) $ord = " order by name"; else $ord = " order by pos";

	$sql_where=" where ".implode(' and ',$where);

	$sql="select distinct p.id,type,name,pos,shortname as unit $field_value from property p ".
		"left outer join property_conn on p.id=sub ".
		"left outer join unit u on unit=u.id ".
		$join_filter.
		$join_value.
		$join_category.
		"$sql_where $ord";
	//echo $sql."<br><br>";
	$res=sql_query($sql,$dbi);

	$this->level++;

	$this->getParentChain($parent);
	$parent_chain=$this->parents;

	$n=sql_num_rows($res,$dbi); $i=0;
	while($a=sql_fetch_assoc($res,$dbi)) {
		$i++;
		$a['first']=($i==1);
		$a['last']=($i==$n);

		$a['level']=$this->level;
		$a['main']=$parent;

		$a['conn']=$this->checkPropertyConnection($a[id]);

		if($a['value'])
			foreach($parent_chain as $pid)
				$this->group_active[$pid]=1;

		$id=$a['id'];
		if($mode!='flat') {
			if($mode=='tree') $tree=$this->getTree($mode,$id,$target_type,$target);
			else $tree=$id;
			if($this->node_name){
				$nodename=$this->getSeoName($a[name]);
			}else{
				$nodename=$id;
			}
			$data[$nodename]=array_merge($a,array(tree=>$tree));
		}else{
			$this->list[]=array_merge(array('id'=>$id),$a);
			if($a['type']=='G') $this->getTree($mode,$id,$target_type,$target);
		}
		
	}
	$this->level--;
	//echo"<pre>";print_r($this->list);

	return $data;
}

function catchPropertyValues($list,$array_to_string=0, $show_groups=false){

	foreach($list as $key => $item){
		if(($item[value]) || ($show_groups && $item[type]=="G")){
			if($array_to_string){
				$result.=$item[name]." ".$item[unit];
			}else{
				$result[]=$item;
			}
		}
	}
//echo "<pre>";print_r($result);
	return $result;

}


function copyProperty($post){

	global $dbi;

	$copyid=$post[copy];
	$newname=$post[name_whencopy];

	unset($this->list);

	$d=$this->getPropertyData($copyid);
	$d['name']=$newname;
	$d['parent']=$post['parent'];
	$this->getTree('flat',$copyid);
	unset($d[id]);
	$newid=$this->saveNewProperty($d);
	$subtree=$this->list;
	foreach($subtree as &$s){
		$s['parent']=$newid;
		unset($s[id]);
		unset($s[first]);
		unset($s[last]);
		unset($s[level]);
		unset($s[conn]);
		unset($s[main]);
		$this->saveNewProperty($s);
	}

}

function getPropertyJustUnit(){

	// azokat a tulajdons�gokat gy�tji le, amikre van �rt�k, teh�t
	// term�khez m�r be van �ll�tva
	// egyenl�re a Normal tulajdons�gokat gy�jti keres� comb�ba

	global $dbi;

	if($this->prod_idlist) $sql_where = " and pv.target_type='p' and target_id in ($this->prod_idlist) ";

	$sql=" select distinct pp.id as group_id, pv.property_id as id, pp.name as groupname, p.name as name, p.unit as unit_id, pv.value ".
		 " from property_value pv inner join property p on p.id=pv.property_id ".
		 " left outer join property_conn pc on pc.sub=pv.property_id ".
		 " left outer join property pp on pp.id=pc.main ".
		 " where p.type in ('U') $sql_where order by pv.value";

	$res=sql_query($sql,$dbi);
	while($tmp=sql_fetch_assoc($res,$dbi)){
		$tmp[unit]=$this->getUnitData($tmp[unit_id]);
		$data[]=$tmp;
	}
	//echo "<pre>";print_r($data);
	return $data;
}

function getPropertyJustNormal(){

	// azokat a tulajdons�gokat gy�tji le, amikre van �rt�k, teh�t
	// term�khez m�r be van �ll�tva
	// egyenl�re a Normal tulajdons�gokat gy�jti keres� comb�ba

	global $dbi;

	if($this->prod_idlist && !$this->property_id) $sql_where = " and pv.target_type='p' and target_id in ($this->prod_idlist) ";
	if(is_array($this->property_id)){
		$ids=implode(',',$this->property_id);
		$sql_where=" and pv.property_id in ($ids)";
	}

	$sql=" select distinct pp.id as group_id, pp.name as groupname, pv.property_id as id, p.name as name ".
		 " from property_value pv inner join property p on p.id=pv.property_id ".
		 " left outer join property_conn pc on pc.sub=pv.property_id ".
		 " left outer join property pp on pp.id=pc.main ".
		 " where p.type in ('N') $sql_where order by groupname,name";
	$res=sql_query($sql,$dbi);
	while($tmp=sql_fetch_assoc($res,$dbi)){
		$data[]=$tmp;
		$this->group_idlist[]=$tmp[group_id];
	}

	return $data;
}

function getPropertyJustGroup(){

	// azokat a tulajdons�gokat gy�tji le, amikre van �rt�k, teh�t
	// term�khez m�r be van �ll�tva
	// egyenl�re a CSOPORT tulajdons�gokat gy�jti keres� comb�ba

	global $dbi;
	if(is_array($this->list)) $this->group_idlist=$this->list;
	if($this->group_idlist){
		if(is_array($this->group_idlist)) $group_idlist=implode(',',$this->group_idlist);
		else $group_idlist=$this->group_idlist;
		$sql_where = " and id in ($group_idlist) ";
	}

	$sql=" select distinct p.id as group_id, p.name as groupname ".
		 " from property p  ".
		 " where id not in (select distinct sub from property_conn) ".
		 " and p.type in ('G') $sql_where order by groupname,name ";

	$res=sql_query($sql,$dbi);
	while($tmp=sql_fetch_assoc($res,$dbi)){
		$data[]=$tmp;
	}
	//echo "<pre>";print_r($this->group_idlist);
	return $data;
}

function getParent($id) {
	global $dbi;
	$sql="select main from property_conn where sub=$id";
	$res=sql_query($sql,$dbi);
	list($parent)=sql_fetch_array($res,$dbi);
	return $parent;
}

function getParentChain($id,$reset=true) {
	global $dbi;
	if($reset) $this->parents=array($id);

	$sql="select main from property_conn where sub='$id'";
	$res=sql_query($sql,$dbi);
	list($main)=sql_fetch_array($res,$dbi);
	if($main) {
		$this->parents[]=$main;
		$this->getParentChain($main,false);
	}
}
/**
* get all parent names of property id to $this->parents_name
*
* @param integer $id property_id
*/
function getParentNamesChain($id,$reset=true) {

	global $dbi;

		if($reset) $this->parents_name="";

		$sql="select name, main from property_conn
			left join property on property_conn.main = property.id
			where sub='$id'";
		$res=sql_query($sql,$dbi);
		list($name, $main)=sql_fetch_array($res,$dbi);
		if($name) {
			$this->parents_name_array[] = $name;
			$this->getParentNamesChain($main,false);
		}
	for ($i=count($this->parents_name_array)-1; $i>=0; $i--)
		$this->parents_name .= $this->parents_name_array[$i]."->";
	$this->parents_name_array=array();
}

function checkPropertyConnection($pid){

	global $dbi;

	if($this->conn_user_type){
		$u=new user;
		$sql="select user_type_id from property_user_type where property_id=$pid";
		$res=sql_query($sql,$dbi);
		while($r=sql_fetch_assoc($res,$dbi)){
			$r=$u->getUserTypeData($r[user_type_id]);
			$data[user_type][]=$r;
		}
		if(is_array($data)) return $data;
	}

	if($this->conn_webshop_category){
		$sql="select category_id from property_webshop_category where property_id=$pid";
		$res=sql_query($sql,$dbi);
		while($r=sql_fetch_assoc($res,$dbi)){
			$data[webshop_category][]=$r;
		}
		return $data;
	}


}

function getGroupsWscatConn($category_id) {
	global $dbi;

	$this->getWebshopParentCategories($category_id);
	$cat_chain=implode(',',$this->ws_cat_parents);

	$sql="select p.id,type,name,category_id as cat from property p ".
		"left join property_conn on p.id=sub ".
		"left join unit u on unit=u.id ".
		"left join (select * from property_webshop_category where category_id in ($cat_chain)) pc on p.id=pc.property_id ".
		"where sub is null order by pos";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)) {
		extract($a);
		$on = ($cat==$category_id);
		$parent_on = ($cat && $cat!=$category_id);
		if(!$data[$id][on])
			$data[$id]=compact(type,name,on,parent_on);
	}
	return $data;
}

function getPropertyData($id,$parent=false) {
	if(!$id) return;
	global $dbi;

	$sql="select p.type,p.name,u.shortname,p.pos from property p ".
		"left outer join unit u on unit=u.id ".
		"where p.id=$id";
	$res=sql_query($sql,$dbi);
	list($type,$name,$unit,$pos)=sql_fetch_array($res,$dbi);

	if(!empty($parent))
	{
		unset($this->parents);
		$this->getParentChain($id,false);
		foreach(array_reverse($this->parents) as $parent_id){
			$tmpPar[]=$this->getPropertyData($parent_id,false);
		}
	}else
		$tmpPar=null;
	
	$data=array(
		'id'=>$id,
		'type'=>$type,
		'name'=>$name,
		'unit'=>$unit,
		'pos'=>$pos,
		'parents'=>$tmpPar,
	);
	return $data;
}

/**
  * lek�rdezi a tulajdons�g testv�reit
  *
  */
function getPropertyFathers($product_id, $prop_id){
	if (!$prop_id && !$product_id) return;
	global $dbi;

	//echo "ezek a hozz�adott term�kek tulajond�sgai".$prop_id."<br>";
	//most a tulajdons�gok apj�nak a lek�rdez�se
	$sql="select distinct main from property_conn where sub=".$prop_id;
	$res=sql_query($sql,$dbi);
	if(list($main_id)=sql_fetch_array($res,$dbi)){

		//echo "ezek a tulajond�s apja
		//az apa gyerekeinek a lek�rdez�se
		//azok a gyerekek maradnak a list�ban, akik nem voltam lek�rdezett tulajdons�gok list�j�ban
		$this->list = "";
		$this->getTree('flat',$main_id,'p',$product_id);
		foreach ($this->list as $property){
			if ($property[type]="U" && $property[type]="N" && $property[value] && $property[id]!=$prop_id){
			$active_props[] = $property[id];
		}
		return $active_props;
		}

	}
}

function getProducts($property,$category=false, $only_configured_products=false, $details=false, $getConflictedValues=false) {
	global $dbi;
	if ($category) $w[] = " category_id='$category'";
	if ($only_configured_products) $w[] = " value=1";
	if ($this->getInactive) $w[]=" p.inactive<>0 ";
	else $w[]=" p.inactive=0 ";

	$where=" where ".implode(' and ',$w);

	if ($details)
	$sql="select p.id,name,inactive,value, img.file as pic from webshop_product p ".
		"left outer join (select * from property_value where target_type='p' and property_id='$property') v on p.id=v.target_id ".
		"left join (select product, file from image_products where main=1) img on img.product=p.id ".
		"left outer join webshop_product_conn px on p.id=px.product_id ".$where;
	else
	$sql="select id,name,inactive,value from webshop_product p ".
		"left outer join (select * from property_value where target_type='p' and property_id='$property') v on p.id=v.target_id ".
		"left outer join webshop_product_conn px on p.id=px.product_id ".$where;
	$res=sql_query($sql,$dbi);

	while($a=sql_fetch_assoc($res,$dbi)){
		if ($getConflictedValues && is_array($this->getPropertyFathers($a[id], $property))){
			$a[conflicted_prop] = 1;
			$data[] = $a;
		} else{
			$data[] = $a;
		}
	}
	return $data;
}

function getUsers($property,$user_type=null) 
{
	global $dbi;
	
	if(!empty($user_type)) $w[]=" u.user_type={$user_type} ";
	if ($this->getInactive) $w[]=" u.inactive<>0 ";
	else $w[]=" u.inactive=0 ";

	$where=" where ".implode(' and ',$w);

	$sql="select id,concat(firstname,' ',lastname) as name,email,inactive,value from users u "
		."inner join (select * from property_value where target_type='u' and property_id='{$property}') v on u.id=v.target_id "
		.$where." order by firstname,lastname";

	$res=sql_query($sql,$dbi);

	while($a=sql_fetch_assoc($res,$dbi)){
		$data[] = $a;
	}
	return $data;
}

function getWebshopProductAllParentCategories($product) {
	// verzi� 2.0
	// 1 termek tobb csoport!!
	global $dbi;
	$cats=array();

	$sql="select category_id from webshop_product_conn where product_id=$product";
	$res=sql_query($sql,$dbi);
	while(list($cat)=sql_fetch_row($res,$dbi)){ // tobb is lehet!!
		$this->getWebshopParentCategories($cat);
		$cats=array_merge($cats,$this->ws_cat_parents);
	}

	return $cats;
}

// a kateg�ria sz�leinek kigy�jt�se
// rekurz�v met�dus
// megegyezik a webshop ugyanezen met�dus�val
function getWebshopParentCategories($id,$reset=true) {
	global $dbi;
	if($reset) $this->ws_cat_parents=array($id);

	$sql="select main from webshop_category_conn where sub='$id'";
	$res=sql_query($sql,$dbi);
	list($main)=sql_fetch_row($res,$dbi);
	if($main) {
		$this->ws_cat_parents[]=$main;
		$this->getWebshopParentCategories($main,$with_names,false);
	}
}

function checkWebshopCategoryConn($product,$property_group) {
	global $dbi;

	if(!($maincat=$this->getWebshopMainCategory($product))) return false;

	$this->getParentChain($property_group);
	$property_root=end($this->parents);

	$sql="select 1 from property_webshop_category where property_id='$property_root' and category_id='$maincat'";
	$res=sql_query($sql,$dbi);
	return sql_num_rows($res,$dbi);
}

function searchItems($target_type,$post) {
	global $dbi;

	//where
	foreach($post as $gid=>$group) {
		unset($a);
		foreach($group as $id=>$v) {
			$all_inputs_empty=true;
			foreach($v as $part) if($part) {$all_inputs_empty=false;break;}
			if($all_inputs_empty) continue;

			if($v[N]){
				$a[]="(property_id='$id' and value='$v[N]')";
			}else {
				$from=''; $to='';

				if($x=$v[F]){
						// FT-I. fuggveny (tol)
						// F-I. fuggveny 
						$from=	"and value>=".round($x*0.9,2); 	
						$F=$x;
					if($x=$v[t]){
						// FT-I. fuggveny (tol-ig)
						$to="and value_extra<=".round($x*1.1,2); 		

						// akiknek csak a FROM van kitoltve db-ben
						// FT-II. fuggveny (nincs from)
						$a[]="(property_id='$id' and value_extra>=$F and value_extra<=$x and (value='' or value is null)  )";

						// akiknek csak a TO van kitoltve db-ben
						// FT-III. fuggveny (nincs to)
						$a[]="(property_id='$id' and value>=$F and value<=$x and (value_extra='' or value_extra is null)  )";

						// FT-IV. fuggveny (tol-ig-en kivul)
						$a[]="(property_id='$id' and value<=$F and value_extra>=$x)";

						// FT-V. fuggveny (felfele tolt halmaz)
						$a[]="(property_id='$id' and value>=$F and value<=".round($x*0.8,2)." and value_extra>=".round($x*0.8,2).")";

						// FT-VI. fuggveny (felfele tolt halmaz)
						$a[]="(property_id='$id' and value<=".round($F*1.2,2)." and value_extra>=".round($F*1.2,2)." and value_extra<=$x)";

					}else{
						// ha nincs to postbol, de db-ben van beallitva TO, es csak az
						// FIII
						$a[]="(property_id='$id' and value<=$F and (value_extra='' or value_extra is null)  )";
						
						// FII
						$a[]="(property_id='$id' and (value is null or value='') and value_extra>=$F )";
						
						// FIV
						$a[]="(property_id='$id' and value<=$F and value_extra>$F  )";
						
					}
				}elseif($x=$v[f]){

					$from=	"and value>=".round($x*0.9,2);

				}elseif($x=$v[t]) 	$to=	"and value_extra<=".round($x*1.1,2);

				$a[]="(property_id='$id' $from $to)";
			}
		}
		if($a) $where[$gid]="( ".implode(' OR ',$a)." )";
	}
	if(!$where) return false;
	//$w=implode(' or ',$where);
	//echo"<pre>";print_r($where);
	foreach($where as $gid=>$w) {
		$sql="select distinct target_id from property_value where $w and target_type='$target_type'";
		//echo "$sql<br>";
		$res=sql_query($sql,$dbi);
		while(list($id)=sql_fetch_array($res,$dbi)){
			if($target_type!='p' || $this->checkWebshopCategoryConn($id,$gid))
				$count[$id]++;
		}
	}
	arsort($count);
	//echo"<pre>";print_r($count);
	if($count) foreach($count as $id=>$c) {
		if($c==count($where)) $ids[]=$id;
	}

	return $ids;
}

function saveNewProperty($post) {

	global $dbi;

	if(empty($post['pos'])){
		//pos
		if($par=$post['parent']) $sql="select max(pos) from property inner join property_conn on id=sub where main=$par";
		else     $sql="select max(pos) from property left outer join property_conn on id=sub where main is null";
		$res=sql_query($sql,$dbi);
		list($pos)=sql_fetch_array($res,$dbi);
		$pos+=10;
	}else
		$pos=$post['pos'];
	
	//property insert
	$sql="insert into property (type,name,unit,pos) values('$post[type]','$post[name]','$post[unit]',$pos)";
	sql_query($sql,$dbi);

	$sql="select id from property where id=LAST_INSERT_ID()";
	$res=sql_query($sql,$dbi);
	list($id)=sql_fetch_array($res,$dbi);

	//property_conn insert
	if($par=$post['parent']){
		$sql="insert into property_conn values($par,$id)";
		sql_query($sql,$dbi);
	}

	// ha van jogozas
	if($post[user_type_id]){
		foreach($post[user_type_id] as $utid){
			$sql="insert into property_user_type (user_type_id,property_id) values ($utid,$id)";
			sql_query($sql,$dbi);
		}
	}
	return $id;
}

function updateProperty($post) {
	global $dbi;
	$sql="update property set type='$post[type]', name='$post[name]', unit='$post[unit]' where id=$post[id]";
	sql_query($sql,$dbi);
}

function switchProperties($id1,$direction) {
	global $dbi;
	if($direction=='up') {
		$rel='<'; $ord='desc';
	}else{
		$rel='>'; $ord='asc';
	}

	$parent=$this->getParent($id1);
	$where_parent=$parent?"=$parent":" is null";

	$sql="select pos from property where id=$id1";
	$res=sql_query($sql,$dbi);
	list($pos1)=sql_fetch_array($res,$dbi);

	$sql="select id,pos from property ".
		"left outer join property_conn on id=sub ".
		"where main$where_parent and pos$rel$pos1 order by pos $ord limit 1";
	$res=sql_query($sql,$dbi);
	list($id2,$pos2)=sql_fetch_array($res,$dbi);

	$sql="update property set pos=$pos2 where id=$id1";
	sql_query($sql,$dbi);
	$sql="update property set pos=$pos1 where id=$id2";
	sql_query($sql,$dbi);
}

function deleteProperty($id) {
	global $dbi;

	unset($this->list);
	$this->getTree('flat',$id);
	$this->list[]=$this->getPropertyData($id);

	foreach($this->list as $p){
		$sql="delete from property where id=$p[id]";
		sql_query($sql,$dbi);

		$sql="delete from property_conn where sub=$p[id]";
		sql_query($sql,$dbi);

		if($p[type]!='G') {
			$sql="delete from property_value where property_id=$p[id]";
			sql_query($sql,$dbi);
		}
	}
}

function saveValue($target_type,$target,$property,$value,$value_extra) {

	global $dbi;

	$sql="delete from property_value where target_type='$target_type' and target_id='$target' and property_id='$property'";
	sql_query($sql,$dbi);

	if($value || $value=='0' || $value_extra || $value_extra=='0') {
		$sql="insert into property_value (target_type,target_id,property_id,value,value_extra) ".
			"values ('$target_type','$target','$property','$value','$value_extra')";
		sql_query($sql,$dbi);
	}

}

function saveValues($target_type,$target,$property,$value,$value_extra) {

	global $dbi;

	if (is_array($property) && ($value || $value=='0' || $value_extra || $value_extra=='0')){
		foreach ($property as $prop)
			$insert_sql_array[] = "('$target_type',$target,$prop,'$value','$value_extra')";

		$sql_del = implode(" or property_id=", $property);
		$sql="delete from property_value where target_type='$target_type' and target_id='$target' and (property_id=$sql_del)";
		sql_query($sql,$dbi);

		$sql_ins = implode (", ", $insert_sql_array);
		$sql="insert into property_value (target_type,target_id,property_id,value,value_extra) ".
				"values $sql_ins";
		sql_query($sql,$dbi);

	}
}

function getWebshopCategoriesByProperty($pid) {
	global $dbi;
	$ws=new webshop;

	$this->getParentChain($pid);
	$main=end($this->parents);

	$sql="select category_id from property_webshop_category where property_id='$main'";
	$res=sql_query($sql,$dbi);
	while(list($cat)=sql_fetch_array($res,$dbi)){
		$ids[]=$cat;
		$ws->categoryTree($cat);
		if($ws->selected_category_ids)
			$ids=array_merge($ids,$ws->selected_category_ids);
	}
	if(!$ids) return;
	$data=$ws->getCategories(implode(',',$ids));
	return $data;

}

function saveWebshopCategory($post,$cat) {
	global $dbi;

	if($post[add]) foreach($post[add] as $id) {
		$sql="insert into property_webshop_category (category_id,property_id) values ('$cat','$id')";
		sql_query($sql,$dbi);
	}
	if($post[del]) foreach($post[del] as $id) {
		$sql="delete from property_webshop_category where category_id='$cat' and property_id='$id'";
		sql_query($sql,$dbi);
	}
}

function getUnitData($uid) {
	global $dbi;
	$sql="select id,shortname,longname from unit where id=$uid";
	$res=sql_query($sql,$dbi);
	return sql_fetch_assoc($res,$dbi);
}

function getUnits($skip_null=false) {
	global $dbi;
	$sql="select id,shortname,longname from unit";
	$res=sql_query($sql,$dbi);
	while(list($id,$shortname,$longname)=sql_fetch_array($res,$dbi)){
		if($id==0 and $skip_null) continue;
		$data[]=array(id=>$id, shortname=>$shortname, longname=>$longname);
	}
	return $data;
}

function saveNewUnit($post){
	global $dbi;

	$sql="select shortname from unit where shortname='".$post[new_shortname]."'";
	$res=sql_query($sql,$dbi);
	if(list($shortname)=@sql_fetch_row($res,$dbi)) return $shortname;

	$sql="insert into unit (shortname,longname) values ('$post[new_shortname]','$post[new_longname]')";
	sql_query($sql,$dbi);
	return false;
}

function updateUnit($post){
	global $dbi;
	$sql="update unit set shortname='$post[mod_shortname]', longname='$post[mod_longname]' where id=$post[mod_id]";
	sql_query($sql,$dbi);
}

function deleteUnit($id){
	global $dbi;
	$sql="delete from unit where id=$id";
	sql_query($sql,$dbi);
}

/**
 * get property values with property id, name and parents name
 *
 * @param boolean $withCompatibilityList sql condition for compatibility list (product_collection_compatible)
 * @return array|NULL
 */
function getPropertyValues($withCompatibilityList=true, $arrayOutput=false){
	global $dbi;
	if ($withCompatibilityList) $cond=" and id not in (select property_id from product_collection_compatible)";
	$sql="select id, name from property where type!='G'".$cond;
	$res=sql_query($sql,$dbi);

	while(list($id,$name)=sql_fetch_array($res,$dbi)){
		$this->parents_name_array=array();
		$this->parents_name = "";
		$this->getParentNamesChain($id, true, $arrayOutput);
		$this->parents_name = substr($this->parents_name, 0, -2);
		if ($arrayOutput)
				$data[]=array(id=>$id, name=>$name, parents_name=>$this->parents_name, parents_name_array=>$this->parents_name_array, main_parent=>$this->parents_name_array[count($this->parents_name_array)-1]);
			else
				$data[]=array(id=>$id, name=>$name, parents_name=>$this->parents_name);

	}
	return $data;
}

function renameProperty($property_id, $new_name){
	global $dbi;

	$sql = "update property set name='$new_name' where id=".$property_id;
	sql_query($sql,$dbi);
}


function getSeoName($n){

	global $SITEGLOBALS;

	return strtolower(str_replace($SITEGLOBALS[REPLACE_CHARS][mit],$SITEGLOBALS[REPLACE_CHARS][mire],$n));

}


} // end class
?>