0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-13 21:42:53 +01:00
discourse-custom-wizard/lib/custom_wizard/builder.rb

351 Zeilen
9,2 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2017-09-23 04:34:07 +02:00
class CustomWizard::Builder
attr_accessor :wizard, :updater, :template
2017-10-13 15:02:34 +02:00
2023-01-18 19:53:36 +01:00
def initialize(wizard_id, user = nil, guest_id = nil)
@template = CustomWizard::Template.create(wizard_id)
return nil if @template.nil?
2023-01-18 19:53:36 +01:00
@wizard = CustomWizard::Wizard.new(template.data, user, guest_id)
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
2021-03-11 07:30:15 +01:00
def build(build_opts = {}, params = {})
2020-04-13 14:17:22 +02:00
return nil if !SiteSetting.custom_wizard_enabled || !@wizard
return @wizard if !@wizard.can_access? && !build_opts[:force]
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
build_opts[:reset] = build_opts[:reset] || @wizard.restart_on_revisit
@template.steps.each do |step_template|
next if !check_condition(step_template)
2021-03-11 07:30:15 +01:00
@wizard.append_step(step_template['id']) do |step|
step = check_if_permitted(step, step_template)
next if !step.permitted
save_permitted_params(step_template, params)
step = add_step_attributes(step, step_template)
step = append_step_fields(step, step_template, build_opts)
2017-09-23 04:34:07 +02:00
step.on_update do |updater|
@updater = updater
@submission = @wizard.current_submission
@submission.fields.merge!(@updater.submission)
2021-03-11 07:30:15 +01:00
@updater.validate
next if @updater.errors.any?
2017-10-05 02:36:46 +02:00
apply_step_handlers
next if @updater.errors.any?
2017-10-13 15:02:34 +02:00
run_step_actions
2021-03-11 07:30:15 +01:00
if @updater.errors.empty?
route_to = @submission.route_to
@submission.route_to = nil
@submission.save
2021-03-11 07:30:15 +01:00
@wizard.update!
@updater.result[:redirect_on_next] = route_to if route_to
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
true
else
false
2018-05-20 03:57:34 +02:00
end
end
end
end
2021-03-11 07:30:15 +01:00
@wizard.update!
CustomWizard::Submission.cleanup_incomplete_submissions(@wizard)
2018-05-20 03:57:34 +02:00
@wizard
end
2017-10-17 15:17:53 +02:00
2021-09-07 14:06:13 +02:00
def check_condition(template)
if template['condition'].present?
result = CustomWizard::Mapper.new(
inputs: template['condition'],
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta,
opts: {
multiple: true
}
).perform
result.any?
else
true
end
end
private
def mapper
CustomWizard::Mapper.new(
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta
)
end
def append_field(step, step_template, field_template, build_opts)
params = {
id: field_template['id'],
type: field_template['type'],
required: field_template['required']
}
2021-03-11 07:30:15 +01:00
%w(label description image key validations min_length max_length char_counter tag_groups).each do |key|
params[key.to_sym] = field_template[key] if field_template[key]
end
2020-10-31 08:05:50 +01:00
params[:value] = prefill_field(field_template, step_template)
2021-03-11 07:30:15 +01:00
if !build_opts[:reset] && (submission = @wizard.current_submission).present?
params[:value] = submission.fields[field_template['id']] if submission.fields[field_template['id']]
end
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
if field_template['type'] === 'group' && params[:value].present?
2020-03-30 08:16:03 +02:00
params[:value] = params[:value].first
end
if field_template['type'] === 'checkbox'
params[:value] = standardise_boolean(params[:value])
end
2019-07-19 05:47:17 +02:00
if field_template['type'] === 'upload'
params[:file_types] = field_template['file_types']
end
2021-03-11 07:30:15 +01:00
2020-07-16 05:26:56 +02:00
if ['date', 'time', 'date_time'].include?(field_template['type'])
params[:format] = field_template['format']
end
2021-03-11 07:30:15 +01:00
if field_template['type'] === 'category' || field_template['type'] === 'tag'
params[:limit] = field_template['limit']
end
2021-03-11 07:30:15 +01:00
2022-10-26 10:04:50 +02:00
if field_template['type'] === 'tag'
params[:can_create_tag] = standardise_boolean(field_template['can_create_tag'])
end
if field_template['type'] === 'category'
params[:property] = field_template['property']
end
2021-03-11 07:30:15 +01:00
2020-04-06 10:36:38 +02:00
if (content_inputs = field_template['content']).present?
content = CustomWizard::Mapper.new(
inputs: content_inputs,
2020-03-30 08:16:03 +02:00
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta,
2020-04-06 10:36:38 +02:00
opts: {
with_type: true
}
).perform
2021-03-11 07:30:15 +01:00
if content.present? &&
content[:result].present?
2021-03-11 07:30:15 +01:00
2020-04-19 08:11:07 +02:00
if content[:type] == 'association'
content[:result] = content[:result].map do |item|
2021-03-11 07:30:15 +01:00
{
2020-04-19 08:11:07 +02:00
id: item[:key],
2021-03-11 07:30:15 +01:00
name: item[:value]
2020-04-19 08:11:07 +02:00
}
end
2020-04-06 10:36:38 +02:00
end
2021-03-11 07:30:15 +01:00
2020-04-19 08:11:07 +02:00
params[:content] = content[:result]
2020-04-06 10:36:38 +02:00
end
2020-03-24 10:35:46 +01:00
end
2021-03-11 07:30:15 +01:00
if field_template['index'].present?
index = CustomWizard::Mapper.new(
inputs: field_template['index'],
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta
).perform
params[:index] = index.to_i unless index.nil?
end
if field_template['description'].present?
params[:description] = mapper.interpolate(
field_template['description'],
2023-01-18 19:53:36 +01:00
user: @wizard.user,
value: true,
wizard: true,
template: true
)
end
if field_template['preview_template'].present?
preview_template = mapper.interpolate(
field_template['preview_template'],
2023-01-18 19:53:36 +01:00
user: @wizard.user,
value: true,
wizard: true,
template: true
)
params[:preview_template] = PrettyText.cook(preview_template)
end
2021-07-05 08:22:29 +02:00
if field_template['placeholder'].present?
params[:placeholder] = mapper.interpolate(
field_template['placeholder'],
2023-01-18 19:53:36 +01:00
user: @wizard.user,
2021-07-05 08:22:29 +02:00
value: true,
wizard: true,
template: true
)
end
2018-07-04 05:48:03 +02:00
field = step.add_field(params)
end
2021-03-11 07:30:15 +01:00
2020-03-21 18:30:11 +01:00
def prefill_field(field_template, step_template)
if (prefill = field_template['prefill']).present?
2020-03-30 08:16:03 +02:00
CustomWizard::Mapper.new(
inputs: prefill,
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta
2020-04-06 10:36:38 +02:00
).perform
end
end
def check_if_permitted(step, step_template)
step.permitted = true
if step_template['required_data']
step = ensure_required_data(step, step_template)
2018-05-20 03:57:34 +02:00
end
if !step.permitted
if step_template['required_data_message']
step.permitted_message = step_template['required_data_message']
end
end
step
end
def add_step_attributes(step, step_template)
%w(index title banner key force_final).each do |attr|
step.send("#{attr}=", step_template[attr]) if step_template[attr]
end
if step_template['description']
step.description = mapper.interpolate(
step_template['description'],
2023-01-18 19:53:36 +01:00
user: @wizard.user,
value: true,
wizard: true,
template: true
)
step.description = PrettyText.cook(step.description)
end
step
end
def append_step_fields(step, step_template, build_opts)
if step_template['fields'] && step_template['fields'].length
step_template['fields'].each do |field_template|
next if !check_condition(field_template)
append_field(step, step_template, field_template, build_opts)
end
2018-05-20 03:57:34 +02:00
end
step.update_field_order!
step
end
def standardise_boolean(value)
ActiveRecord::Type::Boolean.new.cast(value)
2017-09-23 04:34:07 +02:00
end
2021-03-11 07:30:15 +01:00
def save_permitted_params(step_template, params)
return unless step_template['permitted_params'].present?
permitted_params = step_template['permitted_params']
2020-10-31 08:05:50 +01:00
permitted_data = {}
submission_key = nil
params_key = nil
submission = @wizard.current_submission
2020-10-31 08:05:50 +01:00
permitted_params.each do |pp|
pair = pp['pairs'].first
params_key = pair['key'].to_sym
submission_key = pair['value'].to_sym
if submission_key && params_key && params[params_key].present?
submission.permitted_param_keys << submission_key.to_s
submission.fields[submission_key] = params[params_key]
end
2020-10-31 08:05:50 +01:00
end
submission.save
2020-10-31 08:05:50 +01:00
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
def ensure_required_data(step, step_template)
step_template['required_data'].each do |required|
pairs = required['pairs'].select do |pair|
pair['key'].present? && pair['value'].present?
end
2021-03-11 07:30:15 +01:00
if pairs.any? && !@wizard.current_submission.present?
2020-10-31 08:05:50 +01:00
step.permitted = false
break
end
2021-03-11 07:30:15 +01:00
pairs.each do |pair|
pair['key'] = @wizard.current_submission.fields[pair['key']]
2020-10-31 08:05:50 +01:00
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
if !mapper.validate_pairs(pairs)
step.permitted = false
break
end
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
step
end
def apply_step_handlers
CustomWizard::Builder.step_handlers.each do |handler|
if handler[:wizard_id] == @wizard.id
handler[:block].call(self)
end
end
end
def run_step_actions
if @template.actions.present?
@template.actions.each do |action_template|
if action_template['run_after'] === updater.step.id
result = CustomWizard::Action.new(
action: action_template,
wizard: @wizard,
submission: @submission
).perform
if result.success?
@submission = result.submission
end
end
end
end
end
2017-09-23 04:34:07 +02:00
end