Project

General

Profile

Feature #4742 » 0001-Make-CSV-export-work-like-PDF.patch

Makes CSV export work like PDF export. - Irfan Adilovic, 2011-05-26 18:13

View differences:

app/controllers/issues_controller.rb
91 91
        format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
92 92
        format.api
93 93
        format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
94
        format.csv  { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
94
        format.csv  { send_data(issues_to_csv(@issues, @project, @query), :type => 'text/csv; header=present', :filename => 'export.csv') }
95 95
        format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
96 96
      end
97 97
    else
app/helpers/issues_helper.rb
263 263
    end
264 264
  end
265 265

  
266
  def issues_to_csv(issues, project = nil)
266
  def issues_to_csv(issues, project = nil, query = nil)
267 267
    ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
268 268
    decimal_separator = l(:general_csv_decimal_separator)
269 269
    export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
270 270
      # csv header fields
271
      headers = [ "#",
272
                  l(:field_status),
273
                  l(:field_project),
274
                  l(:field_tracker),
275
                  l(:field_priority),
276
                  l(:field_subject),
277
                  l(:field_assigned_to),
278
                  l(:field_category),
279
                  l(:field_fixed_version),
280
                  l(:field_author),
281
                  l(:field_start_date),
282
                  l(:field_due_date),
283
                  l(:field_done_ratio),
284
                  l(:field_estimated_hours),
285
                  l(:field_parent_issue),
286
                  l(:field_created_on),
287
                  l(:field_updated_on)
288
                  ]
289
      # Export project custom fields if project is given
290
      # otherwise export custom fields marked as "For all projects"
291
      custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
292
      custom_fields.each {|f| headers << f.name}
293
      # Description in the last column
294
      headers << l(:field_description)
295
      csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
271
      csv << [ "#" ] + query.columns.collect {|c| begin; ic.iconv(c.caption.to_s); rescue; c.to_s; end }
296 272
      # csv lines
297 273
      issues.each do |issue|
298
        fields = [issue.id,
299
                  issue.status.name,
300
                  issue.project.name,
301
                  issue.tracker.name,
302
                  issue.priority.name,
303
                  issue.subject,
304
                  issue.assigned_to,
305
                  issue.category,
306
                  issue.fixed_version,
307
                  issue.author.name,
308
                  format_date(issue.start_date),
309
                  format_date(issue.due_date),
310
                  issue.done_ratio,
311
                  issue.estimated_hours.to_s.gsub('.', decimal_separator),
312
                  issue.parent_id,
313
                  format_time(issue.created_on),
314
                  format_time(issue.updated_on)
315
                  ]
316
        custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
317
        fields << issue.description
318
        csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
274
        col_values = query.columns.collect do |column|
275
          s = if column.is_a?(QueryCustomFieldColumn)
276
            cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
277
            show_value(cv)
278
          else
279
            value = issue.send(column.name)
280
            if value.is_a?(Date)
281
              format_date(value)
282
            elsif value.is_a?(Time)
283
              format_time(value)
284
            else
285
              value
286
            end
287
          end
288
          s.to_s
289
        end
290
        csv << [ issue.id.to_s ] + col_values.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
319 291
      end
320 292
    end
321 293
    export
(1-1/3)