Project

General

Profile

Feature #7037 » 7037-csv-bom-2.diff

Toshi MARUYAMA, 2015-04-02 11:02

View differences:

app/helpers/queries_helper.rb
162 162
  end
163 163

  
164 164
  def query_to_csv(items, query, options={})
165
    encoding = l(:general_csv_encoding)
165
    encoding = Setting.csv_utf8_bom? ? "UTF-8" : l(:general_csv_encoding)
166 166
    columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
167 167
    query.available_block_columns.each do |column|
168 168
      if options[column.name].present?
......
178 178
        csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) }
179 179
      end
180 180
    end
181
    export = "\xEF\xBB\xBF".force_encoding("UTF-8") + export if Setting.csv_utf8_bom?
181 182
    export
182 183
  end
183 184

  
app/helpers/timelog_helper.rb
90 90

  
91 91
  def report_to_csv(report)
92 92
    decimal_separator = l(:general_csv_decimal_separator)
93
    encoding = Setting.csv_utf8_bom? ? "UTF-8" : l(:general_csv_encoding)
93 94
    export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
94 95
      # Column headers
95 96
      headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
......
97 98
      headers << l(:label_total_time)
98 99
      csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
99 100
                                    c.to_s,
100
                                    l(:general_csv_encoding) ) }
101
                                    encoding)}
101 102
      # Content
102 103
      report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
103 104
      # Total row
104
      str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding))
105
      str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), encoding)
105 106
      row = [ str_total ] + [''] * (report.criteria.size - 1)
106 107
      total = 0
107 108
      report.periods.each do |period|
......
112 113
      row << ("%.2f" % total).gsub('.',decimal_separator)
113 114
      csv << row
114 115
    end
116
    export = "\xEF\xBB\xBF".force_encoding("UTF-8") + export if Setting.csv_utf8_bom?
115 117
    export
116 118
  end
117 119

  
118 120
  def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
119 121
    decimal_separator = l(:general_csv_decimal_separator)
122
    encoding = Setting.csv_utf8_bom? ? "UTF-8" : l(:general_csv_encoding)
120 123
    hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
121 124
      hours_for_value = select_hours(hours, criteria[level], value)
122 125
      next if hours_for_value.empty?
123 126
      row = [''] * level
124 127
      row << Redmine::CodesetUtil.from_utf8(
125 128
                        format_criteria_value(available_criteria[criteria[level]], value).to_s,
126
                        l(:general_csv_encoding) )
129
                        encoding)
127 130
      row += [''] * (criteria.length - level - 1)
128 131
      total = 0
129 132
      periods.each do |period|
......
137 140
        report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
138 141
      end
139 142
    end
143
    csv = "\xEF\xBB\xBF".force_encoding("UTF-8") + csv if Setting.csv_utf8_bom?
140 144
  end
141 145
end
app/views/settings/_general.html.erb
35 35
<p><%= setting_text_field :repositories_encodings, :size => 60 %>
36 36
<em class="info"><%= l(:text_comma_separated) %></em></p>
37 37

  
38
<p><%= setting_check_box :csv_utf8_bom %></p>
39

  
38 40
<%= call_hook(:view_settings_general_form) %>
39 41
</div>
40 42

  
config/locales/en.yml
416 416
  setting_link_copied_issue: Link issues on copy
417 417
  setting_max_additional_emails: Maximum number of additional email addresses
418 418
  setting_search_results_per_page: Search results per page
419
  setting_csv_utf8_bom: CSV in UTF-8 BOM (Byte Order Mark)
419 420

  
420 421
  permission_add_project: Create project
421 422
  permission_add_subprojects: Create subprojects
config/settings.yml
240 240
  default:
241 241
  - '6'
242 242
  - '7'
243
csv_utf8_bom:
244
  default: 1
(3-3/6)