builder.rb 10 KB

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