<?
/* FIELD TYPES:
 * 
 * i : input		S : select
 * t : textarea		R : radio group
 * c : checkbox		C : checkbox group
 *
 * h : hidden (csak az owner_id)
 */

class company{

var $fields=array(
	'name'=>  array(type=>'i', length=>40), 
	'inactive'=> array(type=>'c'),
	'taxnr'=> array(type=>'i', length=>20),
	'owner_id'=> array(type=>'h'),
);

// léptető
var $split=10; // hány tétel per oldal
var $range=3;  // hány szomszédos oldal látsszon a léptetőben


function company($detach_multiple=false) {
	global $dbi;

	$sql="select name,id,type,length,onchange,multiple from company_custom_field order by pos";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)){
		$name=array_shift($a);
		if($detach_multiple && $a[multiple]) {
			$this->fields_multiple[$name]=$a;
		}else{
			$this->fields[$name]=$a;
		}
	}
}

function detachMultipleFields() {
	foreach($this->fields as $f=>$f_params)
		if($f_params[multiple]) {
			$this->fields_multiple[$f]=$f_params;
			unset($this->fields[$f]);
		}
}

function getForm() {
	global $dbi;
	
	$sql="select * from company_custom_value";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)){
		extract($a);
		$values[$group_id][$value]=$label;
	}
	
	foreach($this->fields as &$a)	
		if($a[type]==strtoupper($a[type])) $a[values]=$values[$a[id]];
}

function getCompanies($p='') {
	
	
	// a $p parameterben tobb where is megadhato
	// kell ellenorzes, hogy a where az tomb-e, vagy sem
	
	global $dbi;
	$this->detachMultipleFields();

	//léptető hatására limit
	if($this->split) {
		if(!($o=$p[o]) && !($o=$_GET[o])) $o=0;
		$o_item=$o*$this->split;
		$limit="limit $o_item,$this->split";
	}

	if($x=$p[where]){
		$w=explode(';',$x);
	}

	if(!$p[_inactive]) $w[]='inactive=0';
	if($x=$p[company]) $w[]="name like '%$x%'"; 

	if($w) $where='where '.implode(' and ',$w);
	
	$sql="select c.*$p[sql_extra_field] from company c $where order by name $limit";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)){
		$a[id]=$a[company_id];
		if($p[extra_fields_required] && ($m=$this->getCompanyMultipleData($a[id],true,$p[extra_fields_required])))
			$a=array_merge($a,$m);
		$data[]=$a;
	}

	//léptető
	if($this->split) {
		$sql="select count(company_id) from company $where $sql_inactive";
		$res1=sql_query($sql,$dbi);
		list($items)=sql_fetch_row($res1,$dbi);		
		$this->makePages($items,$o);
	}
	
	return $data;
}

function makePages($items,$o) {
	$pages=ceil($items/$this->split);
	if(!$pages) return;

	$sight_min=$o-$this->range; if($sight_min<0) $sight_min=0;
	$sight_max=$o+$this->range; if($sight_max>=$pages) $sight_max=$pages-1;

	for($i=$sight_min; $i<=$sight_max; $i++) $sight[$i].=$i+1;

	$this->pages=array(items=>$items, pages=>$pages, sight=>$sight, act=>$o);
	//print'<pre>';print_r($this->pages);print'</pre>';
}

function getCompanyData($id) {
	global $dbi;
	$this->detachMultipleFields();
	
	$sql="select company_id as id,c.* from company c where company_id='$id'";
	$res=sql_query($sql,$dbi);
	$data=sql_fetch_assoc($res,$dbi);
	if($m=$this->getCompanyMultipleData($id,true))
		$data=array_merge($data,$m);
	return $data;
}

function getCompanyName($id) {
	global $dbi;
	$sql="select name from company where company_id='$id'";
	$res=sql_query($sql,$dbi);
	$a=sql_fetch_assoc($res,$dbi);
	return $a[name];
}

function getCompanyMultipleData($id,$getValueLabels=false,$fields_filter='') {
	if(!$this->fields_multiple) return;
	global $dbi;
	if($fields_filter) $fields_filter=explode(',',$fields_filter);

	// multiple mezők kigyűjtése vagy beadott mező használata
	
	if($fields_filter)
		foreach($this->fields_multiple as $f=>$f_params)
			if(in_array($f,$fields_filter)) $fields_filter_ids[]=$f_params[id];
	
	// multiple mező értékek nevei
	if($getValueLabels) {
		$sql="select value,group_id,label from company_custom_value ".
			"inner join (select id from company_custom_field where multiple=1) f on group_id=f.id ";
		if($fields_filter_ids) $sql.='where group_id in ('.implode(',',$fields_filter_ids).')'; 
		$res=sql_query($sql,$dbi);
		while($a=sql_fetch_assoc($res,$dbi)) {
			extract($a);
			$value_labels[$group_id][$value]=$label;
		}
	}

	// céghez tartozó értékek kiolvasása
	foreach($this->fields_multiple as $f=>$f_params) {
		if(!$fields_filter || in_array($f,$fields_filter)) {
			if($getValueLabels) unset($text);
			
			$sql="select value from company_$f where company_id='$id'";
			$res=sql_query($sql,$dbi);
			while(list($value)=sql_fetch_row($res,$dbi)) {
				$data[$f][$value]=1;
				if($getValueLabels) $text[]=$value_labels[$f_params[id]][$value];
			}
			if($text) $data[$f][text]=implode(', ',$text);
		}
	}
	
	return $data;
}

function saveNewCompany($post) {
	global $dbi;
	$this->detachMultipleFields();

	if(!$post[owner_id]) {
		global $auth;
		$post[owner_id]=$auth->uid;
	}
	
	foreach($this->fields as $f=>$f_params) {
		$fields[]=$f;
		$values[]=$post[$f];
	}
	$sql="insert into company (".implode(',',$fields).") values ('".implode("','",$values)."')";
	sql_query($sql,$dbi);
	
	$sql="select max(company_id) from company";
	$res=sql_query($sql,$dbi);
	list($id)=sql_fetch_row($res,$dbi);
	
	$this->saveCompanyMultipleData($id,$post);
	
	return $id;
}

function updateCompany($id,$post) {
	global $dbi;
	$this->detachMultipleFields();
	
	foreach($this->fields as $f=>$f_params) {
		$set[]="$f='$post[$f]'";
	}
	$sql="update company set ".implode(',',$set)." where company_id='$id'";
	sql_query($sql,$dbi);
	
	$this->saveCompanyMultipleData($id,$post,true);
}

function saveCompanyMultipleData($id,$post,$update=false) {
	if(!$this->fields_multiple) return;
	global $dbi;
	
	foreach($this->fields_multiple as $f=>$f_params) {
		unset($values);
		if($post[$f]) {
			if($update) {
				$sql="delete from company_$f where company_id='$id'";
				sql_query($sql,$dbi);
			}
			foreach($post[$f] as $v) $values[]="('$id','$v')";
			$sql="insert into company_$f (company_id,value) values ".implode(',',$values);
			sql_query($sql,$dbi);
		}
	}
}

function setCompanyInactive($id,$value) {
	global $dbi;
	$sql="update company set inactive='$value' where company_id='$id'";
	sql_query($sql,$dbi);
}
	
function deleteCompany($id) {
	global $dbi;
	$this->detachMultipleFields();

	$sql="delete from company where company_id='$id'";
	sql_query($sql,$dbi);
	
	foreach($this->fields_multiple as $f=>$f_params) {
		$sql="delete from company_$f where company_id='$id'";
		sql_query($sql,$dbi);
	}
}

}
?>