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