3 * Table Definition for Groups
7 * 0 = permission group..
12 * NOTE - used to be called Groups ....
15 require_once 'DB/DataObject.php';
17 class Pman_Core_DataObjects_Core_group extends DB_DataObject
20 /* the code below is auto generated do not remove the above tag */
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?
29 /* the code above is auto generated do not remove the tag below */
33 function personTable()
35 $ff = HTML_FlexyFramework::get();
36 return empty($ff->Pman['authTable']) ? 'core_person' : $ff->Pman['authTable'];
41 function applyFilters($q, $au, $roo)
43 if (!empty($q['query']['name_starts'])) {
44 $v = $this->escape($q['query']['name_starts']);
45 $this->whereAdd("{$this->tableName()}.name like '{$v}%'");
49 function toEventString() {
53 function beforeInsert($q,$roo)
55 if (isset($q['_action'])) {
58 if (!$g->get($q['group_id'])) {
59 $roo->jerr("missing group id");
62 foreach(explode(',', $q['user_ids']) as $uid) {
63 switch($q['_action']) {
68 $g->removeMember($uid);
71 $roo->jerr('invalid action');
85 function beforeDelete()
87 $x = DB_DataObject::factory('core_group_right');
88 $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
89 $x = DB_DataObject::factory('core_group_member');
90 $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
93 * check who is trying to access this. false == access denied..
95 function checkPerm($lvl, $au)
97 print_R($au->hasPerm("Core.Groups", $lvl));exit;
98 return $au->hasPerm("Core.Groups", $lvl);
100 function onUpdate($old, $req, $roo)
102 $this->ensureLeaderMembership($roo);
104 function onInsert($req, $roo)
106 $this->ensureLeaderMembership($roo);
108 function ensureLeaderMembership($roo)
111 // groups - make sure the leader is a member...
112 if (!$this->type || !$this->leader)
117 $pi = DB_DataObject::factory('core_person');
118 $pi->get($this->leader);
120 $p = DB_DataObject::factory('core_group_member');
121 $p->group_id = $this->id;
122 $p->user_id = $this->leader;
123 //$p->type = 1; //???????
127 $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
133 function memberCount()
135 $gm = DB_Dataobject::factory('core_group_member');
136 $gm->group_id = $this->id;
142 $gm = DB_Dataobject::factory('core_group_member');
143 $gm->group_id = $this->id;
144 return $gm->fetchAll('user_id');
147 function isMember($person)
149 $gm = DB_Dataobject::factory('core_group_member');
150 $gm->group_id = $this->id;
151 $gm->user_id = is_object($person) ? $person->id : $person;
155 function addMember($person)
157 $gm = DB_Dataobject::factory('core_group_member');
158 $gm->group_id = $this->id;
159 $gm->user_id = is_object($person) ? $person->id : $person;
165 function removeMember($person)
167 $gm = DB_Dataobject::factory('core_group_member');
168 $gm->group_id = $this->id;
169 $gm->user_id = is_object($person) ? $person->id : $person;
177 * grab a list of members - default is the array of person objects..
178 * @param $what = set to 'email' to get a list of email addresses.
183 function members($what = false)
185 $ids = $this->memberIds();
189 //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
190 // groups databse is hard coded to person.. so this should not be used for other tables.????
191 $p = DB_Dataobject::factory( 'core_person' );
195 $p->whereAdd('id IN ('. implode(',', $ids) .')');
197 return $p->fetchAll($what);
203 function lookup($k,$v = false) {
213 function lookUpMembers($name, $what=false)
215 if (!$this->get('name', $name)) {
218 return $this->members($what);
222 function lookupMembersByGroupId($id, $what=false)
224 if (!$this->get($id)) {
228 return $this->members($what);
231 function postListFilter($ar, $au, $req)
233 if(empty($req['_add_everyone'])){
237 $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
238 $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
239 return array_merge($ret, $ar);
243 function initGroups()
246 $g = DB_DataObject::factory($this->tableName());
248 $g->name = 'Administrators';
253 $gr = DB_DataObject::factory('core_group_right');
257 function initDatabase($roo, $data)
261 foreach($data as $gi) {
262 $g = DB_DataObject::factory($this->tableName());
269 if(count($g->members()) || empty($gi['members'])){
273 foreach ($gi['members'] as $m){