Bläddra i källkod

Fixing jqgrid queries

Julio Montoya 14 år sedan
förälder
incheckning
01c273e183
3 ändrade filer med 55 tillägg och 16 borttagningar
  1. 18 11
      main/inc/ajax/model.ajax.php
  2. 31 3
      main/inc/lib/database.lib.php
  3. 6 2
      main/inc/lib/model.lib.php

+ 18 - 11
main/inc/ajax/model.ajax.php

@@ -1,4 +1,7 @@
 <?php
+/* For licensing terms, see /license.txt */
+
+//@todo this could be integrated in the inc/lib/model.lib.php
 $action = $_GET['a'];
 
 require_once '../global.inc.php';
@@ -11,7 +14,7 @@ $table = '';
 
 $page  = intval($_REQUEST['page']); //page
 $limit = intval($_REQUEST['rows']); // quantity of rows
-$sidx  = intval($_REQUEST['sidx']); //index to filter         
+$sidx  = $_REQUEST['sidx']; //index to filter         
 $sord  = $_REQUEST['sord'];         //asc or desc
 if (!in_array($sord, array('asc','desc'))) {
     $sord = 'desc';
@@ -37,10 +40,11 @@ switch ($action) {
         exit;   
 }
         
-if( $count >0 ) { 
-    $total_pages = ceil($count/$limit); 
-} else { 
-    $total_pages = 0; 
+$total_pages = 0;
+if ($count >0) { 
+    if (!empty($limit)) {
+        $total_pages = ceil($count/$limit);
+    }
 }
 
 if ($page > $total_pages) { 
@@ -55,24 +59,27 @@ switch ($action) {
         if ($_REQUEST['oper'] == 'del') {
             $obj->delete($_REQUEST['id']);
         }
-        $columns = array('name', 'description', 'actions');        
+        $columns = array('name', 'description', 'actions');                
+        if(!in_array($sidx, $columns)) {
+        	$sidx = 'name';
+        }
         $result     = Database::select('*', $obj->table, array('order'=>"$sidx $sord", 'LIMIT'=> "$start , $limit"));
     break;
     case 'get_promotions':
         if ($_REQUEST['oper'] == 'del') {
         	$obj->delete($_REQUEST['id']);
         }
-        $columns = array('name', 'career', 'description', 'actions');                        
-        $result     = Database::select('p.id, p.name, p.description, c.name as career', "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c  ON c.id = p.career_id ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit"));       
+        $columns = array('name', 'career', 'description', 'actions');
+        if(!in_array($sidx, $columns)) {
+            $sidx = 'name';
+        }                  
+        $result     = Database::select('p.id,p.name, p.description, c.name as career', "$obj->table p LEFT JOIN ".Database::get_main_table(TABLE_CAREER)." c  ON c.id = p.career_id ", array('order' =>"$sidx $sord", 'LIMIT'=> "$start , $limit"));       
     break;
     default:
         exit;            
 }
-
-
 //echo '<pre>';
 
-
 if (in_array($action, array('get_careers','get_promotions'))) {
     //3. Creating an obj to return a json
     $responce = new stdClass();           

+ 31 - 3
main/inc/lib/database.lib.php

@@ -1349,7 +1349,7 @@ class Database {
         }
         
              
-        $sql    = "SELECT $clean_columns FROM $table_name $conditions";
+         $sql    = "SELECT $clean_columns FROM $table_name $conditions";
       
                
         
@@ -1380,7 +1380,8 @@ class Database {
         	return '';
         }        
         $return_value = '';     
-        foreach ($conditions as $type_condition => $condition_data) {            
+        foreach ($conditions as $type_condition => $condition_data) {    
+            $type_condition = strtolower($type_condition);        
              switch($type_condition) {
                 case 'where':                    
                     foreach ($condition_data as $condition => $value_array) {                     
@@ -1405,7 +1406,34 @@ class Database {
                     }
                 break;
                 case 'order':
-                    $return_value .= " ORDER BY $condition_data";
+                    $order_array = explode(' ', $condition_data);
+                    
+                    if (!empty($order_array)) {
+                        if (count($order_array) > 1) {
+                            $order_array[0] = self::escape_string($order_array[0]);
+                            if (!empty($order_array[1])) {
+                                $order_array[1] = strtolower($order_array[1]);
+                                $order = 'desc';
+                            	if (in_array($order_array[1], array('desc', 'asc'))) {
+                            		$order = $order_array[1];
+                            	}
+                            }
+                            $return_value .= ' ORDER BY '.$order_array[0].'  '.$order;
+                        }  else {
+                            $return_value .= ' ORDER BY '.$order_array[0].' DESC ';
+                        }
+                    }
+                break;
+                
+                case 'limit':
+                    $limit_array = explode(',', $condition_data);                    
+                    if (!empty($limit_array)) {
+                        if (count($limit_array) > 1) {
+                            $return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]);
+                        }  else {
+                            $return_value .= ' LIMIT '.intval($limit_array[0]);
+                        }
+                    }
                 break;
                 
             } 

+ 6 - 2
main/inc/lib/model.lib.php

@@ -13,8 +13,13 @@ class Model {
     var $columns;
     
 	public function __construct() {
+        
 	}
     
+    public function find() {
+    	
+    }
+    
     
     /**
      * Delets an item
@@ -76,8 +81,7 @@ class Model {
      * @return unknown
      *
      */
-	function javascript()
-	{
+	function javascript() {
 		
 	}