<?
class vtigerConnector{
	var $www="http://e5crm.more7.com/e5ltd/crm/sugar.php";
	var $data;
	var $limit;
	var $where;
	var $count;
	var $pp=50;
	var $order_by="purchaseorder.duedate";
	var $sorder="asc";
	function vtigerConnector(){
	}
	function curl_get_contents($URL){
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
    }
	function getData(){
		$www=$this->www;
		$this->www.="?ok=ok";
		if($this->order_by){
			$opts['order_by']=$this->order_by;
			$opts['sorder']=$this->sorder;
			$this->www.="&order_by=".$this->order_by."&sorder=".$this->sorder;
		}
		$opts['limit']=$this->limit;
		$this->www.="&limit=".$this->limit;
		if($this->where){
			$opts['where']=$this->where;
			$this->www.="&where=".$this->where;
		}

		$source=file_get_contents($www."?content=".base64_encode(serialize($opts)));
		//$file=fopen($this->www,"r");
		//$source = fread($file,filesize($this->www)); 
		$this->data=unserialize(base64_decode($source));
		//echo $source."<br><br>";
		//echo "<a href='".$www."?content=".base64_encode(serialize($opts))."'>click</a><br><br>";
		//print_r($this->data)."<br><br>";
	}
	function getCount(){
		$this->count=$this->data[0]['count'];
	}
	function getIdByCode($code){
		$r=mysql_fetch_array(mysql_query("select id from ecmproducts where code like '".$code."'"));
		return $r['id'];
	}
	function formatNumber($no){
		return number_format($no,$GLOBALS['sugar_config']['default_currency_significant_digits'],$GLOBALS['sugar_config']['default_decimal_seperator'],$GLOBALS['sugar_config']['default_number_grouping_seperator']);
	}
	function timeDate($time){
		if($time!='0000-00-00')return $GLOBALS['timedate']->to_display_date($time);
	}
	function sortableColumn($name,$field,$ascdesc,$width=10,$index="index"){
		$img="";
		if($ascdesc=="asc"){
			$sorder="desc";
			if($this->order_by==$field)$img="_up";
		}
		else {
			$sorder="asc";
			if($this->order_by==$field)$img="_down";
		}
		$str='<td class="listViewThS1" width="'.$width.'%"><a class="listViewThLinkS1" href="'.$this->getAddr($index).'&order_by='.$field.'&sorder='.$sorder.'">'.$name.'&nbsp;<img src="themes/Sugar/images/arrow'.$img.'.gif" border="0" /></a></td>';
		return $str;
	}
	function selectStatus($value){
		$arr=array("Created","Approved","Delivered","Canceled");
		$str.='<option value="">select</option>';
		foreach($arr as $a){
			$str.='<option value="'.$a.'"';
			if($a==$value)$str.=' selected';
			$str.='>'.$a.'</option>';
		}
		return $str;
	}
	function unformatNumber($no){
		$v=str_replace(".","",$no);
		$v=str_replace(",",".",$v);
		return $v;
	}
	function getAddr($index="index"){
		$str=$index.".php?ok=ok";
		foreach($_GET as $k=>$v){
			if($k!="ok")$str.="&".$k."=".$v;
		}
		return $str;
	}
	function generateButtons($btns){	
		if($btns[0]){
			$start="";	
			$dstart="";		
		}
		if($btns[1]){
			$previous="";
			$dprevious="";				
		}
		if($btns[2]){
			$next="";	
			$dnext="";			
		}
		if($btns[3]){
			$end="";
			$dend="";	
		}	
		if(!$btns[0]){
			$start="_off";
			$dstart=' disabled="disabled"';			
		}
		if(!$btns[1]){
			$previous="_off";
			$dprevious=' disabled="disabled"';				
		}
		if(!$btns[2]){
			$next="_off";
			$dnext=' disabled="disabled"';				
		}
		if(!$btns[3]){
			$end="_off";
			$dend=' disabled="disabled"';						
		}
		$to=$this->pp+$this->start-1;
		if($this->pp+$this->start-1>$this->count)$to=$this->count;
		
		$str='
		<a class="listViewThLinkS1" href="'.$this->getAddr().'&start=0">
		<button type="button" title="Start" class="button"'.$dstart.'>
			<img src="themes/Sugar/images/start'.$start.'.gif" alt="Start" width="13" align="absmiddle" border="0" height="11">
		</button>
		</a>
		<a class="listViewThLinkS1" href="'.$this->getAddr().'&start='.($this->start-$this->pp-1).'">
		<button type="button" title="Previous" class="button"'.$dprevious.'>
			<img src="themes/Sugar/images/previous'.$previous.'.gif" alt="Previous" width="8" align="absmiddle" border="0" height="11">
		</button>
		</a>
		<span class="pageNumbers">('.$this->start.' - '.$to.' of '.$this->count.')</span>
		<a class="listViewThLinkS1" href="'.$this->getAddr().'&start='.($this->start+$this->pp-1).'">
		<button type="button" title="Next" class="button"'.$dnext.'>
			<img src="themes/Sugar/images/next'.$next.'.gif" alt="Next" width="8" align="absmiddle" border="0" height="11">
		</button>
		<a class="listViewThLinkS1" href="'.$this->getAddr().'&start='.($this->count-$this->pp-1).'">
		<button type="button" title="End" class="button"'.$dend.'>
			<img src="themes/Sugar/images/end'.$end.'.gif" alt="End" width="13" align="absmiddle" border="0" height="11">							
		</button>
		</a>';
		return $str;
	}

}
?>