DataObjects/Core_watch.php
[Pman.Core] / DataObjects / Events.php
index 15fd237..9e54039 100644 (file)
@@ -31,6 +31,218 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
     
     
     
+    
+    //  ------------ROO HOOKS------------------------------------
+    function applyFilters($q, $au ,$roo)
+    {
+        $tn = $this->tableName();
+        // if not empty on_table
+        if(!empty($q['person_table'])){
+            $jt = DB_DataObject::factory($q['person_table']);
+        
+            $this->_join = "LEFT JOIN {$jt->tableName()} AS join_person_id_id ON (join_person_id_id.id=Events.person_id)";
+            $this->selectAdd();
+            $this->selectAs();
+            
+            $this->selectAs($jt, 'person_id_%s', 'join_person_id_id');
+        
+            if (method_exists($jt,'nameColumn')) {
+                $this->selectAdd("join_person_id_id.{$jt->nameColumn()} as person_id_name");
+            }
+        
+        
+        } else {
+            $person = 'Person';
+            $cfg = HTML_FlexyFramework::get()->Pman;
+            if (!empty($cfg['authTable'])) {
+                $person =$cfg['authTable'];
+            }
+            
+            $jt = DB_DataObject::factory($person);
+            $this->whereAdd("
+                    person_table  = '{$jt->tableName()}'
+                    OR
+                    person_table = ''
+                    OR person_table IS NULL"
+            ); // default to  our standard.. - unless otherwise requested..
+        }
+        
+        
+        if (!empty($q['query']['from'])) {
+            $dt = date('Y-m-d' , strtotime($q['query']['from']));
+            $this->whereAdd(" {$tn}.event_when >=  '$dt' ");
+        }
+        if (!empty($q['query']['to'])) {
+            $dt = date('Y-m-d' , strtotime($q['query']['to']));
+            $this->whereAdd(" {$tn}.event_when <=  '$dt' ");
+        }
+        /*
+        if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
+            // grouped..
+            DB_DataObject::Debuglevel(1);
+            $this->groupBy('on_id');
+            $this->selectAdd('
+                (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
+                ');
+        }
+        */
+        
+        if (!$au->hasPerm("Admin.Admin_Tab", 'S')) {
+            //DB_DataObject::DebugLevel(1);
+            // they can only view their changes..
+            $this->person_id = $au->id;
+            
+        }
+        // _join = tablename,tablename...
+        
+        /// on_table=cohead
+        //   &_join=cohead
+        //   &_join_cols=cohead_number
+        //    &_columns=on_id_cohead_number,event_when << this is ignored at present.
+        // max(event_when) is not supported... by any query yet..
+        
+        if (isset($q['on_table']) && !strlen($q['on_table'])) {
+            // empty ontable queries.. these are valid..
+            $this->whereAdd("$tn.on_table = ''");
+        }
+        
+        
+        
+        if (isset($q['query']['person_sum'])) {
+            //DB_DataObject::debugLevel(1);
+            $this->_extra_cols = array('qty' );
+            $this->selectAdd("count($tn.id) as qty");
+            $this->selectAdd("count( distinct $tn.on_id) as uqty");
+            $this->whereAdd('LENGTH(join_person_id_id.name) > 0 ');
+            $this->groupBy('person_id,join_person_id_id.name,join_person_id_id.email');
+        }
+         if (isset($q['query']['table_sum'])) {
+            //DB_DataObject::debugLevel(1);
+            $this->_extra_cols = array('qty' , 'uqty');
+            $this->selectAdd("count($tn.id) as qty");
+            $this->selectAdd("count( distinct $tn.on_table, $tn.on_id) as uqty");
+            
+            $this->groupBy('on_table');
+        }
+         if (isset($q['query']['day_sum'])) {
+            //DB_DataObject::debugLevel(1);
+            $this->_extra_cols = array('qty' , 'uqty');
+            $this->selectAdd("DATE_FORMAT(event_when, '%Y-%m-%d') as on_day");
+            $this->selectAdd("count($tn.id) as qty");
+            $this->selectAdd("count( distinct $tn.on_id) as uqty");
+            
+            $this->groupBy('on_day');
+        }
+        
+        if (isset($q['_join'])) {
+            //DB_DataObject::DebugLevel(1);
+            $joins = explode(',',$q['_join']);
+            
+            $this->selectAdd(); // ???
+            $distinct = false;
+            
+            foreach($joins as $t) {
+                $t = preg_replace('/[^a-z_]+/', '', $t); // protection.
+                $x = DB_DataObject::Factory($t);
+                if (!is_a($x,'DB_DataObject')) {
+                    continue;
+                }
+                $jtn = $x->tableName();
+                $jk = array_shift($x->keys());
+                $this->_join .= "
+                
+                    LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
+                        AND on_table = '{$jtn}'
+                ";
+                $keys = array_keys($x->table());
+                if (isset($q['_join_cols'])) {
+                    $jcs = explode(',',$q['_join_cols'] );
+                    //DB_DataObject::DebugLevel(1);
+                    
+                    foreach($jcs as $jc) { 
+                        if (! in_array($jc, $keys)) {
+                            continue;
+                        }
+                        if ($distinct) { 
+                        
+                       
+                            $this->selectAdd( " join_on_id_{$jtn}.{$jc}   as on_id_{$jc} ");
+                        } else {
+                            $this->selectAdd( " distinct(join_on_id_{$jtn}.{$jc}  ) as on_id_{$jc} ");
+                            $distinct = true;
+                        }
+                        $this->groupBy("on_id_{$jc} ");
+                        $this->whereAdd("join_on_id_{$jtn}.{$jc} IS NOT NULL");
+                    }
+                    $this->selectAdd( "MAX(events.event_when) as event_when");
+                    $this->orderBy('event_when DESC');
+                   // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
+                } else { 
+                    $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
+                }
+            }
+                 
+            
+        }
+        
+        if (isset($q['_related_on_id']) && isset($q['_related_on_table'])) {
+            // example: sales order - has invoices,
+            ///DB_DataObject::DebugLevel(1);
+            $ev  =$this->factory('Events');
+            $ev->setFrom(array(
+                'on_id' => $q['_related_on_id'],
+                'on_table' => $q['_related_on_table'],
+                               ));
+            $obj = $ev->object();
+            
+            if (!$obj) {
+                $roo->jerr("ontable is invalid");
+            }
+            if (!method_exists($obj,'relatedWhere')) {
+                $roo->jerr( $q['_related_on_table'] . " Does not have method relatedWhere :" .
+                           implode(',', get_class_methods($obj)));
+            }
+            if ($obj && method_exists($obj,'relatedWhere')) {
+                $ar = $obj->relatedWhere();
+                $tn = $this->tableName();
+                
+                $w = array();
+                $w[] = "( {$tn}.on_table = '" .
+                        $this->escape($q['_related_on_table']) .
+                        "' AND {$tn}.on_id = ". ((int)  $q['_related_on_id']) .
+                    ")";
+                
+                
+                foreach($ar as $k=>$v) {
+                    if (empty($v)) {
+                        continue;
+                    }                
+                     $w[] = "( {$tn}.on_table = '$k' AND {$tn}.on_id IN (". implode(',', $v). "))";
+                    
+                }
+                $this->whereAdd(implode(' OR ' , $w));
+            }
+            
+            
+            
+            
+            
+        }
+        // since roo does not support autojoin yet..
+        if (!isset($q['_distinct'])) {
+            //$this->autoJoinExtra();
+        }
+        
+        if(!empty($q['query']['action'])) {
+            $act = $this->escape($q['query']['action']);
+            $this->whereAdd("Events.action LIKE '%{$act}%'");
+        }
+        
+        if(!empty($q['query']['on_table'])) {
+            $tnb = $this->escape($q['query']['on_table']);
+            $this->whereAdd("Events.on_table LIKE '%{$tnb}%'");
+        } 
+    }
       
     
     
