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