Pman/Images.php
[Pman.Base] / Pman / Roo.php
index 1402241..46bcb61 100644 (file)
@@ -73,7 +73,7 @@ class Pman_Roo extends Pman
     function get($tab)
     {
          //  $this->jerr("Not authenticated", array('authFailure' => true));
-        // DB_DataObject::debuglevel(1);
+       //echo '<PRE>';print_R($_GET);  DB_DataObject::debuglevel(1);
         
         $this->init(); // from pnan.
         
@@ -148,36 +148,23 @@ class Pman_Roo extends Pman
             $this->jok("Updated");
             
         }
-      // DB_DataObject::debugLevel(1);
+       //DB_DataObject::debugLevel(1);
         if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser))  {
             $this->jerr("PERMISSION DENIED");
         }
-        $map = $this->loadMap($x, $_columns);
         
-        $this->setFilters($x,$_REQUEST);
-        
-         
+        // sets map and countWhat
+        $this->loadMap($x, $_columns, empty($_REQUEST['_distinct']) ? false:  $_REQUEST['_distinct']);
         
+        $this->setFilters($x,$_REQUEST);
+      
+         //print_r($x);
         // build join if req.
-        $countWhat = false;
-        if (!empty($_REQUEST['_distinct'])) {
-            $cols = $x->table();
-           // print_r($cols);
-          
-            if (isset($cols[$_REQUEST['_distinct']])) {
-                $countWhat = 'distinct ' . $_REQUEST['_distinct'];
-                $x->selectAdd();
-                $x->selectAdd('distinct('.$_REQUEST['_distinct'].')');
-                $_columns = array( $_REQUEST['_distinct'] );
-            } else {
-                $this->jerr('invalid distinct');
-            }
-             
-        }
-        $total = $x->count($countWhat);
+          //DB_DataObject::debugLevel(1);
+        $total = $x->count($this->countWhat);
         // sorting..
-      //   DB_DataObject::debugLevel(1);
-        
+      //   
+        //var_dump($total);exit;
         $this->applySort($x);
         
         
@@ -272,12 +259,13 @@ class Pman_Roo extends Pman
      * @arg   DB_DataObject $x
      * 
      */
