FilterResParam.php 12.4 KB
<?php
/**
 * @class FilterResParam
 * @version $Id: FilterResParam.php 2482 2014-07-28 10:13:23Z 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;
                    }
	
                     

                    $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');   
                        
  		    		$datasetTarget = ($datasets->item($k)->hasAttribute('target') &&
  		    		                  ($datasets->item($k)->getAttribute('target') != ''));
  		    		
  		    		if (!$missionTarget && !$datasetTarget)
  		    			$targetId = 'none';	    			
  		    		else if ($datasetTarget)
  		    			$targetId = $datasets->item($k)->getAttribute('target');
                                else if ($missionTarget)
                                        $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();
  		}
  	}
  }

?>