class.Group.php 2.25 KB
Newer Older
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
<?php

class Group {

  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;

  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;
  }


}
?>