DataObjects/Core_notify.php
[Pman.Core] / DataObjects / Person.php
index d2a1d4e..7d5f3ba 100644 (file)
@@ -11,32 +11,59 @@ 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 $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
     public $phone;                           // string(32)  not_null
     public $fax;                             // string(32)  not_null
-    public $email;                           // string(128)  not_null
-    public $company_id;                      // int(11)  
+    
     public $role;                            // string(32)  not_null
-    public $active;                          // int(11)  not_null
     public $remarks;                         // blob(65535)  not_null blob
     public $passwd;                          // string(64)  not_null
     public $owner_id;                        // int(11)  not_null
     public $lang;                            // string(8)  
     public $no_reset_sent;                   // int(11)  
     public $action_type;                     // string(32)  
-    public $project_id;                      // int(11)  
+    public $project_id;                      // int(11)
+
+    
+    public $active;                          // int(11)  not_null
     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;
+    }
+    
     /**
      *
-     * @param {String} $templateFile  (mail/XXXXXXX.txt) exclude the mail and .txt bit.
-     * @param {Array|Object} $args   data to send out..
-     * @return {Array|PEAR_Error} array of $recipents, $header, $body 
+     *
+     *
+     *
+     *  FIXME !!!! -- USE Pman_Core_Mailer !!!!!
+     *
+     *
+     *
+     *  
      */
     function buildMail($templateFile, $args)
     {
@@ -75,21 +102,42 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             (isset($ff->HTTP_HOST) ? $ff->HTTP_HOST : 'localhost');
             
         /* use the regex compiler, as it doesnt parse <tags */
-        require_once 'HTML/Template/Flexy.php';
-        $template = new HTML_Template_Flexy( array(
+        
+        $tops = array(
             'compiler'    => 'Flexy',
             'nonHTML' => true,
             'filters' => array('SimpleTags','Mail'),
             //     'debug'=>1,
-        ));
+        );
+        
         
-     
-         
         
+        if (!empty($args['templateDir'])) {
+            $tops['templateDir'] = $args['templateDir'];
+        }
+        
+        
+        
+        require_once 'HTML/Template/Flexy.php';
+        $template = new HTML_Template_Flexy( $tops );
         $template->compile("mail/$templateFile.txt");
         
         /* use variables from this object to ouput data. */
         $mailtext = $template->bufferedOutputObject($content);
+        
+        $htmlbody = false;
+        // if a html file with the same name exists, use that as the body
+        // I've no idea where this code went, it was here before..
+        if (false !== $template->resolvePath ( "mail/$templateFile.html" )) {
+            $tops['nonHTML'] = false;
+            $template = new HTML_Template_Flexy( $tops );
+            $template->compile("mail/$templateFile.html");
+            $htmlbody = $template->bufferedOutputObject($content);
+            
+        }
+        
+        
+        
         //echo "<PRE>";print_R($mailtext);
         //print_R($mailtext);exit;
         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
@@ -98,6 +146,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
         $decoder = new Mail_mimeDecode($mailtext);
         $parts = $decoder->getSendArray();
+        
         if (PEAR::isError($parts)) {
             return $parts;
             //echo "PROBLEM: {$parts->message}";
@@ -110,6 +159,20 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         $headers['Date'] = date('r');
         
+        if ($htmlbody !== false) {
+            require_once 'Mail/mime.php';
+            $mime = new Mail_mime(array('eol' => "\n"));
+            $mime->setTXTBody($body);
+            $mime->setHTMLBody($htmlbody);
+            // I think there might be code in mediaoutreach toEmail somewhere
+            // h embeds images here..
+            $body = $mime->get();
+            $headers = $mime->headers($headers);
+            
+        }
+        
+         
+        
         return array(
             'recipients' => $recipents,
             'headers'    => $headers,
@@ -180,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']);
@@ -207,11 +271,30 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             return true;
         }
            
+        // http basic auth..
+        $u = DB_DataObject::factory('Person');
+
+        if (!empty($_SERVER['PHP_AUTH_USER']) 
+            &&
+            !empty($_SERVER['PHP_AUTH_PW'])
+            &&
+            $u->get('email', $_SERVER['PHP_AUTH_USER'])
+            &&
+            $u->checkPassword($_SERVER['PHP_AUTH_PW'])
+           ) {
+            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
+            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...
@@ -231,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']);
             
@@ -241,9 +326,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;
@@ -256,15 +352,21 @@ 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');
         $g->type = 0;
         $g->get('name', 'Administrators');
-        $gm = DB_DataObject::Factory('Group_Members');
+        $gm = DB_DataObject::Factory('group_members');
         if (in_array($g->id,$gm->listGroupMembership($this))) {
             // refresh admin groups.
-            $gr = DB_DataObject::Factory('Group_Rights');
+            $gr = DB_DataObject::Factory('group_rights');
             $gr->applyDefs($g, 0);
         }
              
@@ -311,16 +413,29 @@ 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;
     }
-    
+    function loadCompany()
+    {
+        $this->company = $this->company();
+    }
     
     function active()
-    {
+    { 
         return $this->active;
     }
     function authUserName($n) // set username prior to acheck user exists query.
@@ -383,7 +498,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
       
         // perms + groups.
         $aur['perms']  = $this->getPerms();
-        $g = DB_DataObject::Factory('Group_Members');
+        $g = DB_DataObject::Factory('group_members');
         $aur['groups']  = $g->listGroupMembership($this, 'name');
         
         $aur['passwd'] = '';
@@ -402,7 +517,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
         // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
         
-        $g = DB_DataObject::Factory('Group_Rights');
+        $g = DB_DataObject::Factory('group_rights');
         if (!$g->count()) {
             $g->genDefault();
         }
@@ -411,13 +526,13 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             return $g->adminRights(); // system is not set up - so they get full rights.
         }
         //DB_DataObject::debugLevel(1);
-        $g = DB_DataObject::Factory('Group_Members');
+        $g = DB_DataObject::Factory('group_members');
         $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
         if (!$g->count()) {
             // add the current user to the admin group..
             $g = DB_DataObject::Factory('Groups');
             if ($g->get('name', 'Administrators')) {
-                $gm = DB_DataObject::Factory('Group_Members');
+                $gm = DB_DataObject::Factory('group_members');
                 $gm->group_id = $g->id;
                 $gm->user_id = $this->id;
                 $gm->insert();
@@ -426,16 +541,16 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         
         // ------ STANDARD PERMISSION HANDLING.
-        
-        $g = DB_DataObject::Factory('Group_Members');
+        $isOwner = $this->company()->comptype == 'OWNER';
+        $g = DB_DataObject::Factory('group_members');
         $grps = $g->listGroupMembership($this);
        //var_dump($grps);
         $isAdmin = $g->inAdmin;
         //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
         // the load all the perms for those groups, and add them all together..
         // then load all those 
-        $g = DB_DataObject::Factory('Group_Rights');
-        $ret =  $g->listPermsFromGroupIds($grps, $isAdmin);
+        $g = DB_DataObject::Factory('group_rights');
+        $ret =  $g->listPermsFromGroupIds($grps, $isAdmin, $isOwner);
         //echo '<PRE>';print_r($ret);
         return $ret;
          
@@ -449,7 +564,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     
     function groups($what=false)
     {
-        $g = DB_DataObject::Factory('Group_Members');
+        $g = DB_DataObject::Factory('group_members');
         $grps = $g->listGroupMembership($this);
         $g = DB_DataObject::Factory('Groups');
         $g->whereAddIn('id', $grps, 'int');
@@ -457,6 +572,8 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
     }
     
+    
+    
     function hasPerm($name, $lvl) 
     {
         static $pcache = array();
@@ -481,6 +598,8 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         if (!empty($q['query']['person_not_internal'])) {
             $this->whereAdd(" join_company_id_id.isOwner = 0 ");
         }
+        
+        
         if (!empty($q['query']['person_internal_only_all'])) {
             
             
@@ -525,7 +644,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $this->active = 1;
         }
         $tn_p = $this->tableName();
-        $tn_gm = DB_DataObject::Factory('Group_Members')->tableName();
+        $tn_gm = DB_DataObject::Factory('group_members')->tableName();
         $tn_g = DB_DataObject::Factory('Groups')->tableName();
 
         ///---------------- Group views --------
@@ -569,12 +688,27 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             if ( $q['query']['not_in_directory'] > -1) {
                 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
                 // can list current - so that it does not break!!!
-                $x->whereAdd("$tn_p.id NOT IN 
+                $this->whereAdd("$tn_p.id NOT IN 
                     ( SELECT distinct person_id FROM $tn_pd WHERE
                         project_id = " . $q['query']['not_in_directory'] . " AND 
                         company_id = " . $this->company_id . ')');
             }
         }
+           
+        if (!empty($q['query']['role'])) { 
+            // it's a Person list..
+            // DB_DATaobjecT::debugLevel(1);
+            
+            // specific to project directory which is single comp. login
+            //
+            $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
+                // can list current - so that it does not break!!!
+            $this->whereAdd("$tn_p.id IN 
+                    ( SELECT distinct person_id FROM $tn_pd WHERE
+                        role = '". $this->escape($q['query']['role']) ."'
+            )");
+        
+        }
         
         
         if (!empty($q['query']['project_member_of'])) {
@@ -594,13 +728,13 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             
         }
         
-        
         if (!empty($q['query']['search'])) {
             $s = $this->escape($q['query']['search']);
                     $this->whereAdd("
                         $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%' 
                         
                     ");
@@ -705,33 +839,29 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             // extra case change passwod?
             case 'P': //??? password
                 // standard perms -- for editing + if the user is dowing them selves..
-                $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
+                $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
                 return $ret || $au->id == $this->id;
             
-            case 'S': // list..
-                return $au->hasPerm("Core.Person", "S");
-            
-            case 'E': // edit
-                return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
-            
-            case 'A': // add
-                return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
-            
-            case 'D': // add
-                return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
+            default:                
+                return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
         
         }
         return false;
     }
     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');
             
-            $p = DB_DataObject::factory('Group_Members');
+            $p = DB_DataObject::factory('group_members');
             $p->group_id = $g->id;
             $p->user_id = $this->id;     
             if (!$p->count()) {
@@ -751,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);
+        }
+    }
  }