builder.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. field = step.add_field(params)
  53. if f['type'] === 'dropdown'
  54. if f['choices'] && f['choices'].length > 0
  55. f['choices'].each do |c|
  56. field.add_choice(c['value'], label: c['label'])
  57. end
  58. elsif f['choices_key'] && f['choices_key'].length > 0
  59. choices = I18n.t(f['choices_key'])
  60. if choices.is_a?(Hash)
  61. choices.each do |k, v|
  62. field.add_choice(k, label: v)
  63. end
  64. end
  65. elsif f['choices_preset'] && f['choices_preset'].length > 0
  66. objects = []
  67. if f['choices_preset'] === 'categories'
  68. objects = Site.new(Guardian.new(@wizard.user)).categories
  69. end
  70. if f['choices_filters'] && f['choices_filters'].length > 0
  71. f['choices_filters'].each do |f|
  72. objects.reject! { |o| o[f['key']] != f['value'] }
  73. end
  74. end
  75. if objects.length > 0
  76. objects.each do |o|
  77. field.add_choice(o.id, label: o.name)
  78. end
  79. end
  80. end
  81. end
  82. end
  83. end
  84. step.on_update do |updater|
  85. @updater = updater
  86. user = @wizard.user
  87. if s['fields'] && s['fields'].length
  88. s['fields'].each do |f|
  89. value = updater.fields[f['id']]
  90. min_length = f['min_length']
  91. if min_length && value.is_a?(String) && value.length < min_length.to_i
  92. label = f['label'] || I18n.t("#{f['key']}.label")
  93. updater.errors.add(f['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
  94. end
  95. end
  96. end
  97. next if updater.errors.any?
  98. CustomWizard::Builder.step_handlers.each do |handler|
  99. if handler[:wizard_id] == @wizard.id
  100. handler[:block].call(self)
  101. end
  102. end
  103. next if updater.errors.any?
  104. step_input = updater.fields.to_h
  105. data = step_input
  106. final_step = updater.step.next.nil?
  107. ## if the wizard has data from the previous steps make that accessible to the actions.
  108. if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
  109. submission = @submissions.last
  110. data = submission.merge(data)
  111. end
  112. if s['actions'] && s['actions'].length
  113. s['actions'].each do |a|
  114. if a['type'] === 'create_topic' && data
  115. title = data[a['title']]
  116. post = data[a['post']]
  117. if title
  118. params = {
  119. title: title,
  120. raw: post,
  121. skip_validations: true
  122. }
  123. params[:category] = a['category_id'] if a['category_id']
  124. topic_custom_fields = {}
  125. if a['add_fields']
  126. a['add_fields'].each do |f|
  127. value = data[f['value']]
  128. key = f['key']
  129. if key.include?('custom_fields')
  130. keyArr = key.split('.')
  131. if keyArr.length === 3
  132. custom_key = keyArr.last
  133. type = keyArr.first
  134. if type === 'topic'
  135. topic_custom_fields[custom_key] = value
  136. elsif type === 'post'
  137. params[:custom_fields] ||= {}
  138. params[:custom_fields][custom_key.to_sym] = value
  139. end
  140. end
  141. else
  142. params[key.to_sym] = value
  143. end
  144. end
  145. end
  146. creator = PostCreator.new(user, params)
  147. post = creator.create
  148. if creator.errors.present?
  149. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  150. else
  151. if topic_custom_fields.present?
  152. topic_custom_fields.each do |k, v|
  153. post.topic.custom_fields[k] = v
  154. end
  155. post.topic.save_custom_fields(true)
  156. end
  157. data['redirect_to'] = post.topic.url
  158. end
  159. end
  160. end
  161. if a['type'] === 'send_message' && data
  162. title = data[a['title']]
  163. post = data[a['post']]
  164. if title && post
  165. creator = PostCreator.new(user,
  166. title: title,
  167. raw: post,
  168. archetype: Archetype.private_message,
  169. target_usernames: a['username'])
  170. post = creator.create
  171. if creator.errors.present?
  172. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  173. else
  174. data['redirect_to'] = post.topic.url
  175. end
  176. end
  177. end
  178. if a['type'] === 'update_profile' && a['profile_updates'].length && data
  179. user_updater = UserUpdater.new(user, user)
  180. attributes = {}
  181. a['profile_updates'].each do |pu|
  182. attributes[pu['value'].to_sym] = data[pu['key']]
  183. end
  184. user_updater.update(attributes) if attributes.present?
  185. end
  186. end
  187. end
  188. if @wizard.save_submissions && updater.errors.empty?
  189. if step_input
  190. step_input.each do |key, value|
  191. data[key] = value
  192. end
  193. end
  194. if final_step
  195. data['submitted_at'] = Time.now.iso8601
  196. end
  197. if data.present?
  198. @submissions.pop(1) if @wizard.unfinished?
  199. @submissions.push(data)
  200. PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
  201. end
  202. end
  203. # Ensure there is no submission left over after the user has completed a wizard with save_submissions off
  204. if !@wizard.save_submissions && final_step
  205. PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
  206. end
  207. if @wizard.after_time && final_step
  208. @wizard.user.custom_fields.delete('redirect_to_wizard');
  209. @wizard.user.save_custom_fields(true)
  210. end
  211. if updater.errors.empty?
  212. # If the user will be redirected to a new wizard send them there straight away
  213. user_redirect = user.custom_fields['redirect_to_wizard']
  214. redirect_to = user_redirect ? "/w/#{user_redirect}" : data['redirect_to']
  215. updater.result = { redirect_to: redirect_to } if redirect_to
  216. end
  217. end
  218. end
  219. end
  220. end
  221. @wizard
  222. end
  223. end