builder.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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['key'], label: c['value'])
  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 |field|
  117. validate_field(field, updater)
  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. data = updater.fields.to_h
  128. ## if the wizard has data from the previous steps make that accessible to the actions.
  129. if @submissions && @submissions.last && !@submissions.last.key?("submitted_at")
  130. submission = @submissions.last
  131. data = submission.merge(data)
  132. end
  133. if s['actions'] && s['actions'].length && data
  134. s['actions'].each do |action|
  135. self.send(action['type'].to_sym, user, action, data)
  136. end
  137. end
  138. final_step = updater.step.next.nil?
  139. if @wizard.save_submissions && updater.errors.empty?
  140. save_submissions(data, final_step)
  141. elsif final_step
  142. PluginStore.remove("#{@wizard.id}_submissions", @wizard.user.id)
  143. end
  144. if @wizard.after_time && final_step
  145. @wizard.user.custom_fields.delete('redirect_to_wizard');
  146. @wizard.user.save_custom_fields(true)
  147. end
  148. if updater.errors.empty?
  149. user_redirect = user.custom_fields['redirect_to_wizard']
  150. redirect_to = user_redirect ? "/w/#{user_redirect}" : data['redirect_to']
  151. updater.result = { redirect_to: redirect_to } if redirect_to
  152. end
  153. end
  154. end
  155. end
  156. end
  157. @wizard
  158. end
  159. def validate_field(field, updater)
  160. value = updater.fields[field['id']]
  161. min_length = field['min_length']
  162. if min_length && value.is_a?(String) && value.length < min_length.to_i
  163. label = field['label'] || I18n.t("#{field['key']}.label")
  164. updater.errors.add(field['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
  165. end
  166. end
  167. def create_topic(user, action, data)
  168. if action['custom_title']
  169. title = action['custom_title']
  170. else
  171. title = data[action['title']]
  172. end
  173. if action['post_builder']
  174. post = CustomWizard::Builder.build_post(action['post_template'], user, data)
  175. else
  176. post = data[action['post']]
  177. end
  178. if title
  179. params = {
  180. title: title,
  181. raw: post,
  182. skip_validations: true
  183. }
  184. if action['custom_category_enabled'] &&
  185. !action['custom_category_wizard_field'] &&
  186. action['custom_category_user_field_key']
  187. if action['custom_category_user_field_key'].include?('custom_fields')
  188. field = action['custom_category_user_field_key'].split('.').last
  189. category_id = user.custom_fields[field]
  190. else
  191. category_id = user.send(action['custom_category_user_field_key'])
  192. end
  193. else
  194. category_id = action['category_id']
  195. end
  196. params[:category] = category_id
  197. topic_custom_fields = {}
  198. if action['add_fields']
  199. action['add_fields'].each do |field|
  200. if field['value_custom']
  201. value = field['value_custom']
  202. else
  203. value = data[field['value']]
  204. end
  205. key = field['key']
  206. if key && key.include?('custom_fields')
  207. keyArr = key.split('.')
  208. if keyArr.length === 3
  209. custom_key = keyArr.last
  210. type = keyArr.first
  211. if type === 'topic'
  212. topic_custom_fields[custom_key] = value
  213. elsif type === 'post'
  214. params[:custom_fields] ||= {}
  215. params[:custom_fields][custom_key.to_sym] = value
  216. end
  217. end
  218. else
  219. params[key.to_sym] = value
  220. end
  221. end
  222. end
  223. creator = PostCreator.new(user, params)
  224. post = creator.create
  225. if creator.errors.present?
  226. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  227. else
  228. if topic_custom_fields.present?
  229. topic_custom_fields.each do |k, v|
  230. post.topic.custom_fields[k] = v
  231. end
  232. post.topic.save_custom_fields(true)
  233. end
  234. data['redirect_to'] = post.topic.url
  235. end
  236. end
  237. end
  238. def send_message(user, action, data)
  239. title = data[action['title']]
  240. if action['post_builder']
  241. post = CustomWizard::Builder.build_post(action['post_template'], user, data)
  242. else
  243. post = data[action['post']]
  244. end
  245. if title && post
  246. creator = PostCreator.new(user,
  247. title: title,
  248. raw: post,
  249. archetype: Archetype.private_message,
  250. target_usernames: action['username']
  251. )
  252. post = creator.create
  253. if creator.errors.present?
  254. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  255. else
  256. data['redirect_to'] = post.topic.url
  257. end
  258. end
  259. end
  260. def update_profile(user, action, data)
  261. return unless action['profile_updates'].length
  262. attributes = {}
  263. custom_fields = {}
  264. action['profile_updates'].each do |pu|
  265. value = pu['value']
  266. custom_field = pu['value_custom']
  267. key = pu['key']
  268. if custom_field
  269. custom_fields[custom_field] = data[key]
  270. else
  271. attributes[value.to_sym] = data[key]
  272. end
  273. end
  274. if custom_fields.present?
  275. custom_fields.each { |k, v| user.custom_fields[k] = v }
  276. user.save_custom_fields(true)
  277. end
  278. if attributes.present?
  279. user_updater = UserUpdater.new(user, user)
  280. user_updater.update(attributes)
  281. end
  282. end
  283. def save_submissions(data, final_step)
  284. if final_step
  285. data['submitted_at'] = Time.now.iso8601
  286. end
  287. if data.present?
  288. @submissions.pop(1) if @wizard.unfinished?
  289. @submissions.push(data)
  290. PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
  291. end
  292. end
  293. end