2019-07-12 03:55:53 +02:00
|
|
|
TagStruct = Struct.new(:id, :name)
|
|
|
|
|
2017-09-23 04:34:07 +02:00
|
|
|
class CustomWizard::Builder
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-10-22 05:37:58 +02:00
|
|
|
attr_accessor :wizard, :updater, :submissions
|
2017-10-13 15:02:34 +02:00
|
|
|
|
2019-06-19 07:23:10 +02:00
|
|
|
def initialize(user=nil, wizard_id)
|
2017-09-29 13:27:03 +02:00
|
|
|
data = PluginStore.get('custom_wizard', wizard_id)
|
2017-10-22 05:37:58 +02:00
|
|
|
return if data.blank?
|
|
|
|
|
2017-11-01 05:21:14 +01:00
|
|
|
@steps = data['steps']
|
2018-05-09 07:06:43 +02:00
|
|
|
@wizard = CustomWizard::Wizard.new(user, data)
|
2019-06-19 07:23:10 +02:00
|
|
|
|
|
|
|
if user
|
|
|
|
@submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id))
|
|
|
|
end
|
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
|
|
|
|
|
2017-11-24 05:32:15 +01:00
|
|
|
USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale']
|
|
|
|
PROFILE_FIELDS = ['location', 'website', 'bio_raw', 'profile_background', 'card_background']
|
|
|
|
|
2018-05-20 05:15:53 +02:00
|
|
|
def self.fill_placeholders(string, user, data)
|
|
|
|
result = string.gsub(/u\{(.*?)\}/) do |match|
|
2017-11-24 05:32:15 +01:00
|
|
|
result = ''
|
2017-11-24 08:53:27 +01:00
|
|
|
result = user.send($1) if USER_FIELDS.include?($1)
|
|
|
|
result = user.user_profile.send($1) if PROFILE_FIELDS.include?($1)
|
2017-11-24 05:32:15 +01:00
|
|
|
result
|
|
|
|
end
|
2018-05-20 05:15:53 +02:00
|
|
|
|
2019-09-11 11:53:51 +02:00
|
|
|
result = result.gsub(/w\{(.*?)\}/) { |match| recurse(data, [*$1.split('.')]) }
|
|
|
|
|
|
|
|
result.gsub(/v\{(.*?)\}/) do |match|
|
|
|
|
attrs = $1.split(':')
|
|
|
|
key = attrs.first
|
|
|
|
format = attrs.length > 1 ? attrs.last : nil
|
|
|
|
v = nil
|
|
|
|
|
|
|
|
if key == 'time'
|
|
|
|
time_format = format.present? ? format : "%B %-d, %Y"
|
|
|
|
v = Time.now.strftime(time_format)
|
|
|
|
end
|
|
|
|
|
|
|
|
v
|
|
|
|
end
|
2019-08-01 03:05:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.recurse(data, keys)
|
|
|
|
k = keys.shift
|
|
|
|
result = data[k]
|
|
|
|
keys.empty? ? result : self.recurse(result, keys)
|
2017-11-24 05:32:15 +01:00
|
|
|
end
|
|
|
|
|
2019-07-02 06:49:14 +02:00
|
|
|
def build(build_opts = {}, params = {})
|
2017-12-25 13:34:33 +01:00
|
|
|
unless (@wizard.completed? && !@wizard.multiple_submissions && !@wizard.user.admin) || !@steps || !@wizard.permitted?
|
2019-01-14 03:53:53 +01:00
|
|
|
|
2019-11-04 18:49:30 +01:00
|
|
|
reset_submissions if build_opts[:reset] || @wizard.restart_on_revisit
|
2019-01-14 03:53:53 +01:00
|
|
|
|
2018-05-20 05:15:53 +02:00
|
|
|
@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']
|
2019-07-02 06:49:14 +02:00
|
|
|
step.permitted = true
|
|
|
|
|
|
|
|
if permitted_params = step_template['permitted_params']
|
|
|
|
permitted_data = {}
|
|
|
|
|
|
|
|
permitted_params.each do |param|
|
|
|
|
key = param['key'].to_sym
|
|
|
|
permitted_data[key] = params[key] if params[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
if permitted_data.present?
|
|
|
|
current_data = @submissions.last || {}
|
|
|
|
save_submissions(current_data.merge(permitted_data), false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if required_data = step_template['required_data']
|
2019-09-24 09:52:09 +02:00
|
|
|
if !@submissions.last && required_data.present?
|
2019-07-02 06:49:14 +02:00
|
|
|
step.permitted = false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
required_data.each do |rd|
|
|
|
|
if rd['connector'] === 'equals'
|
|
|
|
step.permitted = @submissions.last[rd['key']] == @submissions.last[rd['value']]
|
|
|
|
end
|
|
|
|
end
|
2019-07-27 09:01:29 +02:00
|
|
|
|
|
|
|
if !step.permitted
|
|
|
|
step.permitted_message = step_template['required_data_message'] if step_template['required_data_message']
|
|
|
|
next
|
|
|
|
end
|
2019-07-02 06:49:14 +02:00
|
|
|
end
|
2018-05-20 05:15:53 +02:00
|
|
|
|
|
|
|
if step_template['fields'] && step_template['fields'].length
|
|
|
|
step_template['fields'].each do |field_template|
|
2019-01-14 03:53:53 +01:00
|
|
|
append_field(step, step_template, field_template, build_opts)
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
step.on_update do |updater|
|
|
|
|
@updater = updater
|
|
|
|
user = @wizard.user
|
2019-07-27 08:10:26 +02:00
|
|
|
|
2018-05-20 05:15:53 +02:00
|
|
|
if step_template['fields'] && step_template['fields'].length
|
|
|
|
step_template['fields'].each do |field|
|
2018-08-19 02:34:20 +02:00
|
|
|
validate_field(field, updater, step_template) if field['type'] != 'text-only'
|
2017-10-13 15:02:34 +02:00
|
|
|
end
|
2017-10-05 02:36:46 +02:00
|
|
|
end
|
2019-07-27 08:10:26 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
next if updater.errors.any?
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
CustomWizard::Builder.step_handlers.each do |handler|
|
|
|
|
if handler[:wizard_id] == @wizard.id
|
|
|
|
handler[:block].call(self)
|
|
|
|
end
|
2017-10-05 02:36:46 +02:00
|
|
|
end
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
next if updater.errors.any?
|
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
data = updater.fields.to_h
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2017-11-02 08:41:14 +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)
|
2017-11-01 05:21:14 +01:00
|
|
|
end
|
2019-07-27 08:10:26 +02:00
|
|
|
|
2018-05-20 05:15:53 +02:00
|
|
|
if step_template['actions'] && step_template['actions'].length && data
|
|
|
|
step_template['actions'].each do |action|
|
2018-05-20 03:57:34 +02:00
|
|
|
self.send(action['type'].to_sym, user, action, data)
|
|
|
|
end
|
|
|
|
end
|
2017-11-30 03:55:15 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
final_step = updater.step.next.nil?
|
2019-08-29 02:22:02 +02:00
|
|
|
|
|
|
|
if route_to = data['route_to']
|
|
|
|
data.delete('route_to')
|
|
|
|
end
|
2017-11-24 05:32:15 +01:00
|
|
|
|
2018-05-20 03:57:34 +02: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
|
|
|
|
2018-05-24 07:34:58 +02:00
|
|
|
if final_step && @wizard.id === @wizard.user.custom_fields['redirect_to_wizard']
|
2018-05-20 03:57:34 +02:00
|
|
|
@wizard.user.custom_fields.delete('redirect_to_wizard');
|
|
|
|
@wizard.user.save_custom_fields(true)
|
|
|
|
end
|
2017-11-30 03:55:15 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if updater.errors.empty?
|
2019-08-29 01:51:18 +02:00
|
|
|
if final_step
|
2019-08-29 02:22:02 +02:00
|
|
|
updater.result[:redirect_on_complete] = route_to || data['redirect_on_complete']
|
2019-08-29 02:24:10 +02:00
|
|
|
elsif route_to
|
2019-08-29 01:51:18 +02:00
|
|
|
updater.result[:redirect_on_next] = route_to
|
2019-07-02 06:49:14 +02:00
|
|
|
end
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-17 15:17:53 +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
|
|
|
|
|
|
|
|
## If a field updates a profile field, load the current value
|
|
|
|
if step_template['actions'] && step_template['actions'].any?
|
|
|
|
profile_actions = step_template['actions'].select { |a| a['type'] === 'update_profile' }
|
|
|
|
|
|
|
|
if profile_actions.any?
|
|
|
|
profile_actions.each do |action|
|
|
|
|
if update = action['profile_updates'].select { |u| u['key'] === field_template['id'] }.first
|
|
|
|
params[:value] = prefill_profile_field(update)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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-07-19 05:47:17 +02:00
|
|
|
|
2018-07-04 05:48:03 +02:00
|
|
|
field = step.add_field(params)
|
2018-05-20 05:15:53 +02:00
|
|
|
|
|
|
|
if field_template['type'] === 'dropdown'
|
|
|
|
build_dropdown_list(field, field_template)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefill_profile_field(update)
|
|
|
|
attribute = update['value']
|
|
|
|
custom_field = update['value_custom']
|
2018-05-24 07:34:58 +02:00
|
|
|
user_field = update['user_field']
|
2018-05-20 05:15:53 +02:00
|
|
|
|
2018-05-24 07:34:58 +02:00
|
|
|
if user_field || custom_field
|
2019-10-24 20:13:54 +02:00
|
|
|
UserCustomField.where(user_id: @wizard.user.id, name: user_field || custom_field).pluck(:value).first
|
2018-05-20 05:15:53 +02:00
|
|
|
elsif UserProfile.column_names.include? attribute
|
|
|
|
UserProfile.find_by(user_id: @wizard.user.id).send(attribute)
|
|
|
|
elsif User.column_names.include? attribute
|
|
|
|
User.find(@wizard.user.id).send(attribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_dropdown_list(field, field_template)
|
|
|
|
field.dropdown_none = field_template['dropdown_none'] if field_template['dropdown_none']
|
|
|
|
|
|
|
|
if field_template['choices'] && field_template['choices'].length > 0
|
|
|
|
field_template['choices'].each do |c|
|
|
|
|
field.add_choice(c['key'], label: c['value'])
|
|
|
|
end
|
|
|
|
elsif field_template['choices_key'] && field_template['choices_key'].length > 0
|
|
|
|
choices = I18n.t(field_template['choices_key'])
|
|
|
|
|
|
|
|
if choices.is_a?(Hash)
|
|
|
|
choices.each { |k, v| field.add_choice(k, label: v) }
|
|
|
|
end
|
|
|
|
elsif field_template['choices_preset'] && field_template['choices_preset'].length > 0
|
|
|
|
objects = []
|
2019-07-12 03:55:53 +02:00
|
|
|
guardian = Guardian.new(@wizard.user)
|
|
|
|
site = Site.new(guardian)
|
2018-05-20 05:15:53 +02:00
|
|
|
|
|
|
|
if field_template['choices_preset'] === 'categories'
|
2019-07-01 04:31:50 +02:00
|
|
|
objects = site.categories
|
|
|
|
end
|
|
|
|
|
|
|
|
if field_template['choices_preset'] === 'groups'
|
|
|
|
objects = site.groups
|
2018-05-20 05:15:53 +02:00
|
|
|
end
|
|
|
|
|
2019-07-12 03:55:53 +02:00
|
|
|
if field_template['choices_preset'] === 'tags'
|
|
|
|
objects = Tag.top_tags(guardian: guardian).map { |tag| TagStruct.new(tag,tag) }
|
|
|
|
end
|
|
|
|
|
2019-09-11 10:30:38 +02:00
|
|
|
if field_template['choices_preset'] === 'flags'
|
|
|
|
objects = CustomWizard::Flags.list
|
|
|
|
end
|
|
|
|
|
2018-05-20 05:15:53 +02:00
|
|
|
if field_template['choices_filters'] && field_template['choices_filters'].length > 0
|
|
|
|
field_template['choices_filters'].each do |f|
|
|
|
|
objects.reject! do |o|
|
2018-05-21 07:52:39 +02:00
|
|
|
if f['key'].include? 'custom_fields'
|
|
|
|
o.custom_fields[f['key'].split('.')[1]].to_s != f['value'].to_s
|
2018-05-20 05:15:53 +02:00
|
|
|
else
|
2018-05-21 07:52:39 +02:00
|
|
|
o[prop].to_s != f['value'].to_s
|
2018-05-20 05:15:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if objects.length > 0
|
|
|
|
objects.each do |o|
|
|
|
|
field.add_choice(o.id, label: o.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2018-05-20 03:57:34 +02:00
|
|
|
label = field['label'] || I18n.t("#{field['key']}.label")
|
|
|
|
updater.errors.add(field['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
|
|
|
|
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
|
|
|
|
|
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 create_topic(user, action, data)
|
2019-01-22 03:45:49 +01:00
|
|
|
if action['custom_title_enabled']
|
2018-09-18 03:15:11 +02:00
|
|
|
title = CustomWizard::Builder.fill_placeholders(action['custom_title'], user, data)
|
2018-05-20 03:57:34 +02:00
|
|
|
else
|
|
|
|
title = data[action['title']]
|
|
|
|
end
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if action['post_builder']
|
2018-05-20 05:15:53 +02:00
|
|
|
post = CustomWizard::Builder.fill_placeholders(action['post_template'], user, data)
|
2018-05-20 03:57:34 +02:00
|
|
|
else
|
|
|
|
post = data[action['post']]
|
|
|
|
end
|
2019-07-27 08:10:26 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if title
|
|
|
|
params = {
|
|
|
|
title: title,
|
|
|
|
raw: post,
|
|
|
|
skip_validations: true
|
|
|
|
}
|
|
|
|
|
2019-10-02 06:24:02 +02:00
|
|
|
params[:category] = action_category_id(action, data)
|
2019-07-27 08:10:26 +02:00
|
|
|
|
2019-10-03 04:17:33 +02:00
|
|
|
tags = action['tags'] || []
|
|
|
|
params[:tags] = tags
|
2019-07-27 08:10:26 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
topic_custom_fields = {}
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if action['add_fields']
|
|
|
|
action['add_fields'].each do |field|
|
2018-10-07 09:03:53 +02:00
|
|
|
value = field['value_custom'].present? ? field['value_custom'] : data[field['value']]
|
2018-05-20 03:57:34 +02:00
|
|
|
key = field['key']
|
2019-07-26 10:59:21 +02:00
|
|
|
|
2018-06-05 04:54:05 +02:00
|
|
|
if key && (value.present? || value === false)
|
2018-06-05 04:36:56 +02:00
|
|
|
if key.include?('custom_fields')
|
|
|
|
keyArr = key.split('.')
|
2018-05-20 03:57:34 +02:00
|
|
|
|
2018-06-05 04:36:56 +02:00
|
|
|
if keyArr.length === 3
|
|
|
|
custom_key = keyArr.last
|
|
|
|
type = keyArr.first
|
2018-05-20 03:57:34 +02:00
|
|
|
|
2018-06-05 04:36:56 +02:00
|
|
|
if type === 'topic'
|
|
|
|
topic_custom_fields[custom_key] = value
|
|
|
|
elsif type === 'post'
|
|
|
|
params[:custom_fields] ||= {}
|
|
|
|
params[:custom_fields][custom_key.to_sym] = value
|
|
|
|
end
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
2018-06-05 04:36:56 +02:00
|
|
|
else
|
2019-10-03 04:17:33 +02:00
|
|
|
value = [*value] + tags if key === 'tags'
|
2018-06-05 04:36:56 +02:00
|
|
|
params[key.to_sym] = value
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
creator = PostCreator.new(user, params)
|
|
|
|
post = creator.create
|
2018-02-04 08:52:53 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if creator.errors.present?
|
|
|
|
updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
|
|
|
|
else
|
|
|
|
if topic_custom_fields.present?
|
|
|
|
topic_custom_fields.each do |k, v|
|
|
|
|
post.topic.custom_fields[k] = v
|
|
|
|
end
|
|
|
|
post.topic.save_custom_fields(true)
|
|
|
|
end
|
2018-02-04 08:52:53 +01:00
|
|
|
|
2018-09-18 03:56:42 +02:00
|
|
|
unless action['skip_redirect']
|
2019-07-02 06:49:14 +02:00
|
|
|
data['redirect_on_complete'] = post.topic.url
|
2018-09-18 03:56:42 +02:00
|
|
|
end
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-04 08:52:53 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
def send_message(user, action, data)
|
2019-10-01 18:13:15 +02:00
|
|
|
|
2019-10-09 17:02:06 +02:00
|
|
|
if action['required'].present? && data[action['required']].blank?
|
|
|
|
return
|
2019-10-01 19:14:15 +02:00
|
|
|
end
|
2019-10-09 17:02:06 +02:00
|
|
|
|
2019-10-04 23:13:41 +02:00
|
|
|
if action['custom_title_enabled']
|
|
|
|
title = CustomWizard::Builder.fill_placeholders(action['custom_title'], user, data)
|
2019-10-01 18:13:15 +02:00
|
|
|
else
|
2019-10-04 23:13:41 +02:00
|
|
|
title = data[action['title']]
|
2019-10-01 18:13:15 +02:00
|
|
|
end
|
2018-02-04 08:52:53 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if action['post_builder']
|
2018-05-20 05:15:53 +02:00
|
|
|
post = CustomWizard::Builder.fill_placeholders(action['post_template'], user, data)
|
2018-05-20 03:57:34 +02:00
|
|
|
else
|
|
|
|
post = data[action['post']]
|
|
|
|
end
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if title && post
|
|
|
|
creator = PostCreator.new(user,
|
|
|
|
title: title,
|
|
|
|
raw: post,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: action['username']
|
|
|
|
)
|
2017-10-05 02:36:46 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
post = creator.create
|
2017-11-01 10:50:03 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if creator.errors.present?
|
|
|
|
updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
|
|
|
|
else
|
2018-09-18 03:56:42 +02:00
|
|
|
unless action['skip_redirect']
|
2019-07-02 06:49:14 +02:00
|
|
|
data['redirect_on_complete'] = post.topic.url
|
2018-09-18 03:56:42 +02:00
|
|
|
end
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
def update_profile(user, action, data)
|
|
|
|
return unless action['profile_updates'].length
|
2017-11-01 05:21:14 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
attributes = {}
|
|
|
|
custom_fields = {}
|
2017-11-01 10:50:03 +01:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
action['profile_updates'].each do |pu|
|
|
|
|
value = pu['value']
|
2019-07-22 09:16:22 +02:00
|
|
|
custom_field = nil
|
2019-09-11 11:08:00 +02:00
|
|
|
|
|
|
|
if pu['value_custom'].present?
|
2019-07-22 09:16:22 +02:00
|
|
|
custom_parts = pu['value_custom'].split('.')
|
|
|
|
if custom_parts.length == 2 && custom_parts[0] == 'custom_field'
|
|
|
|
custom_field = custom_parts[1]
|
|
|
|
else
|
|
|
|
value = custom_parts[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-24 07:34:58 +02:00
|
|
|
user_field = pu['user_field']
|
2018-05-20 03:57:34 +02:00
|
|
|
key = pu['key']
|
2019-07-26 10:59:21 +02:00
|
|
|
|
2019-10-27 00:51:44 +02:00
|
|
|
return if data[key].blank?
|
|
|
|
|
2018-05-24 07:34:58 +02:00
|
|
|
if user_field || custom_field
|
|
|
|
custom_fields[user_field || custom_field] = data[key]
|
2018-05-20 03:57:34 +02:00
|
|
|
else
|
2019-09-11 11:08:00 +02:00
|
|
|
updater_key = value
|
|
|
|
if ['profile_background', 'card_background'].include?(value)
|
|
|
|
updater_key = "#{value}_upload_url"
|
|
|
|
end
|
|
|
|
attributes[updater_key.to_sym] = data[key] if updater_key
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|
2019-09-11 13:58:57 +02:00
|
|
|
|
|
|
|
if ['user_avatar'].include?(value)
|
|
|
|
this_upload_id = data[key][:id]
|
|
|
|
user.create_user_avatar unless user.user_avatar
|
|
|
|
user.user_avatar.custom_upload_id = this_upload_id
|
|
|
|
user.uploaded_avatar_id = this_upload_id
|
|
|
|
user.save!
|
|
|
|
user.user_avatar.save!
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if custom_fields.present?
|
2018-05-25 02:43:46 +02:00
|
|
|
attributes[:custom_fields] = custom_fields
|
2018-05-20 03:57:34 +02:00
|
|
|
end
|
2019-09-11 11:08:00 +02:00
|
|
|
|
2018-05-20 03:57:34 +02:00
|
|
|
if attributes.present?
|
|
|
|
user_updater = UserUpdater.new(user, user)
|
|
|
|
user_updater.update(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-03 09:09:24 +02:00
|
|
|
def send_to_api(user, action, data)
|
2019-06-04 20:51:24 +02:00
|
|
|
api_body = nil
|
|
|
|
|
|
|
|
if action['api_body'] != ""
|
|
|
|
begin
|
|
|
|
api_body_parsed = JSON.parse(action['api_body'])
|
2019-06-07 01:57:34 +02:00
|
|
|
rescue JSON::ParserError
|
2019-06-04 20:51:24 +02:00
|
|
|
raise Discourse::InvalidParameters, "Invalid API body definition: #{action['api_body']} for #{action['title']}"
|
|
|
|
end
|
2019-08-13 06:11:46 +02:00
|
|
|
api_body = JSON.parse(CustomWizard::Builder.fill_placeholders(JSON.generate(api_body_parsed), user, data))
|
2019-06-04 20:51:24 +02:00
|
|
|
end
|
|
|
|
|
2019-06-16 13:49:51 +02:00
|
|
|
result = CustomWizard::Api::Endpoint.request(user, action['api'], action['api_endpoint'], api_body)
|
2019-06-03 09:09:24 +02:00
|
|
|
|
2019-06-07 01:57:34 +02:00
|
|
|
if error = result['error'] || (result[0] && result[0]['error'])
|
|
|
|
error = error['message'] || error
|
|
|
|
updater.errors.add(:send_to_api, error)
|
2019-06-03 09:09:24 +02:00
|
|
|
else
|
|
|
|
## add validation callback
|
|
|
|
end
|
|
|
|
end
|
2019-08-27 08:05:24 +02:00
|
|
|
|
|
|
|
def open_composer(user, action, data)
|
|
|
|
if action['custom_title_enabled']
|
|
|
|
title = CustomWizard::Builder.fill_placeholders(action['custom_title'], user, data)
|
|
|
|
else
|
|
|
|
title = data[action['title']]
|
|
|
|
end
|
|
|
|
|
|
|
|
url = "/new-topic?title=#{title}"
|
|
|
|
|
|
|
|
if action['post_builder']
|
|
|
|
post = CustomWizard::Builder.fill_placeholders(action['post_template'], user, data)
|
|
|
|
else
|
|
|
|
post = data[action['post']]
|
|
|
|
end
|
|
|
|
|
|
|
|
url += "&body=#{post}"
|
|
|
|
|
2019-10-02 06:24:02 +02:00
|
|
|
if category_id = action_category_id(action, data)
|
|
|
|
if category = Category.find(category_id)
|
2019-08-27 09:28:49 +02:00
|
|
|
url += "&category=#{category.full_slug('/')}"
|
2019-08-27 08:05:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-15 07:34:43 +02:00
|
|
|
if tags = action_tags(action, data)
|
|
|
|
url += "&tags=#{tags.join(',')}"
|
2019-08-27 08:05:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
data['redirect_on_complete'] = Discourse.base_uri + URI.encode(url)
|
|
|
|
end
|
2019-07-12 03:55:53 +02:00
|
|
|
|
2019-07-01 04:31:50 +02:00
|
|
|
def add_to_group(user, action, data)
|
|
|
|
if group_id = data[action['group_id']]
|
|
|
|
if group = Group.find(group_id)
|
|
|
|
group.add(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-02 06:49:14 +02:00
|
|
|
def route_to(user, action, data)
|
|
|
|
url = CustomWizard::Builder.fill_placeholders(action['url'], user, data)
|
|
|
|
if action['code']
|
|
|
|
data[action['code']] = SecureRandom.hex(8)
|
|
|
|
url += "&#{action['code']}=#{data[action['code']]}"
|
|
|
|
end
|
|
|
|
data['route_to'] = URI.encode(url)
|
|
|
|
end
|
2019-06-03 09:09:24 +02:00
|
|
|
|
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
|
2019-10-02 06:24:02 +02:00
|
|
|
|
|
|
|
def action_category_id(action, data)
|
|
|
|
if action['custom_category_enabled']
|
|
|
|
if action['custom_category_wizard_field']
|
2019-10-15 07:34:43 +02:00
|
|
|
data[action['category_id']]
|
2019-10-02 06:24:02 +02:00
|
|
|
elsif action['custom_category_user_field_key']
|
|
|
|
if action['custom_category_user_field_key'].include?('custom_fields')
|
|
|
|
field = action['custom_category_user_field_key'].split('.').last
|
|
|
|
user.custom_fields[field]
|
|
|
|
else
|
|
|
|
user.send(action['custom_category_user_field_key'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
action['category_id']
|
|
|
|
end
|
|
|
|
end
|
2019-10-15 07:34:43 +02:00
|
|
|
|
|
|
|
def action_tags(action, data)
|
|
|
|
if action['custom_tag_enabled']
|
|
|
|
data[action['custom_tag_field']]
|
|
|
|
else
|
|
|
|
action['tags']
|
|
|
|
end
|
|
|
|
end
|
2017-09-23 04:34:07 +02:00
|
|
|
end
|