builder.rb 11 KB

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