Sorting project versions by php function version_compare()

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
Tomacs
Posts: 1
Joined: 04 Mar 2009, 12:01

Sorting project versions by php function version_compare()

Post by Tomacs »

I wrote a "patch" for Mantis 1.1.6 which sorts project versions by the php function version_compare() to get a better ordered list of versions. Maybe it is useful for someone.

Replace the whole function version_get_all_rows() in core/version_api.php with the following code:

Code: Select all

	#===================================
	# Data Access
	#===================================

	# --------------------
	# Return all versions for the specified project
	
	function version_cmp( $a, $b )
	{
	    if ( $a == $b ) {
	        return 0;
	    }
	    return ( version_compare( $a, $b, '>') ) ? -1 : 1;
	}
	
	function version_get_all_rows( $p_project_id, $p_released = null ) {
		$c_project_id = db_prepare_int( $p_project_id );

		if ( $p_released === null ) {
			$t_released_where = '';
		} else {
			$c_released = db_prepare_int( $p_released );
			$t_released_where = "AND ( released = $c_released )";
		}

		$t_project_version_table = config_get( 'mantis_project_version_table' );

		$query = "SELECT *
				  FROM $t_project_version_table
				  WHERE project_id='$c_project_id' $t_released_where
				  ORDER BY date_order DESC";
		$result = db_query( $query );
		$count = db_num_rows( $result );
		$rows = array();
		for ( $i = 0 ; $i < $count ; $i++ ) {
			$row = db_fetch_array( $result );
			$row['date_order'] = db_unixtimestamp( $row['date_order'] );
			$rows[] = $row;
		}
				
		$temp_data = array();
		$temp_versions = array();		
		for( $i = 0; $i < sizeof( $rows ); $i++ ) {
			$temp_versions[] = $rows[$i]['version'] ;
			$temp_data[ $rows[$i]['version'] ] = $rows[$i];			
		}
		usort( $temp_versions, 'version_cmp' );
		$rows = array();
		for( $i = 0; $i < sizeof( $temp_versions ); $i++ ) {
			$rows[] = $temp_data[ $temp_versions[$i] ];		
		}
				
		return $rows;
	}
giallu
Posts: 20
Joined: 06 Feb 2008, 11:34

Re: Sorting project versions by php function version_compare()

Post by giallu »

I think it would be useful if you submit a patch with this to our bug tracker (http://mantisbt.org/bugs), even better if done against the current git code instead of 1.1.6.
Post Reply