builder.rb 13 KB

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