2017-09-23 10:34:07 +08:00
|
|
|
class CustomWizard::Builder
|
2017-10-22 11:37:58 +08:00
|
|
|
attr_accessor :wizard, :updater, :submissions
|
2017-10-13 21:02:34 +08:00
|
|
|
|
2020-04-13 22:17:22 +10:00
|
|
|
def initialize(wizard_id, user=nil)
|
2020-10-31 18:05:50 +11:00
|
|
|
template = CustomWizard::Template.find(wizard_id)
|
|
|
|
return nil if template.blank?
|
2020-04-13 22:17:22 +10:00
|
|
|
|
2020-10-31 18:05:50 +11:00
|
|
|
@wizard = CustomWizard::Wizard.new(template, user)
|
|
|
|
@steps = template['steps'] || []
|
|
|
|
@actions = template['actions'] || []
|
|
|
|
@submissions = @wizard.submissions
|
2017-10-05 08:36:46 +08: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 10:34:07 +08:00
|
|
|
end
|
2020-04-14 15:46:06 +10:00
|
|
|
|
|
|
|
def mapper
|
|
|
|
CustomWizard::Mapper.new(
|
|
|
|
user: @wizard.user,
|
|
|
|
data: @submissions.last
|
|
|
|
)
|
|
|
|
end
|
2018-08-19 10:34:20 +10:00
|
|
|
|
2019-07-02 14:49:14 +10:00
|
|
|
def build(build_opts = {}, params = {})
|
2020-04-13 22:17:22 +10:00
|
|
|
return nil if !SiteSetting.custom_wizard_enabled || !@wizard
|
|
|
|
return @wizard if !@wizard.can_access?
|
2019-12-05 17:48:32 +11:00
|
|
|
|
2020-10-31 18:05:50 +11:00
|
|
|
build_opts[:reset] = build_opts[:reset] || @wizard.restart_on_revisit
|
2019-12-05 17:48:32 +11:00
|
|
|
|
|
|
|
@steps.each do |step_template|
|
|
|
|
@wizard.append_step(step_template['id']) do |step|
|
2020-10-31 18:05:50 +11:00
|
|
|
step.permitted = true
|
|
|
|
|
|
|
|
if step_template['required_data']
|
|
|
|
step = ensure_required_data(step, step_template)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !step.permitted
|
|
|
|
if step_template['required_data_message']
|
|
|
|
step.permitted_message = step_template['required_data_message']
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
step.title = step_template['title'] if step_template['title']
|
|
|
|
step.banner = step_template['banner'] if step_template['banner']
|
2020-10-31 18:05:50 +11:00
|
|
|
step.key = step_template['key'] if step_template['key']
|
2020-04-14 15:46:06 +10:00
|
|
|
|
|
|
|
if step_template['description']
|
2020-04-16 15:14:03 +10:00
|
|
|
step.description = mapper.interpolate(
|
|
|
|
step_template['description'],
|
|
|
|
user: true,
|
|
|
|
value: true
|
|
|
|
)
|
2020-04-14 15:46:06 +10:00
|
|
|
end
|
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
if permitted_params = step_template['permitted_params']
|
2020-10-31 18:05:50 +11:00
|
|
|
save_permitted_params(permitted_params, params)
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
2018-05-20 13:15:53 +10:00
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
if step_template['fields'] && step_template['fields'].length
|
|
|
|
step_template['fields'].each do |field_template|
|
|
|
|
append_field(step, step_template, field_template, build_opts)
|
2017-09-23 10:34:07 +08:00
|
|
|
end
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
2017-09-23 10:34:07 +08:00
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
step.on_update do |updater|
|
|
|
|
@updater = updater
|
|
|
|
user = @wizard.user
|
|
|
|
|
2020-11-26 14:05:50 +11:00
|
|
|
updater.validate
|
2020-10-20 10:15:03 +11:00
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
next if updater.errors.any?
|
2017-10-05 08:36:46 +08:00
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
CustomWizard::Builder.step_handlers.each do |handler|
|
|
|
|
if handler[:wizard_id] == @wizard.id
|
|
|
|
handler[:block].call(self)
|
2017-10-05 08:36:46 +08:00
|
|
|
end
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
2017-10-05 08:36:46 +08:00
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
next if updater.errors.any?
|
2017-10-13 21:02:34 +08:00
|
|
|
|
2020-11-26 14:05:50 +11:00
|
|
|
submission = updater.submission
|
2017-11-01 12:21:14 +08:00
|
|
|
|
2020-11-26 14:05:50 +11:00
|
|
|
if current_submission = @wizard.current_submission
|
|
|
|
submission = current_submission.merge(submission)
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
2020-04-08 12:52:07 +10:00
|
|
|
|
|
|
|
final_step = updater.step.next.nil?
|
2019-12-09 16:51:42 +11:00
|
|
|
|
2020-04-08 12:52:07 +10:00
|
|
|
if @actions.present?
|
|
|
|
@actions.each do |action|
|
2020-10-20 10:15:03 +11:00
|
|
|
|
2020-04-08 17:59:54 +10:00
|
|
|
if (action['run_after'] === updater.step.id) ||
|
|
|
|
(final_step && (!action['run_after'] || (action['run_after'] === 'wizard_completion')))
|
2020-10-20 10:15:03 +11:00
|
|
|
|
2020-04-08 12:52:07 +10:00
|
|
|
CustomWizard::Action.new(
|
2020-04-15 10:46:44 +10:00
|
|
|
wizard: @wizard,
|
2020-04-08 12:52:07 +10:00
|
|
|
action: action,
|
|
|
|
user: user,
|
2020-11-26 14:05:50 +11:00
|
|
|
data: submission
|
2020-04-08 12:52:07 +10:00
|
|
|
).perform
|
|
|
|
end
|
2018-05-20 11:57:34 +10:00
|
|
|
end
|
2019-12-05 17:48:32 +11:00
|
|
|
end
|
2020-11-03 11:24:20 +11:00
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
if updater.errors.empty?
|
2020-11-26 14:05:50 +11:00
|
|
|
if route_to = submission['route_to']
|
|
|
|
submission.delete('route_to')
|
2020-11-03 11:24:20 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
if @wizard.save_submissions
|
2020-11-26 14:05:50 +11:00
|
|
|
save_submissions(submission, final_step)
|
2020-11-03 11:24:20 +11:00
|
|
|
end
|
|
|
|
|
2019-12-05 17:48:32 +11:00
|
|
|
if final_step
|
2020-11-03 11:24:20 +11:00
|
|
|
if @wizard.id == @wizard.user.custom_fields['redirect_to_wizard']
|
|
|
|
@wizard.user.custom_fields.delete('redirect_to_wizard');
|
|
|
|
@wizard.user.save_custom_fields(true)
|
|
|
|
end
|
|
|
|
|
2020-11-26 14:05:50 +11:00
|
|
|
redirect_url = route_to || submission['redirect_on_complete'] || submission["redirect_to"]
|
2020-05-01 18:16:58 +10:00
|
|
|
updater.result[:redirect_on_complete] = redirect_url
|
2019-12-05 17:48:32 +11:00
|
|
|
elsif route_to
|
|
|
|
updater.result[:redirect_on_next] = route_to
|
2018-05-20 11:57:34 +10:00
|
|
|
end
|
2020-11-03 11:24:20 +11:00
|
|
|
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
2018-05-20 11:57:34 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-13 22:17:22 +10:00
|
|
|
|
2018-05-20 11:57:34 +10:00
|
|
|
@wizard
|
|
|
|
end
|
2017-10-17 21:17:53 +08:00
|
|
|
|
2019-01-14 13:53:53 +11:00
|
|
|
def append_field(step, step_template, field_template, build_opts)
|
2018-05-20 13:15:53 +10:00
|
|
|
params = {
|
|
|
|
id: field_template['id'],
|
|
|
|
type: field_template['type'],
|
|
|
|
required: field_template['required']
|
|
|
|
}
|
2019-10-01 17:13:15 +01:00
|
|
|
|
2018-05-20 13:15:53 +10:00
|
|
|
params[:label] = field_template['label'] if field_template['label']
|
|
|
|
params[:description] = field_template['description'] if field_template['description']
|
|
|
|
params[:image] = field_template['image'] if field_template['image']
|
|
|
|
params[:key] = field_template['key'] if field_template['key']
|
2020-10-31 18:05:50 +11:00
|
|
|
params[:min_length] = field_template['min_length'] if field_template['min_length']
|
2020-12-08 12:44:37 +05:30
|
|
|
params[:max_length] = field_template['max_length'] if field_template['max_length']
|
2021-01-12 16:16:24 +05:30
|
|
|
params[:char_counter] = field_template['char_counter'] if field_template['char_counter']
|
2020-10-31 18:05:50 +11:00
|
|
|
params[:value] = prefill_field(field_template, step_template)
|
2020-11-03 11:24:20 +11:00
|
|
|
|
|
|
|
if !build_opts[:reset] && (submission = @wizard.current_submission)
|
2018-05-20 13:15:53 +10:00
|
|
|
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
|
|
|
end
|
2020-03-30 17:16:03 +11:00
|
|
|
|
2020-04-13 22:17:22 +10:00
|
|
|
if field_template['type'] === 'group' && params[:value].present?
|
2020-03-30 17:16:03 +11:00
|
|
|
params[:value] = params[:value].first
|
|
|
|
end
|
2018-05-20 13:15:53 +10:00
|
|
|
|
2018-06-29 12:51:59 +10:00
|
|
|
if field_template['type'] === 'checkbox'
|
|
|
|
params[:value] = standardise_boolean(params[:value])
|
|
|
|
end
|
|
|
|
|
2019-07-19 13:47:17 +10:00
|
|
|
if field_template['type'] === 'upload'
|
|
|
|
params[:file_types] = field_template['file_types']
|
|
|
|
end
|
2019-07-26 18:59:21 +10:00
|
|
|
|
2020-07-16 13:26:56 +10:00
|
|
|
if ['date', 'time', 'date_time'].include?(field_template['type'])
|
|
|
|
params[:format] = field_template['format']
|
|
|
|
end
|
|
|
|
|
2019-07-26 18:59:21 +10:00
|
|
|
if field_template['type'] === 'category' || field_template['type'] === 'tag'
|
|
|
|
params[:limit] = field_template['limit']
|
|
|
|
end
|
2019-07-26 19:12:58 +10:00
|
|
|
|
|
|
|
if field_template['type'] === 'category'
|
|
|
|
params[:property] = field_template['property']
|
|
|
|
end
|
2019-12-12 15:43:11 +11:00
|
|
|
|
2020-03-19 18:58:45 +11:00
|
|
|
if field_template['type'] === 'category'
|
|
|
|
@wizard.needs_categories = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if field_template['type'] === 'group'
|
|
|
|
@wizard.needs_groups = true
|
2019-12-12 15:43:11 +11:00
|
|
|
end
|
2020-03-22 04:30:11 +11:00
|
|
|
|
2020-04-06 18:36:38 +10:00
|
|
|
if (content_inputs = field_template['content']).present?
|
|
|
|
content = CustomWizard::Mapper.new(
|
|
|
|
inputs: content_inputs,
|
2020-03-30 17:16:03 +11:00
|
|
|
user: @wizard.user,
|
2020-04-06 18:36:38 +10:00
|
|
|
data: @submissions.last,
|
|
|
|
opts: {
|
|
|
|
with_type: true
|
|
|
|
}
|
|
|
|
).perform
|
2020-04-19 21:02:14 +10:00
|
|
|
|
2020-06-10 19:52:28 +10:00
|
|
|
if content.present? &&
|
|
|
|
content[:result].present?
|
|
|
|
|
2020-04-19 16:11:07 +10:00
|
|
|
if content[:type] == 'association'
|
|
|
|
content[:result] = content[:result].map do |item|
|
|
|
|
{
|
|
|
|
id: item[:key],
|
|
|
|
name: item[:value]
|
|
|
|
}
|
|
|
|
end
|
2020-04-06 18:36:38 +10:00
|
|
|
end
|
2020-04-19 16:11:07 +10:00
|
|
|
|
2020-04-19 21:02:14 +10:00
|
|
|
if content[:type] == 'assignment' && field_template['type'] === 'dropdown'
|
|
|
|
content[:result] = content[:result].map do |item|
|
|
|
|
{
|
|
|
|
id: item,
|
|
|
|
name: item
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-19 16:11:07 +10:00
|
|
|
params[:content] = content[:result]
|
2020-04-06 18:36:38 +10:00
|
|
|
end
|
2020-03-24 20:35:46 +11:00
|
|
|
end
|
2020-04-13 22:17:22 +10:00
|
|
|
|
2018-07-04 13:48:03 +10:00
|
|
|
field = step.add_field(params)
|
2018-05-20 13:15:53 +10:00
|
|
|
end
|
2020-03-22 04:30:11 +11:00
|
|
|
|
|
|
|
def prefill_field(field_template, step_template)
|
|
|
|
if (prefill = field_template['prefill']).present?
|
2020-03-30 17:16:03 +11:00
|
|
|
CustomWizard::Mapper.new(
|
|
|
|
inputs: prefill,
|
|
|
|
user: @wizard.user,
|
2020-04-07 18:33:51 +10:00
|
|
|
data: @submissions.last
|
2020-04-06 18:36:38 +10:00
|
|
|
).perform
|
2018-05-20 13:15:53 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-29 12:51:59 +10:00
|
|
|
def standardise_boolean(value)
|
2019-09-11 21:30:59 +10:00
|
|
|
ActiveRecord::Type::Boolean.new.cast(value)
|
2018-06-29 12:51:59 +10:00
|
|
|
end
|
|
|
|
|
2020-11-26 14:05:50 +11:00
|
|
|
def save_submissions(submission, final_step)
|
2018-05-20 11:57:34 +10:00
|
|
|
if final_step
|
2020-11-26 14:05:50 +11:00
|
|
|
submission['submitted_at'] = Time.now.iso8601
|
2018-05-20 11:57:34 +10:00
|
|
|
end
|
|
|
|
|
2020-11-26 14:05:50 +11:00
|
|
|
if submission.present?
|
2018-05-20 11:57:34 +10:00
|
|
|
@submissions.pop(1) if @wizard.unfinished?
|
2020-11-26 14:05:50 +11:00
|
|
|
@submissions.push(submission)
|
2020-11-03 11:24:20 +11:00
|
|
|
@wizard.set_submissions(@submissions)
|
2018-05-20 11:57:34 +10:00
|
|
|
end
|
2017-09-23 10:34:07 +08:00
|
|
|
end
|
2020-10-31 18:05:50 +11:00
|
|
|
|
|
|
|
def save_permitted_params(permitted_params, params)
|
|
|
|
permitted_data = {}
|
|
|
|
|
|
|
|
permitted_params.each do |pp|
|
|
|
|
pair = pp['pairs'].first
|
|
|
|
params_key = pair['key'].to_sym
|
|
|
|
submission_key = pair['value'].to_sym
|
|
|
|
permitted_data[submission_key] = params[params_key] if params[params_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
if permitted_data.present?
|
|
|
|
current_data = @submissions.last || {}
|
|
|
|
save_submissions(current_data.merge(permitted_data), false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if pairs.any? && !@submissions.last
|
|
|
|
step.permitted = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
pairs.each do |pair|
|
|
|
|
pair['key'] = @submissions.last[pair['key']]
|
|
|
|
end
|
|
|
|
|
|
|
|
if !mapper.validate_pairs(pairs)
|
|
|
|
step.permitted = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
step
|
|
|
|
end
|
2017-09-23 10:34:07 +08:00
|
|
|
end
|