class.Group.php 6.17 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
EOS;

12
13
14
15
  private static $queryOne = <<<EOS
    select * from Grupper where ID = ?
EOS;

16
17
18
19
20
21
22
23
24
25
26
27
28
  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;

29
30
31
32
33
  private static $queryMail = <<<EOS
    select Epost from Adresser, GruppAdress where
    Adresser.ID = AdressID and GruppID = ?
EOS;

34
35
36
37
38
39
  private static $queryUpdate = <<<EOS
    update Grupper set where ID = ?
EOS;


  public static $groupFields = array('ID', 'Gruppnamn', 'GruppInfo');
Leif Andersson's avatar
B    
Leif Andersson committed
40
  private static $buttonFields = array('change','print');
Leif Andersson's avatar
A    
Leif Andersson committed
41

42
43
  private static $instance;

Leif Andersson's avatar
A    
Leif Andersson committed
44
45
  private static $list = array();

46
  public function __construct($param = NULL) {
Leif Andersson's avatar
A    
Leif Andersson committed
47
    foreach (self::$groupFields as $field) {$this->$field = '';}
Leif Andersson's avatar
B    
Leif Andersson committed
48
    foreach (self::$buttonFields as $field) {$this->$field = '';}
Leif Andersson's avatar
A    
Leif Andersson committed
49
50
51
52
53
    if (is_null($param)) return;
    if (is_array($param)) {
      foreach (self::$groupFields as $field) {
	$this->$field = $param[$field];
      }
54
      return;
Leif Andersson's avatar
A    
Leif Andersson committed
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
    $db = AddressDB::getDB();
    $result = $db->Execute(self::$queryOne,array($param));
    if ($result === false) { die("Database error!<br>" . $db->ErrorMsg());}
    $count = $result->RecordCount();
    switch ($count) {
    case 0:
      die(sprintf('Bad group ID %s'.PHP_EOL,$adressID));
      break;
    case 1:
      $row = $result->FetchRow();
      foreach (self::$groupFields as $field) {
	if (isset($row[$field])) $this->$field = $row[$field];
      }
      break;
    default:
      die(sprintf(
         'Strange error. Exactly one group record expected for key %s',
         $ID));
    }
  }

  private function createRecord() {
    $db = AddressDB::getDB();
    $query = 'insert into Grupper set Gruppnamn = ""';
    $result = $db->Execute($query,array());
    if ($result === false) {die("Database error!<br>" . $publDB->ErrorMsg()); }
    $this->ID = $db->Insert_ID();
Leif Andersson's avatar
A    
Leif Andersson committed
83
  }
84
85
86
87
88
 
  public function updateRecord() {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
    if ($this->ID == '') { $this->createRecord(); }
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    $query = 'update Grupper set ';
    $qfields = array(); $values = array();
    foreach (self::$groupFields as $field) {
      if ($field == 'ID') continue;
      $val = trim($this->$field);
      if (strlen($val) == 0) $val = null;
      $qfields[] = sprintf('%s = ?',$field);
      $values[] = $val;
    }
    $values[] = $this->ID;
    $query .= join(', ',$qfields) . ' where ID=?';
    $result = $db->Execute($query,$values);
    if ($result === false) {die("Database error!<br>" . $db->ErrorMsg());}
  }
 
Leif Andersson's avatar
A    
Leif Andersson committed
105
106
107
108
109
110
111
  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());}
112
    while ($row = $result->FetchRow()) {
Leif Andersson's avatar
A    
Leif Andersson committed
113
      self::$list[] = new $c($row);
114
    }
Leif Andersson's avatar
A    
Leif Andersson committed
115
    return self::$list;
116
  }
Leif Andersson's avatar
A    
Leif Andersson committed
117
118
119
120
121
122
123
124
125
	
  /*   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']; */
  /*   } */
  /* } */
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  
  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
141
142
143
144
145
146
    $allgroups = array();
    self::getGroupList();
    foreach (self::$list as $group) {
      $allgroups[$group->Gruppnamn] = $group->ID;
    }
    $nonmembers = array_diff_assoc($allgroups,$members);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    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
168
169
170
171
172
173
174
  /* public static function getGroup() { */
  /*   if (!isset(self::$instance)) { */
  /*     $c = __CLASS__; */
  /*     self::$instance = new $c; */
  /*   } */
  /*   return self::$instance->list; */
  /* } */
175

176
177
178
  public static function setMailLists() {
    global $gCms; if (!isset($gCms)) exit;
    $db = AddressDB::getDB();
Leif Andersson's avatar
A    
Leif Andersson committed
179
    $groups = self::getGroupList();
180
    if(!is_dir(self::$maildir)) mkdir(self::$maildir,'0755',true);
Leif Andersson's avatar
A    
Leif Andersson committed
181
182
    foreach ($groups as $group) {
      $pos = strrpos($group->Gruppnamn, '.dis');
183
184
      if ($pos === false) continue;
      $epost = array();
Leif Andersson's avatar
A    
Leif Andersson committed
185
      $values = array($group->ID);
186
187
188
189
190
191
192
193
      $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
194
	$filename  = self::$maildir . '/' . $group->Gruppnamn;
195
196
197
198
199
200
201
202
	$file = fopen($filename,'w');
	if ($file === false) die('Cannot write '.$filename);
	foreach ($epost as $line) fwrite($file,$line.PHP_EOL);
	fclose($file);
      }
    }
    //  exit;
  }
203
204
}
?>