1<?php
2# MantisBT - A PHP based bugtracking system
3
4# MantisBT is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 2 of the License, or
7# (at your option) any later version.
8#
9# MantisBT is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file implements CSV export functionality within MantisBT
19 * @package MantisBT
20 * @copyright Copyright 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
21 * @copyright Copyright 2002  MantisBT Team - mantisbt-dev@lists.sourceforge.net
22 * @link http://www.mantisbt.org
23 *
24 * @uses core.php
25 * @uses authentication_api.php
26 * @uses columns_api.php
27 * @uses constant_inc.php
28 * @uses csv_api.php
29 * @uses file_api.php
30 * @uses filter_api.php
31 * @uses helper_api.php
32 * @uses print_api.php
33 */
34
35# Prevent output of HTML in the content if errors occur
36define( 'DISABLE_INLINE_ERROR_REPORTING', true );
37
38require_once( 'core.php' );
39require_api( 'authentication_api.php' );
40require_api( 'columns_api.php' );
41require_api( 'constant_inc.php' );
42require_api( 'csv_api.php' );
43require_api( 'file_api.php' );
44require_api( 'filter_api.php' );
45require_api( 'helper_api.php' );
46require_api( 'print_api.php' );
47
48auth_ensure_user_authenticated();
49
50helper_begin_long_process();
51
52$t_nl = csv_get_newline();
53$t_sep = csv_get_separator();
54
55# Get current filter
56$t_filter = filter_get_bug_rows_filter();
57
58$t_filter_query = new BugFilterQuery( $t_filter );
59$t_filter_query->set_limit( EXPORT_BLOCK_SIZE );
60
61if( 0 == $t_filter_query->get_bug_count() ) {
62	print_header_redirect( 'view_all_set.php?type=0' );
63}
64
65# Get columns to be exported
66$t_columns = csv_get_columns();
67
68csv_start( csv_get_default_filename() );
69
70# export the titles
71$t_first_column = true;
72ob_start();
73$t_titles = array();
74foreach ( $t_columns as $t_column ) {
75	if( !$t_first_column ) {
76		echo $t_sep;
77	} else {
78		$t_first_column = false;
79	}
80
81	echo column_get_title( $t_column );
82}
83
84echo $t_nl;
85
86$t_header = ob_get_clean();
87
88# Fixed for a problem in Excel where it prompts error message "SYLK: File Format Is Not Valid"
89# See Microsoft Knowledge Base Article - 323626
90# http://support.microsoft.com/default.aspx?scid=kb;en-us;323626&Product=xlw
91$t_first_three_chars = mb_substr( $t_header, 0, 3 );
92if( strcmp( $t_first_three_chars, 'ID' . $t_sep ) == 0 ) {
93	$t_header = str_replace( 'ID' . $t_sep, 'Id' . $t_sep, $t_header );
94}
95# end of fix
96
97echo $t_header;
98
99$t_end_of_results = false;
100$t_offset = 0;
101do {
102	# Clear cache for next block
103	bug_clear_cache_all();
104
105	# select a new block
106	$t_filter_query->set_offset( $t_offset );
107	$t_result = $t_filter_query->execute();
108	$t_offset += EXPORT_BLOCK_SIZE;
109
110	# Keep reading until reaching max block size or end of result set
111	$t_read_rows = array();
112	$t_count = 0;
113	$t_bug_id_array = array();
114	$t_unique_user_ids = array();
115	while( $t_count < EXPORT_BLOCK_SIZE ) {
116		$t_row = db_fetch_array( $t_result );
117		if( false === $t_row ) {
118			# a premature end indicates end of query results. Set flag as finished
119			$t_end_of_results = true;
120			break;
121		}
122		$t_bug_id_array[] = (int)$t_row['id'];
123		$t_read_rows[] = $t_row;
124		$t_count++;
125	}
126	# Max block size has been reached, or no more rows left to complete the block.
127	# Either way, process what we have
128
129	# convert and cache data
130	$t_rows = filter_cache_result( $t_read_rows, $t_bug_id_array );
131	bug_cache_columns_data( $t_rows, $t_columns );
132
133	# Clear arrays that are not needed
134	unset( $t_read_rows );
135	unset( $t_unique_user_ids );
136	unset( $t_bug_id_array );
137
138	# export the rows
139	foreach ( $t_rows as $t_row ) {
140		$t_first_column = true;
141
142		foreach ( $t_columns as $t_column ) {
143			if( !$t_first_column ) {
144				echo $t_sep;
145			} else {
146				$t_first_column = false;
147			}
148
149			$t_custom_field = column_get_custom_field_name( $t_column );
150			if( $t_custom_field !== null ) {
151				echo csv_format_custom_field( $t_row->id, $t_row->project_id, $t_custom_field );
152			} else if( column_is_plugin_column( $t_column ) ) {
153				echo csv_format_plugin_column_value( $t_column, $t_row );
154			} else {
155				$t_function = 'csv_format_' . $t_column;
156				if( function_exists( $t_function ) ) {
157					echo $t_function( $t_row );
158				} else {
159					# Field is unknown
160					echo '';
161				}
162			}
163		}
164
165		echo $t_nl;
166	}
167
168} while ( false === $t_end_of_results );
169
170