2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2022-03-16 12:33:34 +01:00
|
|
|
class CustomWizard::WizardController < ::ActionController::Base
|
|
|
|
helper ApplicationHelper
|
|
|
|
|
|
|
|
include CurrentUser
|
|
|
|
include CanonicalURL::ControllerExtensions
|
|
|
|
include GlobalPath
|
|
|
|
|
2022-03-12 14:00:07 +01:00
|
|
|
prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'app', 'views'))
|
2022-03-16 12:33:34 +01:00
|
|
|
layout :set_wizard_layout
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-03-16 12:33:34 +01:00
|
|
|
before_action :preload_wizard_json
|
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]
|
2022-03-16 12:33:34 +01:00
|
|
|
|
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
|
|
|
|
2022-03-16 12:33:34 +01:00
|
|
|
def set_wizard_layout
|
|
|
|
action_name === 'qunit' ? 'qunit' : 'wizard'
|
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
|
2022-03-16 12:33:34 +01:00
|
|
|
if wizard.present?
|
|
|
|
render json: CustomWizard::WizardSerializer.new(wizard, scope: guardian, root: false).as_json, status: 200
|
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
|
2022-03-16 12:33:34 +01:00
|
|
|
format.html do
|
|
|
|
render "default/empty"
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
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
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-03-16 12:33:34 +01:00
|
|
|
if current_user && wizard.can_access?
|
2022-01-31 10:18:04 +01:00
|
|
|
if redirect_to = wizard.current_submission&.redirect_to
|
|
|
|
result.merge!(redirect_to: redirect_to)
|
2019-06-19 07:23:10 +02:00
|
|
|
end
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2022-01-31 10:18:04 +01:00
|
|
|
wizard.cleanup_on_skip!
|
2017-11-01 05:21:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2022-03-16 12:33:34 +01:00
|
|
|
def qunit
|
|
|
|
raise Discourse::InvalidAccess.new if Rails.env.production?
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
render "default/empty"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def ensure_logged_in
|
|
|
|
raise Discourse::NotLoggedIn.new unless current_user.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(current_user, request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wizard
|
|
|
|
@wizard ||= begin
|
|
|
|
builder = CustomWizard::Builder.new(params[:wizard_id].underscore, current_user)
|
|
|
|
return nil unless builder.present?
|
|
|
|
opts = {}
|
|
|
|
opts[:reset] = params[:reset]
|
|
|
|
builder.build(opts, params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wizard_page_title
|
|
|
|
wizard ? (wizard.name || wizard.id) : I18n.t('wizard.custom_title')
|
|
|
|
end
|
|
|
|
|
|
|
|
def wizard_theme_id
|
|
|
|
wizard ? wizard.theme_id : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def wizard_theme_lookup(name)
|
|
|
|
Theme.lookup_field(wizard_theme_id, view_context.mobile_view? ? :mobile : :desktop, name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wizard_theme_translations_lookup
|
|
|
|
Theme.lookup_field(wizard_theme_id, :translations, I18n.locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preload_wizard_json
|
|
|
|
return if request.xhr? || request.format.json?
|
|
|
|
return if request.method != "GET"
|
|
|
|
|
|
|
|
store_preloaded("siteSettings", SiteSetting.client_settings_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_preloaded(key, json)
|
|
|
|
@preloaded ||= {}
|
|
|
|
@preloaded[key] = json.gsub("</", "<\\/")
|
|
|
|
end
|
|
|
|
|
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
|