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