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             $v = $this->escape($q['_count_member_by_name']);
50         }
51         
52     }
53
54     function toEventString() {
55         return $this->name;
56     }
57
58     function beforeInsert($q,$roo)
59     {
60         if (isset($q['_action'])) {
61             // add // sub...
62             $g = clone($this);
63             if (!$g->get($q['group_id'])) {
64                 $roo->jerr("missing group id");
65
66             }
67              foreach(explode(',', $q['user_ids']) as $uid) {
68                 switch($q['_action']) {
69                     case 'add':
70                         $g->addMember($uid);
71                         break;
72                     case 'sub':
73                         $g->removeMember($uid);
74                         break;
75                     default:
76                         $roo->jerr('invalid action');
77                 }
78             }
79             $roo->jok('updated');
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($person)
148     {
149         $gm = DB_Dataobject::factory('core_group_member');
150         $gm->group_id = $this->id;
151         $gm->user_id = is_object($person) ? $person->id : $person;
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
171         if ($gm->find(true)) {
172             $gm->delete();
173         }
174     }
175
176     /**
177      *
178      *  grab a list of members - default is the array of person objects..
179      *  @param $what  = set to 'email' to get a list of email addresses.
180      *
181      *
182      */
183
184     function members($what = false)
185     {
186         $ids = $this->memberIds();
187         if (!$ids) {
188             return array();
189         }
190         //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
191         // groups databse is hard coded to person.. so this should not be used for other tables.????
192         $p = DB_Dataobject::factory( 'core_person' );
193
194
195
196         $p->whereAdd('id IN ('. implode(',', $ids) .')');
197         $p->active = 1;
198
199         $p->orderBy('name');
200         return $p->fetchAll($what);
201     }
202
203
204
205
206     function lookup($k,$v = false) {
207         if ($v === false) {
208             $v = $k;
209             $k = 'id';
210         }
211         $this->get($k,$v);
212
213         return $this;
214     }
215
216     function lookUpMembers($name, $what=false)
217     {
218         if (!$this->get('name', $name)) {
219             return array();
220         }
221         return $this->members($what);
222
223     }
224
225     function lookupMembersByGroupId($id, $what=false)
226     {
227         if (!$this->get($id)) {
228             return array();
229         }
230
231         return $this->members($what);
232     }
233
234     function postListFilter($ar, $au, $req)
235     {
236         if(empty($req['_add_everyone'])){
237             return $ar;
238         }
239
240         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
241         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
242         return array_merge($ret, $ar);
243
244     }
245
246     function initGroups()
247     {
248
249         $g = DB_DataObject::factory($this->tableName());
250         $g->type = 0;
251         $g->name = 'Administrators';
252         if ($g->count()) {
253             $g->find(true);;
254         } else {
255             $g->insert();
256             $gr = DB_DataObject::factory('core_group_right');
257             $gr->genDefault();
258         }
259         $m = $g->members();
260         if (empty($m)) {
261             $p = DB_DAtaObject::factory('core_person');
262             $p->orderBy('id ASC');
263             $p->limit(1);
264             if ($p->find(true)) {
265                 $g->addMember($p);
266             }
267
268
269         }
270     }
271
272     function initDatabase($roo, $data)
273     {
274         $this->initGroups();
275
276         foreach($data as $gi) {
277             $g = DB_DataObject::factory($this->tableName());
278             $g->setFrom($gi);
279
280             if(!$g->find(true)){
281                 $g->insert();
282             }
283
284             if(count($g->members()) || empty($gi['members'])){
285                 continue;
286             }
287
288             foreach ($gi['members'] as $m){
289                 $g->addMember($m);
290             }
291
292         }
293
294     }
295
296 }