summaryrefslogtreecommitdiffstats
path: root/library/Pbs/Graph.php
blob: 7823a4b5163a3a936bb78ae16945ed4535a3a83c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?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;
  }
}