Project

General

Profile

Feature #26279 » select_encoding.patch

Mizuki ISHIKAWA, 2017-07-04 02:01

View differences:

app/helpers/application_helper.rb
1495 1495
    encoding = l(:general_csv_encoding)
1496 1496
  end
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
  def wiki_helper
app/helpers/queries_helper.rb
271 271
  def query_to_csv(items, query, options={})
272 272
    columns = query.columns
273
    Redmine::Export::CSV.generate do |csv|
273
    Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
274 274
      # csv header fields
275 275
      csv << columns.map {|c| c.caption.to_s}
276 276
      # csv lines
......
366 366
    tags
367 367
  end
368

  
368

  
369 369
  def query_hidden_sort_tag(query)
370 370
    hidden_field_tag("sort", query.sort_criteria.to_param, :id => nil)
371 371
  end
app/helpers/timelog_helper.rb
76 76
  end
77 77
  def report_to_csv(report)
78
    Redmine::Export::CSV.generate do |csv|
78
    Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
79 79
      # Column headers
80 80
      headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
81 81
      headers += report.periods
app/views/issues/index.html.erb
39 39
    <label><%= check_box_tag 'c[]', 'description', @query.has_column?(:description) %> <%= l(:field_description) %></label>
40 40
    <label><%= check_box_tag 'c[]', 'last_notes', @query.has_column?(:last_notes) %> <%= l(:label_last_notes) %></label>
41 41
  </p>
42
  <%= export_csv_encoding_select_tag %>
42 43
  <% if @issue_count > Setting.issues_export_limit.to_i %>
43 44
  <p class="icon icon-warning">
44 45
    <%= l(:setting_issues_export_limit) %>: <%= Setting.issues_export_limit.to_i %>
app/views/timelog/index.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>
......
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
<% if @query.valid? %>
......
62 63
</div>
63 64
<% other_formats_links do |f| %>
64
  <%= f.link_to_with_query_parameters 'CSV' %>
65
  <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
65 66
<% end %>
66 67
<% end %>
68
<div id="csv-export-options" style="display: none;">
69
  <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
70
  <%= export_csv_encoding_select_tag %>
71
  <p class="buttons">
72
    <%= submit_tag l(:button_export), :name => nil, :id => 'csv-export-button' %>
73
    <%= submit_tag l(:button_cancel), :name => nil, :onclick => 'hideModal(this);', :type => 'button' %>
74
  </p>
75
</div>
67 76
<% end %>
68 77
<% end %>
......
74 83
<% html_title(@query.new_record? ? l(:label_spent_time) : @query.name, l(:label_report)) %>
84

  
85
<%= javascript_tag do %>
86
$(document).ready(function(){
87
  $('input#csv-export-button').click(function(){
88
    $('form input#encoding').val($('select#encoding option:selected').val());
89
    $('form#query_form').attr('action', '<%= report_time_entries_path(:format => 'csv') %>').submit();
90
    $('form#query_form').attr('action', '<%= report_time_entries_path %>');
91
    hideModal(this);
92
  });
93
});
94
<% end %>
lib/redmine/export/csv.rb
31 31
        class << self
32
          def generate(&block)
32
          def generate(options = {}, &block)
33 33
            col_sep = l(:general_csv_separator)
34
            encoding = l(:general_csv_encoding)
34
            encoding = options[:encoding].presence || l(:general_csv_encoding)
35 35
            str = ''.force_encoding(encoding)
36 36
            if encoding == 'UTF-8'
test/unit/helpers/application_helper_test.rb
1597 1597
    assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">:45</span>', html_hours('0:45')
1598 1598
    assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">.75</span>', html_hours('0.75')
1599 1599
  end
1600

  
1601
  def test_export_csv_encoding_select_tag_should_return_nil_when_general_csv_encoding_is_UTF8
1602
    with_locale 'az' do
1603
      assert_equal l(:general_csv_encoding), 'UTF-8'
1604
      assert_nil export_csv_encoding_select_tag
1605
    end
1606
  end
1607

  
1608
  def test_export_csv_encoding_select_tag_should_have_two_option_when_general_csv_encoding_is_not_UTF8
1609
    with_locale 'en' do
1610
      assert_not_equal l(:general_csv_encoding), 'UTF-8'
1611
      result = export_csv_encoding_select_tag
1612
      assert_select_in result, "option[selected='selected'][value=#{l(:general_csv_encoding)}]", :text => l(:general_csv_encoding)
1613
      assert_select_in result, "option[value='UTF-8']", :text => 'UTF-8'
1614
    end
1615
  end
1600 1616
end
(2-2/6)