diff options
author | Go MAEDA <maeda@farend.jp> | 2018-08-05 12:30:40 +0000 |
---|---|---|
committer | Go MAEDA <maeda@farend.jp> | 2018-08-05 12:30:40 +0000 |
commit | 83939be2b52c07cbb0d4dc6f784b979c10cc4229 (patch) | |
tree | 281e118c9b751c54787b601b96663b4fc919bee6 /app | |
parent | eee1c36b4b7f0cee23ebc5d3848fa2b606f603c8 (diff) | |
download | redmine-83939be2b52c07cbb0d4dc6f784b979c10cc4229.tar.gz redmine-83939be2b52c07cbb0d4dc6f784b979c10cc4229.zip |
Export users list to CSV (#5957).
Contriubted by Mizuki ISHIKAWA and Yuki Kita.
git-svn-id: http://svn.redmine.org/redmine/trunk@17463 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/users_controller.rb | 4 | ||||
-rw-r--r-- | app/helpers/users_helper.rb | 35 | ||||
-rw-r--r-- | app/views/users/index.html.erb | 24 |
3 files changed, 58 insertions, 5 deletions
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 04d9a78ad..aed4cd83f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -28,6 +28,7 @@ class UsersController < ApplicationController include SortHelper helper :custom_fields include CustomFieldsHelper + include UsersHelper helper :principal_memberships helper :activities include ActivitiesHelper @@ -61,6 +62,9 @@ class UsersController < ApplicationController @groups = Group.givable.sort render :layout => !request.xhr? } + format.csv { + send_data(users_to_csv(scope.order(sort_clause)), :type => 'text/csv; header=present', :filename => 'users.csv') + } format.api end end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index d7812867b..c0d954499 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -18,12 +18,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module UsersHelper + include ApplicationHelper + def users_status_options_for_select(selected) user_count_by_status = User.group('status').count.to_hash - options_for_select([[l(:label_all), ''], - ["#{l(:status_active)} (#{user_count_by_status[1].to_i})", '1'], - ["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", '2'], - ["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", '3']], selected.to_s) + options_for_select([[l(:label_all), '']] + (User.valid_statuses.map {|c| ["#{l('status_' + User::LABEL_BY_STATUS[c])} (#{user_count_by_status[c].to_i})", c]}), selected.to_s) end def user_mail_notification_options(user) @@ -61,4 +60,32 @@ module UsersHelper end tabs end + + def users_to_csv(users) + Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv| + columns = [ + 'login', + 'firstname', + 'lastname', + 'mail', + 'admin', + 'created_on', + 'last_login_on', + 'status' + ] + + # csv header fields + csv << columns.map{|column| l('field_' + column)} + # csv lines + users.each do |user| + csv << columns.map do |column| + if column == 'status' + l(("status_#{User::LABEL_BY_STATUS[user.status]}")) + else + format_object(user.send(column), false) + end + end + end + end + end end diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 3fc82b640..75c9eb465 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -4,7 +4,7 @@ <h2><%=l(:label_user_plural)%></h2> -<%= form_tag(users_path, :method => :get) do %> +<%= form_tag(users_path, { :method => :get, :id => 'users_form' }) do %> <fieldset><legend><%= l(:label_filter_plural) %></legend> <label for='status'><%= l(:field_status) %>:</label> <%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;" %> @@ -19,6 +19,7 @@ <%= submit_tag l(:button_apply), :class => "small", :name => nil %> <%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %> </fieldset> +<%= hidden_field_tag 'encoding', l(:general_csv_encoding) unless l(:general_csv_encoding).casecmp('UTF-8') == 0 %> <% end %> @@ -55,6 +56,27 @@ </table> </div> <span class="pagination"><%= pagination_links_full @user_pages, @user_count %></span> +<% other_formats_links do |f| %> + <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %> +<% end %> +<div id="csv-export-options" style="display: none;"> + <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3> + <%= export_csv_encoding_select_tag %> + <p class="buttons"> + <%= submit_tag l(:button_export), :name => nil, :id => 'csv-export-button' %> + <%= submit_tag l(:button_cancel), :name => nil, :onclick => 'hideModal(this);', :type => 'button' %> + </p> +</div> +<%= javascript_tag do %> +$(document).ready(function(){ + $('input#csv-export-button').click(function(){ + $('form input#encoding').val($('select#encoding option:selected').val()); + $('form#users_form').attr('action', "<%= users_path(:format => 'csv') %>").submit(); + $('form#users_form').attr('action', '<%= users_path %>'); + hideModal(this); + }); +}); +<% end %> <% else %> <p class="nodata"><%= l(:label_no_data) %></p> <% end %> |