--- /dev/null
+<?php
+/**
+ * Table Definition for Groups
+ *
+ * group types
+ *
+ * 0 = permission group..
+ * 1 = team
+ * 2 = contact group
+ *
+ *
+ * NOTE - used to be called Groups ....
+ *
+ */
+require_once 'DB/DataObject.php';
+
+class Pman_Core_DataObjects_Core_groups extends DB_DataObject
+{
+ ###START_AUTOCODE
+ /* the code below is auto generated do not remove the above tag */
+
+ public $__table = 'core_groups'; // table name
+ public $id; // int(11) not_null primary_key auto_increment
+ public $name; // string(64) not_null
+ public $type; // int(11)
+ public $leader; // int(11) not_null
+ public $is_system; // used by timesheets?
+
+ /* the code above is auto generated do not remove the tag below */
+ ###END_AUTOCODE
+
+
+ function memberTable()
+ {
+ return 'core_group_members';
+ }
+ function rightsTable()
+ {
+ return 'core_group_rights';
+ }
+ function personTable()
+ {
+ $ff = HTML_FlexyFramework::get();
+ return empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
+ }
+
+
+ // group types??
+ function applyFilters($q, $au, $roo)
+ {
+ if (!empty($q['query']['name_starts'])) {
+ $v = $this->escape($q['query']['name_starts']);
+ $this->whereAdd("{$this->tableName()}.name like '{$v}%'");
+ }
+ }
+
+ function toEventString() {
+ return $this->name;
+ }
+
+ function beforeDelete()
+ {
+ $x = DB_DataObject::factory($this->tableName());
+ $x->query("DELETE FROM {$this->rightsTable()} WHERE group_id = {$this->id}");
+ $x->query("DELETE FROM {$this->membersTable()} WHERE group_id = {$this->id}");
+ }
+ /**
+ * check who is trying to access this. false == access denied..
+ */
+ function checkPerm($lvl, $au)
+ {
+ return $au->hasPerm("Core.Groups", $lvl);
+ }
+ function onUpdate($old, $req, $roo)
+ {
+ $this->ensureLeaderMembership($roo);
+ }
+ function onInsert($req, $roo)
+ {
+ $this->ensureLeaderMembership($roo);
+ }
+ function ensureLeaderMembership($roo)
+ {
+
+ // groups - make sure the leader is a member...
+ if (!$this->type || !$this->leader)
+ {
+ return true;
+ }
+
+ $pi = DB_DataObject::factory($this->personTable());
+ $pi->get($this->leader);
+
+ $p = DB_DataObject::factory($this->membersTable());
+ $p->group_id = $this->id;
+ $p->user_id = $this->leader;
+ //$p->type = 1; //???????
+ if (!$p->count()) {
+
+ $p->insert();
+ $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
+ }
+
+ }
+
+
+ function memberCount()
+ {
+ $gm = DB_Dataobject::factory($this->membersTable());
+ $gm->group_id = $this->id;
+ return $gm->count();
+ }
+
+ function memberIds()
+ {
+ $gm = DB_Dataobject::factory($this->membersTable());
+ $gm->group_id = $this->id;
+ return $gm->fetchAll('user_id');
+
+ }
+
+
+ function addMember($person)
+ {
+ $gm = DB_Dataobject::factory($this->membersTable());
+ $gm->group_id = $this->id;
+ $gm->user_id = $person->id;
+ if (!$gm->count()) {
+ $gm->insert();
+ }
+ }
+ /**
+ *
+ * grab a list of members - default is the array of person objects..
+ * @param $what = set to 'email' to get a list of email addresses.
+ *
+ *
+ */
+
+ function members($what = false)
+ {
+ $ids = $this->memberIds();
+ if (!$ids) {
+ return array();
+ }
+ //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
+ // groups databse is hard coded to person.. so this should not be used for other tables.????
+ $p = DB_Dataobject::factory( 'Person' );
+
+ $p->whereAdd('id IN ('. implode(',', $ids) .')');
+ $p->active = 1;
+ return $p->fetchAll($what);
+ }
+
+
+
+
+ function lookup($k,$v = false) {
+ if ($v === false) {
+ $v = $k;
+ $k = 'id';
+ }
+ $this->get($k,$v);
+
+ return $this;
+ }
+
+ function lookUpMembers($name, $what=false)
+ {
+ if (!$this->get('name', $name)) {
+ return array();
+ }
+ return $this->members($what);
+
+ }
+
+ function lookupMembersByGroupId($id, $what=false)
+ {
+ if (!$this->get($id)) {
+ return array();
+ }
+
+ return $this->members($what);
+ }
+
+ function postListFilter($ar, $au, $req)
+ {
+ if(!empty($req['_direct_return'])){
+ return $ar;
+ }
+
+ $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
+ $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
+ return array_merge($ret, $ar);
+
+ }
+
+ function initGroups()
+ {
+
+ $g = DB_DataObject::factory($this->tableName());
+ $g->type = 0;
+ $g->name = 'Administrators';
+ if ($g->count()) {
+ return;
+ }
+ $g->insert();
+ $gr = DB_DataObject::factory($this->rightsTable());
+ $gr->genDefault();
+ }
+
+ function initDatabase($roo, $data)
+ {
+ $this->initGroups();
+
+ foreach($data as $gi) {
+ $g = DB_DataObject::factory($this->tableName());
+ $g->setFrom($gi);
+
+ if(!$g->find(true)){
+ $g->insert();
+ }
+
+ if(count($g->members()) || empty($gi['members'])){
+ continue;
+ }
+
+ foreach ($gi['members'] as $m){
+ $g->addMember($m);
+ }
+
+ }
+
+ }
+
+}