<?php

Doo::loadCore ( 'db/DooModel' );

class company extends DooModel {
	
	public $cid;
	public $companyname;
	public $nature;
	public $local;
	public $fax;
	public $webservice;
	public $department;
	public $staffid;
	
	public $district;
	
	public $position;
	public $office;
	public $address;
	public $ride;
	public $landmarks;
	public $stay;
	public $tag;
	
	public $_table = 'CLD_company';
	public $_primarykey = 'cid';
	public $_fields = array ('cid', 'companyname','staffid', 'district' ,'nature' ,'local', 'fax', 'webservice','department'  , 'position', 'office', 'address', 'ride','landmarks','stay' );
	
	public function getCompanyByCid($cid){
		 $list=$this->find ( array ('where' => "cid= '".$cid."'", 'asArray' => TRUE ) );
		 Doo::loadClass ( 'XDeode' );
		 $XDeode = new XDeode ( 5 );
		foreach ($list as $key=>$value){
			$list[$key]['cidKey'] = $XDeode->encode( $value['cid'] );
		}
		return $list;
	}
	
	public function getCompanyByStaff($sid){
		return $this->find ( array ('where' => "staffid= '".$sid."'", 'asArray' => TRUE ) );
	}
	
	public function getCompanyByDistrict($sid,$district,$isadmin,$page,$size,$tagSQL){
		
		if($page==1)
			$page=0;
		if($isadmin!=0){
			if($tagSQL){
				$sql = "select b.cid,a.tag,b.companyname,b.nature,b.local,b.fax,b.webservice,b.position,b.office,b.address,b.ride,b.district,b.landmarks from CLD_tagCompanyNexus as a left join CLD_company as b on (a.company=b.cid )  where ".$district." limit ".$page.','.$size ;
		    	
				$query = Doo::db ()->query ( $sql );
				$result = $query->fetchAll ();
				
			}else
				$result= $this->find ( array ('select'=>'companyname,cid,local,district,staffid,nature','where'=>$district,'limit'=>$page.','.$size, 'asArray' => TRUE ) );
		}else{//"staffid= '".$sid."' and ".
			$result= $this->find ( array ('select'=>'companyname,cid,local,district,staffid,nature','where' => $district, 'limit'=>$page.','.$size, 'asArray' => TRUE ) );
		}
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ($result as $key=>$value){
			$result[$key]['cidKey'] = $XDeode->encode( $value['cid'] );
		}
		
		return $result;
	}
	
	public function getCompanyByCompanyname($name){
		return $this->find ( array ('where' => "companyname= '".$name."'", 'asArray' => TRUE ) );
	}
	
	public function searchCompany($search){
		return $this->find ( array ('where' => "companyname like '%".$search."%'", 'asArray' => TRUE ) );
	}
	
	public function getCompanyByName($name,$local){
		return $this->getOne ( array ('where' => "companyname= '".$name."' and local='".$local."'", 'asArray' => TRUE ) );
	}
	
}

?>