2020-11-03 01:24:20 +01:00
|
|
|
class CustomWizard::AdminTransferController < CustomWizard::AdminController
|
2019-07-27 23:08:22 +02:00
|
|
|
skip_before_action :check_xhr, :only => [:export]
|
|
|
|
|
|
|
|
def export
|
2020-11-03 01:24:20 +01:00
|
|
|
wizard_ids = params['wizards']
|
|
|
|
templates = []
|
2019-08-06 13:08:05 +02:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
if wizard_ids.nil?
|
2019-08-07 12:16:05 +02:00
|
|
|
render json: { error: I18n.t('wizard.export.error.select_one') }
|
2019-07-30 19:04:18 +02:00
|
|
|
return
|
|
|
|
end
|
2019-07-27 23:08:22 +02:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
wizard_ids.each do |wizard_id|
|
|
|
|
if template = CustomWizard::Template.find(wizard_id)
|
|
|
|
templates.push(template)
|
|
|
|
end
|
2019-07-27 23:08:22 +02:00
|
|
|
end
|
2019-07-30 19:04:18 +02:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
send_data templates.to_json,
|
2019-08-07 12:16:05 +02:00
|
|
|
type: "application/json",
|
|
|
|
disposition: 'attachment',
|
|
|
|
filename: 'wizards.json'
|
2019-07-29 10:00:56 +02:00
|
|
|
end
|
|
|
|
|
2019-07-27 23:08:22 +02:00
|
|
|
def import
|
2019-07-29 10:00:56 +02:00
|
|
|
file = File.read(params['file'].tempfile)
|
2020-11-03 01:24:20 +01:00
|
|
|
|
2019-07-30 19:04:18 +02:00
|
|
|
if file.nil?
|
2019-08-07 12:16:05 +02:00
|
|
|
render json: { error: I18n.t('wizard.import.error.no_file') }
|
2019-07-30 19:04:18 +02:00
|
|
|
return
|
|
|
|
end
|
2019-08-06 13:08:05 +02:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
file_size = file.size
|
|
|
|
max_file_size = 512 * 1024
|
|
|
|
|
|
|
|
if max_file_size < file_size
|
2019-08-07 12:16:05 +02:00
|
|
|
render json: { error: I18n.t('wizard.import.error.file_large') }
|
2019-07-31 06:57:40 +02:00
|
|
|
return
|
2019-07-29 10:00:56 +02:00
|
|
|
end
|
2020-11-03 01:24:20 +01:00
|
|
|
|
2019-08-07 12:16:05 +02:00
|
|
|
begin
|
2020-11-03 01:24:20 +01:00
|
|
|
template_json = JSON.parse file
|
2019-08-07 12:16:05 +02:00
|
|
|
rescue JSON::ParserError
|
|
|
|
render json: { error: I18n.t('wizard.import.error.invalid_json') }
|
2019-07-31 06:57:40 +02:00
|
|
|
return
|
2019-07-29 10:00:56 +02:00
|
|
|
end
|
2019-08-07 12:16:05 +02:00
|
|
|
|
2019-07-30 19:04:18 +02:00
|
|
|
success_ids = []
|
|
|
|
failed_ids = []
|
2019-08-07 12:16:05 +02:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
template_json.each do |t_json|
|
|
|
|
template = CustomWizard::Template.new(t_json)
|
|
|
|
template.save(skip_jobs: true)
|
|
|
|
|
|
|
|
if template.errors.any?
|
|
|
|
failed_ids.push t_json['id']
|
|
|
|
else
|
|
|
|
success_ids.push t_json['id']
|
2019-08-07 12:16:05 +02:00
|
|
|
end
|
2019-07-29 10:00:56 +02:00
|
|
|
end
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
if success_ids.length == 0
|
2019-08-07 12:16:05 +02:00
|
|
|
render json: { error: I18n.t('wizard.import.error.no_valid_wizards') }
|
2019-07-29 10:00:56 +02:00
|
|
|
else
|
2019-08-07 12:16:05 +02:00
|
|
|
render json: { success: success_ids, failed: failed_ids }
|
2019-07-27 23:08:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|