
<?php

class CHostDetails extends CZBXAPI{

	public static function update( $params ){
		$name = $params['name'];
		$os = $params['os'];
		$deviceType = $params['deviceType'];
		$serialNumber = $params['serialNumber'];
		$tag = $params['tag'];
		$macaddress = $params['macaddress'];
            $dns = $params['dns'];
		
		//getting hostid from dns name
		$db_items = DBselect('SELECT hostid FROM hosts WHERE dns LIKE '.zbx_dbstr($dns) );
		$db_item = DBfetch( $db_items );
		if( !$db_item ) {
			self::setError(__METHOD__, ZBX_API_ERROR_INTERNAL, "database does not contain host with specified dns");
			return false;
		}
		$hostid = $db_item['hostid'];
		$db_items = DBselect('SELECT hostid FROM hosts_profiles');
		$db_item = DBfetch( $db_items );
		
		if( $db_item ){ //update host profile
			$sql = 'UPDATE hosts_profiles SET '.
					' devicetype='.zbx_dbstr($devicetype).', '.
					' name='.zbx_dbstr($name).', '.
					' os='.zbx_dbstr($os).', '.
					' serialno='.zbx_dbstr($serialNumber).', '.
					' tag='.zbx_dbstr($tag).', '.
					' macaddress='.zbx_dbstr($macaddress).
				' WHERE hostid='.zbx_dbstr($hostid);
			$res = DBexecute($sql);
			if($res){
				return "host profile updated";
			}
			self::setError(__METHOD__, ZBX_API_ERROR_INTERNAL, "error during updating profile");
			return false;
		}else{ // add profile for host
			$insert[] = array(
				'hostid' => $hostid,
				'devicetype' => $deviceType,
				'name' => $name,
				'os' => $os,
				'serialno' => $serialNumber,
				'tag' => $tag,
				'macaddress' => $macaddress
			);			
			$res = DB::insert('hosts_profiles', $insert);
			if($res){
				return 'host profile added';
			}
			self::setError(__METHOD__, ZBX_API_ERROR_INTERNAL, "error when adding profile");
			return false;
		}
	}
}

?>
