Location: PHPKode > projects > Monyet > classes/netrelation.php
<?php
Class NetRelation Extends EntityBase implements Observer{

	private $agent;
	private $net;

	public function __construct(){
		//PHYSIC TABLE DEFINITION for NetRelation
		//-----------------------------------------------------
		$tablename = 'netrelation';

		$field_id = 'rel_id';	
		$field_agent = 'agent_id';
		$field_net = 'net_id';
		$field_ping = 'ping_id';
		//-----------------------------------------------------
		
		$rawfields = array('id'=>$field_id,'agent'=>$field_agent,'net'=>$field_net,'ping'=>$field_ping);
		$agent = new Agent();	 
		$net = new NetMapNet();
		$attributes = array($agent->getAbsField('description'),$net->getAbsField('name'),$field_ping);
		$condition = $agent->getPrimaryKey().' = '.$tablename.'.'.$field_agent.' AND '.$net->getPrimaryKey().' = '.$tablename.'.'.$field_net;

		parent::__construct(array($tablename,$agent->getPrimaryTable(),$net->getPrimaryTable()),$field_id,$attributes,$rawfields,$condition);
	}

	//overwrite the select
	//FIXME: refine the field name if you get spare time
	public function selectOffset($offset,$limit){
		//XML Generator need 'itemid' keyword for building a table
		$sql = 'select rel_id as itemid,agents.description as hostname,mapnetwork_net.name as name,agent2.description as ping 
				  from netrelation,agents,mapnetwork_net,agents as agent2 
				  where netrelation.agent_id=agents.agent_id AND netrelation.ping_id=agent2.agent_id AND mapnetwork_net.id=netrelation.net_id';
		return $this->dba->query($sql);
	}


	//not yet tested
	//should be listen to Agent
	public function notifyRemove($obj,$id){
		if($obj instanceOf Group){
			$this->removeWith($this->getField('group'),$id);
		}
	}
	//this is only wrapper to provide single object
	//just copy paste for all children
	public function selectByID($id){
		$tempobj = parent::selectByID($id);
		if($tempobj){
			foreach($this->rawfields as $key => $value){
				$this->$key = $tempobj->$value;
			}
		}
	}
	public function getNet(){
		return $this->net;
	}
	public function getPing(){
		return $this->ping;
	}
	public function getAgent(){
		return $this->agent;
	}

	//retur associative array of agents that not yet has agent-net relationship for net=$netid
	public function selectRemaining($net_id){
		$agent = new Agent();
		$field_agent_id = $agent->getAbsField('id');
		$fieldwrapper = $agent->getField('id');
		//feel free to replace hostname with description just like i do ;)
		$field_agent_hostname = $agent->getField('description');

		$netmapagent = new NetMapAgent();
		$field_netmapagent_id = $netmapagent->getAbsField('agent');

		$sql = 'SELECT '.$field_agent_id.' AS '.$fieldwrapper.','.$field_agent_hostname.' FROM '.$agent->getPrimaryTable().','.$netmapagent->getPrimaryTable().
				  ' WHERE '.$field_netmapagent_id.'='.$field_agent_id.' AND '.
				  $field_agent_id.' NOT IN (SELECT '.$this->getField('agent').' FROM '.$this->getPrimaryTable().' WHERE '.$this->getField('net').'='.$net_id.')';
		$temp = $this->dba->query($sql);
		$result = array();
		for($i=0;$i<sizeof($temp);$i++){
			$result[$temp[$i]->$fieldwrapper] = $temp[$i]->$field_agent_hostname;
		}
		return $result;
	}

	public function __destruct(){
		parent::__destruct();	
	}
}

?>
Return current item: Monyet