Project

General

Profile

Patch #4727 ยป csv-custom-query-fields.patch

Patch for 0.9.1 to export CSV files with custom query fields - Sean Myers, 2010-02-03 17:36

View differences:

app/helpers/issues_helper.rb (working copy)
141 141
    end
142 142
  end
143 143
  
144
  def issues_to_csv(issues, project = nil)
144
  def issues_to_csv(issues, project, query)
145 145
    ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')    
146 146
    decimal_separator = l(:general_csv_decimal_separator)
147

  
147 148
    export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
148
      # csv header fields
149
      headers = [ "#",
150
                  l(:field_status), 
151
                  l(:field_project),
152
                  l(:field_tracker),
153
                  l(:field_priority),
154
                  l(:field_subject),
155
                  l(:field_assigned_to),
156
                  l(:field_category),
157
                  l(:field_fixed_version),
158
                  l(:field_author),
159
                  l(:field_start_date),
160
                  l(:field_due_date),
161
                  l(:field_done_ratio),
162
                  l(:field_estimated_hours),
163
                  l(:field_created_on),
164
                  l(:field_updated_on)
165
                  ]
166
      # Export project custom fields if project is given
167
      # otherwise export custom fields marked as "For all projects"
168
      custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
169
      custom_fields.each {|f| headers << f.name}
170
      # Description in the last column
171
      headers << l(:field_description)
149
      headers = ["#"]
150
      query.columns.each do |column|
151
          headers << column.caption
152
      end
153

  
172 154
      csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
173 155
      # csv lines
174 156
      issues.each do |issue|
175
        fields = [issue.id,
176
                  issue.status.name, 
177
                  issue.project.name,
178
                  issue.tracker.name, 
179
                  issue.priority.name,
180
                  issue.subject,
181
                  issue.assigned_to,
182
                  issue.category,
183
                  issue.fixed_version,
184
                  issue.author.name,
185
                  format_date(issue.start_date),
186
                  format_date(issue.due_date),
187
                  issue.done_ratio,
188
                  issue.estimated_hours.to_s.gsub('.', decimal_separator),
189
                  format_time(issue.created_on),  
190
                  format_time(issue.updated_on)
191
                  ]
192
        custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
193
        fields << issue.description
157
        fields = [issue.id]
158
        query.columns.each do |column|
159
          s = if column.is_a?(QueryCustomFieldColumn)
160
            cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
161
            fields << show_value(cv)
162
          else
163
            value = issue.send(column.name)
164
            if value.is_a?(Date)
165
              format_date(value)
166
            elsif value.is_a?(Time)
167
              format_time(value)
168
            else
169
              value
170
            end
171
          end
172
          fields << value.to_s
173
        end
194 174
        csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
195 175
      end
196 176
    end
app/controllers/issues_controller.rb (working copy)
76 76
      respond_to do |format|
77 77
        format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
78 78
        format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
79
        format.csv  { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
79
        format.csv  { send_data(issues_to_csv(@issues, @project, @query), :type => 'text/csv; header=present', :filename => 'export.csv') }
80 80
        format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
81 81
      end
82 82
    else
    (1-1/1)