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