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