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 * Excel (2003 SP2 and above) export page
19 *
20 * @package MantisBT
21 * @copyright Copyright 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
22 * @copyright Copyright 2002  MantisBT Team - mantisbt-dev@lists.sourceforge.net
23 * @link http://www.mantisbt.org
24 *
25 * @uses core.php
26 * @uses authentication_api.php
27 * @uses bug_api.php
28 * @uses columns_api.php
29 * @uses config_api.php
30 * @uses excel_api.php
31 * @uses file_api.php
32 * @uses filter_api.php
33 * @uses gpc_api.php
34 * @uses helper_api.php
35 * @uses print_api.php
36 * @uses utility_api.php
37 */
38
39# Prevent output of HTML in the content if errors occur
40define( 'DISABLE_INLINE_ERROR_REPORTING', true );
41
42require_once( 'core.php' );
43require_api( 'authentication_api.php' );
44require_api( 'bug_api.php' );
45require_api( 'columns_api.php' );
46require_api( 'config_api.php' );
47require_api( 'excel_api.php' );
48require_api( 'file_api.php' );
49require_api( 'filter_api.php' );
50require_api( 'gpc_api.php' );
51require_api( 'helper_api.php' );
52require_api( 'print_api.php' );
53require_api( 'utility_api.php' );
54
55auth_ensure_user_authenticated();
56
57$f_export = gpc_get_string( 'export', '' );
58
59helper_begin_long_process();
60
61$t_export_title = excel_get_default_filename();
62
63$t_short_date_format = config_get( 'short_date_format' );
64
65header( 'Content-Type: application/vnd.ms-excel; charset=UTF-8' );
66header( 'Pragma: public' );
67header( 'Content-Disposition: attachment; filename="' . urlencode( file_clean_name( $t_export_title ) ) . '.xml"' ) ;
68
69echo excel_get_header( $t_export_title );
70echo excel_get_titles_row();
71
72$f_bug_arr = explode( ',', $f_export );
73
74$t_columns = excel_get_columns();
75
76
77# Get current filter
78$t_filter = filter_get_bug_rows_filter();
79
80$t_filter_query = new BugFilterQuery( $t_filter );
81$t_filter_query->set_limit( EXPORT_BLOCK_SIZE );
82
83if( 0 == $t_filter_query->get_bug_count() ) {
84	print_header_redirect( 'view_all_set.php?type=0&print=1' );
85}
86
87$t_end_of_results = false;
88$t_offset = 0;
89do {
90	# Clear cache for next block
91	bug_clear_cache_all();
92
93	# select a new block
94	$t_filter_query->set_offset( $t_offset );
95	$t_result = $t_filter_query->execute();
96	$t_offset += EXPORT_BLOCK_SIZE;
97
98	# Keep reading until reaching max block size or end of result set
99	$t_read_rows = array();
100	$t_count = 0;
101	$t_bug_id_array = array();
102	$t_unique_user_ids = array();
103	while( $t_count < EXPORT_BLOCK_SIZE ) {
104		$t_row = db_fetch_array( $t_result );
105		if( false === $t_row ) {
106			# a premature end indicates end of query results. Set flag as finished
107			$t_end_of_results = true;
108			break;
109		}
110		# @TODO, the "export" bug list parameter functionality should be implemented in a more efficient way
111		if( is_blank( $f_export ) || in_array( $t_row['id'], $f_bug_arr ) ) {
112			$t_bug_id_array[] = (int)$t_row['id'];
113			$t_read_rows[] = $t_row;
114			$t_count++;
115		}
116	}
117	# Max block size has been reached, or no more rows left to complete the block.
118	# Either way, process what we have
119	if( 0 === $t_count && !$t_end_of_results ) {
120		continue;
121	}
122	if( 0 === $t_count && $t_end_of_results ) {
123		break;
124	}
125
126	# convert and cache data
127	$t_rows = filter_cache_result( $t_read_rows, $t_bug_id_array );
128	bug_cache_columns_data( $t_rows, $t_columns );
129
130	# Clear arrays that are not needed
131	unset( $t_read_rows );
132	unset( $t_unique_user_ids );
133	unset( $t_bug_id_array );
134
135	# export the rows
136	foreach ( $t_rows as $t_row ) {
137
138		echo excel_get_start_row();
139
140		foreach ( $t_columns as $t_column ) {
141			$t_custom_field = column_get_custom_field_name( $t_column );
142			if( $t_custom_field !== null ) {
143				echo excel_format_custom_field( $t_row->id, $t_row->project_id, $t_custom_field );
144			} else if( column_is_plugin_column( $t_column ) ) {
145				echo excel_format_plugin_column_value( $t_column, $t_row );
146			} else {
147				$t_function = 'excel_format_' . $t_column;
148				if( function_exists( $t_function ) ) {
149					echo $t_function( $t_row );
150				} else {
151					# field is unknown
152					echo '';
153				}
154			}
155		}
156
157		echo excel_get_end_row();
158	}
159
160} while ( false === $t_end_of_results );
161
162echo excel_get_footer();
163
164