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