builder.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. class CustomWizard::Builder
  2. attr_accessor :wizard, :updater, :submissions
  3. def initialize(user, wizard_id)
  4. data = PluginStore.get('custom_wizard', wizard_id)
  5. return if data.blank?
  6. @steps = data['steps']
  7. @wizard = CustomWizard::Wizard.new(user,
  8. id: wizard_id,
  9. save_submissions: data['save_submissions'],
  10. multiple_submissions: data['multiple_submissions'],
  11. background: data["background"],
  12. name: data["name"],
  13. after_time: data["after_time"],
  14. after_signup: data["after_signup"],
  15. required: data["required"]
  16. )
  17. @submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id))
  18. end
  19. def self.sorted_handlers
  20. @sorted_handlers ||= []
  21. end
  22. def self.step_handlers
  23. sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
  24. end
  25. def self.add_step_handler(priority = 0, wizard_id, &block)
  26. sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
  27. @sorted_handlers.sort_by! { |h| -h[:priority] }
  28. end
  29. def build
  30. unless (@wizard.completed? && !@wizard.respond_to?(:multiple_submissions)) ||
  31. !@steps
  32. @steps.each do |s|
  33. @wizard.append_step(s['id']) do |step|
  34. step.title = s['title'] if s['title']
  35. step.description = s['description'] if s['description']
  36. step.banner = s['banner'] if s['banner']
  37. step.key = s['key'] if s['key']
  38. if s['fields'] && s['fields'].length
  39. s['fields'].each do |f|
  40. params = {
  41. id: f['id'],
  42. type: f['type'],
  43. required: f['required']
  44. }
  45. params[:label] = f['label'] if f['label']
  46. params[:description] = f['description'] if f['description']
  47. params[:key] = f['key'] if f['key']
  48. if @submissions.last && @wizard.unfinished?
  49. submission = @submissions.last
  50. params[:value] = submission[f['id']] if submission[f['id']]
  51. end
  52. if s['actions'] && s['actions'].any?
  53. profile_actions = s['actions'].select { |a| a['type'] === 'update_profile' }
  54. if profile_actions.any?
  55. profile_actions.each do |action|
  56. if update = action['profile_updates'].select { |u| u['key'] === f['id'] }.first
  57. attribute = update['value']
  58. if UserProfile.column_names.include? attribute
  59. params[:value] = UserProfile.find_by(user_id: @wizard.user.id).send(attribute)
  60. elsif User.column_names.include? attribute
  61. params[:value] = User.find(@wizard.user.id).send(attribute)
  62. end
  63. end
  64. end
  65. end
  66. end
  67. field = step.add_field(params)
  68. if f['type'] === 'dropdown'
  69. if f['choices'] && f['choices'].length > 0
  70. f['choices'].each do |c|
  71. field.add_choice(c['value'], label: c['label'])
  72. end
  73. elsif f['choices_key'] && f['choices_key'].length > 0
  74. choices = I18n.t(f['choices_key'])
  75. if choices.is_a?(Hash)
  76. choices.each do |k, v|
  77. field.add_choice(k, label: v)
  78. end
  79. end
  80. elsif f['choices_preset'] && f['choices_preset'].length > 0
  81. objects = []
  82. if f['choices_preset'] === 'categories'
  83. objects = Site.new(Guardian.new(@wizard.user)).categories
  84. end
  85. if f['choices_filters'] && f['choices_filters'].length > 0
  86. f['choices_filters'].each do |f|
  87. objects.reject! do |o|
  88. prop = f['key']
  89. if prop.include? 'custom_fields'
  90. o.custom_fields[prop.split('.')[1]].to_s != f['value'].to_s
  91. else
  92. o[prop].to_s != f['value'].to_s
  93. end
  94. end
  95. end
  96. end
  97. if objects.length > 0
  98. objects.each do |o|
  99. field.add_choice(o.id, label: o.name)
  100. end
  101. end
  102. end
  103. end
  104. end
  105. end
  106. step.on_update do |updater|
  107. @updater = updater
  108. user = @wizard.user
  109. if s['fields'] && s['fields'].length
  110. s['fields'].each do |f|
  111. value = updater.fields[f['id']]
  112. min_length = f['min_length']
  113. if min_length && value.is_a?(String) && value.length < min_length.to_i
  114. label = f['label'] || I18n.t("#{f['key']}.label")
  115. updater.errors.add(f['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
  116. end
  117. end
  118. end
  119. next if updater.errors.any?
  120. CustomWizard::Builder.step_handlers.each do |handler|
  121. if handler[:wizard_id] == @wizard.id
  122. handler[:block].call(self)
  123. end
  124. end
  125. next if updater.errors.any?
  126. step_input = updater.fields.to_h
  127. data = step_input
  128. final_step = updater.step.next.nil?
  129. ## if the wizard has data from the previous steps make that accessible to the actions.
  130. if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
  131. submission = @submissions.last
  132. data = submission.merge(data)
  133. end
  134. if s['actions'] && s['actions'].length
  135. s['actions'].each do |a|
  136. if a['type'] === 'create_topic' && data
  137. title = data[a['title']]
  138. post = data[a['post']]
  139. if title
  140. params = {
  141. title: title,
  142. raw: post,
  143. skip_validations: true
  144. }
  145. params[:category] = a['category_id'] if a['category_id']
  146. topic_custom_fields = {}
  147. if a['add_fields']
  148. a['add_fields'].each do |f|
  149. value = data[f['value']]
  150. key = f['key']
  151. if key.include?('custom_fields')
  152. keyArr = key.split('.')
  153. if keyArr.length === 3
  154. custom_key = keyArr.last
  155. type = keyArr.first
  156. if type === 'topic'
  157. topic_custom_fields[custom_key] = value
  158. elsif type === 'post'
  159. params[:custom_fields] ||= {}
  160. params[:custom_fields][custom_key.to_sym] = value
  161. end
  162. end
  163. else
  164. params[key.to_sym] = value
  165. end
  166. end
  167. end
  168. creator = PostCreator.new(user, params)
  169. post = creator.create
  170. if creator.errors.present?
  171. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  172. else
  173. if topic_custom_fields.present?
  174. topic_custom_fields.each do |k, v|
  175. post.topic.custom_fields[k] = v
  176. end
  177. post.topic.save_custom_fields(true)
  178. end
  179. data['redirect_to'] = post.topic.url
  180. end
  181. end
  182. end
  183. if a['type'] === 'send_message' && data
  184. title = data[a['title']]
  185. post = data[a['post']]
  186. if title && post
  187. creator = PostCreator.new(user,
  188. title: title,
  189. raw: post,
  190. archetype: Archetype.private_message,
  191. target_usernames: a['username'])
  192. post = creator.create
  193. if creator.errors.present?
  194. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  195. else
  196. data['redirect_to'] = post.topic.url
  197. end
  198. end
  199. end
  200. if a['type'] === 'update_profile' && a['profile_updates'].length && data
  201. user_updater = UserUpdater.new(user, user)
  202. attributes = {}
  203. a['profile_updates'].each do |pu|
  204. attributes[pu['value'].to_sym] = data[pu['key']]
  205. end
  206. user_updater.update(attributes) if attributes.present?
  207. end
  208. end
  209. end
  210. if @wizard.save_submissions && updater.errors.empty?
  211. if step_input
  212. step_input.each do |key, value|
  213. data[key] = value
  214. end
  215. end
  216. if final_step
  217. data['submitted_at'] = Time.now.iso8601
  218. end
  219. if data.present?
  220. @submissions.pop(1) if @wizard.unfinished?
  221. @submissions.push(data)
  222. PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
  223. end
  224. end
  225. # Ensure there is no submission left over after the user has completed a wizard with save_submissions off
  226. if !@wizard.save_submissions && final_step
  227. PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
  228. end
  229. if @wizard.after_time && final_step
  230. @wizard.user.custom_fields.delete('redirect_to_wizard');
  231. @wizard.user.save_custom_fields(true)
  232. end
  233. if updater.errors.empty?
  234. # If the user will be redirected to a new wizard send them there straight away
  235. user_redirect = user.custom_fields['redirect_to_wizard']
  236. redirect_to = user_redirect ? "/w/#{user_redirect}" : data['redirect_to']
  237. updater.result = { redirect_to: redirect_to } if redirect_to
  238. end
  239. end
  240. end
  241. end
  242. end
  243. @wizard
  244. end
  245. end