From dc8a12d89af955f8d078863c3cea37ab140935a2 Mon Sep 17 00:00:00 2001 From: pabois <pierreandre.boissinot@noesya.coop> Date: Mon, 13 Mar 2023 10:37:49 +0100 Subject: [PATCH] wip exports --- .../extranets/contacts_controller.rb | 20 ++++++++++++++++--- .../extranets/contacts/index.html.erb | 7 +++++-- .../extranets/contacts/persons.xlsx.axlsx | 14 +++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx diff --git a/app/controllers/admin/communication/extranets/contacts_controller.rb b/app/controllers/admin/communication/extranets/contacts_controller.rb index d74dbda0e..874d4cf96 100644 --- a/app/controllers/admin/communication/extranets/contacts_controller.rb +++ b/app/controllers/admin/communication/extranets/contacts_controller.rb @@ -1,7 +1,21 @@ class Admin::Communication::Extranets::ContactsController < Admin::Communication::Extranets::ApplicationController def index - @persons = current_university.people.ordered.page params[:persons_page] - @organizations = current_university.organizations.ordered.page params[:organizations_page] + @persons = current_university.people.ordered + @organizations = current_university.organizations.ordered + respond_to do |format| + format.html { + @persons = @persons.page params[:persons_page] + @organizations = @organizations.page params[:organizations_page] + } + format.xlsx { + @export = params['export'] + filename = "#{@export}-#{Time.now.strftime("%Y%m%d%H%M%S")}.xlsx" + response.headers['Content-Disposition'] = "attachment; filename=#{filename}" + render @export + } + end + + breadcrumb add_breadcrumb Communication::Extranet.human_attribute_name(:feature_contacts) end @@ -26,4 +40,4 @@ class Admin::Communication::Extranets::ContactsController < Admin::Communication object_id = params[:objectId] @object = object_type.constantize.find object_id end -end \ No newline at end of file +end diff --git a/app/views/admin/communication/extranets/contacts/index.html.erb b/app/views/admin/communication/extranets/contacts/index.html.erb index ca91cce03..d79fc8d55 100644 --- a/app/views/admin/communication/extranets/contacts/index.html.erb +++ b/app/views/admin/communication/extranets/contacts/index.html.erb @@ -2,7 +2,7 @@ <%= render 'admin/communication/extranets/sidebar' do %> <% action = link_to t('export'), - admin_communication_extranet_contacts_path(extranet_id: @extranet.id, export: 'persons'), + admin_communication_extranet_contacts_path(extranet_id: @extranet.id, export: 'persons', format: :xlsx), class: button_classes('ms-1') if can?(:show, University::Person) %> <%= osuny_panel University::Person.model_name.human(count: 2), action: action do %> <div class="table-responsive"> @@ -40,7 +40,10 @@ </div> <%= paginate @persons, theme: 'bootstrap-5', param_name: :persons_page %> <% end %> - <%= osuny_panel University::Organization.model_name.human(count: 2) do %> + <% action = link_to t('export'), + admin_communication_extranet_contacts_path(extranet_id: @extranet.id, export: 'organizations', format: :xlsx), + class: button_classes('ms-1') if can?(:show, University::Person) %> + <%= osuny_panel University::Organization.model_name.human(count: 2), action: action do %> <div class="table-responsive"> <table class="<%= table_classes%>"> <tbody> diff --git a/app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx b/app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx new file mode 100644 index 000000000..47e62236f --- /dev/null +++ b/app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx @@ -0,0 +1,14 @@ +@users ||= users + +wb = xlsx_package.workbook +wb.add_worksheet(name: "index") do |sheet| + + headers = User::xls_header_for(current_brand) + + # for headers types are always :string (because those are labels) + sheet.add_row headers[:labels], types: Array.new(headers[:types].length) { |_| :string } + + @users.each do |user| + sheet.add_row user.xls_basic_row, types: headers[:types] + end +end -- GitLab