Project

General

Profile

Patch #5957 » users_export.patch

Yuki Kita, 2010-07-25 16:03

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, project = nil)
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 project custom fields if project is given
72
      # otherwise export custom fields marked as "For all projects"
73
      custom_fields = UserCustomField.all
74
      custom_fields.each {|f| headers << f.name}
75
      # Description in the last column
76
      csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
77
      # csv lines
78
      users.each do |user|
79
        fields = [user.login,
80
                  user.firstname, 
81
                  user.lastname,
82
                  user.mail, 
83
                  user.admin,
84
                  user.created_on,
85
                  user.last_login_on
86
                  ]
87
        custom_fields.each {|f| fields << show_value(user.custom_value_for(f)) }
88
        csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
89
      end
90
    end
91
    export
92
  end
57 93
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, @project), :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)) -%>
(1-1/9)