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