Skip to content

Commit

Permalink
Importe new version init
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby232 committed Dec 26, 2020
1 parent a047343 commit 96c3661
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 48 deletions.
39 changes: 0 additions & 39 deletions app/controllers/entities/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

class CampaignsController < EntitiesController
before_action :get_data_for_sidebar, only: :index

Expand Down Expand Up @@ -161,37 +160,6 @@ def filter
end
end

# get /campaigns/import AJAX
#----------------------------------------------------------------------------
def import
@importer = Importer.new
respond_with(@importer)
end

# patch /campaigns/import AJAX
#----------------------------------------------------------------------------
def import_upload
@error = false
@result = {
items: [],
errors: []
}

if params[:importer]
@importer = Importer.create(import_params)
@importer.entity_type = 'campaign'
if @importer.valid?
@importer.save
@result = FatFreeCRM::ImportHandle.process(@importer)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@error,@result)
end

private

#----------------------------------------------------------------------------
Expand Down Expand Up @@ -236,11 +204,4 @@ def get_data_for_sidebar
end
@campaign_status_total[:other] += @campaign_status_total[:all]
end

def import_params
return {} unless params[:importer]

params[:importer]
.permit(:attachment)
end
end
108 changes: 108 additions & 0 deletions app/controllers/importers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

class ImportersController < ApplicationController

=begin
# get /campaigns/import AJAX
#----------------------------------------------------------------------------
def import
@importer = Importer.new
@importer.entity_type = 'Campaign'
respond_with(@importer)
end
# patch /campaigns/import AJAX
#----------------------------------------------------------------------------
def import_upload
@error = false
@result = {
items: [],
errors: []
}
if params[:importer]
@importer = Importer.create(import_params)
if @importer.valid?
@importer.save
@result = FatFreeCRM::ImportHandle.process(@importer)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@error,@result)
end
# get /campaigns/%id/import AJAX
#----------------------------------------------------------------------------
def import_leads
@importer = Importer.new
@importer.entity_type = 'Lead'
respond_with(@importer)
end
# patch /campaigns/import AJAX
#----------------------------------------------------------------------------
def uploads_import_leads
@error = false
@result = {
items: [],
errors: []
}
if params[:importer]
@importer = Importer.create(import_params)
if @importer.valid?
@importer.save
@colummns = FatFreeCRM::ImportHandle.get_columns(@importer.attachment.path)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@colummns) do |format|
format.js { render :uploads_import_leads }
end
end
=end
# post /importers/create
#----------------------------------------------------------------------------
=begin
def create
@error = false
@result = {
items: [],
errors: []
}
if params[:importer]
@importer = Importer.create(import_params)
if @importer.valid?
@importer.save
@result = FatFreeCRM::ImportHandle.process(@importer)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@error,@result)
end
=end

private

def importer_params
params.require(:importer).permit(:attachment,:entity_type,:entity_id)
end
end
8 changes: 8 additions & 0 deletions app/models/entities/lead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ def full_name(format = nil)
end
alias name full_name

# Save Campaign from row in xls
def self.import_from_xls(row,headers)
lead = Lead.new
lead.name = row[headers['Name']]
lead.save
lead
end

private

#----------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion app/views/campaigns/_title_bar.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#confirm{ hidden }
.title_tools#menu
= link_to_inline(:edit_campaign, edit_campaign_path(@campaign), text: t(:edit)) + " | "
= link_to_function(t(:delete) + '?', confirm_delete(@campaign))
= link_to_function(t(:delete) + '?', confirm_delete(@campaign)) + " | "
= link_to_inline(:import_leads, import_leads_campaign_path(@campaign), text: t("import_leads".to_sym))

.title_tools#buttons
= view_buttons
.title#edit_campaign_title
Expand Down
3 changes: 3 additions & 0 deletions app/views/campaigns/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
= render 'campaigns/title_bar', campaign: @campaign

= render "comments/new", commentable: @campaign
.remote#campaign_import_leads{ hidden }
%iframe#uploading{ name: "uploading", style: "width:100px; height:10px; border:5px" }

= render partial: "shared/timeline", collection: @timeline

= hook(:show_campaign_bottom, self, {entity: @campaign}) do
Expand Down
6 changes: 6 additions & 0 deletions app/views/campaigns/uploads_import_leads.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- import_id = "campaign_import_leads"

