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