2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2017-09-23 04:34:07 +02:00
|
|
|
class CustomWizard::Builder
|
2021-04-20 19:58:19 +02:00
|
|
|
attr_accessor :wizard, :updater, :template
|
2017-10-13 15:02:34 +02:00
|
|
|
|
2021-03-11 07:30:15 +01:00
|
|
|
def initialize(wizard_id, user = nil)
|
2021-04-20 19:58:19 +02:00
|
|
|
@template = CustomWizard::Template.create(wizard_id)
|
|
|
|
return nil if @template.nil?
|
|
|
|
@wizard = CustomWizard::Wizard.new(template.data, user)
|
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
|
|
|
|
2020-04-14 07:46:06 +02:00
|
|
|
def mapper
|
|
|
|
CustomWizard::Mapper.new(
|
|
|
|
user: @wizard.user,
|
2021-06-17 09:50:22 +02:00
|
|
|
data: @wizard.current_submission&.fields_and_meta
|
2020-04-14 07:46:06 +02:00
|
|
|
)
|
|
|
|
end
|
2018-08-19 02:34:20 +02:00
|
|
|
|
2019-07-02 06:49:14 +02:00
|
|
|
def build(build_opts = {}, params = {})
|
2020-04-13 14:17:22 +02:00
|
|
|
return nil if !SiteSetting.custom_wizard_enabled || !@wizard
|
2021-06-16 08:21:24 +02:00
|
|
|
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
|
2019-12-05 07:48:32 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
@template.steps.each do |step_template|
|
|
|
|
next if !check_condition(step_template)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
@wizard.append_step(step_template['id']) do |step|
|
|
|
|
step = check_if_permitted(step, step_template)
|
|
|
|
next if !step.permitted
|
2018-05-20 05:15:53 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
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
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
step.on_update do |updater|
|
|
|
|
@updater = updater
|
2021-06-23 08:13:58 +02:00
|
|
|
@submission = @wizard.current_submission
|
|
|
|
@submission.fields.merge!(@updater.submission)
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
@updater.validate
|
|
|
|
next if @updater.errors.any?
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
apply_step_handlers
|
|
|
|
next if @updater.errors.any?
|
2017-10-13 15:02:34 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
run_step_actions
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
if @updater.errors.empty?
|
2021-06-17 09:50:22 +02:00
|
|
|
route_to = @submission.route_to
|
|
|
|
@submission.route_to = nil
|
|
|
|
@submission.save
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
@wizard.update!
|
2021-04-20 19:58:19 +02:00
|
|
|
@updater.result[:redirect_on_next] = route_to if route_to
|
2021-06-23 08:13:58 +02: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
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
@wizard.update!
|
2018-05-20 03:57:34 +02:00
|
|
|
@wizard
|
|
|
|
end
|
2017-10-17 15:17:53 +02:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
def append_field(step, step_template, field_template, build_opts)
|
2018-05-20 05:15:53 +02:00
|
|
|
params = {
|
|
|
|
id: field_template['id'],
|
|
|
|
type: field_template['type'],
|
2021-04-20 19:58:19 +02:00
|
|
|
required: field_template['required']
|
2018-05-20 05:15:53 +02:00
|
|
|
}
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
%w(label description image key validations min_length max_length char_counter).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
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
if !build_opts[:reset] && (submission = @wizard.current_submission).present?
|
2021-06-17 09:50:22 +02:00
|
|
|
params[:value] = submission.fields[field_template['id']] if submission.fields[field_template['id']]
|
2018-05-20 05:15:53 +02:00
|
|
|
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
|
2018-05-20 05:15:53 +02:00
|
|
|
|
2018-06-29 04:51:59 +02:00
|
|
|
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
|
|
|
|
2019-07-26 10:59:21 +02:00
|
|
|
if field_template['type'] === 'category' || field_template['type'] === 'tag'
|
|
|
|
params[:limit] = field_template['limit']
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2019-07-26 11:12:58 +02:00
|
|
|
if field_template['type'] === 'category'
|
|
|
|
params[:property] = field_template['property']
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2021-02-18 04:40:59 +01:00
|
|
|
if field_template['type'] === 'category' || (
|
|
|
|
field_template['validations'] &&
|
|
|
|
field_template['validations']['similar_topics'] &&
|
|
|
|
field_template['validations']['similar_topics']['categories'].present?
|
|
|
|
)
|
2020-03-19 08:58:45 +01:00
|
|
|
@wizard.needs_categories = true
|
|
|
|
end
|
2021-03-11 07:30:15 +01:00
|
|
|
|
2020-03-19 08:58:45 +01:00
|
|
|
if field_template['type'] === 'group'
|
|
|
|
@wizard.needs_groups = true
|
2019-12-12 05:43:11 +01:00
|
|
|
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,
|
2021-06-17 09:50:22 +02:00
|
|
|
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
|
|
|
|
2020-06-10 11:52:28 +02: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 13:02:14 +02:00
|
|
|
if content[:type] == 'assignment' && field_template['type'] === 'dropdown'
|
|
|
|
content[:result] = content[:result].map do |item|
|
2021-03-11 07:30:15 +01:00
|
|
|
{
|
2020-04-19 13:02:14 +02:00
|
|
|
id: item,
|
|
|
|
name: item
|
|
|
|
}
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
if field_template['index'].present?
|
|
|
|
index = CustomWizard::Mapper.new(
|
|
|
|
inputs: field_template['index'],
|
|
|
|
user: @wizard.user,
|
2021-06-17 09:50:22 +02:00
|
|
|
data: @wizard.current_submission&.fields_and_meta
|
2021-04-20 19:58:19 +02:00
|
|
|
).perform
|
|
|
|
|
|
|
|
params[:index] = index.to_i unless index.nil?
|
|
|
|
end
|
|
|
|
|
2021-04-23 13:51:41 +02:00
|
|
|
if field_template['description'].present?
|
|
|
|
params[:description] = mapper.interpolate(
|
|
|
|
field_template['description'],
|
|
|
|
user: true,
|
|
|
|
value: true,
|
|
|
|
wizard: true,
|
|
|
|
template: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-07-05 08:22:29 +02:00
|
|
|
if field_template['placeholder'].present?
|
|
|
|
params[:placeholder] = mapper.interpolate(
|
|
|
|
field_template['placeholder'],
|
|
|
|
user: true,
|
|
|
|
value: true,
|
|
|
|
wizard: true,
|
|
|
|
template: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-07-04 05:48:03 +02:00
|
|
|
field = step.add_field(params)
|
2018-05-20 05:15:53 +02:00
|
|
|
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,
|
2021-06-17 09:50:22 +02:00
|
|
|
data: @wizard.current_submission&.fields_and_meta
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2018-05-20 05:15:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
def check_condition(template)
|
|
|
|
if template['condition'].present?
|
2021-04-23 08:52:04 +02:00
|
|
|
result = CustomWizard::Mapper.new(
|
2021-04-20 19:58:19 +02:00
|
|
|
inputs: template['condition'],
|
|
|
|
user: @wizard.user,
|
2021-06-17 09:50:22 +02:00
|
|
|
data: @wizard.current_submission&.fields_and_meta,
|
2021-04-23 08:52:04 +02:00
|
|
|
opts: {
|
|
|
|
multiple: true
|
|
|
|
}
|
2021-04-20 19:58:19 +02:00
|
|
|
).perform
|
2021-04-23 08:52:04 +02:00
|
|
|
|
|
|
|
result.any?
|
2021-04-20 19:58:19 +02:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2018-06-29 04:51:59 +02:00
|
|
|
end
|
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
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
|
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
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'],
|
|
|
|
user: true,
|
2021-04-23 13:51:41 +02:00
|
|
|
value: true,
|
|
|
|
wizard: true,
|
|
|
|
template: true
|
2021-04-20 19:58:19 +02:00
|
|
|
)
|
2021-04-23 13:51:41 +02:00
|
|
|
step.description = PrettyText.cook(step.description)
|
2021-04-20 19:58:19 +02:00
|
|
|
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
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-20 19:58:19 +02: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 = {}
|
2021-04-20 19:58:19 +02:00
|
|
|
submission_key = nil
|
|
|
|
params_key = nil
|
2021-06-23 08:13:58 +02:00
|
|
|
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
|
|
|
|
|
2021-06-23 08:13:58 +02:00
|
|
|
if submission_key && params_key && params[params_key].present?
|
|
|
|
submission.permitted_param_keys << submission_key.to_s
|
2021-06-17 09:50:22 +02:00
|
|
|
submission.fields[submission_key] = params[params_key]
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
2020-10-31 08:05:50 +01:00
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2021-06-17 09:50:22 +02:00
|
|
|
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
|
|
|
|
2021-06-23 08:13:58 +02: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|
|
2021-06-17 09:50:22 +02:00
|
|
|
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
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
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
|
2021-06-23 08:13:58 +02:00
|
|
|
result = CustomWizard::Action.new(
|
2021-04-20 19:58:19 +02:00
|
|
|
action: action_template,
|
|
|
|
wizard: @wizard,
|
2021-06-17 09:50:22 +02:00
|
|
|
submission: @submission
|
2021-04-20 19:58:19 +02:00
|
|
|
).perform
|
2021-06-23 08:13:58 +02:00
|
|
|
|
|
|
|
if result.success?
|
|
|
|
@submission = result.submission
|
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|