DataObjects/Core_group_member.php
[Pman.Core] / DataObjects / Core_group_member.php
1 <?php
2 /**
3  * Table Definition for Group_Members
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_group_member extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_group_member';                   // table name
13     public $group_id;                        // int(11)  
14     public $id;                              // int(11)  not_null primary_key auto_increment
15     public $user_id;                         // int(11)  not_null
16
17     
18     /* the code above is auto generated do not remove the tag below */
19     ###END_AUTOCODE
20     
21    
22     
23     var $inAdmin = false;
24     
25     
26     function change($person, $group, $state)
27     {
28         $gm = DB_DataObject::factory($this->tableName());
29         $gm->group_id = $group->id;
30         $gm->user_id = $person->id;
31         $gm->find(true);
32         if ($state) {
33             if (!$gm->id) {
34                 $gm->insert();
35             }
36             return;
37         }
38         // remove..
39         if ($gm->id) {
40             $gm->delete();
41         }
42         
43     }
44     
45     /**
46      * Get a list of memberships for a person
47      * @param Pman_Core_DataObjects_Person $person who
48      * @param String column to fetch.. eg. group_id or 'name'
49      *
50      */
51     
52     
53     function listGroupMembership($person, $arrayof = 'group_id') 
54     {
55         $this->inAdmin = false;
56         $t = clone($this);
57         //DB_DataObject::debugLevel(1);
58          
59         $grp = DB_DataObject::factory('core_group');
60         $t->joinAdd($grp , 'LEFT');
61         //$person->id = (int)$person->id;
62         $t->whereAdd("
63             user_id = {$person->id}
64         ");
65         $t->selectAdd();
66         $t->selectAdd("distinct(group_id), {$grp->tableName()}.name as name");
67         $t->whereAdd('group_id IS NOT NULL');
68         
69         $t->find();
70         
71         $ret = array() ;
72        // $ret = $arrayof == 'group_id' ? array(0) : array();
73         // default member of 'All groups'!!
74         
75         while ($t->fetch()) {
76             $ret[] = $t->$arrayof;
77             if ($t->name == 'Administrators') { /// mmh... bit risky?
78                 $this->inAdmin = true;
79             }
80         }
81         if ($arrayof == 'group_id' && !count($ret)) {
82             $ret = array(0); /// default if they are not a member of any group.
83         }
84         //var_dump($ret);
85         return $ret;
86         
87     }
88     
89     function checkPerm($lvl, $au) 
90     {
91         return false;
92     }
93     
94 }