DataObjects/Core_enum.php
[Pman.Core] / DataObjects / Groups.php
1 <?php
2 /**
3  * Table Definition for Groups
4  *
5  *group types
6  *
7  * 0 = permission group..
8  * 1 = team
9  * 2 = contact group
10  *
11  * 
12  */
13 require_once 'DB/DataObject.php';
14
15 class Pman_Core_DataObjects_Groups extends DB_DataObject 
16 {
17     ###START_AUTOCODE
18     /* the code below is auto generated do not remove the above tag */
19
20     public $__table = 'Groups';                          // table name
21     public $id;                              // int(11)  not_null primary_key auto_increment
22     public $name;                            // string(64)  not_null
23     public $type;                            // int(11)  
24     public $leader;                          // int(11)  not_null
25
26     
27     /* the code above is auto generated do not remove the tag below */
28     ###END_AUTOCODE
29     
30     // group types??
31     
32     function toEventString() {
33         return $this->name;
34     }
35     
36     function beforeDelete()
37     {
38         $x = DB_DataObject::factory('Groups');
39         $x->query("DELETE FROM group_rights WHERE group_id = {$this->id}");
40         $x->query("DELETE FROM group_members WHERE group_id = {$this->id}");
41     }
42     /**
43      * check who is trying to access this. false == access denied..
44      */
45     function checkPerm($lvl, $au) 
46     {
47         return $au->hasPerm("Core.Groups", $lvl);    
48     } 
49     function onUpdate($old, $req, $roo)
50     {
51         $this->ensureLeaderMembership($roo);
52     }
53     function onInsert($req, $roo)
54     {
55         $this->ensureLeaderMembership($roo);
56     }
57     function ensureLeaderMembership($roo)
58     {
59         
60         // groups - make sure the leader is a member...
61         if (!$this->type || !$this->leader)
62         {
63             return true;
64         }
65         
66         $pi = DB_DataObject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
67         $pi->get($this->leader);
68             
69         $p = DB_DataObject::factory('group_members');
70         $p->group_id = $this->id;
71         $p->user_id = $this->leader;
72         //$p->type = 1; //???????
73         if (!$p->count()) {
74             
75             $p->insert();
76             $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
77         }
78              
79     }
80     
81     function memberIds()
82     {
83         $gm = DB_Dataobject::factory('group_members');
84         $gm->group_id = $this->id;
85         return $gm->fetchAll('user_id');
86         
87     }
88     
89     
90     function addMember($person)
91     {
92         $gm = DB_Dataobject::factory('group_members');
93         $gm->group_id = $this->id;
94         $gm->user_id = $person->id;
95         if (!$gm->count()) {
96             $gm->insert();
97         }
98     }
99     /**
100      *
101      *  grab a list of members - default is the array of person objects..
102      *  @param $what  = set to 'email' to get a list of email addresses.
103      *
104      *
105      */
106     
107     function members($what = false)
108     {
109         $ids = $this->memberIds();
110         if (!$ids) {
111             return array();
112         }
113         //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
114         // groups databse is hard coded to person.. so this should not be used for other tables.????
115         $p = DB_Dataobject::factory( 'Person' );
116         
117         $p->whereAdd('id IN ('. implode(',', $ids) .')');
118         $p->active = 1;
119         return $p->fetchAll($what);
120     }
121     
122     function lookup($k,$v = false) {
123         if ($v === false) {
124             $v = $k;
125             $k = 'id';
126         }
127         $this->get($k,$v);
128
129         return $this;
130     } 
131     
132     function postListFilter($ar, $au, $req)
133     {      
134         
135         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
136         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
137         return array_merge($ret, $ar);
138             //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
139
140     }
141     
142     function initGroups()
143     {
144         
145         $g = DB_DataObject::factory('Groups');
146         $g->type = 0;
147         $g->name = 'Administrators';
148         if ($g->count()) {
149             return;
150         }
151         $g->insert();
152         $gr = DB_DataObject::factory('group_rights');
153         $gr->genDefault();
154     
155         
156     }
157     
158     function initDatabase($roo, $data)
159     {
160         $this->initGroups();
161         foreach($data as $gi) {
162             $g = DB_DataObject::factory('Groups');
163             $g->setFrom($gi);
164             if ($g->count()) {
165                 continue;
166             }
167             $g->insert();
168             
169             
170         }
171      
172     }
173     
174 }