DataObjects/Core_notify_recur.php
[Pman.Core] / DataObjects / Events.php
index b1005b3..33d1b76 100644 (file)
@@ -1,6 +1,11 @@
 <?php
 /**
  * Table Definition for Events
+ *
+ * objects can implement relatedWhere(), which should return
+ *    'tablename' => array of ids
+ *
+ * 
  */
 require_once 'DB/DataObject.php';
 
@@ -57,9 +62,52 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
             
         }
         // _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');
+        }
+         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);
@@ -74,17 +122,27 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
                         AND on_table = '{$jtn}'
                 ";
                 $keys = array_keys($x->table());
-                if (isset($q['_join_cols']) && in_array($q['_join_cols'], $keys)) {
-                    DB_DataObject::DebugLevel(1);
-                     $this->selectAdd();
-                   
-                    $this->selectAdd( "
-                                distinct(join_on_id_{$jtn}.{$q['_join_cols']}  )
-                                as on_id_{$q['_join_cols']},
-                                events.event_when as event_when 
-                                
-                                ");
+                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}");
@@ -96,10 +154,111 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
             
         }
         
+        if (isset($q['_related_on_id']) && isset($q['_related_on_table'])) {
+            // example: sales order - has invoices,
+            $ev  =$this->factory('Events');
+            $ev->setFrom(array(
+                'on_id' => $q['_related_on_id'],
+                'on_table' => $q['_related_on_table'],
+                               ));
+            $obj = $ev->object();
+            
+            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();
+        }
+        
+        
+        
         
+            
+    }
+     
+    function autoJoinExtra()
+    {
+        //$ret = parent::autoJoin();
+        // DB_DataObject::debugLevel(1);
         
+        // now try and magically join person_table to the right table..
+        $tn = $this->tableName();
+        $pt = DB_DataObject::Factory($this->tableName());
+        $pt->selectAdD();
+        $pt->selectAdD('distinct(person_table) as person_table');
+        $pt->whereAdd('person_table IS NOT NULL AND LENGTH(person_table) > 0');
+        $tbls = $pt->fetchAll('person_table');
+        $pers = DB_DataObject::Factory('Person');
+        $ptbl = $pers->tableName();
+        if (!in_array($ptbl,$tbls)) {
+            $tbls[] = $ptbl;
+            
+        }
+        foreach($tbls as $tbl) {
+            
+            // find all the columns from the joined table..
+            $st = DB_DataObject::Factory($tbl);
+            $tcols = array_keys($st->table());
+            
+            $cond = "{$tn}.person_table = '{$tbl}'";
+            if ($tbl == $ptbl) {
+                $cond = "( $cond OR {$tn}.person_table  = '')";
+            }
             
+            foreach($tcols as $col) {
+                if ($col == 'passwd') {
+                    continue;
+                }
+                $cols[$col]  = isset($cols[$col] ) ? $cols[$col]  : array();
+                $cols[$col][] = "WHEN $cond  THEN join_person_table_{$tbl}.{$col}";
+            }
+            // id's are hard coded...
+            
+            $this->_join .= "
+                LEFT JOIN {$tbl} AS  join_person_table_{$tbl}
+                    ON {$tn}.person_id = join_person_table_{$tbl}.id
+                        AND $cond
+            ";
+        }
+        foreach($cols as $col=>$whens) {
+            
+            $this->selectAdd("
+                    CASE
+                        ". implode("\n", $whens) ." 
+                        ELSE ''
+                    END
+                    as person_table_{$col}"
+            );
+        }
+        //return $ret;
+         
     }
+    
+    
     /**
      * check who is trying to access this. false == access denied..
      * @return {boolean} true if access is allowed.
@@ -142,15 +301,15 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
         $pg = $ff->page;
         $au = $pg->getAuthUser();
         
-        if ($ff->cli && empty($au) && isset($obj->person_id)) {
-            $au = DB_DataObject::Factory('Person'); // not always a person..
-            $au->get($obj->person_id);
+        if ($ff->cli) { // && empty($au) && isset($obj->person_id)) {
+            $au = false;
+           // $au = DB_DataObject::Factory('Person'); // not always a person..
+           // $au->get($obj->person_id);
         } 
-         
-         
-         
+          
         $this->person_name = $au && !empty($au->name) ? $au->name : '';
-        $this->person_id = $au ? $au->id : '';
+        $this->person_id = $au ? $au->id : -1;
+        $this->person_table = $au ? $au->tableName() : '';
         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
         $this->action = $act;
         $this->on_table = $obj ? $obj->tableName() : '';