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