builder.rb 13 KB

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