CSV Export - custom fields

Get help from other users here.

Moderators: Developer, Contributor

Post Reply
sunq
Posts: 14
Joined: 12 Apr 2005, 06:19

CSV Export - custom fields

Post by sunq »

Hi,
I have a custom field of type date called requested_finished_date. Now I want it to be shown in the CSV export, what do I need to do???
/A
hgaland
Posts: 8
Joined: 30 May 2005, 09:12
Location: Switzerland

Post by hgaland »

I used the custom_function_inc.php to override the standard function custom_function_default_get_columns_to_view that is defined in columns_api.php. In fact I copied the code of the original function and inserted output of custom fields.

Starting with V 1.0.0a3, Insert in custom_function_inc.php the following code:

# --------------------
# returns an array of the column names to be displayed.
# The column names to use are those of the field names in the bug table.
# In addition, you can use the following:
# - "selection" for selection checkboxes.
# - "edit" for icon to open the edit page.
# - "custom_xxxx" were xxxx is the name of the custom field that is valid for the
# current project. In case of "All Projects, the field will be empty where it is
# not applicable.
function custom_function_override_get_columns_to_view( $p_columns_target = COLUMNS_TARGET_VIEW_PAGE ) {
$t_columns = array();

if ( $p_columns_target == COLUMNS_TARGET_CSV_PAGE ) {
$t_columns[] = 'id'; // localized: 'id',
$t_columns[] = 'project_id'; // 'email_project'
$t_columns[] = 'reporter_id'; // 'reporter'
$t_columns[] = 'handler_id'; // 'assigned_to'
$t_columns[] = 'priority'; // 'priority'
$t_columns[] = 'severity'; // 'severity'
$t_columns[] = 'reproducibility'; // 'reproducibility'
$t_columns[] = 'version'; // 'version'
$t_columns[] = 'projection'; // 'projection'
$t_columns[] = 'category'; // 'category'
$t_columns[] = 'date_submitted'; // 'date_submitted'
$t_columns[] = 'eta'; // 'eta'
$t_columns[] = 'os'; // 'os'
$t_columns[] = 'os_build'; // 'os_version'
$t_columns[] = 'platform'; // 'platform'
$t_columns[] = 'view_state'; // 'view_status'
$t_columns[] = 'last_updated'; // 'last_update'
$t_columns[] = 'summary'; // 'summary'
$t_columns[] = 'status'; // 'status'
$t_columns[] = 'resolution'; // 'resolution'
$t_columns[] = 'fixed_in_version'; // 'fixed_in_version';


# if viewing only one Project, Add all custom fields linked to this project
if ( helper_get_current_project() != ALL_PROJECTS ) {
$t_custom_fields = custom_field_get_ids(helper_get_current_project());

foreach( $t_custom_fields as $t_field_id ) {
$t_desc = custom_field_get_definition( $t_field_id );

$t_columns[] = 'custom_' . $t_desc['name'];
}
}


if ( OFF == config_get( 'enable_relationship' ) ) {
$t_columns[] = 'duplicate_id'; // 'duplicate_id'
}
} else {
$t_columns[] = 'selection';

if ( $p_columns_target == COLUMNS_TARGET_VIEW_PAGE ) {
$t_columns[] = 'edit';
}

$t_columns[] = 'priority';
$t_columns[] = 'id';

$t_enable_sponsorship = config_get( 'enable_sponsorship' );
if ( ON == $t_enable_sponsorship ) {
$t_columns[] = 'sponsorship';
}

$t_columns[] = 'bugnotes_count';

$t_show_attachments = config_get( 'show_attachment_indicator' );
if ( ON == $t_show_attachments ) {
$t_columns[] = 'attachment';
}

$t_columns[] = 'reporter_id';

$t_columns[] = 'category';
$t_columns[] = 'severity';
$t_columns[] = 'status';
$t_columns[] = 'last_updated';
$t_columns[] = 'summary';
}

return $t_columns;
}
teejay
Posts: 1
Joined: 22 May 2006, 23:24

bug with relationships off?

Post by teejay »

First off, thanks for that code chunk, it was a HUGE help! I have v1.0.3 and wasnt able to export the custom fields. After disabling relationships (didnt need them) I started getting an error in the export too.

I noticed if I comment out this line in your post...

Code: Select all

$t_columns[] = 'duplicate_id'; // 'duplicate_id'
...then it was working just fine.

I also found other posts (in my research for a solution) that claimed duplicate_id was no longer needed as it was an artifact from earlier development.

