2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2020-11-09 04:32:36 +01:00
|
|
|
class CustomWizard::AdminManagerController < CustomWizard::AdminController
|
|
|
|
skip_before_action :check_xhr, only: [:export]
|
|
|
|
before_action :get_wizard_ids, except: [:import]
|
2024-10-16 14:18:23 +02:00
|
|
|
requires_plugin "discourse-custom-wizard"
|
2020-11-09 04:32:36 +01:00
|
|
|
|
|
|
|
def export
|
|
|
|
templates = []
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
@wizard_ids.each do |wizard_id|
|
|
|
|
if template = CustomWizard::Template.find(wizard_id)
|
|
|
|
templates.push(template)
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
return render_error(I18n.t("wizard.export.error.invalid_wizards")) if templates.empty?
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
basename = SiteSetting.title.parameterize || "discourse"
|
2021-03-11 07:30:15 +01:00
|
|
|
time = Time.now.to_i
|
2020-11-09 04:32:36 +01:00
|
|
|
filename = "#{basename}-wizards-#{time}.json"
|
|
|
|
|
|
|
|
send_data templates.to_json,
|
2024-10-16 13:52:03 +02:00
|
|
|
type: "application/json",
|
|
|
|
disposition: "attachment",
|
|
|
|
filename: filename
|
2020-11-09 04:32:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def import
|
2024-10-16 13:52:03 +02:00
|
|
|
file = File.read(params["file"].tempfile)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
return render_error(I18n.t("wizard.export.error.no_file")) if file.nil?
|
2020-11-09 04:32:36 +01:00
|
|
|
|
|
|
|
file_size = file.size
|
|
|
|
max_file_size = 512 * 1024
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
return render_error(I18n.t("wizard.import.error.file_large")) if max_file_size < file_size
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
begin
|
2021-04-20 19:58:19 +02:00
|
|
|
template_json = JSON.parse(file)
|
2020-11-09 04:32:36 +01:00
|
|
|
rescue JSON::ParserError
|
2024-10-16 13:52:03 +02:00
|
|
|
return render_error(I18n.t("wizard.import.error.invalid_json"))
|
2020-11-09 04:32:36 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
imported = []
|
|
|
|
failures = []
|
2021-09-07 05:11:29 +02:00
|
|
|
templates = template_json.is_a?(Array) ? template_json : [template_json]
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-09-07 05:11:29 +02:00
|
|
|
templates.each do |raw_template|
|
|
|
|
template = CustomWizard::Template.new(raw_template)
|
2020-11-09 04:32:36 +01:00
|
|
|
template.save(skip_jobs: true, create: true)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
if template.errors.any?
|
2024-10-16 13:52:03 +02:00
|
|
|
failures.push(id: template.data["id"], messages: template.errors.full_messages.join(", "))
|
2020-11-09 04:32:36 +01:00
|
|
|
else
|
2024-10-16 13:52:03 +02:00
|
|
|
imported.push(id: template.data["id"], name: template.data["name"])
|
2020-11-09 04:32:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
render json: success_json.merge(imported: imported, failures: failures)
|
2020-11-09 04:32:36 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
def destroy
|
|
|
|
destroyed = []
|
|
|
|
failures = []
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
@wizard_ids.each do |wizard_id|
|
|
|
|
template = CustomWizard::Template.find(wizard_id)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
if template && CustomWizard::Template.remove(wizard_id)
|
2024-10-16 13:52:03 +02:00
|
|
|
destroyed.push(id: wizard_id, name: template["name"])
|
2020-11-09 04:32:36 +01:00
|
|
|
else
|
|
|
|
failures.push(
|
|
|
|
id: wizard_id,
|
2024-10-16 13:52:03 +02:00
|
|
|
messages: I18n.t("wizard.destroy.error.#{template ? "default" : "no_template"}"),
|
2020-11-09 04:32:36 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
render json: success_json.merge(destroyed: destroyed, failures: failures)
|
2020-11-09 04:32:36 +01:00
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
private
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
def get_wizard_ids
|
2024-10-16 13:52:03 +02:00
|
|
|
return render_error(I18n.t("wizard.export.error.select_one")) if params["wizard_ids"].blank?
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
wizard_ids = []
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
params["wizard_ids"].each do |wizard_id|
|
2020-11-09 04:32:36 +01:00
|
|
|
begin
|
|
|
|
wizard_ids.push(wizard_id.underscore)
|
2024-10-16 13:52:03 +02:00
|
|
|
rescue StandardError
|
2020-11-09 04:32:36 +01:00
|
|
|
#
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2024-10-16 13:52:03 +02:00
|
|
|
return render_error(I18n.t("wizard.export.error.invalid_wizards")) if wizard_ids.empty?
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-09 04:32:36 +01:00
|
|
|
@wizard_ids = wizard_ids
|
|
|
|
end
|
|
|
|
end
|