2017-09-23 04:34:07 +02:00
|
|
|
class CustomWizard::Builder
|
2017-10-22 05:37:58 +02:00
|
|
|
attr_accessor :wizard, :updater, :submissions
|
2017-10-13 15:02:34 +02:00
|
|
|
|
2020-04-13 14:17:22 +02:00
|
|
|
def initialize(wizard_id, user=nil)
|
|
|
|
params = CustomWizard::Wizard.find(wizard_id)
|
|
|
|
return nil if params.blank?
|
|
|
|
|
|
|
|
@wizard = CustomWizard::Wizard.new(params, user)
|
|
|
|
@steps = params['steps'] || []
|
|
|
|
@actions = params['actions'] || []
|
|
|
|
@submissions = @wizard.submissions if user && @wizard
|
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
|
|
|
|
|
2018-08-19 02:34:20 +02:00
|
|
|
def self.sorted_field_validators
|
|
|
|
@sorted_field_validators ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.field_validators
|
|
|
|
sorted_field_validators.map { |h| { type: h[:type], block: h[:block] } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_field_validator(priority = 0, type, &block)
|
|
|
|
sorted_field_validators << { priority: priority, type: type, block: block }
|
|
|
|
@sorted_field_validators.sort_by! { |h| -h[:priority] }
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
return @wizard if !@wizard.can_access?
|
2019-12-05 07:48:32 +01:00
|
|
|
|
|
|
|
reset_submissions if build_opts[:reset]
|
|
|
|
|
|
|
|
@steps.each do |step_template|
|
|
|
|
@wizard.append_step(step_template['id']) do |step|
|
|
|
|
step.title = step_template['title'] if step_template['title']
|
|
|
|
step.description = step_template['description'] if step_template['description']
|
|
|
|
step.banner = step_template['banner'] if step_template['banner']
|
|
|
|
step.key = step_template['key'] if step_template['key']
|
|
|
|
step.permitted = true
|
|
|
|
|
|
|
|
if permitted_params = step_template['permitted_params']
|
|
|
|
permitted_data = {}
|
|
|
|
|
2019-12-06 10:05:19 +01:00
|
|
|
permitted_params.each do |p|
|
2020-03-21 18:30:11 +01:00
|
|
|
pair = p['pairs'].first
|
|
|
|
params_key = pair['key'].to_sym
|
|
|
|
submission_key = pair['value'].to_sym
|
2019-12-06 10:05:19 +01:00
|
|
|
permitted_data[submission_key] = params[params_key] if params[params_key]
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
2019-07-02 06:49:14 +02:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
if permitted_data.present?
|
|
|
|
current_data = @submissions.last || {}
|
|
|
|
save_submissions(current_data.merge(permitted_data), false)
|
2019-07-02 06:49:14 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
2019-07-02 06:49:14 +02:00
|
|
|
|
2020-03-21 18:30:11 +01:00
|
|
|
if (required_data = step_template['required_data']).present?
|
|
|
|
has_required_data = true
|
|
|
|
pairs =
|
|
|
|
|
|
|
|
required_data.each do |required|
|
|
|
|
required['pairs'].each do |pair|
|
|
|
|
if pair['key'].blank? || pair['value'].blank?
|
|
|
|
has_required_data = false
|
|
|
|
end
|
2019-07-27 09:01:29 +02:00
|
|
|
end
|
2019-07-02 06:49:14 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
|
2020-03-21 18:30:11 +01:00
|
|
|
if has_required_data
|
|
|
|
if !@submissions.last
|
|
|
|
step.permitted = false
|
|
|
|
else
|
|
|
|
required_data.each do |required|
|
2020-03-30 08:16:03 +02:00
|
|
|
pairs = required['pairs'].map do |p|
|
|
|
|
p['key'] = @submissions.last[p['key']]
|
|
|
|
end
|
|
|
|
|
|
|
|
unless CustomWizard::Mapper.new(
|
|
|
|
user: @wizard.user,
|
|
|
|
data: @submissions.last
|
|
|
|
).validate_pairs(pairs)
|
|
|
|
step.permitted = false
|
|
|
|
end
|
2020-03-21 18:30:11 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if !step.permitted
|
2020-03-30 08:16:03 +02:00
|
|
|
if step_template['required_data_message']
|
|
|
|
step.permitted_message = step_template['required_data_message']
|
|
|
|
end
|
|
|
|
|
2020-03-21 18:30:11 +01:00
|
|
|
next
|
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
|
|
|
end
|
2018-05-20 05:15:53 +02:00
|
|
|
|
2019-12-05 07:48:32 +01: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 04:34:07 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
step.on_update do |updater|
|
|
|
|
@updater = updater
|
|
|
|
user = @wizard.user
|
|
|
|
|
|
|
|
if step_template['fields'] && step_template['fields'].length
|
|
|
|
step_template['fields'].each do |field|
|
2020-04-11 08:22:12 +02:00
|
|
|
validate_field(field, updater, step_template) if field['type'] != 'text_only'
|
2017-10-05 02:36:46 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
next if updater.errors.any?
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
CustomWizard::Builder.step_handlers.each do |handler|
|
|
|
|
if handler[:wizard_id] == @wizard.id
|
|
|
|
handler[:block].call(self)
|
2017-10-05 02:36:46 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
next if updater.errors.any?
|
2017-10-13 15:02:34 +02:00
|
|
|
|
2019-12-09 02:43:30 +01:00
|
|
|
data = updater.fields
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
## if the wizard has data from the previous steps make that accessible to the actions.
|
|
|
|
if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
|
|
|
|
submission = @submissions.last
|
|
|
|
data = submission.merge(data)
|
|
|
|
end
|
2020-04-08 04:52:07 +02:00
|
|
|
|
|
|
|
final_step = updater.step.next.nil?
|
2019-12-09 06:51:42 +01:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
if @actions.present?
|
|
|
|
@actions.each do |action|
|
2020-04-13 14:17:22 +02:00
|
|
|
|
2020-04-08 09:59:54 +02:00
|
|
|
if (action['run_after'] === updater.step.id) ||
|
|
|
|
(final_step && (!action['run_after'] || (action['run_after'] === 'wizard_completion')))
|
2020-04-13 14:17:22 +02:00
|
|
|
|
2020-04-08 04:52:07 +02:00
|
|
|
CustomWizard::Action.new(
|
|
|
|
action: action,
|
|
|
|
user: user,
|
|
|
|
data: data,
|
|
|
|
updater: updater
|
|
|
|
).perform
|
|
|
|
end
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
2019-12-05 07:48:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if route_to = data['route_to']
|
|
|
|
data.delete('route_to')
|
|
|
|
end
|
2017-11-24 05:32:15 +01:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
if @wizard.save_submissions && updater.errors.empty?
|
|
|
|
save_submissions(data, final_step)
|
|
|
|
elsif final_step
|
|
|
|
PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
|
|
|
|
end
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
if final_step && @wizard.id === @wizard.user.custom_fields['redirect_to_wizard']
|
|
|
|
@wizard.user.custom_fields.delete('redirect_to_wizard');
|
|
|
|
@wizard.user.save_custom_fields(true)
|
|
|
|
end
|
2017-11-30 03:55:15 +01:00
|
|
|
|
2019-12-05 07:48:32 +01:00
|
|
|
if updater.errors.empty?
|
|
|
|
if final_step
|
|
|
|
updater.result[:redirect_on_complete] = route_to || data['redirect_on_complete']
|
|
|
|
elsif route_to
|
|
|
|
updater.result[:redirect_on_next] = route_to
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-13 14:17:22 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
@wizard
|
|
|
|
end
|
2017-10-17 15:17:53 +02:00
|
|
|
|
2019-01-14 03:53:53 +01: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'],
|
|
|
|
required: field_template['required']
|
|
|
|
}
|
2019-10-01 18:13:15 +02:00
|
|
|
|
2018-05-20 05:15:53 +02: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']
|
|
|
|
|
|
|
|
## Load previously submitted values
|
2019-01-14 03:53:53 +01:00
|
|
|
if !build_opts[:reset] && @submissions.last && !@submissions.last.key?("submitted_at")
|
2018-05-20 05:15:53 +02:00
|
|
|
submission = @submissions.last
|
|
|
|
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
|
|
|
end
|
2019-12-06 10:05:19 +01:00
|
|
|
|
2020-03-21 18:30:11 +01:00
|
|
|
params[:value] = prefill_field(field_template, step_template) || params[:value]
|
2020-03-30 08:16:03 +02: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
|
2019-07-26 10:59:21 +02:00
|
|
|
|
|
|
|
if field_template['type'] === 'category' || field_template['type'] === 'tag'
|
|
|
|
params[:limit] = field_template['limit']
|
|
|
|
end
|
2019-07-26 11:12:58 +02:00
|
|
|
|
|
|
|
if field_template['type'] === 'category'
|
|
|
|
params[:property] = field_template['property']
|
|
|
|
end
|
2019-12-12 05:43:11 +01:00
|
|
|
|
2020-03-19 08:58:45 +01:00
|
|
|
if field_template['type'] === 'category'
|
|
|
|
@wizard.needs_categories = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if field_template['type'] === 'group'
|
|
|
|
@wizard.needs_groups = true
|
2019-12-12 05:43:11 +01:00
|
|
|
end
|
2020-03-21 18:30:11 +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,
|
2020-04-06 10:36:38 +02:00
|
|
|
data: @submissions.last,
|
|
|
|
opts: {
|
|
|
|
with_type: true
|
|
|
|
}
|
|
|
|
).perform
|
2020-04-07 09:54:30 +02:00
|
|
|
|
2020-04-06 10:36:38 +02:00
|
|
|
if content[:type] == 'association'
|
|
|
|
content[:result] = content[:result].map do |item|
|
|
|
|
{
|
|
|
|
id: item[:key],
|
|
|
|
name: item[:value]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
params[:content] = content[:result]
|
2020-03-24 10:35:46 +01:00
|
|
|
end
|
2020-04-13 14:17:22 +02:00
|
|
|
|
2018-07-04 05:48:03 +02:00
|
|
|
field = step.add_field(params)
|
2018-05-20 05:15:53 +02:00
|
|
|
end
|
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,
|
2020-04-07 10:33:51 +02:00
|
|
|
data: @submissions.last
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2018-05-20 05:15:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-19 02:34:20 +02:00
|
|
|
def validate_field(field, updater, step_template)
|
2018-05-20 03:57:34 +02:00
|
|
|
value = updater.fields[field['id']]
|
2019-06-11 09:19:46 +02:00
|
|
|
min_length = false
|
2019-12-09 02:43:30 +01:00
|
|
|
label = field['label'] || I18n.t("#{field['key']}.label")
|
|
|
|
|
|
|
|
if field['required'] && !value
|
|
|
|
updater.errors.add(field['id'].to_s, I18n.t('wizard.field.required', label: label))
|
|
|
|
end
|
2019-06-11 09:19:46 +02:00
|
|
|
|
|
|
|
if is_text_type(field)
|
|
|
|
min_length = field['min_length']
|
|
|
|
end
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2019-01-22 03:11:42 +01:00
|
|
|
if min_length && value.is_a?(String) && value.strip.length < min_length.to_i
|
2020-03-30 08:16:03 +02:00
|
|
|
updater.errors.add(
|
|
|
|
field['id'].to_s,
|
|
|
|
I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)
|
|
|
|
)
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
2018-06-05 05:11:52 +02:00
|
|
|
|
2020-03-23 19:40:11 +01:00
|
|
|
if is_url_type(field)
|
|
|
|
if !check_if_url(value)
|
|
|
|
updater.errors.add(field['id'].to_s, I18n.t('wizard.field.not_url', label: label))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-05 05:11:52 +02:00
|
|
|
## ensure all checkboxes are booleans
|
|
|
|
if field['type'] === 'checkbox'
|
2018-06-29 04:51:59 +02:00
|
|
|
updater.fields[field['id']] = standardise_boolean(value)
|
2018-06-05 05:11:52 +02:00
|
|
|
end
|
2018-08-19 02:34:20 +02:00
|
|
|
|
|
|
|
CustomWizard::Builder.field_validators.each do |validator|
|
|
|
|
if field['type'] === validator[:type]
|
|
|
|
validator[:block].call(field, updater, step_template)
|
|
|
|
end
|
|
|
|
end
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2019-06-11 09:19:46 +02:00
|
|
|
def is_text_type(field)
|
|
|
|
['text', 'textarea'].include? field['type']
|
|
|
|
end
|
|
|
|
|
2020-03-23 19:40:11 +01:00
|
|
|
def is_url_type(field)
|
|
|
|
['url'].include? field['type']
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_if_url(value)
|
|
|
|
value =~ URI::regexp
|
|
|
|
end
|
|
|
|
|
2018-06-29 04:51:59 +02:00
|
|
|
def standardise_boolean(value)
|
2019-09-11 13:30:59 +02:00
|
|
|
ActiveRecord::Type::Boolean.new.cast(value)
|
2018-06-29 04:51:59 +02:00
|
|
|
end
|
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
def save_submissions(data, final_step)
|
|
|
|
if final_step
|
|
|
|
data['submitted_at'] = Time.now.iso8601
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.present?
|
|
|
|
@submissions.pop(1) if @wizard.unfinished?
|
|
|
|
@submissions.push(data)
|
|
|
|
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|
2019-01-14 03:53:53 +01:00
|
|
|
|
|
|
|
def reset_submissions
|
|
|
|
@submissions.pop(1) if @wizard.unfinished?
|
|
|
|
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
|
|
|
|
@wizard.reset
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|