Just thought I would share in case someone else has the same difficulties
vboctor
Site Admin
Posts: 1293
Joined: 13 Feb 2005, 22:11
Location: Redmond, Washington
Contact:

Post by vboctor »

duplicate_id is only used if relationships are disabled. It is your only way to store the fact that one issue is a duplicate of another.

The way to do this customisation has changed in v1.1, and even if you use custom functions, duplicate_id will not cause an error (I think I fixed that!).

Regards,
Victor
MantisConnect
http://www.futureware.biz/mantisconnect/
RobW
Posts: 1
Joined: 07 Jun 2006, 12:51

Post by RobW »

First, this thread was very helpful.

I used this code and found that it gets ALL of the custom fields that you have in Mantis. To only get the fields assocaited with THIS project, replace:

Code: Select all

$t_custom_fields = custom_field_get_ids(helper_get_current_project());
with

Code: Select all

$t_custom_fields = custom_field_get_linked_ids(helper_get_current_project());
AJesse
Posts: 1
Joined: 03 Aug 2006, 12:28

Post by AJesse »

hgaland wrote:I used the custom_function_inc.php to override the standard function custom_function_default_get_columns_to_view that is defined in columns_api.php.
In Mantis 1.0.3 I had to name the file custom_functions_inc.php
hgaland wrote: # if viewing only one Project, Add all custom fields linked to this project
if ( helper_get_current_project() != ALL_PROJECTS ) {
$t_custom_fields = custom_field_get_ids(helper_get_current_project());

foreach( $t_custom_fields as $t_field_id ) {
$t_desc = custom_field_get_definition( $t_field_id );

$t_columns[] = 'custom_' . $t_desc['name'];
}
}
In Mantis 1.0.3 you must use:
$t_custom_fields = custom_field_get_linked_ids(helper_get_current_project());


For the rest: good job.

@developers: please consider this change for the next release

greetz
Alexander
ketan_dba
Posts: 2
Joined: 22 Nov 2005, 15:28

remove some column in csv export

Post by ketan_dba »

I have to remove following fields from csv export.

1) Reporter
2) Assigned_to
3) priority
4) Severity
5) Reporoducibility
6) product version
7) projection
8) ETA
9) os
10) osserver
11) plate form
12) view status
13) Resolution
14) fixed in version.

As per this thread, i have create custom_function_inc.php in /matis folder & public_html folder.
Commented all above fields but still in csv export all above fileds is coming.

I don't know php. Can some body will help me.

thanks in advanced.
javcor
Posts: 1
Joined: 11 Jul 2007, 11:42

Post by javcor »

I have probed this modification but I have a problem. When I want to export a lot of events, I have a timeot error like this.

Tipo de suceso: Error
Origen del suceso: W3SVC
Categoría del suceso: Ninguno
Id. suceso: 16
Fecha: 11/07/2007
Hora: 11:20:21
Usuario: No disponible
Equipo: xxxxx
Descripción:
La secuencia de comandos iniciada desde la dirección URL "/xxxx/csv_export.php" con los parámetros "" no ha respondido dentro del tiempo de espera configurado. El servidor HTTP va a abandonar la secuencia de comandos.
Para recibir información adicional referente a este mensaje, visite el sitio Web de soporte técnico de Microsoft que se encuentra en: http://www.microsoft.com/contentredirect.asp.

I'm changing some server parameters like IIS, PHP, MySQL but it doesn't work. Any idea?

Thank you.
vboctor
Site Admin
Posts: 1293
Joined: 13 Feb 2005, 22:11
Location: Redmond, Washington
Contact:

Post by vboctor »

I can't understand your post. But I had a look at the latest code and found that it calls helper_begin_long_process() which shouldn't make the script time out.

If in your version it is not called, then call it after auth_ensure_user_authenticated().
Migrate your MantisBT to the MantisHub Cloud
msuemnig
Posts: 1
Joined: 03 Jan 2008, 19:56

Re: CSV Export - custom fields

Post by msuemnig »

Great work! Thank you for the help and code!

Just a note...perhaps a silly one, but it took me a few minutes to figure it out. If you have custom fields, that apply to all projects, you can remove the 'if' statement that restricts it to just the specific projects.

I only export for all projects that are open, so, with the if statement, it's not helpful in my situation.

Hope that is helpful to someone
myselfhimself
Posts: 1
Joined: 08 Jan 2013, 13:36

Re: CSV Export - custom fields

Post by myselfhimself »

Hello !

Below is a custom code that exports all current project's custom fields, and interprets field named like "*charge*" (ie. workload in french) values in hours into days.
This way you can get CSV exports of your project's tickets and sum up the "*charge*" columns with a spreadsheet/Excel-like application.

