DataObjects/Core_watch.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     function members()
93     {
94         
95         
96         $ids = $this->memberIds();
97         if (!$ids) {
98             return array();
99         }
100         $p = DB_Dataobject::factory(empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable']);
101         $p->whereAdd('id IN ('. implode(',', $ids) .')');
102         $p->active = 1;
103         return $p->fetchAll();
104      
105         
106         
107     }
108     function postListFilter($ar, $au, $req)
109     {      
110         
111         $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
112         $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
113         return array_merge($ret, $ar);
114             //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
115
116     }
117         
118      
119     
120 }