-    function applySort($x)
+    function applySort($x, $sort = '', $dir ='')
     {
         
        // Db_DataObject::debugLevel(1);
-        $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
-        $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
+        $sort = empty($_REQUEST['sort']) ? $sort : $_REQUEST['sort'];
+        $dir = empty($_REQUEST['dir']) ? $dir : $_REQUEST['dir'];
+        $dir = $dir == 'ASC' ? 'ASC' : 'DESC';
         
         
         
@@ -615,16 +603,16 @@ class Pman_Roo extends Pman
     
     
     var $cols = array();
-    function loadMap($do, $filter=false) 
+    function loadMap($do, $filter=false, $distinct = false
     {
         //DB_DataObject::debugLevel(1);
         $conf = array();
         
-        
         $this->init();
+        
         $mods = explode(',',$this->appModules);
         
-        $ff = HTML_FlexyFramework::geT();
+        $ff = HTML_FlexyFramework::get();
         //$db->databaseName();
         
         //$ff->DB_DataObject['ini_'. $db->database()];
@@ -665,21 +653,32 @@ class Pman_Roo extends Pman
         $xx = array_keys($tabdef);
         $do->selectAdd(); // we need thsi as normally it's only cleared by an empty selectAs call.
         
-        if ($filter) {
+        $selectAs = array(array(  $xx , '%s', false));
+        $this->countWhat = false;
+        $has_distinct = false;
+        if ($filter || $distinct) {
             $cols = array();
-         
+            //echo '<PRE>' ;print_r($filter);exit;
             foreach($xx as $c) {
-                if (in_array($c, $filter)) {
+                if ($distinct && $distinct == $c) {
+                    $has_distinct = 'DISTINCT( ' . $do->tableName() .'.'. $c .') as ' . $c;
+                    $this->countWhat =  'DISTINCT  ' . $do->tableName() .'.'. $c .'';
+                    continue;
+                }
+                if (!$filter || in_array($c, $filter)) {
                     $cols[] = $c;
                 }
             }
-            $do->selectAs($cols);
-        } else {
-            $do->selectAs($xx);
-        }
-        
+            
+            
+            $selectAs = empty($cols) ?  array() : array(array(  $cols , '%s', false)) ;
+            
+            
+            
+        } 
         
         $this->cols = array();
+        $this->colsJoinName =array();
         foreach($xx as $k) {
             $this->cols[$k] = $do->tableName(). '.' . $k;
         }
@@ -709,29 +708,51 @@ class Pman_Roo extends Pman
             $xx = array_keys($tabdef);
             
             
-            if ($filter) {
+            if ($filter || $distinct) {
                 $cols = array();
                 foreach($xx as $c) {
-                    
                     $tn = sprintf($ocl.'_%s', $c);
-                    if (in_array($tn, $filter)) {
+                  // echo '<PRE>'; var_dump($tn);
+                    if ($distinct && $tn == $distinct) {
+                        $has_distinct = 'DISTINCT( ' . 'join_'.$ocl.'_'.$col.'.'.$k .')  as ' . $tn ;
+                        $this->countWhat =  'DISTINCT  join_'.$ocl.'_'.$col.'.'.$k;
+                        continue;
+                    }
+                    
+                    
+                    if (!$filter || in_array($tn, $filter)) {
                         $cols[] = $c;
                     }
                 }
-                $do->selectAs($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
+                if (!empty($cols)) {
+                     $selectAs[] = array($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
+                }
+               
+                
             } else {
-                $do->selectAs($xx,  $ocl.'_%s', 'join_'.$ocl.'_'. $col);
+                $selectAs[] = array($xx, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
+                
             }
              
             
             foreach($xx as $k) {
                 $this->cols[sprintf($ocl.'_%s', $k)] = $tab.'.'.$k;
+                $this->colsJname[sprintf($ocl.'_%s', $k)] = 'join_'.$ocl.'_'.$col.'.'.$k;
             }
             
             
         }
-        //DB_DataObject::debugLevel(1);
         
+        if ($has_distinct) {
+            $do->selectAdd($has_distinct);
+        }
+        //DB_DataObject::debugLevel(1);
+        // we do select as after everything else as we need to plop distinct at the beginning??
+        /// well I assume..
+       // echo '<PRE>';print_r($this->colsJname);exit;
+        foreach($selectAs as $ar) {
+            $do->selectAs($ar[0], $ar[1], $ar[2]);
+        }
         
         
     }
@@ -793,6 +814,7 @@ class Pman_Roo extends Pman
         
         foreach($q as $key=>$val) {
             
+            // value is an array..
             if (is_array($val) ) {
                 
                 if (!in_array( $key,  array_keys($this->cols))) {
@@ -807,6 +829,8 @@ class Pman_Roo extends Pman
                         $ar = array();
                         break;
                     }
+                    // FIXME: note this is not typesafe for anything other than mysql..
+                    
                     if (!is_numeric($v) || !is_long($v)) {
                         $quote = true;
                     }
@@ -814,7 +838,13 @@ class Pman_Roo extends Pman
                     
                 }
                 if (count($ar)) {
-                    $x->whereAddIn($x->tableName(). '.'.$key,$ar, $quote ? 'string' : 'int');
+                    
+                    
+                    $x->whereAddIn(
+                        isset($this->colsJname[$key]) ? 
+                            $this->colsJname[$key] :
+                            ($x->tableName(). '.'.$key),
+                        $ar, $quote ? 'string' : 'int');
                 }
                 
                 continue;
@@ -823,8 +853,13 @@ class Pman_Roo extends Pman
             
             
             if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
-                    
-                $x->whereAdd( $x->tableName() .'.' .substr($key, 1) . ' != ' .
+                
+                $key  = substr($key, 1) ;
+                
+                $x->whereAdd(   (
+                        isset($this->colsJname[$key]) ? 
+                            $this->colsJname[$key] :
+                            $x->tableName(). '.'.$key ) . ' != ' .
                     (is_numeric($val) ? $val : "'".  $x->escape($val) . "'")
                 );
                 continue;
@@ -835,7 +870,7 @@ class Pman_Roo extends Pman
             
             switch($key) {
                     
-                // Events and remarks
+                // Events and remarks -- fixme - move to events/remarsk...
                 case 'on_id':  // where TF is this used...
                     if (!empty($q['query']['original'])) {
                       //  DB_DataObject::debugLevel(1);
@@ -855,14 +890,31 @@ class Pman_Roo extends Pman
                         $q_filtered[$key] = $val;
                     }
                     
+                    // subjoined columns = check the values.
+                    // note this is not typesafe for anything other than mysql..
+                    
+                    if (isset($this->colsJname[$key])) {
+                        $quote = false;
+                        if (!is_numeric($val) || !is_long($val)) {
+                            $quote = true;
+                        }
+                        $x->whereAdd( "{$this->colsJname[$key]} = " . ($quote ? "'". $x->escape($val) ."'" : $val));
+                        
+                    }
+                    
+                    
                     continue;
             }
         }
+        
         $x->setFrom($q_filtered);
+        
+        
+        
        
         // nice generic -- let's get rid of it.. where is it used!!!!
         // used by: 
-        // Person / Group
+        // Person / Group / most of my queries noww...
         if (!empty($q['query']['name'])) {
             $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
         }