class.Group.php 3.34 KB
Newer Older
1
2
3
4
<?php

class Group {

5
6
7
  private static $maildir = '/tmp/maildir';


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  private static $queryList = <<<EOS
    select ID, Gruppnamn from Grupper order by Gruppnamn
EOS;

  private static $queryMembership = <<<EOS
    select Grupper.ID, Gruppnamn from Grupper,GruppAdress where 
    GruppID = Grupper.ID and Adressid = ? order by Gruppnamn
EOS;

  private static $querySetGroup = <<<EOS
    replace into GruppAdress set AdressID = ?, GruppID = ?
EOS;

  private static $queryUnsetGroup = <<<EOS
    delete from GruppAdress where AdressID = ? and GruppID = ?
EOS;

25
26
27
28
29
  private static $queryMail = <<<EOS
    select Epost from Adresser, GruppAdress where
    Adresser.ID = AdressID and GruppID = ?
EOS;

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
  private static $instance;

  private $list;

  private function __construct() {
    global $gCms;
    if (!isset($gCms)) exit;
    $ADB = AddressDB::getDB();
    $result = $ADB->Execute(self::$queryList,array());
    while ($row = $result->FetchRow()) {
      $this->list[$row['Gruppnamn']] = $row['ID'];
    }
  }
  
  public static function getMembers($GruppID) {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
    $result = $db->Execute(self::$queryMembership,array($GruppID));
    if ($result === false) { die("Database error!<br>" . $db->ErrorMsg()); }
    $list = array();
    while ($row = $result->FetchRow()) {
      $list[$row['Gruppnamn']] = $row['ID'];
    }
    return $list;
  }

  public static function getNonMembers($GruppID) {
    $members = self::getMembers($GruppID);
    $nonmembers = array_diff_assoc(self::getGroup(),$members);
    return $nonmembers;
  }
    
  public static function setMember($ID,$GroupID) {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
    $values = array($ID, $GroupID);
    $result = $db->Execute(self::$querySetGroup,$values);
    if ($result === false) {die("Database error!<br>" . $db->ErrorMsg()); }
  }  

  public static function unsetMember($ID,$GroupID) {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
    $values = array($ID, $GroupID);
    //   echo '<pre>'; print_r($queryUnsetGroup); print_r($values); echo '</pre>';exit;
    
    $result = $db->Execute(self::$queryUnsetGroup,$values);
    if ($result === false) {die("Database error!<br>" . $db->ErrorMsg()); }
  }  

  public static function getGroup() {
    if (!isset(self::$instance)) {
      $c = __CLASS__;
      self::$instance = new $c;
    }
    return self::$instance->list;
  }

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
  public static function setMailLists() {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
    $groups = self::getGroup();
    if(!is_dir(self::$maildir)) mkdir(self::$maildir,'0755',true);
    foreach ($groups as $name => $ID) {
      $pos = strrpos($name, '.dis');
      if ($pos === false) continue;
      $epost = array();
      $values = array($ID);
      $result = $db->Execute(self::$queryMail,$values);
      if ($result === false) { die("Database error!<br>" . $db->ErrorMsg()); }
      while ($row = $result->FetchRow()) {
	if (!$row['Epost']) continue;
	$epost[] = $row['Epost'];
      }
      if (count($epost) > 0) {
	//	echo '<pre>'; print_r($epost); echo '</pre>';
	$filename  = self::$maildir . '/' . $name;
	$file = fopen($filename,'w');
	if ($file === false) die('Cannot write '.$filename);
	foreach ($epost as $line) fwrite($file,$line.PHP_EOL);
	fclose($file);
      }
    }
    //  exit;
  }
115
116
}
?>