diff --git a/app/controllers/admin/organisateurs_controller.rb b/app/controllers/admin/organisateurs_controller.rb index 4ae89e4..edf3ef9 100644 --- a/app/controllers/admin/organisateurs_controller.rb +++ b/app/controllers/admin/organisateurs_controller.rb @@ -23,6 +23,71 @@ class Admin::OrganisateursController < ApplicationController @organisateurs = @organisateurs.page(page).per(per_page) } + + + format.csv { + @headers = [] + @columns = [] + Organisateur.qi_table_order.each do |key, value| + + if value.instance_of? Hash + name = value[:name] + else + name = value + end + + if name != "Actions" + @headers << name.to_s + @columns << key + end + + end + + + + xlsx_package = Axlsx::Package.new + wb = xlsx_package.workbook + wb.add_worksheet(name: "import") do |sheet| + sheet.add_row @headers + + @organisateurs.each do |p_customer| + line = [] + + @columns.each do |column| + + if (p_customer.respond_to?("csv_"+column.to_s)) + line << p_customer.send("csv_"+column.to_s) + elsif (p_customer.respond_to?(column)) + if p_customer.send(column.to_s).class.to_s == "BigDecimal" + line << p_customer.send(column.to_s).to_s.gsub('.', ',') + else + line << p_customer.send(column.to_s) + end + else + line << column.to_s + end + + end + + + sheet.add_row line, types: line.map{|t| self.cell_type_from_value(t)} + end + + + end + + @final_file = "#{Rails.root}/private_medias/export-organisateurs-#{Time.now.to_s.to_slug}.xlsx" + + + xlsx_package.serialize(@final_file) + + + send_file @final_file + + + + } + end end diff --git a/app/controllers/admin/p_customers_controller.rb b/app/controllers/admin/p_customers_controller.rb index 50ade6e..12ed913 100644 --- a/app/controllers/admin/p_customers_controller.rb +++ b/app/controllers/admin/p_customers_controller.rb @@ -454,28 +454,7 @@ class Admin::PCustomersController < ApplicationController end - def cell_type_from_value(v) - if v.is_a?(Date) - :date - elsif v.is_a?(String) - :string - elsif v.is_a?(Time) - :time - elsif v.is_a?(TrueClass) || v.is_a?(FalseClass) - :boolean - elsif v.to_s =~ /\A[+-]?\d+?\Z/ #numeric - :integer - elsif v.to_s =~ /\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/ #float - :float - # \A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9]) - # T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)? - # (Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z - elsif v.to_s =~/\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/ - :iso_8601 - else - :string - end - end + def show @p_customer = PCustomer.find(params[:id]) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e264cac..db5ef29 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -162,6 +162,29 @@ class ApplicationController < ActionController::Base end + def cell_type_from_value(v) + if v.is_a?(Date) + :date + elsif v.is_a?(String) + :string + elsif v.is_a?(Time) + :time + elsif v.is_a?(TrueClass) || v.is_a?(FalseClass) + :boolean + elsif v.to_s =~ /\A[+-]?\d+?\Z/ #numeric + :integer + elsif v.to_s =~ /\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/ #float + :float + # \A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9]) + # T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)? + # (Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z + elsif v.to_s =~/\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/ + :iso_8601 + else + :string + end + end + @@ -240,5 +263,7 @@ class ApplicationController < ActionController::Base helper_method :current_admin, :get_specific_pref, :sort_by_sorting, :current_p_customer + + end diff --git a/app/views/admin/organisateurs/index.html.haml b/app/views/admin/organisateurs/index.html.haml index 2677117..b97e788 100644 --- a/app/views/admin/organisateurs/index.html.haml +++ b/app/views/admin/organisateurs/index.html.haml @@ -7,7 +7,7 @@ .qi_search_row =form_tag "", :method => "get", :onsubmit => "" do - =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @organisateurs} + =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @organisateurs, :csv => true} =render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @organisateurs}