This worked well for Mantis v1.0.0 rc1.

Code: Select all

<?php
# -------------------- 
# returns an array of the column names to be displayed. 
# The column names to use are those of the field names in the bug table. 
# In addition, you can use the following: 
# - "selection" for selection checkboxes. 
# - "edit" for icon to open the edit page. 
# - "custom_xxxx" were xxxx is the name of the custom field that is valid for the 
# current project. In case of "All Projects, the field will be empty where it is 
# not applicable. 

// @see http://www.mantisbt.org/forums/viewtopic.php?t=400
function custom_function_override_get_columns_to_view($p_columns_target = COLUMNS_TARGET_VIEW_PAGE)
{
    $t_columns = array();
    
    if ($p_columns_target == COLUMNS_TARGET_CSV_PAGE) {
        $t_columns[] = 'id'; // localized: 'id', 
        $t_columns[] = 'project_id'; // 'email_project' 
        $t_columns[] = 'reporter_id'; // 'reporter' 
        $t_columns[] = 'handler_id'; // 'assigned_to' 
        $t_columns[] = 'priority'; // 'priority' 
        $t_columns[] = 'severity'; // 'severity' 
        $t_columns[] = 'reproducibility'; // 'reproducibility' 
        $t_columns[] = 'version'; // 'version' 
        $t_columns[] = 'projection'; // 'projection' 
        $t_columns[] = 'category'; // 'category' 
        $t_columns[] = 'date_submitted'; // 'date_submitted' 
        $t_columns[] = 'eta'; // 'eta' 
        $t_columns[] = 'os'; // 'os' 
        $t_columns[] = 'os_build'; // 'os_version' 
        $t_columns[] = 'platform'; // 'platform' 
        $t_columns[] = 'view_state'; // 'view_status' 
        $t_columns[] = 'last_updated'; // 'last_update' 
        $t_columns[] = 'summary'; // 'summary' 
        $t_columns[] = 'status'; // 'status' 
        $t_columns[] = 'resolution'; // 'resolution' 
        $t_columns[] = 'fixed_in_version'; // 'fixed_in_version'; 
        
        
        # if viewing only one Project, Add all custom fields linked to this project
        if (helper_get_current_project() != ALL_PROJECTS) {
            $t_custom_fields = custom_field_get_linked_ids(helper_get_current_project());
            
            foreach ($t_custom_fields as $t_field_id) {
                $t_desc = custom_field_get_definition($t_field_id);
                
                $t_columns[] = 'custom_' . $t_desc['name'];
            }
        }
        
        
        if (OFF == config_get('enable_relationship')) {
            $t_columns[] = 'duplicate_id'; // 'duplicate_id' 
        }
    } else {
        $t_columns[] = 'selection';
        
        if ($p_columns_target == COLUMNS_TARGET_VIEW_PAGE) {
            $t_columns[] = 'edit';
        }
        
        $t_columns[] = 'priority';
        $t_columns[] = 'id';
        
        $t_enable_sponsorship = config_get('enable_sponsorship');
        if (ON == $t_enable_sponsorship) {
            $t_columns[] = 'sponsorship';
        }
        
        $t_columns[] = 'bugnotes_count';
        
        $t_show_attachments = config_get('show_attachment_indicator');
        if (ON == $t_show_attachments) {
            $t_columns[] = 'attachment';
        }
        
        $t_columns[] = 'reporter_id';
        
        $t_columns[] = 'category';
        $t_columns[] = 'severity';
        $t_columns[] = 'status';
        $t_columns[] = 'last_updated';
        $t_columns[] = 'summary';
    }
    
    return $t_columns;
}

# --------------------
# Print the value of the custom field (if the field is applicable to the project of
# the specified issue and the current user has read access to it.
# see custom_function_default_print_column_title() for rules about column names.
# $p_column: name of field to show in the column.
# $p_row: the row from the bug table that belongs to the issue that we should print the values for.
# $p_columns_target: see COLUMNS_TARGET_* in constant_inc.php

