DataObjects/Core_notify.php
[Pman.Core] / DataObjects / Groups.php
1 <?php
2 /**
3  * Table Definition for Groups
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Groups extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'Groups';                          // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $name;                            // string(64)  not_null
15     public $type;                            // int(11)  
16     public $leader;                          // int(11)  not_null
17
18     
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21     
22     // group types??
23     
24     function toEventString() {
25         return $this->name;
26     }
27     
28     function beforeDelete()
29     {
30         $x = DB_DataObject::factory('Groups');
31         $x->query("DELETE FROM group_rights WHERE group_id = {$this->id}");
32         $x->query("DELETE FROM group_members WHERE group_id = {$this->id}");
33     }
34     /**
35      * check who is trying to access this. false == access denied..
36      */
37     function checkPerm($lvl, $au) 
38     {
39         return $au->hasPerm("Core.Groups", $lvl);    
40     } 
41     function onUpdate($old, $req, $roo)
42     {
43         $this->ensureLeaderMembership($roo);
44     }
45     function onInsert($req, $roo)
46     {
47         $this->ensureLeaderMembership($roo);
48     }
49     function ensureLeaderMembership($roo)
50     {
51         
52         // groups - make sure the leader is a member...
53         if (!$this->type || !$this->leader)
54         {
55             return true;
56         }
57         
58         $pi = DB_DataObject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
59         $pi->get($this->leader);
60             
61         $p = DB_DataObject::factory('group_members');
62         $p->group_id = $this->id;
63         $p->user_id = $this->leader;
64         //$p->type = 1; //???????
65         if (!$p->count()) {
66             
67             $p->insert();
68             $roo->addEvent("ADD", $p, $this->toEventString(). " Added " . $pi->toEventString());
69         }
70              
71     }
72     
73     function memberIds()
74     {
75         $gm = DB_Dataobject::factory('group_members');
76         $gm->group_id = $this->id;
77         return $gm->fetchAll('user_id');
78         
79     }
80     
81     
82     function addMember($person)
83     {
84         $gm = DB_Dataobject::factory('group_members');
85         $gm->group_id = $this->id;
86         $gm->user_id = $person->id;
87         if (!$gm->count()) {
88             $gm->insert();
89         }
90     }
91     /**
92      *
93      *  grab a list of members - default is the array of person objects..
94      *  @param $what  = set to 'email' to get a list of email addresses.
95      *
96      *
97      */
98     
99     function members($what = false)
100     {
101         $ids = $this->memberIds();
102         if (!$ids) {
103             return array();
104         }
105         //$p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
106         // groups databse is hard coded to person.. so this should not be used for other tables.????
107         $p = DB_Dataobject::factory( 'Person' );
108         
109         $p->whereAdd('id IN ('. implode(',', $ids) .')');
110         $p->active = 1;
111         return $p->fetchAll($what);
112     }
113     
114     function lookup($k,$v = false) {
115         if ($v === false) {
116             $v = $k;
117             $k = 'id';
118         }
119         $this->get($k,$v);
120
121         return $this;
122     } 
123     
124     function postListFilter($ar, $au, $req)
125     {      
126         
127         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
128         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
129         return array_merge($ret, $ar);
130             //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
131
132     }
133     
134     function initGroups()
135     {
136         
137         $g = DB_DataObject::factory('Groups');
138         $g->type = 0;
139         $g->name = 'Administrators';
140         if ($g->count()) {
141             return;
142         }
143         $g->insert();
144         $gr = DB_DataObject::factory('group_rights');
145         $gr->genDefault();
146     
147         
148     }
149     
150      
151     
152 }