Project

General

Profile

Feature #26279 » select_encoding4.patch

Mizuki ISHIKAWA, 2018-04-05 08:16

View differences:

app/helpers/application_helper.rb
1487 1487
    encoding = l(:general_csv_encoding)
1488 1488
  end
1489 1489

  
1490
  def export_csv_encoding_select_tag
1491
    return if l(:general_csv_encoding).casecmp('UTF-8') == 0
1492
    options = [l(:general_csv_encoding), 'UTF-8']
1493
    content_tag(:p) do
1494
      concat(
1495
        content_tag(:label) do
1496
          concat l(:label_encoding)
1497
          concat select_tag('encoding', options_for_select(options, l(:general_csv_encoding)))
1498
        end
1499
      )
1500
    end
1501
  end
1502

  
1490 1503
  private
1491 1504

  
1492 1505
  def wiki_helper
app/helpers/queries_helper.rb
275 275
  def query_to_csv(items, query, options={})
276 276
    columns = query.columns
277 277

  
278
    Redmine::Export::CSV.generate do |csv|
278
    Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
279 279
      # csv header fields
280 280
      csv << columns.map {|c| c.caption.to_s}
281 281
      # csv lines
......
370 370

  
371 371
    tags
372 372
  end
373
 
373

  
374 374
  def query_hidden_sort_tag(query)
375 375
    hidden_field_tag("sort", query.sort_criteria.to_param, :id => nil)
376 376
  end
app/helpers/timelog_helper.rb
76 76
  end
77 77

  
78 78
  def report_to_csv(report)
79
    Redmine::Export::CSV.generate do |csv|
79
    Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
80 80
      # Column headers
81 81
      headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
82 82
      headers += report.periods
app/views/issues/index.html.erb
38 38
    <label><%= check_box_tag 'c[]', 'description', @query.has_column?(:description) %> <%= l(:field_description) %></label>
39 39
    <label><%= check_box_tag 'c[]', 'last_notes', @query.has_column?(:last_notes) %> <%= l(:label_last_notes) %></label>
40 40
  </p>
41
  <%= export_csv_encoding_select_tag %>
41 42
  <% if @issue_count > Setting.issues_export_limit.to_i %>
42 43
  <p class="icon icon-warning">
43 44
    <%= l(:setting_issues_export_limit) %>: <%= Setting.issues_export_limit.to_i %>
app/views/timelog/index.html.erb
31 31
    <label><%= radio_button_tag 'c[]', '', true %> <%= l(:description_selected_columns) %></label><br />
32 32
    <label><%= radio_button_tag 'c[]', 'all_inline' %> <%= l(:description_all_columns) %></label>
33 33
  </p>
34
  <%= export_csv_encoding_select_tag %>
34 35
  <p class="buttons">
35 36
    <%= submit_tag l(:button_export), :name => nil, :onclick => "hideModal(this);" %>
36 37
    <%= submit_tag l(:button_cancel), :name => nil, :onclick => "hideModal(this);", :type => 'button' %>
app/views/timelog/report.html.erb
1 1
<div class="contextual">
2
<%= link_to l(:button_log_time), 
2
<%= link_to l(:button_log_time),
3 3
            _new_time_entry_path(@project, @issue),
4 4
            :class => 'icon icon-time-add' if User.current.allowed_to?(:log_time, @project, :global => true) %>
5 5
</div>
......
24 24
                                                          :disabled => (@report.criteria.length >= 3),
25 25
                                                          :id => "criterias") %>
26 26
     <%= link_to l(:button_clear), {:params => request.query_parameters.merge(:criteria => nil)}, :class => 'icon icon-reload' %></p>
27
  <%= hidden_field_tag 'encoding', l(:general_csv_encoding) unless l(:general_csv_encoding).casecmp('UTF-8') == 0 %>
27 28
<% end %>
28 29

  
29 30
<% if @query.valid? %>
......
62 63
</div>
63 64

  
64 65
<% other_formats_links do |f| %>
65
  <%= f.link_to_with_query_parameters 'CSV' %>
66
  <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
66 67
<% end %>
67 68
<% end %>
69
<div id="csv-export-options" style="display: none;">
70
  <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
