DataObjects/Core_notify.php
[Pman.Core] / DataObjects / Person.php
index 32f0ec4..7d5f3ba 100644 (file)
@@ -15,7 +15,6 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     
     public $company_id;                      // int(11)  
     public $office_id;                       // int(11)  
-    
     public $name;                            // string(128)  not_null
     public $firstname;                            // string(128)  not_null
     public $lastname;                            // string(128)  not_null
@@ -36,9 +35,25 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     public $deleted_by;                      // int(11)  not_null
     public $deleted_dt;                      // datetime(19)  binary
 
+
+    public $name_facebook; // VARCHAR(128) NULL;
+    public $url_blog; // VARCHAR(256) NULL ;
+    public $url_twitter; // VARCHAR(256) NULL ;
+    public $url_linkedin; // VARCHAR(256) NULL ;
+    
+    public $phone_mobile; // varchar(32)  NOT NULL  DEFAULT '';
+    public $phone_direct; // varchar(32)  NOT NULL  DEFAULT '';
     
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
+    
+    function owner()
+    {
+        $p = DB_DataObject::Factory('Person');
+        $p->get($this->owner_id);
+        return $p;
+    }
+    
     /**
      *
      *
@@ -228,6 +243,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
         
         @session_start();
+         
         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
             // in session...
             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
@@ -270,13 +286,15 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             return true; 
         }
         
-        
-        
+        if (!empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
+            return false;
+        }
         
         // not in session or not matched...
         $u = DB_DataObject::factory('Person');
         $u->whereAdd(' LENGTH(passwd) > 0');
         $n = $u->count();
+        $_SESSION[__CLASS__][$sesPrefix .'-empty']  = $n;
         $error =  PEAR::getStaticProperty('DB_DataObject','lastError');
         if ($error) {
             die($error->toString()); // not really a good thing to do...
@@ -296,6 +314,8 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $db = $this->getDatabaseConnection();
         $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
         
+        
+        
         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
             
@@ -306,13 +326,18 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
              
         }
         
-        $u = DB_DataObject::factory('Person');
-        $u->whereAdd(' LENGTH(passwd) > 0');
-        if (!$u->count()){
+        if (empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
+            $u = DB_DataObject::factory('Person');
+            $u->whereAdd(' LENGTH(passwd) > 0');
+             $_SESSION[__CLASS__][$sesPrefix .'-empty']  = $u->count();
+        }
+                
+             
+        if (empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
             
             // fake person - open system..
-            $ce = DB_DataObject::factory('core_enum');
-            $ce->initEnums();
+            //$ce = DB_DataObject::factory('core_enum');
+            //$ce->initEnums();
             
             
             $u = DB_DataObject::factory('Person');
@@ -327,6 +352,12 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $this->isAuth(); // force session start..
         $this->verifyAuth();
         $db = $this->getDatabaseConnection();
+        
+        
+        // open up iptables at login..
+        $dbname = $this->database();
+        touch( '/tmp/run_pman_admin_iptables-'.$dbname);
+         
         // refresh admin group if we are logged in as one..
         //DB_DataObject::debugLevel(1);
         $g = DB_DataObject::factory('Groups');
@@ -382,9 +413,19 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
        
     }      
     
+    function generatePassword() // genearte a password (add set 'rawPasswd' to it's value)
+    {
+        require_once 'Text/Password.php';
+        $this->rawPasswd = strtr(ucfirst(Text_Password::create(5)).ucfirst(Text_Password::create(5)), array(
+        "a"=>"4", "e"=>"3",  "i"=>"1",  "o"=>"0", "s"=>"5",  "t"=>"7"));
+        $this->setPassword($this->rawPasswd);
+        return $this->rawPasswd;
+    }
+    
     function company()
     {
         $x = DB_DataObject::factory('Companies');
+        $x->autoJoin();
         $x->get($this->company_id);
         return $x;
     }
@@ -809,9 +850,14 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     }
     function onInsert($req, $roo)  
     {
-        
-        if ($roo->authUser->id < 0) {
+         
+        $p = DB_DataObject::factory('person');
+        if ($roo->authUser->id < 0 && $p->count() == 1) {
+            // this seems a bit risky...
+            
             $g = DB_DataObject::factory('Groups');
+            $g->initGroups();
+            
             $g->type = 0;
             $g->get('name', 'Administrators');
             
@@ -835,4 +881,38 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         
     }
+    
+    function importFromArray($roo, $persons, $prefix)
+    {
+        foreach($persons as $person){
+            $p = DB_DataObject::factory('person');
+            if($p->get('name', $person['name'])){
+                continue;
+            }
+            $p->setFrom($person);
+            
+            $companies = DB_DataObject::factory('companies');
+            if(!$companies->get('comptype', 'OWNER')){
+                $roo->jerr("Missing OWNER companies!");
+            }
+            $p->company_id = $companies->pid();
+            // strip the 'spaces etc.. make lowercase..
+            $name = strtolower(str_replace(' ', '', $person['name']));
+            $p->setPassword("$prefix$name");
+            $p->insert();
+            // set up groups
+            // if $person->groups is set.. then
+            // add this person to that group eg. groups : [ 'Administrator' ] 
+            if(!empty($person['groups'])){
+                $groups = DB_DataObject::factory('groups');
+                if(!$groups->get('name', $person['groups'])){
+                    $roo->jerr("Missing groups : {$person['groups']}");
+                }
+                $gm = DB_DataObject::factory('group_members');
+                $gm->change($p, $groups, true);
+            }
+            
+            $p->onInsert(array(), $roo);
+        }
+    }
  }