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

class Group {

Leif Andersson's avatar
A    
Leif Andersson committed
5
  private static $maildir = '/home/leif/slask/maildir';
6
7


8
  private static $queryList = <<<EOS
Leif Andersson's avatar
A    
Leif Andersson committed
9
    select ID, Gruppnamn, GruppInfo from Grupper order by Gruppnamn
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;

Leif Andersson's avatar
A    
Leif Andersson committed
30
31
32
  private static $groupFields = array('ID', 'Gruppnamn', 'GruppInfo');


33
34
  private static $instance;

Leif Andersson's avatar
A    
Leif Andersson committed
35
36
37
38
39
40
41
42
43
44
45
  private static $list = array();

  private function __construct($param = NULL) {
    foreach (self::$groupFields as $field) {$this->$field = '';}
    if (is_null($param)) return;
    if (is_array($param)) {
      foreach (self::$groupFields as $field) {
	$this->$field = $param[$field];
      }
    }
  }
46

Leif Andersson's avatar
A    
Leif Andersson committed
47
48
49
50
51
52
53
  public static function getGroupList() {
    if (count(self::$list) > 0) return $list;
    global $gCms; if (!isset($gCms)) exit;
    $c = __CLASS__;
    $db = AddressDB::getDB();
    $result = $db->Execute(self::$queryList,array());
    if ($result === false) { die("Database error!<br>" . $db->ErrorMsg());}
54
    while ($row = $result->FetchRow()) {
Leif Andersson's avatar
A    
Leif Andersson committed
55
      self::$list[] = new $c($row);
56
    }
Leif Andersson's avatar
A    
Leif Andersson committed
57
    return self::$list;
58
  }
Leif Andersson's avatar
A    
Leif Andersson committed
59
60
61
62
63
64
65
66
67
	
  /*   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']; */
  /*   } */
  /* } */
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  
  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);
Leif Andersson's avatar
A    
Leif Andersson committed
83
84
85
86
87
88
    $allgroups = array();
    self::getGroupList();
    foreach (self::$list as $group) {
      $allgroups[$group->Gruppnamn] = $group->ID;
    }
    $nonmembers = array_diff_assoc($allgroups,$members);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    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()); }
  }  

Leif Andersson's avatar
A    
Leif Andersson committed
110
111
112
113
114
115
116
  /* public static function getGroup() { */
  /*   if (!isset(self::$instance)) { */
  /*     $c = __CLASS__; */
  /*     self::$instance = new $c; */
  /*   } */
  /*   return self::$instance->list; */
  /* } */
117

118
119
120
  public static function setMailLists() {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
Leif Andersson's avatar
A    
Leif Andersson committed
121
    $groups = self::getGroupList();
122
    if(!is_dir(self::$maildir)) mkdir(self::$maildir,'0755',true);
Leif Andersson's avatar
A    
Leif Andersson committed
123
124
    foreach ($groups as $group) {
      $pos = strrpos($group->Gruppnamn, '.dis');
125
126
      if ($pos === false) continue;
      $epost = array();
Leif Andersson's avatar
A    
Leif Andersson committed
127
      $values = array($group->ID);
128
129
130
131
132
133
134
135
      $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>';
Leif Andersson's avatar
A    
Leif Andersson committed
136
	$filename  = self::$maildir . '/' . $group->Gruppnamn;
137
138
139
140
141
142
143
144
	$file = fopen($filename,'w');
	if ($file === false) die('Cannot write '.$filename);
	foreach ($epost as $line) fwrite($file,$line.PHP_EOL);
	fclose($file);
      }
    }
    //  exit;
  }
145
146
}
?>