FilterResParam.php 14.2 KB
<?php
/**
 * @class FilterResParam
 * @version $Id: FilterResParam.php 3222 2016-04-27 14:44:55Z elena $
 * @author benjamin
 */
  class FilterResParam extends FilterRes
    {
        protected $localXmlName, $localXml;
        protected $rootElement, $xp;
        
        function __construct() 
        {
                $this->localXmlName = USERWSDIR."LocalParams.xml";
                $this->localXml = new DomDocument("1.0");
                $this->localXml->load($this->localXmlName); 
                $this->rootElement = $this->localXml->documentElement;
                
                $this->xp = new domxpath($this->localXml);
        }
  	
        function getTarget($value,$opId)
        {
                $result = array();	

                $missions = $this->xp->query("//mission[@xml:id]"); 	
                for ($i = 0; $i < $missions->length; $i++)
                {
                    $missionId  = $missions->item($i)->getAttribute('xml:id');

                    $missionTarget = ($missions->item($i)->hasAttribute('target') &&
                                        ($missions->item($i)->getAttribute('target') != ''));
                    if ($missionTarget)
                    {
                            $targetId = $missions->item($i)->getAttribute('target');
                            $missionTargetId = $targetId;
                    }
	            // if there is observatory layer
                    $observatoryTargets = false;
                    $obsTarget = false;

                    $observatories = $this->xp->query("//mission[@xml:id='".$missionId."']/observatory");
 
                    if ($observatories->length > 1) {

                        $n_targets = 0;
                        $obsTargets = array();
                        $obsIds = array();
                        
                        for ($i_obs = 0; $i_obs < $observatories->length; $i_obs++) {

                            $obs = $observatories->item($i_obs);
                            
                            if ( $obs->hasAttribute('target') && ($obs->getAttribute('target') !== '') ) {
                                $obsTargets[] = $obs->getAttribute('target'); 
                                $obsIds[] = $obs->getAttribute('xml:id');
                                $n_targets++;                            
                            }                         
                        } 
                        if ($n_targets > 0) {
                            $observatoryTargets = true;                            
                        }
                    }

                    $instruments = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument");

  		    for ($j = 0; $j < $instruments->length; $j++)
  		    { 
  		    	$instrumentId =  $instruments->item($j)->getAttribute('xml:id');

                        $obsTarget = false;

  		  	if ($observatoryTargets) {                          
                           for ($i_obs = 0; $i_obs < $n_targets; $i_obs++) { 
                                if ($instruments->item($j)->parentNode->getAttribute('xml:id') == $obsIds[$i_obs]) {
                                    $obsTarget = true;
                                    $obsTargetId = $obsTargets[$i_obs];
                                    break;
                                }       
                           }
                        }

                        $datasets = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument[@xml:id='".$instrumentId."']//dataset");
                                                     
  		    	for ($k = 0; $k < $datasets->length; $k++)
  		    	{
  		    		 
                                $datasetId = $datasets->item($k)->getAttribute('xml:id');   
                        
  		    		$datasetTarget = ($datasets->item($k)->hasAttribute('target') &&
  		    		                  ($datasets->item($k)->getAttribute('target') != ''));
  		    		
  		    		if (!$missionTarget && !$obsTarget && !$datasetTarget)
  		    			$targetId = 'none';	    			
  		    		else if ($datasetTarget)
  		    			$targetId = $datasets->item($k)->getAttribute('target'); 
                                else if ($obsTarget)
                                        $targetId = $obsTargetId; 
                                else if ($missionTarget && !$obsTarget)
                                        $targetId = $missionTargetId; 
                               
  		     			
  		    		//fprintf($log,"%s\n",$targetId);
  		    			
  		    		$ok = ($value == $targetId);
  		    		if (!$ok)
  		    		  if (strlen($targetId) > strlen($value))
  		    		    $ok = ((substr($targetId,0,strlen($value)) == $value) && ($targetId[strlen($value)]=='.'));   
                               
                                $id = $datsetId;
                                $chain = $datasetId;
                                $node = $datasets->item($k);

                                while ($id !== $missionId) {
                                    $node = $node->parentNode;
                                    $id = $node->getAttribute('xml:id');
                                    $chain = $id.";".$chain;
                                }  	  	    		
  		    		switch ($opId)
  		    		{
  		    			case 'eq' :
  		    				if ($ok)  
                                                    array_push($result,$chain);                                                         
  		    				break;
  		    			case 'ne' :
  		    				if (!$ok)                                     
                                                    array_push($result,$chain);
  		    				break;
  		    		}
  		    	}
  		    }
  		}

  		return $result;
  	}
  	
        function getTime($kind,$value,$opId)
        {
                $result = array();	
                
                $missions = $this->xp->query("//mission[@xml:id]"); 	
                for ($i = 0; $i < $missions->length; $i++)  
                {
                    $missionId   = $missions->item($i)->getAttribute('xml:id');
 
  			
                    $instruments = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument");
	  
  		    for ($j = 0; $j < $instruments->length; $j++)
  		    {  	
                        $instrumentId =  $instruments->item($j)->getAttribute('xml:id');
  		  		
                        $datasets = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument[@xml:id='".$instrumentId."']//dataset");
  		    	
                        for ($k = 0; $k < $datasets->length; $k++)
  		    	{  		    		  
                                $datasetId = $datasets->item($k)->getAttribute('xml:id'); 
    		    		
  		    		switch($kind)
  		    		{
  		    			case 'sta' :
  		    				$dataTime = $datasets->item($k)->getAttribute('dataStart');
  		    				break;
  		    			case 'sto' :
  		    				$dataTime = $datasets->item($k)->getAttribute('dataStop');
  		    				break;
  		    		}
  		    		
  		    		list($day, $month, $year) = explode('-', $dataTime);
  		    		$date = mktime(0, 0, 0, $month, $day, $year);
  		    		list($year, $month, $day, $hour, $min, $sec) = split('[-T:]', $value);
  		    		$date_value = mktime($hour, $min, $sec, $month, $day, $year);
  		    		
                                $id = $datsetId;
                                $chain = $datasetId;
                                $node = $datasets->item($k);

                                while ($id !== $missionId) {
                                    $node = $node->parentNode;
                                    $id = $node->getAttribute('xml:id');
                                    $chain = $id.";".$chain;
                                }  

  		    		switch($opId)
  		    		{
  		    			case 'le' :
  		    				if ($date <= $date_value)                                                         
                                                        array_push($result,$chain);                                               
  		    				break;
  		    			case 'ge' :
  		    				if ($date >= $date_value)                                                           
                                                        array_push($result,$chain);                                                       		    					                                                  
  		    				break;
  		    		}
  		    	}
  		    }
  		}
  		
  		return $result;
  	}
  	
  	function getMissionName($value,$opId)
  	{
  		$result = array();	
  		
  		$missions = $this->xp->query("//mission[@name]"); 	
  		for ($i = 0; $i < $missions->length; $i++)  
  		{		
  		  $missionName = $missions->item($i)->getAttribute('name');
  		  $missionId   = $missions->item($i)->getAttribute('xml:id');

  		  if ((!fnmatch($value,$missionName,FNM_CASEFOLD) && ($opId == 'eq')) ||
  		      (fnmatch($value,$missionName,FNM_CASEFOLD) && ($opId == 'ne')))
  		    continue;

  		  $instruments = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument");	 
 
  		  for ($j = 0; $j < $instruments->length; $j++)
  		  {  	
  		    $instrumentId =  $instruments->item($j)->getAttribute('xml:id');   		
  		  	
                    $datasets = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument[@xml:id='".$instrumentId."']//dataset");
                                                    
                    for ($k = 0; $k < $datasets->length; $k++)
                    {                         
                        $datasetId = $datasets->item($k)->getAttribute('xml:id');

                        $id = $datsetId;
                        $chain = $datasetId;
                        $node = $datasets->item($k);

                        while ($id !== $missionId) {
                            $node = $node->parentNode;
                            $id = $node->getAttribute('xml:id');
                            $chain = $id.";".$chain;
                        }  
 
                     array_push($result,$chain);
                          
                    }
                    }
                }
 		return $result;
  	}
  	
  	function getDataType($value,$opId)
  	{
  		
  	}
  	
  	function getProvider($value,$opId)
  	{
  		$result = array();	
  		
  		$missions = $this->xp->query("//mission[@xml:id]"); 	
  		for ($i = 0; $i < $missions->length; $i++)  
  		{
                    $missionId   = $missions->item($i)->getAttribute('xml:id');

                    $instruments = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument");
	  
  		    for ($j = 0; $j < $instruments->length; $j++)
  		    {  	
                        $instrumentId =  $instruments->item($j)->getAttribute('xml:id');
  		  		
                        $datasets = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument[@xml:id='".$instrumentId."']//dataset");
                                                       
  		    	for ($k = 0; $k < $datasets->length; $k++)
  		    	{
                                $provider  = $datasets->item($k)->getAttribute('dataSource');
                                
                               
                                $datasetId = $datasets->item($k)->getAttribute('xml:id'); 
                                
                                if ((!preg_match("/".$value."/i",$provider) && ($opId == 'eq')) ||
                                        (preg_match("/".$value."/i",$provider) && ($opId == 'ne')))
                                        continue;

                                $id = $datsetId;
                                $chain = $datasetId;
                                $node = $datasets->item($k);

                                while ($id !== $missionId) {
                                    $node = $node->parentNode;
                                    $id = $node->getAttribute('xml:id');
                                    $chain = $id.";".$chain;
                                }    
                 
                                
                                array_push($result,$chain);
                                  
  		    	}
  		    }		
  		}
  		
  		return $result;
  	}
  	
    function getAll()
  	{
  		$result = array();	
  		 
  		$missions = $this->xp->query("//mission[@xml:id]"); 	
  		for ($i = 0; $i < $missions->length; $i++)
  		{
                    $missionId   = $missions->item($i)->getAttribute('xml:id');
                                                                                          
                    $instruments = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument");	
  
  		    for ($j = 0; $j < $instruments->length; $j++)
  		    {  	
                        $instrumentId =  $instruments->item($j)->getAttribute('xml:id');
                        
                        $datasets = $this->xp->query("//mission[@xml:id='".$missionId."']//instrument[@xml:id='".$instrumentId."']//dataset");
                                                       

  		    	for ($k = 0; $k < $datasets->length; $k++)
  		    	{
                                  
                                $datasetId = $datasets->item($k)->getAttribute('xml:id'); 
 
                                $id = $datsetId;
                                $chain = $datasetId;
                                $node = $datasets->item($k);

                                while ($id !== $missionId) {
                                    $node = $node->parentNode;
                                    $id = $node->getAttribute('xml:id');
                                    $chain = $id.";".$chain;
                                }  
                                
                                array_push($result,$chain);
                                   
  		    	}
  		    }
  		}
  		
  		return $result;
  	}
  	
  	function getCondResult($cond)
  	{
  		$condId = $cond['condId'];
  		switch($condId)
  		{
  			case 'tar' : //target
  				return $this->getTarget($cond['value'],$cond['opId']);
  			case 'sta' : //start time
  				return $this->getTime('sta',$cond['value'],$cond['opId']);
  			case 'sto' : //stop time
  				return $this->getTime('sto',$cond['value'],$cond['opId']);
  			case 'mis' : //mission name
  				return $this->getMissionName(trim($cond['value']),$cond['opId']);
  			case 'typ' : //dataset type
  				return $this->getAll();
  			case 'pro' : //provider
  				return $this->getProvider(trim($cond['value']),$cond['opId']);
  			default :
  				return array();
  		}
  	}
  }

?>