DataObjects/Core_notify_recur.php
[Pman.Core] / DataObjects / Group_Members.php
index 4b1db59..f0c305d 100755 (executable)
@@ -14,37 +14,60 @@ class Pman_Core_DataObjects_Group_Members extends DB_DataObject
     public $id;                              // int(11)  not_null primary_key auto_increment
     public $user_id;                         // int(11)  not_null
 
-  
+    
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
     
     var $inAdmin = false;
     
+    
+    function change($person, $group, $state)
+    {
+        $gm = DB_DataObject::factory('Group_Members');
+        $gm->group_id = $group->id;
+        $gm->user_id = $person->id;
+        $gm->find(true);
+        if ($state) {
+            if (!$gm->id) {
+                $gm->insert();
+            }
+            return;
+        }
+        // remove..
+        if ($gm->id) {
+            $gm->delete();
+        }
+        
+    }
+    
     /**
      * Get a list of memberships for a person
      * @param Pman_Core_DataObjects_Person $person who
-     * @param String column to fetch..
+     * @param String column to fetch.. eg. group_id or 'name'
      *
      */
     
+    
     function listGroupMembership($person, $arrayof = 'group_id') 
     {
         $this->inAdmin = false;
         $t = clone($this);
         //DB_DataObject::debugLevel(1);
          
-        
-        $t->joinAdd(DB_DataObject::factory('Groups'), 'LEFT');
+        $grp = DB_DataObject::factory('Groups');
+        $t->joinAdd($grp , 'LEFT');
         //$person->id = (int)$person->id;
         $t->whereAdd("
             user_id = {$person->id}
         ");
         $t->selectAdd();
-        $t->selectAdd('distinct(group_id), Groups.name as name');
+        $t->selectAdd("distinct(group_id), {$grp->tableName()}.name as name");
+        $t->whereAdd('group_id IS NOT NULL');
         
         $t->find();
         
-        $ret = $arrayof == 'group_id' ? array(0) : array();
+        $ret = array() ;
+       // $ret = $arrayof == 'group_id' ? array(0) : array();
         // default member of 'All groups'!!
         
         while ($t->fetch()) {
@@ -53,6 +76,10 @@ class Pman_Core_DataObjects_Group_Members extends DB_DataObject
                 $this->inAdmin = true;
             }
         }
+        if ($arrayof == 'group_id' && !count($ret)) {
+            $ret = array(0); /// default if they are not a member of any group.
+        }
+        //var_dump($ret);
         return $ret;
         
     }