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 */
34 function rightsTable()
36 return 'core_group_rights';
38 function personTable()
40 $ff = HTML_FlexyFramework::get();
41 return empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
46 function applyFilters($q, $au, $roo)
48 if (!empty($q['query']['name_starts'])) {
49 $v = $this->escape($q['query']['name_starts']);
50 $this->whereAdd("{$this->tableName()}.name like '{$v}%'");
54 function toEventString() {
58 function beforeDelete()
60 $x = DB_DataObject::factory('core_group_right');
61 $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
62 $x = DB_DataObject::factory('core_group_member');
63 $x->query("DELETE FROM {$x->tableName()} WHERE group_id = {$this->id}");
66 * check who is trying to access this. false == access denied..
68 function checkPerm($lvl, $au)
70 return $au->hasPerm("Core.Groups", $lvl);
72 function onUpdate($old, $req, $roo)
74 $this->ensureLeaderMembership($roo);
76 function onInsert($req, $roo)
78 $this->ensureLeaderMembership($roo);
80 function ensureLeaderMembership($roo)
83 // groups - make sure the leader is a member...
84 if (!$this->type || !$this->leader)
89 $pi = DB_DataObject::factory($this->personTable());
90 $pi->get($this->leader);
92 $p = DB_DataObject::factory($this->membersTable());
93 $p->group_id = $this->id;
94 $p->user_id = $this->leader;
95 //$p->type = 1; //???????
99 $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
105 function memberCount()
107 $gm = DB_Dataobject::factory($this->membersTable());
108 $gm->group_id = $this->id;
114 $gm = DB_Dataobject::factory($this->membersTable());
115 $gm->group_id = $this->id;
116 return $gm->fetchAll('user_id');
121 function addMember($person)
123 $gm = DB_Dataobject::factory($this->membersTable());
124 $gm->group_id = $this->id;
125 $gm->user_id = $person->id;
132 * grab a list of members - default is the array of person objects..
133 * @param $what = set to 'email' to get a list of email addresses.
138 function members($what = false)
140 $ids = $this->memberIds();
144 //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
145 // groups databse is hard coded to person.. so this should not be used for other tables.????
146 $p = DB_Dataobject::factory( 'Person' );
148 $p->whereAdd('id IN ('. implode(',', $ids) .')');
150 return $p->fetchAll($what);
156 function lookup($k,$v = false) {
166 function lookUpMembers($name, $what=false)
168 if (!$this->get('name', $name)) {
171 return $this->members($what);
175 function lookupMembersByGroupId($id, $what=false)
177 if (!$this->get($id)) {
181 return $this->members($what);
184 function postListFilter($ar, $au, $req)
186 if(!empty($req['_direct_return'])){
190 $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
191 $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
192 return array_merge($ret, $ar);
196 function initGroups()
199 $g = DB_DataObject::factory($this->tableName());
201 $g->name = 'Administrators';
206 $gr = DB_DataObject::factory('core_group_right');
210 function initDatabase($roo, $data)
214 foreach($data as $gi) {
215 $g = DB_DataObject::factory($this->tableName());
222 if(count($g->members()) || empty($gi['members'])){
226 foreach ($gi['members'] as $m){