DataObjects/core.sql
[Pman.Core] / DataObjects / Person.php
index d4433c9..02c363d 100644 (file)
@@ -4,6 +4,7 @@
  */
 require_once 'DB/DataObject.php';
 
+
 class Pman_Core_DataObjects_Person extends DB_DataObject 
 {
     ###START_AUTOCODE
@@ -12,10 +13,10 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     public $__table = 'Person';                          // table name
     public $id;                              // int(11)  not_null primary_key auto_increment
     public $email;                           // string(128)  not_null
+    public $alt_email;
     
     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 +37,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;
+    }
+    
     /**
      *
      *
@@ -209,7 +226,16 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     function verifyAuth()
     { 
         $ff= HTML_FlexyFramework::get();
-        if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
+        if (!empty($ff->Pman['auth_comptype']) &&
+            (!$this->company_id || ($ff->Pman['auth_comptype'] != $this->company()->comptype))
+           ){
+            
+            // force a logout - without a check on the isAuth - as this is called from there..
+            $db = $this->getDatabaseConnection();
+            $sesPrefix = $ff->appNameShort .'-'.get_class($this) .'-'.$db->dsn['database'] ;
+            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
+            return false;
+            
             $ff->page->jerr("Login not permited to outside companies");
         }
         return true;
@@ -224,18 +250,22 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         // we combine db + project names,
         // otherwise if projects use different 'auth' objects
         // then we get unserialize issues.
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
         
         
         @session_start();
+         
         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
             // in session...
             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
             
             $u = DB_DataObject::factory('Person');
             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
-                $u->verifyAuth();
+              
+                return $u->verifyAuth();
                 
+   
                 return true;
             }
             
@@ -270,13 +300,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...
@@ -294,7 +326,11 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             return false;
         }
         $db = $this->getDatabaseConnection();
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
+
+        
         
         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
@@ -306,9 +342,20 @@ 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();
+            
+            
             $u = DB_DataObject::factory('Person');
             $u->id = -1;
             return $u;
@@ -319,8 +366,16 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     function login()
     {
         $this->isAuth(); // force session start..
-        $this->verifyAuth();
+        if (!$this->verifyAuth()) {
+            return false;
+        }
         $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');
@@ -332,16 +387,20 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $gr = DB_DataObject::Factory('group_rights');
             $gr->applyDefs($g, 0);
         }
-             
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
+
+
         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
         
     }
     function logout()
     {
         $this->isAuth(); // force session start..
-         $db = $this->getDatabaseConnection();
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        $db = $this->getDatabaseConnection();
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
+
         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
         
     }    
@@ -376,9 +435,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;
     }
@@ -397,15 +466,19 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $this->whereAdd('LENGTH(passwd) > 1'); 
         $this->email = $n;
     }
-    function lang($val)
+    function lang()
     {
+        if (!func_num_args()) {
+            return $this->lang;
+        }
+        $val = array_shift(func_get_args());
         if ($val == $this->lang) {
             return;
         }
         $uu = clone($this);
         $this->lang = $val;
         $this->update($uu);
-
+        return $this->lang;
     }
             
     
@@ -681,17 +754,42 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             
         }
         
-        
         if (!empty($q['query']['search'])) {
-            $s = $this->escape($q['query']['search']);
-                    $this->whereAdd("
+            
+            // use our magic search builder...
+            
+             require_once 'Text/SearchParser.php';
+            $x = new Text_SearchParser($q['query']['search']);
+            
+            $props = array(
+                    "$tn_p.name",
+                    "$tn_p.email",
+                    "$tn_p.role",
+                    "$tn_p.phone",
+                    "$tn_p.remarks",
+                    "join_company_id_id.name"
+            );
+            
+            $str =  $x->toSQL(array(
+                'default' => $props,
+                'map' => array(
+                    'company' => 'join_company_id_id.name',
+                    //'country' => 'Clipping.country',
+                    //  'media' => 'Clipping.media_name',
+                ),
+                'escape' => array($this->getDatabaseConnection(), 'escapeSimple'), /// pear db or mdb object..
+
+            ));
+            
+            
+            $this->whereAdd($str); /*
                         $tn_p.name LIKE '%$s%'  OR
                         $tn_p.email LIKE '%$s%'  OR
                         $tn_p.role LIKE '%$s%'  OR
                         $tn_p.phone LIKE '%$s%' OR
                         $tn_p.remarks LIKE '%$s%' 
                         
-                    ");
+                    ");*/
         }
         
         //
@@ -804,9 +902,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');
             
@@ -830,4 +933,43 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         
     }
+    
+    function importFromArray($roo, $persons, $prefix)
+    {
+        if (!is_array($persons) || empty($persons)) {
+            $roo->jerr("error in the person data. - empty on not valid");
+        }
+        DB_DataObject::factory('groups')->initGroups();
+        
+        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);
+        }
+    }
  }