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     
115     
116     function addMember($person)
117     {
118         $gm = DB_Dataobject::factory('core_group_member');
119         $gm->group_id = $this->id;
120         $gm->user_id = $person->id;
121         if (!$gm->count()) {
122             $gm->insert();
123         }
124     }
125     /**
126      *
127      *  grab a list of members - default is the array of person objects..
128      *  @param $what  = set to 'email' to get a list of email addresses.
129      *
130      *
131      */
132     
133     function members($what = false)
134     {
135         $ids = $this->memberIds();
136         print_r($ids);exit;
137         if (!$ids) {
138             return array();
139         }
140         //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
141         // groups databse is hard coded to person.. so this should not be used for other tables.????
142         $p = DB_Dataobject::factory( 'core_person' );
143         
144         $p->whereAdd('id IN ('. implode(',', $ids) .')');
145         $p->active = 1;
146         return $p->fetchAll($what);
147     }
148     
149     
150     
151     
152     function lookup($k,$v = false) {
153         if ($v === false) {
154             $v = $k;
155             $k = 'id';
156         }
157         $this->get($k,$v);
158
159         return $this;
160     } 
161     
162     function lookUpMembers($name, $what=false)
163     {
164         if (!$this->get('name', $name)) {
165             return array();
166         }
167         return $this->members($what);
168         
169     }
170     
171     function lookupMembersByGroupId($id, $what=false)
172     {
173         if (!$this->get($id)) {
174             return array();
175         }
176         
177         return $this->members($what);
178     }
179     
180     function postListFilter($ar, $au, $req)
181     {      
182         if(!empty($req['_direct_return'])){
183             return $ar;
184         }
185         
186         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
187         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
188         return array_merge($ret, $ar);
189
190     }
191     
192     function initGroups()
193     {
194         
195         $g = DB_DataObject::factory($this->tableName());
196         $g->type = 0;
197         $g->name = 'Administrators';
198         if ($g->count()) {
199             return;
200         }
201         $g->insert();
202         $gr = DB_DataObject::factory('core_group_right');
203         $gr->genDefault();
204     }
205     
206     function initDatabase($roo, $data)
207     {
208         $this->initGroups();
209         
210         foreach($data as $gi) {
211             $g = DB_DataObject::factory($this->tableName());
212             $g->setFrom($gi);
213             
214             if(!$g->find(true)){
215                 $g->insert();
216             }
217             
218             if(count($g->members()) || empty($gi['members'])){
219                 continue;
220             }
221             
222             foreach ($gi['members'] as $m){
223                 $g->addMember($m);
224             }
225             
226         }
227      
228     }
229     
230 }