init
[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.".$this->tableName(), $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('Person');
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     function members()
82     {
83         
84         
85         $ids = $this->memberIds();
86         if (!$ids) {
87             return array();
88         }
89         $p = DB_Dataobject::factory('Person');
90         $p->whereAdd('id IN ('. implode(',', $ids) .')');
91         return $p->fetchAll();
92      
93         
94         
95     }
96     
97     
98 }