71
  <%= export_csv_encoding_select_tag %>
72
  <p class="buttons">
73
    <%= submit_tag l(:button_export), :name => nil, :id => 'csv-export-button' %>
74
    <%= submit_tag l(:button_cancel), :name => nil, :onclick => 'hideModal(this);', :type => 'button' %>
75
  </p>
76
</div>
68 77
<% end %>
69 78
<% end %>
70 79

  
......
74 83

  
75 84
<% html_title(@query.new_record? ? l(:label_spent_time) : @query.name, l(:label_report)) %>
76 85

  
86

  
87
<%= javascript_tag do %>
88
$(document).ready(function(){
89
  $('input#csv-export-button').click(function(){
90
    $('form input#encoding').val($('select#encoding option:selected').val());
91
    $('form#query_form').attr('action', '<%= report_time_entries_path(:format => 'csv') %>').submit();
92
    $('form#query_form').attr('action', '<%= report_time_entries_path %>');
93
    hideModal(this);
94
  });
95
});
96
<% end %>
lib/redmine/export/csv.rb
31 31

  
32 32
        class << self
33 33

  
34
          def generate(&block)
34
          def generate(options = {}, &block)
35 35
            col_sep = l(:general_csv_separator)
36
            encoding = l(:general_csv_encoding)
36
            encoding = Encoding.find(options[:encoding]) rescue Encoding.find(l(:general_csv_encoding))
37 37

  
38 38
            str = ''.force_encoding(encoding)
39
            if encoding == 'UTF-8'
39
            if encoding == Encoding::UTF_8
40 40
              # BOM
41 41
              str = "\xEF\xBB\xBF".force_encoding(encoding)
42 42
            end
test/helpers/application_helper_test.rb
1568 1568
    assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">:45</span>', html_hours('0:45')
1569 1569
    assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">.75</span>', html_hours('0.75')
1570 1570
  end
1571

  
1572
  def test_export_csv_encoding_select_tag_should_return_nil_when_general_csv_encoding_is_UTF8
1573
    with_locale 'az' do
1574
      assert_equal l(:general_csv_encoding), 'UTF-8'
1575
      assert_nil export_csv_encoding_select_tag
1576
    end
1577
  end
1578

  
1579
  def test_export_csv_encoding_select_tag_should_have_two_option_when_general_csv_encoding_is_not_UTF8
1580
    with_locale 'en' do
1581
      assert_not_equal l(:general_csv_encoding), 'UTF-8'
1582
      result = export_csv_encoding_select_tag
1583
      assert_select_in result, "option[selected='selected'][value=#{l(:general_csv_encoding)}]", :text => l(:general_csv_encoding)
1584
      assert_select_in result, "option[value='UTF-8']", :text => 'UTF-8'
1585
    end
1586
  end
1571 1587
end
test/unit/lib/redmine/export/csv_test.rb
18 18
require File.expand_path('../../../../../test_helper', __FILE__)
19 19

  
20 20
class CsvTest < ActiveSupport::TestCase
21

  
21
  include Redmine::I18n
22 22
  BOM = "\xEF\xBB\xBF".force_encoding('UTF-8')
23 23

  
24 24
  def test_should_include_bom_when_utf8_encoded
......
28 28
      assert string.starts_with?(BOM)
29 29
    end
30 30
  end
31

  
32
  def test_generate_should_return_strings_encoded_with_general_csv_encoding
33
    with_locale 'en' do
34
      string = Redmine::Export::CSV.generate({encoding: 'dummy-encoding'}) {|csv| csv << %w(Foo Bar)}
35
      assert_equal l(:general_csv_encoding), string.encoding.name
36
    end
37
  end
38

  
39
  def test_generate_should_return_strings_encoded_with_encoding_option_if_encoding_option_exists
40
    with_locale 'en' do
41
      string = Redmine::Export::CSV.generate({encoding: 'UTF-8'}) {|csv| csv << %w(Foo Bar)}
42
      assert_equal 'UTF-8', string.encoding.name
43
      assert_not_equal l(:general_csv_encoding), string.encoding.name
44
    end
45
  end
31 46
end
(5-5/6)