builder.rb 9.1 KB

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