2017-09-25 16:47:40 +02:00
|
|
|
class CustomWizard::WizardController < ::ApplicationController
|
2017-10-17 09:18:53 +02:00
|
|
|
prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'views'))
|
|
|
|
layout 'wizard'
|
2017-11-24 08:53:27 +01:00
|
|
|
|
2018-02-09 09:39:47 +01:00
|
|
|
requires_login
|
2017-10-17 09:18:53 +02:00
|
|
|
helper_method :wizard_page_title
|
2017-12-17 04:43:18 +01:00
|
|
|
helper_method :theme_key
|
|
|
|
|
|
|
|
def wizard
|
2018-04-06 06:21:11 +02:00
|
|
|
CustomWizard::Template.new(PluginStore.get('custom_wizard', params[:wizard_id].underscore))
|
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
|
|
|
|
|
2017-12-17 04:43:18 +01:00
|
|
|
def theme_key
|
2018-04-06 06:21:11 +02:00
|
|
|
wizard ? wizard.theme_key : nil
|
2017-12-17 04:43:18 +01: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
|
2017-11-01 05:21:14 +01:00
|
|
|
builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore)
|
|
|
|
if builder.wizard.present?
|
|
|
|
wizard = builder.build
|
2017-10-22 05:37:58 +02:00
|
|
|
render_serialized(wizard, WizardSerializer)
|
|
|
|
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
|
|
|
|
|
|
|
## clean up if user skips wizard
|
|
|
|
def skip
|
|
|
|
wizard_id = params[:wizard_id]
|
|
|
|
|
|
|
|
wizard = PluginStore.get('custom_wizard', wizard_id.underscore)
|
|
|
|
|
|
|
|
if wizard['required']
|
|
|
|
return render json: { error: I18n.t('wizard.no_skip') }
|
|
|
|
end
|
|
|
|
|
|
|
|
user = current_user
|
|
|
|
result = success_json
|
|
|
|
submission = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)).last
|
|
|
|
|
|
|
|
if submission && submission['redirect_to']
|
|
|
|
result.merge!(redirect_to: submission['redirect_to'])
|
|
|
|
end
|
|
|
|
|
|
|
|
if submission && !wizard['save_submissions']
|
|
|
|
PluginStore.remove("#{wizard['id']}_submissions", user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if user.custom_fields['redirect_to_wizard'] === wizard_id
|
|
|
|
user.custom_fields.delete('redirect_to_wizard')
|
|
|
|
user.save_custom_fields(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|