From 76736dfab279f6357774f0a4c4776a7e06f6f5c0 Mon Sep 17 00:00:00 2001
From: petertc <petertc.chu@gmail.com>
Date: Mon, 4 Oct 2010 14:44:31 +0800
Subject: [PATCH] add ability of "sort by bugnotes count"

---
 core/columns_api.php |    5 ++++-
 core/filter_api.php  |   21 +++++++++++++++++----
 core/string_api.php  |    1 +
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/core/columns_api.php b/core/columns_api.php
index da0f19c..85d00cf 100644
--- a/core/columns_api.php
+++ b/core/columns_api.php
@@ -773,7 +773,10 @@ function print_column_title_summary( $p_sort, $p_dir, $p_columns_target = COLUMN
  * @access public
  */
 function print_column_title_bugnotes_count( $p_sort, $p_dir, $p_columns_target = COLUMNS_TARGET_VIEW_PAGE ) {
-	echo '<td> # </td>';
+	echo '<td>';
+        print_view_bug_sort_link( '#', 'bugnotes_count', $p_sort, $p_dir, $p_columns_target );
+        print_sort_icon( $p_dir, $p_sort, 'bugnotes_count' );	
+	echo '</td>';
 }
 
 /**
diff --git a/core/filter_api.php b/core/filter_api.php
index 6630094..2b51e16 100644
--- a/core/filter_api.php
+++ b/core/filter_api.php
@@ -597,7 +597,7 @@ function filter_ensure_valid_filter( $p_filter_arr ) {
 	$t_fields = helper_get_columns_to_view();
 	$t_n_fields = count( $t_fields );
 	for( $i = 0;$i < $t_n_fields;$i++ ) {
-		if( isset( $t_fields[$i] ) && in_array( $t_fields[$i], array( 'selection', 'edit', 'bugnotes_count', 'attachment' ) ) ) {
+		if( isset( $t_fields[$i] ) && in_array( $t_fields[$i], array( 'selection', 'edit', 'attachment' ) ) ) {
 			unset( $t_fields[$i] );
 		}
 	}
@@ -886,6 +886,7 @@ function filter_get_field( $p_filter_id, $p_field_name ) {
 function filter_get_query_sort_data( &$p_filter, $p_show_sticky, $p_query_clauses ) {
 	$t_bug_table = db_get_table( 'mantis_bug_table' );
 	$t_custom_field_string_table = db_get_table( 'mantis_custom_field_string_table' );
+	$t_bugnote_table = db_get_table( 'mantis_bugnote_table' );
 
 	# if sort is blank then default the sort and direction.  This is to fix the
 	# symptoms of #3953.  Note that even if the main problem is fixed, we may
@@ -896,6 +897,7 @@ function filter_get_query_sort_data( &$p_filter, $p_show_sticky, $p_query_clause
 	}
 
 	$p_query_clauses['order'] = array();
+	$p_query_clauses['group'] = array();
 	$t_sort_fields = explode( ',', $p_filter[FILTER_PROPERTY_SORT_FIELD_NAME] );
 	$t_dir_fields = explode( ',', $p_filter[FILTER_PROPERTY_SORT_DIRECTION] );
 
@@ -946,12 +948,22 @@ function filter_get_query_sort_data( &$p_filter, $p_show_sticky, $p_query_clause
 					}
 				}
 
+
+			# if sorting by bugnote_count
+			} else if ( $c_sort == 'bugnotes_count' ){
+				$p_query_clauses['join'][] = "LEFT JOIN $t_bugnote_table ON $t_bug_table.id =  $t_bugnote_table.bug_id";
+				$p_query_clauses['group'][] = "$t_bug_table.id";
+				$p_query_clauses['order'][] = "COUNT($t_bugnote_table.id) $c_dir";
+				 
 			# standard column
 			} else {
 				if ( 'last_updated' == $c_sort ) {
 					$c_sort = "last_updated";
 				}
-				$p_query_clauses['order'][] = "$t_bug_table.$c_sort $c_dir";
+				
+				if ( 'bugnotes_count' != $c_sort){												
+					$p_query_clauses['order'][] = "$t_bug_table.$c_sort $c_dir";
+				}
 			}
 		}
 	}
@@ -2001,9 +2013,10 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p
 	$t_select_string = "SELECT DISTINCT " . implode( ', ', $t_query_clauses['select'] );
 	$t_from_string = " FROM " . implode( ', ', $t_query_clauses['from'] );
 	$t_order_string = " ORDER BY " . implode( ', ', $t_query_clauses['order'] );
+	$t_group_string = count( $t_query_clauses['group'] ) > 0 ? " GROUP BY " . implode( ', ', $t_query_clauses['group'] ) : '';
 	$t_join_string = count( $t_query_clauses['join'] ) > 0 ? implode( ' ', $t_query_clauses['join'] ) : '';
 	$t_where_string = count( $t_query_clauses['where'] ) > 0 ? 'WHERE ' . implode( ' AND ', $t_query_clauses['where'] ) : '';
-	$t_result = db_query_bound( "$t_select_string $t_from_string $t_join_string $t_where_string $t_order_string", $t_query_clauses['where_values'], $p_per_page, $t_offset );
+	$t_result = db_query_bound( "$t_select_string $t_from_string $t_join_string $t_where_string $t_group_string $t_order_string", $t_query_clauses['where_values'], $p_per_page, $t_offset );
 	$t_row_count = db_num_rows( $t_result );
 
 	$t_id_array_lastmod = array();
@@ -4055,7 +4068,7 @@ function print_filter_show_sort() {
 	$t_n_fields = count( $t_fields );
 	$t_shown_fields[''] = '';
 	for( $i = 0;$i < $t_n_fields;$i++ ) {
-		if( !in_array( $t_fields[$i], array( 'selection', 'edit', 'bugnotes_count', 'attachment' ) ) ) {
+		if( !in_array( $t_fields[$i], array( 'selection', 'edit', 'attachment' ) ) ) {
 			if( strpos( $t_fields[$i], 'custom_' ) === 0 ) {
 				$t_field_name = string_display( lang_get_defaulted( utf8_substr( $t_fields[$i], utf8_strlen( 'custom_' ) ) ) );
 			} else {
diff --git a/core/string_api.php b/core/string_api.php
index fae7194..2ffe9d6 100644
--- a/core/string_api.php
+++ b/core/string_api.php
@@ -843,6 +843,7 @@ function string_get_field_name( $p_string ) {
 		'project_id' => 'email_project',
 		'reporter_id' => 'reporter',
 		'view_state' => 'view_status',
+		'bugnotes_count' => 'bugnote'
 	);
 
 	$t_string = $p_string;
-- 
1.7.0.4

