From 48f15fe6f5822040f2b4e04c59aef502e153192b Mon Sep 17 00:00:00 2001
From: mcramer <m.cramer@pixcept.de>
Date: Tue, 09 Jul 2013 04:18:40 -0400
Subject: [PATCH] - Implemented: Virtual columns (columns that are not in the db but shown in the list) can now be sorted if defined correctly

---
 interface/web/mail/templates/user_quota_stats_list.htm   |    9 +++-
 interface/web/mail/list/user_quota_stats.list.php        |    2 +
 interface/lib/classes/listform_actions.inc.php           |   72 +++++++++++++++++++++++++++--------
 interface/web/mail/user_quota_stats.php                  |    7 +++
 interface/web/mail/lib/lang/en_user_quota_stats_list.lng |    1 
 interface/web/mail/lib/lang/de_user_quota_stats_list.lng |    1 
 6 files changed, 71 insertions(+), 21 deletions(-)

diff --git a/interface/lib/classes/listform_actions.inc.php b/interface/lib/classes/listform_actions.inc.php
index 39225df..acb0b96 100644
--- a/interface/lib/classes/listform_actions.inc.php
+++ b/interface/lib/classes/listform_actions.inc.php
@@ -36,7 +36,27 @@
 	public $SQLExtWhere = '';
 	public $SQLOrderBy = '';
 	public $SQLExtSelect = '';
