Project

General

Profile

Patch #5957 » users_export2.patch

Yuki Kita, 2010-07-25 16:08

View differences:

app/helpers/users_helper.rb (working copy)
54 54
    end
55 55
    tabs
56 56
  end
57

  
58
  def users_to_csv(users)
59
    ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')    
60
    decimal_separator = l(:general_csv_decimal_separator)
61
    export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
62
      # csv header fields
63
      headers = [ l(:field_login), 
64
                  l(:field_firstname),
65
                  l(:field_lastname),
66
                  l(:field_mail),
67
                  l(:field_admin),
68
                  l(:field_created_on),
69
                  l(:field_last_login_on)
70
                  ]
71
      # Export user custom fields
72
      custom_fields = UserCustomField.all
73
      custom_fields.each {|f| headers << f.name}
74
      # Description in the last column
75
      csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
76
      # csv lines
77
      users.each do |user|
78
        fields = [user.login,
79
                  user.firstname, 
80
                  user.lastname,
81
                  user.mail, 
82
                  user.admin,
83
                  user.created_on,
84
                  user.last_login_on
85
                  ]
86
        custom_fields.each {|f| fields << show_value(user.custom_value_for(f)) }
87
        csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
88
      end
89
    end
90
    export
91
  end
57 92
end
app/controllers/users_controller.rb (working copy)
24 24
  include SortHelper
25 25
  helper :custom_fields
26 26
  include CustomFieldsHelper   
27
  include UsersHelper
27 28

  
28 29
  def index
29 30
    sort_init 'login', 'asc'
......
41 42
    @user_pages = Paginator.new self, @user_count,
42 43
								per_page_option,
43 44
								params['page']								
45
    limit = (params[:format] == 'csv') ? nil : @user_pages.items_per_page
44 46
    @users =  User.find :all,:order => sort_clause,
45 47
                        :conditions => c.conditions,
46
						:limit  =>  @user_pages.items_per_page,
48
						:limit  =>  limit,
47 49
						:offset =>  @user_pages.current.offset
48 50

  
49
    render :layout => !request.xhr?	
51
    respond_to do |format|
52
      format.html { render :layout => !request.xhr? }
53
      format.csv  { send_data(users_to_csv(@users), :type => 'text/csv; header=present', :filename => 'export.csv') }
54
    end
50 55
  end
51 56
  
52 57
  def show
app/views/users/index.rhtml (working copy)
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', :url => { :project_id => @project } %>
50
<% end %>
51

  
48 52
<% html_title(l(:label_user_plural)) -%>
(2-2/9)