summaryrefslogblamecommitdiffstats
path: root/library/Pbs/Graph.php
blob: f9b236a22e853c2c7e0afc72dc80de0fcf4632e0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
     









                                                                           





                             
                       
                       
                              
                                  



                                                               
                                

        


                                            
                                              
                                           
                                        
                                                             
                                                                               




                                                                                                                                                    






                                                                                                                                           


                                                 






                                                          

                                                                                         



























                                                                                                                             
                               






                                                                   


                                      

                                                              


                                                                






                                                                                             



                                                                                                                                                        






                                                                           
                                                                

                                                           






                                                                                             
                                                                          


                                                                                                                                                  


                                                                                                                                                                           



                                                                           


                                             
 
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class Pbs_Graph{

	private $db = null;
	private $membership;
	private $graphstring;
	private $level;
	private $edges;
	private $crawledNodes;
	private $childs = array();
	
	public function graph($groupID)
    {
		$this->db = Zend_Db_Table::getDefaultAdapter();
        $db = $this->db;        
        
        
        $path = "../resources/groupgraphs/";
    	@mkdir($path ,0777, true);
        
        $this->graphstring = 'digraph groups {
				size="7,5";
				pad=0.1;
				graph[ bgcolor=transparent ];
				node [  style=filled fillcolor="#ffffffff" ];';
		if($this->level <0)
			$this->graphstring .= '"'.$this->getGroupTitle($groupID).'"';
		else
			$this->graphstring .= '"'.$this->getGroupTitle($groupID).'" [ fontcolor="#ffffffff", style=filled, fillcolor="#004A99FF"];';
				
		if(count($this->childs) >=1){
			foreach($this->childs as $childID){
				$this->edges[$groupID][$childID] = 1;
				$this->graphstring .= '"'.$this->getGroupTitle($groupID).'" -> "'.$this->getGroupTitle($childID).'";'."\n";
				$this->getChildGroups($childID);
			}
		}				
		$this->getParentGroups($groupID);
		$this->getChildGroups($groupID);
		$this->graphstring .= '}';
		
		// for debugging
		# $fp = fopen($path.'thisgraph.dot', "w");
        # fputs ($fp, $this->graphstring);
        # fclose ($fp);
		
		
		$this->graphstring = str_replace(array("\t","\n"),"",$this->graphstring);
		$this->graphstring = str_replace('"','\"',$this->graphstring);
		
		$md5 = md5($this->graphstring);
		$path = "../resources/groupgraphs/";
    	@mkdir($path ,0777, true);
    	
    	// randomly delete cached files, old files are not needed
    	// so delete it sometimes
		if(rand(0,20) == 1){
			$days = "14"; // delete all files older than this many days
			$seconds = ($days*24*60*60);

			$files = scandir($path);			 
			foreach ($files as $num => $fname){
				if (file_exists("{$path}{$fname}") && ((time() - filemtime("{$path}{$fname}")) > $seconds)) {
					$mod_time = filemtime("{$path}{$fname}");
					unlink("{$path}{$fname}");
				}
			}
		}
		
		if(!file_exists($path.$md5.".png")){	      
			$str =  'echo "';
			$str .=  $this->graphstring;
			$str .=  '" | dot -Tpng  >'.$path.$md5.".png";
			exec($str);
		}
		
		passthru("cat ".$path.$md5.".png", $result);
		return $result;
	}
	private function getGroupTitle($groupID){
		$group = new Application_Model_Group();
		$groupmapper = new Application_Model_GroupMapper();
		$groupmapper->find($groupID,$group);
		return $group->getTitle();
	}
	public function newChild($id){
		$this->childs[] = $id;
	}
				
	private function getParentGroups($groupID, $level=1) {
		if($this->crawledNodes['parent'][$groupID] == 1)
			return;
		$this->crawledNodes['parent'][$groupID] = 1;
		$db = Zend_Db_Table::getDefaultAdapter();
		$query = 'SELECT parentID FROM pbs_groupgroups WHERE groupID="'.$groupID.'"';
		$stmt = $db->query($query);
		$result = $stmt->fetchAll();
		foreach($result as $row){			
			// save the current groupID in level-list
			#$data[$level][] = $row['parentID'];
			if($this->edges[$row['parentID']][$groupID] != 1){
				$this->graphstring .= '"'.$this->getGroupTitle($row['parentID']).'" -> "'.$this->getGroupTitle($groupID).'";'."\n";	
				$this->edges[$row['parentID']][$groupID] = 1;
			}	
			// get the function recursive an increase the level
			$this->getParentGroups($row['parentID'], $level+1);
		}
	}
	
	// Gets all childs-groups from a given group
	private function getChildGroups($groupID, $level=1) {
		if(@$this->crawledNodes['child'][$groupID] == 1)
			return;
		$this->crawledNodes['child'][$groupID] = 1;
		$db = Zend_Db_Table::getDefaultAdapter();
		$query = 'SELECT groupID FROM pbs_groupgroups WHERE parentID="'.$groupID.'"';
		$stmt = $db->query($query);
		$result = $stmt->fetchAll();
		foreach($result as $row){	
			// save the current groupID in level-list
			#$data[$level][] = $row['groupID'];
			if(@$this->edges[$groupID][$row['groupID']] != 1){
				$this->graphstring .= '"'.$this->getGroupTitle($groupID).'" -> "'.$this->getGroupTitle($row['groupID']).'";'."\n";
				$this->edges[$groupID][$row['groupID']] = 1;
			}
			if($this->level >= $level){
				$this->graphstring .= '"'.$this->getGroupTitle($row['groupID']).'"'.' [ fontcolor="#ffffffff", style=filled, fillcolor="#004A99FF"];'."\n";
			}
			// get the function recursive an increase the level
			$this->getChildGroups($row['groupID'], $level+1);
		}
	}
	public function setHiglightLevel($i){
		$this->level = $i;
	}
}