Location: PHPKode > projects > Monyet > controllers/manageagent.php
<?php
Class ManageAgent extends Management {

	private $agent;

	public function __construct(){
		parent::__construct();
   	$this->agent = new Agent();
   	$ping = new Ping();
		$agentmon = new ServiceMon();
		$this->agent->registerObserver($ping);
		$this->agent->registerObserver($agentmon);
	}
	public function show(){
		parent::show();
	}

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

	public function removeItem(){
		$this->agent->remove($_POST['id']);
	}

	public function populateShowContent(){
		$array_column = array("Hostname","IP Address","Description","Alarm",'Type','Priority','Status');	  
		$this->xml->addTable($this->agent->select($this->offset,$this->pglimit),
				  $array_column,'Table Agents Definition',$this->navigator->getAccess($this),$this->offset,'rightcontent',0);
	}

	public function populateAddForm(){
		$this->xml->addTextField('Hostname',null,'hostname','text');
		$this->xml->addTextField('IP Address',null,'ipaddress','text');
		$this->xml->addTextField('Description',null,'desc','text');
		$arrayalarm = array('TRUE'=>'TRUE','FALSE'=>'FALSE');
		$this->xml->addSelect('Alarm',$arrayalarm,'TRUE','alarm');
		$arrayagent = array('Server'=>'Server','Network'=>'Network');
		$this->xml->addSelect('Agent Type',$arrayagent,'Server','type');
		//priority should be empty on nonexisting 
		$this->xml->addSelect('Priority',array('no'=>'-'),'','priority');
		$this->xml->addSelect('Status',array('Active'=>'Active','Inactive'=>'Inactive'),'Active','status');
	}
	public function populateEditForm(){
		$this->agent->selectByID($_POST['id']);	  
		$this->xml->addTextField('Hostname',$this->agent->getHostname(),'hostname','text');
		$this->xml->addTextField('IP Address',$this->agent->getIPAddress(),'ipaddress','text');
		$this->xml->addTextField('Description',$this->agent->getDescription(),'desc','text');
		$arrayalarm = array('TRUE'=>'TRUE','FALSE'=>'FALSE');
		$this->xml->addSelect('Alarm',$arrayalarm,$this->agent->getAlarm(),'alarm');
		$arrayagent = array('Server'=>'Server','Network'=>'Network');
		$this->xml->addSelect('Agent Type',$arrayagent,$this->agent->getType(),'type');
		$arraypriority = array_merge(array('no'=>'no priority'),$this->agent->getAvailableMonitoring());
		$this->xml->addSelect('Priority',$arraypriority,$this->agent->getPriority(),'priority');
		$this->xml->addSelect('Status',array('Active'=>'Active','Inactive'=>'Inactive'),$this->agent->getStatus(),'status');
	}

	//return string to set the title
	public function getTitle(){
		return SITENAME.' -agent';
	}

	public function getFormPlace(){
		return 'panel';
	}

	public function editItem(){
		$array = array('hostname'=>$_POST['hostname'],'ip_address'=>$_POST['ipaddress'],
						 'description'=>$_POST['desc'],'is_alarm'=>$_POST['alarm'],'type'=>$_POST['type'],'priority'=>$_POST['priority'],'status'=>$_POST['status']);	  
		return $this->agent->update($array,$_POST['id']);	  
	}

	public function getKeyVar(){
		return 'hostname';
	}

	public function getTotal(){
		return $this->agent->getTotal();
	}

	public function addItem(){
		$array = array('',$_POST['hostname'],$_POST['ipaddress'],$_POST['desc'],$_POST['alarm'],$_POST['type'],$_POST['priority'],$_POST['status']);
		$this->agent->add($array);
	}

	//agent definition
	//hostname and ip address are similar. but i use hostname as the key because it gives portability
	//although it's very rare to attach a server ip address using DHCP
	
	public function checkAdd(){
	  	if(($_POST['ipaddress']=='')&&($_POST['hostname']=='')){
			die('At least one of IP Address and hostname should not be empty');
		}
		if($_POST['hostname']==''){
			$_POST['hostname'] = $_POST['ipaddress'];
			if($_POST['desc']==''){
				$_POST['desc'] = $_POST['ipaddress'];
			}
		}else{
			if($_POST['desc']==''){
				$_POST['desc'] = $_POST['hostname'];
			}
		}
	}
	public function checkEdit(){
		$this->checkAdd();
	}
}
?>
Return current item: Monyet