DataObjects/Core_group.php
[Pman.Core] / DataObjects / Core_group.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  *  NOTE - used to be called Groups ....
13  * 
14  */
15 require_once 'DB/DataObject.php';
16
17 class Pman_Core_DataObjects_Core_group extends DB_DataObject 
18 {
19     ###START_AUTOCODE
20     /* the code below is auto generated do not remove the above tag */
21
22     public $__table = 'core_group';                          // table name
23     public $id;                              // int(11)  not_null primary_key auto_increment
24     public $name;                            // string(64)  not_null
25     public $type;                            // int(11)  
26     public $leader;                          // int(11)  not_null
27     public $is_system;                       // used by timesheets?
28     
29     /* the code above is auto generated do not remove the tag below */
30     ###END_AUTOCODE
31     
32       
33     function personTable()
34     {
35         $ff = HTML_FlexyFramework::get();
36         return empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
37     }
38     
39     
40     // group types??
41     function applyFilters($q, $au, $roo)
42     {
43         if (!empty($q['query']['name_starts'])) {
44             $v = $this->escape($q['query']['name_starts']);
45             $this->whereAdd("{$this->tableName()}.name like '{$v}%'");
46         }
47     }
48     
49     function toEventString() {
50         return $this->name;
51     }
52     
53     function beforeInsert($q,$roo)
54     {
55         if (isset($q['_action'])) {
56             // add // sub...
57             $g = clone($this);
58             if (!$g->get($q['group_id'])) {
59                 $roo->jerr("missing group id");
60
61             }
62             foreach(explode(',', $q['user_ids']) as $uid) {
63                 $this->addMember($uid);
64             }
65             
66             
67             
68             $this->jerr('invalid action');
69             
70         }
71         
72         
73     }
74     
75     
76     function beforeDelete()
77     {
78         $x = DB_DataObject::factory('core_group_right');
79         $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
80         $x = DB_DataObject::factory('core_group_member');
81         $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
82     }
83     /**
84      * check who is trying to access this. false == access denied..
85      */
86     function checkPerm($lvl, $au) 
87     {
88         return $au->hasPerm("Core.Groups", $lvl);    
89     } 
90     function onUpdate($old, $req, $roo)
91     {
92         $this->ensureLeaderMembership($roo);
93     }
94     function onInsert($req, $roo)
95     {
96         $this->ensureLeaderMembership($roo);
97     }
98     function ensureLeaderMembership($roo)
99     {
100         
101         // groups - make sure the leader is a member...
102         if (!$this->type || !$this->leader)
103         {
104             return true;
105         }
106         
107         $pi = DB_DataObject::factory('core_person');
108         $pi->get($this->leader);
109             
110         $p = DB_DataObject::factory('core_group_member');
111         $p->group_id = $this->id;
112         $p->user_id = $this->leader;
113         //$p->type = 1; //???????
114         if (!$p->count()) {
115             
116             $p->insert();
117             $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
118         }
119              
120     }
121     
122     
123     function memberCount()
124     {
125         $gm = DB_Dataobject::factory('core_group_member');
126         $gm->group_id = $this->id;
127         return $gm->count();
128     }
129     
130     function memberIds()
131     {
132         $gm = DB_Dataobject::factory('core_group_member');
133         $gm->group_id = $this->id;
134         return $gm->fetchAll('user_id');
135         
136     }
137     function isMember($uid)
138     {
139         $gm = DB_Dataobject::factory('core_group_member');
140         $gm->group_id = $this->id;
141         $gm->user_id = $uid;
142         return $gm->count();
143     }
144     
145     function addMember($person)
146     {
147         $gm = DB_Dataobject::factory('core_group_member');
148         $gm->group_id = $this->id;
149         $gm->user_id = is_object($person) ? $person->id : $person;
150         if (!$gm->count()) {
151             $gm->insert();
152         }
153     }
154     
155     function removeMember($person)
156     {
157         $gm = DB_Dataobject::factory('core_group_member');
158         $gm->group_id = $this->id;
159         $gm->user_id = is_object($person) ? $person->id : $person;
160         if ($gm->count()) {
161             $gm->delete();
162         }
163     }
164     
165     /**
166      *
167      *  grab a list of members - default is the array of person objects..
168      *  @param $what  = set to 'email' to get a list of email addresses.
169      *
170      *
171      */
172     
173     function members($what = false)
174     {
175         $ids = $this->memberIds();
176         if (!$ids) {
177             return array();
178         }
179         //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
180         // groups databse is hard coded to person.. so this should not be used for other tables.????
181         $p = DB_Dataobject::factory( 'core_person' );
182         
183         
184         
185         $p->whereAdd('id IN ('. implode(',', $ids) .')');
186         $p->active = 1;
187         return $p->fetchAll($what);
188     }
189     
190     
191     
192     
193     function lookup($k,$v = false) {
194         if ($v === false) {
195             $v = $k;
196             $k = 'id';
197         }
198         $this->get($k,$v);
199
200         return $this;
201     } 
202     
203     function lookUpMembers($name, $what=false)
204     {
205         if (!$this->get('name', $name)) {
206             return array();
207         }
208         return $this->members($what);
209         
210     }
211     
212     function lookupMembersByGroupId($id, $what=false)
213     {
214         if (!$this->get($id)) {
215             return array();
216         }
217         
218         return $this->members($what);
219     }
220     
221     function postListFilter($ar, $au, $req)
222     {      
223         if(!empty($req['_direct_return'])){
224             return $ar;
225         }
226         
227         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
228         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
229         return array_merge($ret, $ar);
230
231     }
232     
233     function initGroups()
234     {
235         
236         $g = DB_DataObject::factory($this->tableName());
237         $g->type = 0;
238         $g->name = 'Administrators';
239         if ($g->count()) {
240             return;
241         }
242         $g->insert();
243         $gr = DB_DataObject::factory('core_group_right');
244         $gr->genDefault();
245     }
246     
247     function initDatabase($roo, $data)
248     {
249         $this->initGroups();
250         
251         foreach($data as $gi) {
252             $g = DB_DataObject::factory($this->tableName());
253             $g->setFrom($gi);
254             
255             if(!$g->find(true)){
256                 $g->insert();
257             }
258             
259             if(count($g->members()) || empty($gi['members'])){
260                 continue;
261             }
262             
263             foreach ($gi['members'] as $m){
264                 $g->addMember($m);
265             }
266             
267         }
268      
269     }
270     
271 }