@@ -83,9 +295,18 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
         } 
           
         $this->person_name = $au && !empty($au->name) ? $au->name : '';
-        $this->person_id = $au ? $au->id : -1;
+        if (isset($au->id) && empty($au->id)) {
+            // not authenticated - and a standard id based object
+            $this->person_id = 0;
+        } else {
+            $this->person_id = $au ? (!empty($au->id) ? $au->id : $au->pid()) : -1;
+        }
         $this->person_table = $au ? $au->tableName() : '';
         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
+        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+            $this->ipaddr = $_SERVER['HTTP_X_FORWARDED_FOR'];
+        }
+        
         $this->action = $act;
         $this->on_table = $obj ? $obj->tableName() : '';
         $pk = $obj ? $obj->keys()  : false;
@@ -186,21 +407,28 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
         
         // Remove all the password from logs...
         $p =  empty($_POST) ? array() : $_POST;
-        foreach(array('passwd', 'password', 'passwd2', 'password2') as $rm) {
+        foreach(array('passwd', 'password','passwd1',  'passwd2','password1', 'password2') as $rm) {
             if (isset($p[$rm])) {
-                $p['passwd'] = '******';
+                $p[$rm] = '******';
             }
         }
+        
+        
         $i=0;
         $files = array();
+         
+        $i = 0;
         foreach ($_FILES as $k=>$f){
+            // does not handle any other file[] arrary very well..
             if (empty($f['tmp_name']) || !file_exists($f['tmp_name'])) {
                 continue;
             }
             $i++;
             $files[$k] = $f;
-            $files[$k]['tmp_name'] = $this->id . '.file_'. $i.'.jpg';
-            $nf = $ff->Pman['event_log_dir']. '/'. $this->id . ".file_$i.jpg";
+            
+             
+            $files[$k]['tmp_name'] =  $this->id . '-'. $i;
+            $nf = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/').   $files[$k]['tmp_name']; 
             if (!copy($f['tmp_name'], $nf)) {
                 print_r("failed to copy {$f['tmp_name']}...\n");
             }
@@ -208,11 +436,13 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
         
         file_put_contents($file, json_encode(array(
             'REQUEST_URI' => empty($_SERVER['REQUEST_URI']) ? 'cli' : $_SERVER['REQUEST_URI'],
+            'HTTP_USER_AGENT' => empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'],
             'GET' => empty($_GET) ? array() : $_GET,
             'POST' =>$p,
             'FILES' => $files,
         )));
         
+        
     }