Patch #5957 » users_export4.patch
app/controllers/users_controller.rb | ||
---|---|---|
26 | 26 |
include SortHelper |
27 | 27 |
helper :custom_fields |
28 | 28 |
include CustomFieldsHelper |
29 |
include UsersHelper |
|
29 | 30 | |
30 | 31 |
def index |
31 | 32 |
sort_init 'login', 'asc' |
... | ... | |
52 | 53 |
@users = User.find :all, |
53 | 54 |
:order => sort_clause, |
54 | 55 |
:conditions => c.conditions, |
55 |
:limit => @limit, |
|
56 |
:limit => (params[:format] == 'csv') ? nil : @limit,
|
|
56 | 57 |
:offset => @offset |
57 | 58 | |
58 | 59 |
respond_to do |format| |
59 | 60 |
format.html { render :layout => !request.xhr? } |
61 |
format.csv { send_data(users_to_csv(@users), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
60 | 62 |
format.api |
61 | 63 |
end |
62 | 64 |
end |
app/helpers/users_helper.rb | ||
---|---|---|
58 | 58 |
end |
59 | 59 |
tabs |
60 | 60 |
end |
61 | ||
62 |
def users_to_csv(users) |
|
63 |
ic = Iconv.new(l(:general_csv_encoding), 'UTF-8') |
|
64 |
decimal_separator = l(:general_csv_decimal_separator) |
|
65 |
FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
66 |
# csv header fields |
|
67 |
headers = [ |
|
68 |
l(:field_login), |
|
69 |
l(:field_firstname), |
|
70 |
l(:field_lastname), |
|
71 |
l(:field_mail), |
|
72 |
l(:field_admin), |
|
73 |
l(:field_created_on), |
|
74 |
l(:field_last_login_on) |
|
75 |
] |
|
76 |
# Export user custom fields |
|
77 |
custom_fields = UserCustomField.all |
|
78 |
custom_fields.each do |f| |
|
79 |
headers << f.name |
|
80 |
end |
|
81 |
csv << headers.collect do |c| |
|
82 |
begin |
|
83 |
ic.iconv(c.to_s) |
|
84 |
rescue |
|
85 |
c.to_s |
|
86 |
end |
|
87 |
end |
|
88 |
# csv lines |
|
89 |
users.each do |user| |
|
90 |
fields = [ |
|
91 |
user.login, |
|
92 |
user.firstname, |
|
93 |
user.lastname, |
|
94 |
user.mail, |
|
95 |
user.admin, |
|
96 |
user.created_on, |
|
97 |
user.last_login_on |
|
98 |
] |
|
99 |
custom_fields.each do |f| |
|
100 |
fields << show_value(user.custom_value_for(f)) |
|
101 |
end |
|
102 |
csv << fields.collect do |c| |
|
103 |
begin |
|
104 |
ic.iconv(c.to_s) |
|
105 |
rescue |
|
106 |
c.to_s |
|
107 |
end |
|
108 |
end |
|
109 |
end |
|
110 |
end |
|
111 |
end |
|
61 | 112 |
end |
app/views/users/index.rhtml | ||
---|---|---|
45 | 45 |
</div> |
46 | 46 |
<p class="pagination"><%= pagination_links_full @user_pages, @user_count %></p> |
47 | 47 | |
48 |
<% other_formats_links do |f| %> |
|
49 |
<%= f.link_to 'CSV' %> |
|
50 |
<% end %> |
|
51 | ||
48 | 52 |
<% html_title(l(:label_user_plural)) -%> |