0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 04:12:53 +01:00
discourse-custom-wizard/lib/builder.rb

128 Zeilen
3,9 KiB
Ruby

2017-09-23 04:34:07 +02:00
class CustomWizard::Builder
2017-10-05 02:36:46 +02:00
def initialize(user, wizard_id)
data = PluginStore.get('custom_wizard', wizard_id)
@custom_wizard = CustomWizard::Wizard.new(data)
2017-09-23 04:34:07 +02:00
@wizard = Wizard.new(user)
@wizard.id = wizard_id
2017-10-05 02:36:46 +02:00
@wizard.save_submissions = data['save_submissions']
2017-10-07 04:27:38 +02:00
@wizard.background = data["background"]
2017-10-05 02:36:46 +02:00
end
def self.sorted_handlers
@sorted_handlers ||= []
end
def self.step_handlers
sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
end
def self.add_step_handler(priority = 0, wizard_id, &block)
sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
@sorted_handlers.sort_by! { |h| -h[:priority] }
2017-09-23 04:34:07 +02:00
end
def build
@custom_wizard.steps.each do |s|
@wizard.append_step(s['id']) do |step|
step.title = s['title'] if s['title']
2017-10-07 04:27:38 +02:00
step.description = s['description'] if s['description']
2017-09-25 16:47:40 +02:00
step.banner = s['banner'] if s['banner']
2017-10-07 04:27:38 +02:00
step.translation_key = s['translation_key'] if s['translation_key']
2017-09-23 04:34:07 +02:00
2017-09-25 16:47:40 +02:00
s['fields'].each do |f|
2017-10-07 04:27:38 +02:00
params = {
id: f['id'],
type: f['type'],
required: f['required']
}
params[:label] = f['label'] if f['label']
params[:description] = f['description'] if f['description']
params[:translation_key] = f['translation_key'] if f['translation_key']
field = step.add_field(params)
2017-09-23 04:34:07 +02:00
2017-09-25 16:47:40 +02:00
if f['type'] == 'dropdown'
f['choices'].each do |c|
field.add_choice(c['id'], label: c['label'])
2017-09-23 04:34:07 +02:00
end
end
end
step.on_update do |updater|
2017-10-05 02:36:46 +02:00
@updater = updater
input = updater.fields
user = @wizard.user
2017-10-09 07:52:09 +02:00
if @wizard.save_submissions && input
2017-10-05 02:36:46 +02:00
store_key = @wizard.id
submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", store_key))
submission = {}
if submissions.last && submissions.last['completed'] === false
submission = submissions.last
submissions.pop(1)
end
submission['user_id'] = @wizard.user.id
submission['completed'] = updater.step.next.nil?
input.each do |key, value|
submission[key] = value
end
submissions.push(submission)
PluginStore.set('custom_wizard_submissions', store_key, submissions)
end
if s['actions'] && s['actions'].length
s['actions'].each do |a|
if a['type'] === 'create_topic'
creator = PostCreator.new(user,
title: input[a['title']],
raw: input[a['post']],
category: a['category_id'],
skip_validations: true)
post = creator.create
if creator.errors.present?
raise StandardError, creator.errors.full_messages.join(" ")
end
updater.result = { topic_id: post.topic.id }
end
if a['type'] === 'send_message'
creator = PostCreator.new(user,
title: input[a['title']],
raw: input[a['post']],
archetype: Archetype.private_message,
target_usernames: a['username'])
post = creator.create
if creator.errors.present?
raise StandardError, creator.errors.full_messages.join(" ")
end
updater.result = { topic_id: post.topic.id }
end
end
end
CustomWizard::Builder.step_handlers.each do |handler|
if handler[:wizard_id] == @wizard.id
handler[:block].call(self)
end
end
2017-09-23 04:34:07 +02:00
end
end
end
@wizard
end
end