// @see "NOTE ..." comment within this function for actual overriding code
function custom_function_override_print_column_value( $p_column, $p_issue_row, $p_columns_target = COLUMNS_TARGET_VIEW_PAGE ) {
	if ( COLUMNS_TARGET_CSV_PAGE == $p_columns_target ) {
		$t_column_start = '';
		$t_column_end = '';
		$t_column_empty = '';
	} else {
		$t_column_start = '<td>';
		$t_column_end = '</td>';
		$t_column_empty = '&nbsp;';
	}
	
	

	if ( strpos( $p_column, 'custom_' ) === 0 ) {
		echo $t_column_start;
		$t_custom_field = substr( $p_column, 7 );

		$t_field_id = custom_field_get_id_from_name( $t_custom_field );
		if ( $t_field_id === false ) {
			echo '@', $t_custom_field, '@';
			if(stristr($p_column,'charge') !== FALSE)	echo 'THERE3'; //TODO
		} else {
			$t_issue_id = $p_issue_row['id'];
			$t_project_id = $p_issue_row['project_id'];

			if ( custom_field_is_linked( $t_field_id, $t_project_id ) ) {
				$t_def = custom_field_get_definition( $t_field_id );
				
				// NOTE: FIELD REWRITING HACK IS HERE
				//print_custom_field_value( $t_def, $t_field_id, $t_issue_id );
				$_val = string_custom_field_value( $t_def, $t_field_id, $t_issue_id );
				$_val_rewritten = rewrite_custom_field_value($p_column, $_val);
				echo empty($_val) ? $t_column_empty : $_val_rewritten;
			} else {
				// field is not linked to project
				echo $t_column_empty;
			}
		}
		echo $t_column_end;
	} else {
		if ( $p_columns_target != COLUMNS_TARGET_CSV_PAGE ) {
			$t_function = 'print_column_' . $p_column;
		} else {
			$t_function = 'csv_format_' . $p_column;
		}

		if ( function_exists( $t_function ) ) {
			if ( $p_columns_target != COLUMNS_TARGET_CSV_PAGE ) {
				$t_function( $p_issue_row, $p_columns_target );
			} else {
				$t_function( $p_issue_row[$p_column] );
			}
		} else {
			if ( isset( $p_issue_row[$p_column] ) ) {
				echo $t_column_start . $p_issue_row[$p_column] . $t_column_end;
			} else {
				echo $t_column_start . '@' . $p_column . '@' . $t_column_end;
			}
		}
	}
}
// Rewrite 'Charge' and 'Charge reelle' custom fields before CSV export.
function rewrite_custom_field_value($p_column,$value) {

    // Rewrite work load duration values into days without suffix.
	if(stristr($p_column,'charge') !== FALSE) {
	    $original_value = $value;
		
		// Convert decimals separator to . before number conversion
        $value = str_replace(',','.',$value);
		
		// Try to convert days or hours to days.
		$value = duration_string_days_float($value);
		if(!is_numeric($value)) {
		  $value = $original_value;
		}
        
		// Replace number . by \, for MS Excel badly reading CSV.
		$value = str_replace('.',',',$value);
	}
	
	return $value;
}

//Parse string in 'N hours' or 'N days' into a 0.01-precision number of 8-hours-work days
function duration_string_days_float($value) {

    // If is hours, convert to days.
	$value_hours = preg_replace('/(hours|hour|heures|heure|h)/i','', $value);
	if($value_hours != $value) {
	  $value_days = floatval($value_hours) / 8.0;
	// If is days, keep days as is.
	} else {
		$value_days = preg_replace('/(days|day|jours|jour|j|d)/i','', $value);
	}
	
	// Round at 0.01 precision.
	$value = round(floatval($value_days), 2);

	return $value;
}
sumathi
Posts: 14
Joined: 09 Jun 2015, 05:24

Re: CSV Export - custom fields

Post by sumathi »

Where is this code tell me in mantis folder or if it's ur own code where it should be added i have to add additional field which should be displayed CSV excel file



in one fourm it was said add this below coding from config_default_inc.php to config_inc.php but it doesn't worked same error came
$g_view_issues_page_columns = array ( 'selection', 'edit', 'priority', 'id', 'sponsorship_total', 'bugnotes_count',
'attachment', 'category','severity', 'status', 'last_updated', 'summary' );

$g_print_issues_page_columns = array ( 'selection', 'priority', 'id', 'sponsorship_total', 'bugnotes_count',
'attachment', 'category', 'severity', 'status', 'last_updated', 'summary');

$g_csv_columns = array ( 'id', 'project_id', 'reporter_id', 'handler_id', 'priority', 'reproducibility', 'category',
'date_submitted','view_state', 'last_updated', 'summary', 'status', 'resolution','cspl columns');

this is the Error i am getting in Excel file
<p style="color:red">APPLICATION WARNING #300: String "CSPLcolumns" not found.</p>

NOTE : i dont have this file custom_functions_inc.php
can any one help me
Post Reply