select_encoding5.patch

Go MAEDA, 2018-05-04 04:01

Download (8.55 KB)

View differences:

app/helpers/application_helper.rb
1494 1494
    encoding = l(:general_csv_encoding)
1495 1495
  end
1496 1496

  
1497
  def export_csv_encoding_select_tag
1498
    return if l(:general_csv_encoding).casecmp('UTF-8') == 0
1499
    options = [l(:general_csv_encoding), 'UTF-8']
1500
    content_tag(:p) do
1501
      concat(
1502
        content_tag(:label) do
1503
          concat l(:label_encoding)
1504
          concat select_tag('encoding', options_for_select(options, l(:general_csv_encoding)))
1505
        end
1506
      )
1507
    end
1508
  end
1509

  
1497 1510
  private
1498 1511

  
1499 1512
  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);", :data => { :disable_with => false } %>
36 37
    <%= submit_tag l(:button_cancel), :name => nil, :onclick => "hideModal(this);", :type => 'button', :data => { :disable_with => false } %>
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
1607 1607
    assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">:45</span>', html_hours('0:45')
1608 1608
    assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">.75</span>', html_hours('0.75')
1609 1609
  end
1610

  
1611
  def test_export_csv_encoding_select_tag_should_return_nil_when_general_csv_encoding_is_UTF8
1612
    with_locale 'az' do
1613
      assert_equal l(:general_csv_encoding), 'UTF-8'
1614
      assert_nil export_csv_encoding_select_tag
1615
    end
1616
  end
1617

  
1618
  def test_export_csv_encoding_select_tag_should_have_two_option_when_general_csv_encoding_is_not_UTF8
1619
    with_locale 'en' do
1620
      assert_not_equal l(:general_csv_encoding), 'UTF-8'
1621
      result = export_csv_encoding_select_tag
1622
      assert_select_in result, "option[selected='selected'][value=#{l(:general_csv_encoding)}]", :text => l(:general_csv_encoding)
1623
      assert_select_in result, "option[value='UTF-8']", :text => 'UTF-8'
1624
    end
1625
  end
1610 1626
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_with_given_encoding
33
    with_locale 'en' do
34
      string = Redmine::Export::CSV.generate({encoding: 'ISO-8859-3'}) {|csv| csv << %w(Foo Bar)}
35
      assert_equal 'ISO-8859-3', string.encoding.name
36
      assert_not_equal l(:general_csv_encoding), string.encoding.name
37
    end
38
  end
39

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