2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2017-09-25 16:47:40 +02:00
|
|
|
class CustomWizard::WizardController < ::ApplicationController
|
2021-03-18 20:44:57 +01:00
|
|
|
include ApplicationHelper
|
2017-10-17 09:18:53 +02:00
|
|
|
prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'views'))
|
|
|
|
layout 'wizard'
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
before_action :ensure_plugin_enabled
|
2021-11-02 09:11:46 +01:00
|
|
|
before_action :ensure_logged_in, only: [:skip]
|
2017-10-17 09:18:53 +02:00
|
|
|
helper_method :wizard_page_title
|
2021-06-27 13:28:26 +02:00
|
|
|
helper_method :wizard_theme_id
|
2021-03-18 20:44:57 +01:00
|
|
|
helper_method :wizard_theme_lookup
|
2021-03-30 09:34:52 +02:00
|
|
|
helper_method :wizard_theme_translations_lookup
|
2017-12-17 04:43:18 +01:00
|
|
|
|
|
|
|
def wizard
|
2021-04-30 18:26:00 +02:00
|
|
|
@builder = CustomWizard::Builder.new(params[:wizard_id].underscore, current_user)
|
|
|
|
@wizard ||= @builder.build
|
|
|
|
@wizard
|
2017-12-17 04:43:18 +01:00
|
|
|
end
|
2017-10-17 09:18:53 +02:00
|
|
|
|
|
|
|
def wizard_page_title
|
2018-04-06 06:21:11 +02:00
|
|
|
wizard ? (wizard.name || wizard.id) : I18n.t('wizard.custom_title')
|
2017-09-25 16:47:40 +02:00
|
|
|
end
|
|
|
|
|
2021-06-27 13:28:26 +02:00
|
|
|
def wizard_theme_id
|
|
|
|
wizard ? wizard.theme_id : nil
|
2017-12-17 04:43:18 +01:00
|
|
|
end
|
|
|
|
|
2021-03-18 20:44:57 +01:00
|
|
|
def wizard_theme_lookup(name)
|
2021-06-27 13:28:26 +02:00
|
|
|
Theme.lookup_field(wizard_theme_id, mobile_view? ? :mobile : :desktop, name)
|
2021-03-18 20:44:57 +01:00
|
|
|
end
|
|
|
|
|
2021-03-30 09:34:52 +02:00
|
|
|
def wizard_theme_translations_lookup
|
2021-06-27 13:28:26 +02:00
|
|
|
Theme.lookup_field(wizard_theme_id, :translations, I18n.locale)
|
2021-03-30 09:34:52 +02:00
|
|
|
end
|
|
|
|
|
2017-09-25 16:47:40 +02:00
|
|
|
def index
|
2017-09-23 04:34:07 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2020-04-13 14:17:22 +02:00
|
|
|
builder = CustomWizard::Builder.new(params[:wizard_id].underscore, current_user)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
if builder.wizard.present?
|
2020-04-13 14:17:22 +02:00
|
|
|
builder_opts = {}
|
2020-10-31 08:05:50 +01:00
|
|
|
builder_opts[:reset] = params[:reset]
|
2020-04-13 14:17:22 +02:00
|
|
|
built_wizard = builder.build(builder_opts, params)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
render_serialized(built_wizard, ::CustomWizard::WizardSerializer, root: false)
|
2017-10-22 05:37:58 +02:00
|
|
|
else
|
|
|
|
render json: { error: I18n.t('wizard.none') }
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|
|
|
|
format.html {}
|
|
|
|
end
|
|
|
|
end
|
2017-11-01 05:21:14 +01:00
|
|
|
|
|
|
|
def skip
|
2018-05-09 07:06:43 +02:00
|
|
|
params.require(:wizard_id)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2018-05-09 07:06:43 +02:00
|
|
|
if wizard.required && !wizard.completed? && wizard.permitted?
|
2017-11-01 05:21:14 +01:00
|
|
|
return render json: { error: I18n.t('wizard.no_skip') }
|
|
|
|
end
|
|
|
|
|
|
|
|
result = success_json
|
2020-04-13 14:17:22 +02:00
|
|
|
user = current_user
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-06-16 06:13:12 +02:00
|
|
|
if user && wizard.can_access?
|
2020-11-03 01:24:20 +01:00
|
|
|
submission = wizard.current_submission
|
2021-06-17 09:50:22 +02:00
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
if submission.present? && submission.redirect_to
|
2021-06-17 09:50:22 +02:00
|
|
|
result.merge!(redirect_to: submission.redirect_to)
|
2019-06-19 07:23:10 +02:00
|
|
|
end
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2021-08-10 11:01:59 +02:00
|
|
|
submission.remove if submission.present?
|
2021-08-13 15:26:38 +02:00
|
|
|
wizard.reset
|
2017-11-01 05:21:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
private
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
def ensure_plugin_enabled
|
|
|
|
unless SiteSetting.custom_wizard_enabled
|
|
|
|
redirect_to path("/")
|
|
|
|
end
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|