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