0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-14 13:52:54 +01:00
discourse-custom-wizard/controllers/admin.rb

164 Zeilen
4,2 KiB
Ruby

2017-09-23 04:34:07 +02:00
class CustomWizard::AdminController < ::ApplicationController
before_action :ensure_logged_in
before_action :ensure_admin
2017-09-23 04:34:07 +02:00
def index
render nothing: true
end
2017-10-05 02:36:46 +02:00
def field_types
2017-10-09 07:52:09 +02:00
render json: { types: CustomWizard::Field.types }
2017-10-05 02:36:46 +02:00
end
2017-09-23 04:34:07 +02:00
def save
2017-09-24 05:01:18 +02:00
params.require(:wizard)
2017-09-23 04:34:07 +02:00
2017-09-24 05:01:18 +02:00
wizard = ::JSON.parse(params[:wizard])
2017-09-23 04:34:07 +02:00
existing = PluginStore.get('custom_wizard', wizard['id']) || {}
new_time = false
2017-10-30 07:24:51 +01:00
error = nil
2017-11-01 05:21:14 +01:00
if wizard["id"].blank?
2017-10-30 07:24:51 +01:00
error = 'id_required'
2017-11-01 05:21:14 +01:00
elsif wizard["name"].blank?
2017-10-30 07:24:51 +01:00
error = 'name_required'
2017-11-01 05:21:14 +01:00
elsif wizard["steps"].blank?
2017-10-30 07:24:51 +01:00
error = 'steps_required'
2017-11-01 05:21:14 +01:00
elsif wizard["after_time"]
if !wizard["after_time_scheduled"] && !existing["after_time_scheduled"]
2017-11-01 05:21:14 +01:00
error = 'after_time_need_time'
else
after_time_scheduled = Time.parse(wizard["after_time_scheduled"]).utc
new_time = existing['after_time_scheduled'] ?
after_time_scheduled != Time.parse(existing['after_time_scheduled']).utc :
true
2017-11-01 05:21:14 +01:00
begin
if new_time && after_time_scheduled < Time.now.utc
2017-11-01 05:21:14 +01:00
error = 'after_time_invalid'
end
rescue ArgumentError
error = 'after_time_invalid'
end
end
2017-10-30 07:24:51 +01:00
end
return render json: { error: error } if error
wizard["steps"].each do |s|
if s["id"].blank?
error = 'id_required'
break
end
if s["fields"] && s["fields"].present?
s["fields"].each do |f|
if f["id"].blank?
error = 'id_required'
break
end
2017-11-23 10:02:25 +01:00
if f["type"].blank?
error = 'type_required'
break
end
2017-10-30 07:24:51 +01:00
if f["type"] === 'dropdown'
choices = f["choices"]
2017-11-07 07:28:26 +01:00
if (!choices || choices.length < 1) && !f["choices_key"] && !f["choices_preset"]
2017-10-30 07:24:51 +01:00
error = 'field.need_choices'
break
end
end
end
end
if s["actions"] && s["actions"].present?
s["actions"].each do |a|
if a["id"].blank?
error = 'id_required'
break
end
end
end
end
return render json: { error: error } if error
2017-11-02 03:22:13 +01:00
## end of error checks
wizard['steps'].each do |s|
2017-11-03 09:22:50 +01:00
s['description'] = PrettyText.cook(s['raw_description']) if s['raw_description']
2017-11-02 03:22:13 +01:00
end
2017-11-01 10:50:03 +01:00
if wizard['after_time'] && new_time
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
2017-11-01 05:21:14 +01:00
Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
end
if existing['after_time'] && !wizard['after_time']
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
2017-11-01 05:21:14 +01:00
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
end
PluginStore.set('custom_wizard', wizard["id"], wizard)
2017-09-23 04:34:07 +02:00
render json: success_json
end
def remove
2017-09-24 05:01:18 +02:00
params.require(:id)
2017-09-23 04:34:07 +02:00
2017-11-01 05:21:14 +01:00
wizard = PluginStore.get('custom_wizard', params[:id])
if wizard['after_time']
2017-11-01 10:50:03 +01:00
Jobs.cancel_scheduled_job(:set_after_time_wizard)
2017-11-01 05:21:14 +01:00
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
end
PluginStore.remove('custom_wizard', params[:id])
2017-09-23 04:34:07 +02:00
render json: success_json
end
2017-10-05 02:36:46 +02:00
def find_wizard
params.require(:wizard_id)
2017-09-23 04:34:07 +02:00
2017-10-07 04:27:38 +02:00
wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
2017-09-23 04:34:07 +02:00
render json: success_json.merge(wizard: wizard)
end
2017-10-05 02:36:46 +02:00
def custom_wizards
rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
2017-09-23 04:34:07 +02:00
2017-10-15 05:58:22 +02:00
wizards = [*rows].map { |r| CustomWizard::Template.new(r.value) }
2017-09-23 04:34:07 +02:00
render json: success_json.merge(wizards: wizards)
end
2017-10-05 02:36:46 +02:00
2017-10-22 05:37:58 +02:00
def submissions
2017-10-05 02:36:46 +02:00
params.require(:wizard_id)
2017-11-01 10:50:03 +01:00
rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order('id DESC')
2017-10-05 02:36:46 +02:00
2017-11-01 10:50:03 +01:00
all_submissions = [*rows].map do |r|
submissions = ::JSON.parse(r.value)
if user = User.find_by(id: r.key)
username = user.username
else
username = I18n.t('admin.wizard.submissions.no_user', id: r.key)
end
2018-05-24 14:54:36 +02:00
submissions.map { |s| { username: username }.merge!(s.except("redirect_to")) }
2017-11-01 10:50:03 +01:00
end.flatten
2017-10-05 02:36:46 +02:00
2017-11-01 10:50:03 +01:00
render json: success_json.merge(submissions: all_submissions)
2017-10-05 02:36:46 +02:00
end
2017-09-23 04:34:07 +02:00
end