fix mysql port
[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 class_exists('DB_DataObject') ? '' : 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
44         if (!empty($q['query']['name_starts'])) {
45             $v = $this->escape($q['query']['name_starts']);
46             $this->whereAdd("{$this->tableName()}.name like '{$v}%'");
47         }
48         
49         if(!empty($q['_count_member_by_name'])){
50             
51             $core_group = DB_DataObject::factory('core_group');
52             
53             if($core_group->get('name', $q['_count_member_by_name'])){
54                 $roo->jok($core_group->memberCount());
55             }
56         }
57         
58         if(!empty($q['_is_in_group'])){
59             $this->selectAdd("
60                 COALESCE((
61                     SELECT 
62                             COUNT(id) 
63                     FROM 
64                             core_group_member
65                         WHERE 
66                             user_id = {$q['_is_in_group']}
67                         AND
68                             group_id = {$this->tableName()}.id
69                     ), 0) AS is_in_group
70             ");
71         }
72         
73         
74         $cp = DB_DataObject::Factory('core_person')->tableName();
75         $cgm = DB_DataObject::Factory('core_group_member')->tableName();
76         
77         $this->selectAdd("
78            (
79             SELECT COUNT(${cgm}.user_id) 
80                 FROM 
81                     {$cgm}
82                 LEFT JOIN
83                     {$cp}
84                 ON
85                     ${cp}.id = {$cgm}.user_id
86                 WHERE
87                     ${cgm}.group_id = {$this->tableName()}.id
88                 AND
89                     ${cp}.active = 1
90                 ) AS group_member_count            
91         ");
92         
93     }
94
95     function toEventString() {
96         return $this->name;
97     }
98
99     function beforeInsert($q,$roo)
100     {
101         if (isset($q['_action'])) {
102             // add // sub...
103             $g = clone($this);
104             if (!$g->get($q['group_id'])) {
105                 $roo->jerr("missing group id");
106
107             }
108             
109             $user_ids = explode(',', $q['user_ids']);
110             
111             foreach($user_ids as $uid) {
112                 
113                 switch($q['_action']) {
114                     case 'add':
115                         $g->addMember($uid,$roo);
116                         break;
117                     case 'sub':
118                         $g->removeMember($uid);
119                         break;
120                     default:
121                         $roo->jerr('invalid action');
122                 }
123             }
124             $roo->jok('updated');
125
126         }
127
128     }
129
130
131     function beforeDelete()
132     {
133         $x = DB_DataObject::factory('core_group_right');
134         $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
135         $x = DB_DataObject::factory('core_group_member');
136         $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
137     }
138     /**
139      * check who is trying to access this. false == access denied..
140      */
141     function checkPerm($lvl, $au)
142     {
143         return $au->hasPerm("Core.Groups", $lvl);
144     }
145     function onUpdate($old, $req, $roo)
146     {
147         $this->ensureLeaderMembership($roo);
148     }
149     function onInsert($req, $roo)
150     {
151         $this->ensureLeaderMembership($roo);
152     }
153     function ensureLeaderMembership($roo)
154     {
155
156         // groups - make sure the leader is a member...
157         if (!$this->type || !$this->leader)
158         {
159             return true;
160         }
161
162         $pi = DB_DataObject::factory('core_person');
163         $pi->get($this->leader);
164
165         $p = DB_DataObject::factory('core_group_member');
166         $p->group_id = $this->id;
167         $p->user_id = $this->leader;
168         //$p->type = 1; //???????
169         if (!$p->count()) {
170
171             $p->insert();
172             $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
173         }
174
175     }
176
177
178     function memberCount()
179     {
180         $gm = DB_Dataobject::factory('core_group_member');
181         $gm->group_id = $this->id;
182         $gm->autoJoin();
183         $gm->whereAdd('join_user_id_id.active = 1');
184         //PDO_DAtaObject::DebugLevel(1); 
185         return $gm->count();
186     }
187
188     function memberIds()
189     {
190         $gm = DB_Dataobject::factory('core_group_member');
191         $gm->group_id = $this->id;
192         $gm->autoJoin();
193         $gm->whereAdd('join_user_id_id.active = 1');
194         return $gm->fetchAll('user_id');
195
196     }
197     function isMember($person)
198     {
199         $gm = DB_Dataobject::factory('core_group_member');
200         $gm->group_id = $this->id;
201         $gm->user_id = is_object($person) ? $person->id : $person;
202         return $gm->count();
203     }
204
205     function addMember($person,$roo = false)
206     {
207         if ($this->name == "Empty Group") {
208             $roo->jerr('Cannot add the person into Empty Group');
209         }
210         $gm = DB_Dataobject::factory('core_group_member');
211         $gm->group_id = $this->id;
212         $gm->user_id = is_object($person) ? $person->id : $person;
213         
214         if (!$gm->count()) {
215             $gm->insert();
216         }
217     }
218
219     function removeMember($person)
220     {
221         $gm = DB_Dataobject::factory('core_group_member');
222         $gm->group_id = $this->id;
223         $gm->user_id = is_object($person) ? $person->id : $person;
224
225         if ($gm->find(true)) {
226             $gm->delete();
227         }
228     }
229
230     /**
231      *
232      *  grab a list of members - default is the array of person objects..
233      *  @param $what  = set to 'email' to get a list of email addresses.
234      *
235      *
236      */
237
238     function members($what = false)
239     {
240         $ids = $this->memberIds();
241         if (!$ids) {
242             return array();
243         }
244         //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
245         // groups databse is hard coded to person.. so this should not be used for other tables.????
246         $p = DB_Dataobject::factory( 'core_person' );
247
248         $p->whereAdd('id IN ('. implode(',', $ids) .')');
249         $p->active = 1;
250
251         $p->orderBy('name');
252         return $p->fetchAll($what);
253     }
254
255
256
257
258     function lookup($k,$v = false) {
259         if ($v === false) {
260             $v = $k;
261             $k = 'id';
262         }
263         $this->get($k,$v);
264
265         return $this;
266     }
267
268     function lookUpMembers($name, $what=false)
269     {
270         if (!$this->get('name', $name)) {
271             return array();
272         }
273         return $this->members($what);
274
275     }
276
277     function lookupMembersByGroupId($id, $what=false)
278     {
279         if (!$this->get($id)) {
280             return array();
281         }
282
283         return $this->members($what);
284     }
285
286     function postListFilter($ar, $au, $req)
287     {
288         if(empty($req['_add_everyone'])){
289             return $ar;
290         }
291
292         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
293         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
294         return array_merge($ret, $ar);
295
296     }
297
298     function initGroups()
299     {
300         
301         $g = DB_DataObject::factory($this->tableName());
302         $g->type = 0;
303         $g->name = 'Administrators';
304         if ($g->count()) {
305             $g->find(true);;
306         } else {
307             $g->insert();
308             $gr = DB_DataObject::factory('core_group_right');
309             $gr->genDefault();
310         }
311         $m = $g->members();
312         if (empty($m)) {
313             $p = DB_DAtaObject::factory('core_person');
314             $p->orderBy('id ASC');
315             $p->limit(1);
316             if ($p->find(true)) {
317                 $g->addMember($p);
318             }
319
320
321         }
322     }
323
324     function initDatabase($roo, $data)
325     {
326         $this->initGroups();
327
328         foreach($data as $gi) {
329             $g = DB_DataObject::factory($this->tableName());
330             
331             $o = false;
332             
333             if($g->get('name', $gi['name'])){
334                 $o = clone($g);
335             }
336             
337             $display_name = (isset($gi['display_name'])) ? $gi['display_name'] : '';
338             
339             unset($gi['display_name']);
340             
341             $g->setFrom($gi);
342             
343             if(empty($o) || empty($o->display_name)){
344                 $g->display_name = $display_name;
345             }
346             
347             (empty($o)) ? $g->insert() : $g->update($o);
348
349             if(count($g->members()) || empty($gi['members'])){
350                 continue;
351             }
352
353             foreach ($gi['members'] as $m){
354                 $g->addMember($m);
355             }
356
357         }
358
359     }
360
361 }