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