4db03648d5ac8aa6837c69821387bd6bf8f183b3
[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 lookUpMembers($name, $what=false)
133     {
134         if (!$this->get('name', $name)) {
135             return array();
136         }
137         return $this->members($what);
138         
139     }
140     
141     function postListFilter($ar, $au, $req)
142     {      
143         if(!empty($req['_not_add_others'])){
144             return $ar;
145         }
146         
147         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
148         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
149         return array_merge($ret, $ar);
150             //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
151
152     }
153     
154     function initGroups()
155     {
156         
157         $g = DB_DataObject::factory('Groups');
158         $g->type = 0;
159         $g->name = 'Administrators';
160         if ($g->count()) {
161             return;
162         }
163         $g->insert();
164         $gr = DB_DataObject::factory('group_rights');
165         $gr->genDefault();
166     }
167     
168     function initDatabase($roo, $data)
169     {
170         $this->initGroups();
171         
172         foreach($data as $gi) {
173             $g = DB_DataObject::factory('Groups');
174             $g->setFrom($gi);
175             
176             if(!$g->find(true)){
177                 $g->insert();
178             }
179             
180             if(count($g->members()) || empty($gi['members'])){
181                 continue;
182             }
183             
184             foreach ($gi['members'] as $m){
185                 $g->addMember($m);
186             }
187             
188         }
189      
190     }
191     
192 }