1
0
Fork 0
discourse-custom-wizard-unl.../controllers/admin.rb

149 Zeilen
3,8 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
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"]
error = 'after_time_need_time'
else
after_time_scheduled = Time.parse(wizard["after_time_scheduled"]).utc
begin
if after_time_scheduled < Time.now.utc
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
if f["type"] === 'dropdown'
choices = f["choices"]
if (!choices || choices.length < 1) && !f["choices_key"] && !f["choices_categories"]
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
existing = PluginStore.get('custom_wizard', wizard['id']) || {}
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
2017-11-01 10:50:03 +01:00
if wizard['after_time'] && new_time
2017-11-01 05:21:14 +01:00
Jobs.cancel_scheduled_job(:set_after_time_wizard)
Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
end
if existing['after_time'] && !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
2017-10-13 15:02:34 +02:00
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)
username = User.find(r.key).username
submissions.map { |s| { username: username }.merge!(s) }
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