2017-09-23 04:34:07 +02:00
|
|
|
class CustomWizard::AdminController < ::ApplicationController
|
2017-09-29 13:27:03 +02:00
|
|
|
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
|
|
|
|
render json: { types: CustomWizard::FieldTypes.all }
|
|
|
|
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-09-29 13:27:03 +02:00
|
|
|
saved = false
|
|
|
|
if wizard["existing_id"] && rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
|
|
|
|
rows.each do |r, i|
|
|
|
|
wizard = CustomWizard::Wizard.new(r.value)
|
|
|
|
if wizard.id = wizard["existing_id"]
|
|
|
|
r.update_all(key: wizard['id'], value: wizard)
|
|
|
|
saved = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless saved
|
|
|
|
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
|
|
|
end
|
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-09-29 13:27:03 +02:00
|
|
|
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-05 02:36:46 +02:00
|
|
|
wizard = PluginStore.get('custom_wizard', params[:wizard_id])
|
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
|
2017-09-29 13:27:03 +02:00
|
|
|
rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
wizards = [*rows].map { |r| CustomWizard::Wizard.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
|
|
|
|
|
|
|
def find_submissions
|
|
|
|
params.require(:wizard_id)
|
|
|
|
|
|
|
|
wizard = PluginStore.get('custom_wizard_submissions', params[:wizard_id])
|
|
|
|
|
|
|
|
render json: success_json.merge(submissions: submissions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def submissions
|
|
|
|
rows = PluginStoreRow.where(plugin_name: 'custom_wizard_submissions').order(:id)
|
|
|
|
|
|
|
|
all = [*rows].map do |r|
|
|
|
|
wizard = PluginStore.get('custom_wizard', r.key)
|
|
|
|
name = wizard ? wizard['name'] : r.key
|
|
|
|
{
|
|
|
|
id: r.key,
|
|
|
|
name: name,
|
|
|
|
submissions: ::JSON.parse(r.value)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json.merge(submissions: all)
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|