-	
+	private $sortKeys;
+    
+    private function _sort($aOne, $aTwo) {
+        if(!is_array($aOne) || !is_array($aTwo)) return 0;
+        
+        if(!is_array($this->sortKeys)) $this->sortKeys = array($this->sortKeys);
+        foreach($this->sortKeys as $sKey => $sDir) {
+            if(is_numeric($sKey)) {
+                $sKey = $sDir;
+                $sDir = 'ASC';
+            }
+            $a = $aOne[$sKey];
+            $b = $aTwo[$sKey];
+            if(is_string($a)) $a = strtolower($a);
+            if(is_string($b)) $b = strtolower($b);
+            if($a < $b) return ($sDir == 'DESC' ? 1 : -1);
+            elseif($a > $b) return ($sDir == 'DESC' ? -1 : 1);
+        }
+        return 0;
+    }
+    
 	public function onLoad()
     {
 		global $app, $conf, $list_def_file;
@@ -62,36 +82,43 @@
 		if(!isset($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
 		  $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = '';
 		}
-
+        
+        $php_sort = false;
+        
 		if(!empty($_GET['orderby'])){
 		  $order = str_replace('tbl_col_','',$_GET['orderby']);
 		  
 		  //* Check the css class submited value
 		  if (preg_match("/^[a-z\_]{1,}$/",$order)) {
-		  
-		    // prepend correct table
-			$prepend_table = $app->listform->listDef['table'];
-			if(trim($app->listform->listDef['additional_tables']) != '' && is_array($app->listform->listDef['item']) && count($app->listform->listDef['item']) > 0) {
-				foreach($app->listform->listDef['item'] as $field) {
-					if($field['field'] == $order && $field['table'] != ''){
-						$prepend_table = $field['table'];
-						break;
-					}
-				}
-		    }
-			$order = $prepend_table.'.'.$order;
+            
+            if(isset($app->listform->listDef['phpsort']) && is_array($app->listform->listDef['phpsort']) && in_array($order, $app->listform->listDef['phpsort'])) {
+                $php_sort = true;
+            } else {
+                // prepend correct table
+                $prepend_table = $app->listform->listDef['table'];
+                if(trim($app->listform->listDef['additional_tables']) != '' && is_array($app->listform->listDef['item']) && count($app->listform->listDef['item']) > 0) {
+                    foreach($app->listform->listDef['item'] as $field) {
+                        if($field['field'] == $order && $field['table'] != ''){
+                            $prepend_table = $field['table'];
+                            break;
+                        }
+                    }
+                }
+                $order = $prepend_table.'.'.$order;
+            }
 			
 		    if($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] == $order){
 				$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = $order.' DESC';
 		    } else {
 				$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = $order;
 		    }
+            $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php'] = $php_sort;
 		  }
 		}
 
 		// If a manuel oder by like customers isset the sorting will be infront
-		if(!empty($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
-		  if(empty($this->SQLOrderBy)){
+		if(!empty($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order']) && !$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php']){
+          if(empty($this->SQLOrderBy)){
 		    $this->SQLOrderBy = "ORDER BY ".$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'];
 		  } else {
 		    $this->SQLOrderBy = str_replace("ORDER BY ","ORDER BY ".$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'].', ',$this->SQLOrderBy);
@@ -109,7 +136,18 @@
 				$records_new[] = $this->prepareDataRow($rec);
 			}
 		}
-
+        
+        if(!empty($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order']) && $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php']) {
+            $order_by = $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'];
+            $order_dir = 'ASC';
+            if(substr($order_by, -5) === ' DESC') {
+                $order_by = substr($order_by, 0, -5);
+                $order_dir = 'DESC';
+            }
+            $this->sortKeys = array($order_by => $order_dir);
+            uasort($records_new, array($this, '_sort'));
+        }
+        
 		$app->tpl->setLoop('records',$records_new);
 
 		$this->onShow();
diff --git a/interface/web/mail/lib/lang/de_user_quota_stats_list.lng b/interface/web/mail/lib/lang/de_user_quota_stats_list.lng
index 9a23a97..beb9c18 100755
--- a/interface/web/mail/lib/lang/de_user_quota_stats_list.lng
+++ b/interface/web/mail/lib/lang/de_user_quota_stats_list.lng
@@ -4,4 +4,5 @@
 $wb['name_txt'] = 'Name';
 $wb['email_txt'] = 'E-Mail Adresse';
 $wb['used_txt'] = 'Verbrauchter Speicherplatz';
+$wb['percentage_txt'] = 'Verbraucht in %';
 ?>
diff --git a/interface/web/mail/lib/lang/en_user_quota_stats_list.lng b/interface/web/mail/lib/lang/en_user_quota_stats_list.lng
index 2a493ce..1804193 100755
--- a/interface/web/mail/lib/lang/en_user_quota_stats_list.lng
+++ b/interface/web/mail/lib/lang/en_user_quota_stats_list.lng
@@ -4,4 +4,5 @@
 $wb["name_txt"] = 'Name';
 $wb["email_txt"] = 'Email Address';
 $wb["used_txt"] = 'Used space';
+$wb["percentage_txt"] = 'Used %';
 ?>
diff --git a/interface/web/mail/list/user_quota_stats.list.php b/interface/web/mail/list/user_quota_stats.list.php
index 1123b93..5787556 100644
--- a/interface/web/mail/list/user_quota_stats.list.php
+++ b/interface/web/mail/list/user_quota_stats.list.php
@@ -40,6 +40,8 @@
 // Enable auth
 $liste["auth"]				= "yes";
 
+// mark columns for php sorting (no real mySQL columns)
+$liste["phpsort"] = array('used_sort', 'percentage_sort');
 
 /*****************************************************
 * Suchfelder
diff --git a/interface/web/mail/templates/user_quota_stats_list.htm b/interface/web/mail/templates/user_quota_stats_list.htm
index fdf7541..c2a5397 100644
--- a/interface/web/mail/templates/user_quota_stats_list.htm
+++ b/interface/web/mail/templates/user_quota_stats_list.htm
@@ -9,8 +9,9 @@
                     <tr class="caption">
                         <th class="tbl_col_email" scope="col"><tmpl_var name="email_txt"></th>
                         <th class="tbl_col_name" scope="col"><tmpl_var name="name_txt"></th>
-                        <th class="tbl_col_used tbl_col_nosort" scope="col"><tmpl_var name="used_txt"></th>
+                        <th class="tbl_col_used_sort" scope="col"><tmpl_var name="used_txt"></th>
                         <th class="tbl_col_quota" scope="col"><tmpl_var name="quota_txt"></th>
+                        <th class="tbl_col_percentage_sort" scope="col"><tmpl_var name="percentage_txt"></th>
                         <th class="tbl_col_limit" scope="col">{tmpl_var name='search_limit'}</th>
                     </tr>
                     <tr class="filter">
@@ -18,6 +19,7 @@
                         <td class="tbl_col_name"><input type="text" name="search_system_user" value="{tmpl_var name='search_name'}" /></td>
                         <td class="tbl_col_used">&nbsp;</td>
                         <td class="tbl_col_quota">&nbsp;</td>
+                        <td class="tbl_col_percentage">&nbsp;</td>
                         <td class="tbl_col_buttons">
                             <button type="button" class="button icons16 icoFilter" name="Filter" id="Filter" value="{tmpl_var name="filter_txt"}" onclick="submitForm('pageForm','mail/user_quota_stats.php');"><span>{tmpl_var name="filter_txt"}</span></button>
                         </td>
@@ -30,18 +32,19 @@
                             <td class="tbl_col_name"><a href="#" onclick="loadContent('mail/mail_user_edit.php?id={tmpl_var name='id'}');">{tmpl_var name="name"}</a></td>
                             <td class="tbl_col_used"><a href="#" onclick="loadContent('mail/mail_user_edit.php?id={tmpl_var name='id'}');">{tmpl_var name="used"}</a></td>
                             <td class="tbl_col_quota"><a href="#" onclick="loadContent('mail/mail_user_edit.php?id={tmpl_var name='id'}');">{tmpl_var name="quota"}</a></td>
+                            <td class="tbl_col_percentage"><a href="#" onclick="loadContent('mail/mail_user_edit.php?id={tmpl_var name='id'}');">{tmpl_var name="percentage"}</a></td>
                             <td class="tbl_col_buttons"></td>
                         </tr>
                     </tmpl_loop>
                     <tmpl_unless name="records">
                         <tr class="tbl_row_noresults tbl_row_<tmpl_if name='__EVEN__'}even<tmpl_else>uneven</tmpl_if>">
-                            <td colspan="5">{tmpl_var name='globalsearch_noresults_text_txt'}</td>
+                            <td colspan="6">{tmpl_var name='globalsearch_noresults_text_txt'}</td>
                         </tr>
                     </tmpl_unless>
                 </tbody>
                 <tfoot>
                     <tr>
-                        <td class="tbl_footer tbl_paging" colspan="5"><tmpl_var name="paging"></td>
+                        <td class="tbl_footer tbl_paging" colspan="6"><tmpl_var name="paging"></td>
                     </tr>
                 </tfoot>
             </table>
diff --git a/interface/web/mail/user_quota_stats.php b/interface/web/mail/user_quota_stats.php
index 4933103..dfc1d8a 100644
--- a/interface/web/mail/user_quota_stats.php
+++ b/interface/web/mail/user_quota_stats.php
@@ -53,16 +53,21 @@
 		
 		if($rec['quota'] == 0){
 			$rec['quota'] = $app->lng('unlimited');
+            $rec['percentage'] = '';
+            $rec['percentage_sort'] = 0;
 		} else {
+            $rec['percentage'] = round(100 * $rec['used'] / $rec['quota']) . '%';
+			$rec['percentage_sort'] = round(100 * $rec['used'] / $rec['quota']);
 			$rec['quota'] = round($rec['quota'] / 1048576,4).' MB';
 		}
 
 
+        $rec['used_sort'] = $rec['used'];
         if($rec['used'] < 1544000) {
             $rec['used'] = round($rec['used'] / 1024,4).' KB';
         } else {
             $rec['used'] = round($rec['used'] / 1048576,4).' MB';
-        }   
+        }
 
 		//* The variable "id" contains always the index variable
 		$rec['id'] = $rec[$this->idx_key];

--
Gitblit v1.9.1