crm.flick('empty', 'toggle');
crm.flip_form('#{import_id}');
$('##{import_id}').html('#{ j render(partial: "import_leads_maps") }');
crm.set_title('#{import_id}', '#{ j t(import_id) }');
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
= form_for(@importer, url: import_upload_campaign_path(format: "js"), method: "patch", html: { multipart: true, target: "uploading", onsubmit: "$('#import_submit').disabled = true"}) do |f|
= form_for(@importer,
url: import_upload_campaign_path(format: "js"),
method: "patch",
html:{ multipart: true, target: "uploading", onsubmit: "$('#import_submit').disabled = true"}) do |f|
= link_to_close new_campaign_path
-# todo
-#= error_messages_for :avatar, object: @user.avatar, object_name: t('avatar')
Expand Down
17 changes: 17 additions & 0 deletions app/views/importers/_import_leads.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
= form_for(@importer,
url: uploads_import_leads_campaign_path(@campaign,format: "js"),
method: "patch",
html: { multipart: true, target: "uploading", onsubmit: "$('#import_submit').disabled = true"}) do |f|
= link_to_close campaign_path(@campaign)
%p
%small 'Descripcion aqui'

%div 'File':
= fields_for(Importer) do |a|
= f.hidden_field :entity_type
%div= a.file_field :attachment

.buttonbar
= f.submit t(:upload_picture), id: "import_submit"
#{t :or}
= link_to_cancel campaign_path(@campaign)
1 change: 1 addition & 0 deletions app/views/importers/_import_leads_maps.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%div 'File':
File renamed without changes.
6 changes: 6 additions & 0 deletions app/views/importers/import_leads.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- import_id = "campaign_import_leads"

crm.flick('empty', 'toggle');
crm.flip_form('#{import_id}');
$('##{import_id}').html('#{ j render(partial: "import_leads") }');
crm.set_title('#{import_id}', '#{ j t(import_id) }');
File renamed without changes.
7 changes: 7 additions & 0 deletions config/locales/fat_free_crm.en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,10 @@ en-US:
from_to: From %{from} to %{to}
from_only: From %{from}
to_only: Until %{to}


# Import from excel
#----------------------------------------------------------------------------
import_leads: 'Import Leads'
import_leads_help: 'Todo help for import leads from excel'
xls_file: 'EXCEL File'
6 changes: 6 additions & 0 deletions config/locales/fat_free_crm.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,9 @@ es:
from_to: De %{from} a %{to}
from_only: De %{from}
to_only: Antes de %{to}

# Import from excel
#----------------------------------------------------------------------------
import_leads: 'Importar clientes potenciales'
import_leads_help: 'Todo help for import leads from excel'
xls_file: 'Fichero EXCEL'
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@
end
end

resources :importers do
end

resources :fields, as: :custom_fields
resources :fields, as: :core_fields

resources :settings, only: :index
resources :plugins, only: :index
end

get 'campaigns/import',to: 'campaigns#import', as: :import_campaign
match 'campaigns/import-upload',to: 'campaigns#import_upload', as: :import_upload_campaign, via: %i[put patch]
end
34 changes: 30 additions & 4 deletions lib/fat_free_crm/import_handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
module FatFreeCRM
class ImportHandle
class << self
def get_columns(path)
headers = Hash.new

puts path

=begin
xlsx = Roo::Spreadsheet.open(path)
sheet = xlsx.sheets.first
puts sheet.row(1)
sheet.row(1).each_with_index { |header, i|
headers[i] = header
}
=end

return headers

end
def process(importer)
result = {
items: [],
Expand All @@ -23,10 +42,17 @@ def process(importer)
headers[header] = i
}
((sheet.first_row + 1)..sheet.last_row).each do |row|
campaign = Campaign.import_from_xls(sheet.row(row), headers)
result[:items].push(campaign)
if campaign.errors.count
result[:errors].push(campaign.errors.full_messages)
item = nil
if importer.entity_type == 'Campaign'
item = Campaign.import_from_xls(sheet.row(row), headers)
elsif importer.entity_type == 'Lead'
item = Lead.import_from_xls(sheet.row(row), headers)
end
if item
result[:items].push(item)
if item.errors.count
result[:errors].push(item.errors.full_messages)
end
end
end
end
Expand Down

0 comments on commit 96c3661

Please sign in to comment.