builder.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. @template = CustomWizard::Template.new(data)
  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. )
  14. @submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id))
  15. end
  16. def self.sorted_handlers
  17. @sorted_handlers ||= []
  18. end
  19. def self.step_handlers
  20. sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
  21. end
  22. def self.add_step_handler(priority = 0, wizard_id, &block)
  23. sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
  24. @sorted_handlers.sort_by! { |h| -h[:priority] }
  25. end
  26. def build
  27. unless (@wizard.completed? && !@template.respond_to?(:multiple_submissions)) ||
  28. !@template.steps
  29. @template.steps.each do |s|
  30. @wizard.append_step(s['id']) do |step|
  31. step.title = s['title'] if s['title']
  32. step.description = s['description'] if s['description']
  33. step.banner = s['banner'] if s['banner']
  34. step.key = s['key'] if s['key']
  35. if s['fields'] && s['fields'].length
  36. s['fields'].each do |f|
  37. params = {
  38. id: f['id'],
  39. type: f['type'],
  40. required: f['required']
  41. }
  42. params[:label] = f['label'] if f['label']
  43. params[:description] = f['description'] if f['description']
  44. params[:key] = f['key'] if f['key']
  45. if @submissions.last && @submissions.last['completed'] === false
  46. submission = @submissions.last
  47. params[:value] = submission[f['id']] if submission[f['id']]
  48. end
  49. field = step.add_field(params)
  50. if f['type'] === 'dropdown'
  51. if f['choices'] && f['choices'].length > 0
  52. f['choices'].each do |c|
  53. field.add_choice(c['value'], label: c['label'])
  54. end
  55. elsif f['choices_key'] && f['choices_key'].length > 0
  56. choices = I18n.t(f['choices_key'])
  57. if choices.is_a?(Hash)
  58. choices.each do |k, v|
  59. field.add_choice(k, label: v)
  60. end
  61. end
  62. elsif f['choices_preset'] && f['choices_preset'].length > 0
  63. objects = []
  64. if f['choices_preset'] === 'categories'
  65. objects = Site.new(Guardian.new(@wizard.user)).categories
  66. end
  67. if f['choices_filters'] && f['choices_filters'].length > 0
  68. f['choices_filters'].each do |f|
  69. objects.reject! { |o| o[f['key']] != f['value'] }
  70. end
  71. end
  72. if objects.length > 0
  73. objects.each do |o|
  74. field.add_choice(o.id, label: o.name)
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end
  81. step.on_update do |updater|
  82. @updater = updater
  83. submission = @submissions.last || {}
  84. step_input = updater.fields || {}
  85. user = @wizard.user
  86. if s['fields'] && s['fields'].length
  87. s['fields'].each do |f|
  88. value = step_input[f['id']]
  89. min_length = f['min_length']
  90. if min_length && value.is_a?(String) && value.length < min_length.to_i
  91. label = f['label'] || I18n.t("#{f['key']}.label")
  92. updater.errors.add(f['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
  93. end
  94. end
  95. end
  96. next if updater.errors.any?
  97. CustomWizard::Builder.step_handlers.each do |handler|
  98. if handler[:wizard_id] == @wizard.id
  99. handler[:block].call(self)
  100. end
  101. end
  102. next if updater.errors.any?
  103. if s['actions'] && s['actions'].length
  104. s['actions'].each do |a|
  105. if a['type'] === 'create_topic' && submission
  106. title = submission[a['title']]
  107. post = submission[a['post']]
  108. if title
  109. params = {
  110. title: title,
  111. raw: post,
  112. skip_validations: true
  113. }
  114. params[:category] = a['category_id'] if a['category_id']
  115. topic_custom_fields = {}
  116. if a['add_fields']
  117. a['add_fields'].each do |f|
  118. value = submission[f['value']]
  119. key = f['key']
  120. if key.include?('custom_fields')
  121. keyArr = key.split('.')
  122. if keyArr.length === 3
  123. custom_key = keyArr.last
  124. type = keyArr.first
  125. if type === 'topic'
  126. topic_custom_fields[custom_key] = value
  127. elsif type === 'post'
  128. params[:custom_fields] ||= {}
  129. params[:custom_fields][custom_key.to_sym] = value
  130. end
  131. end
  132. else
  133. params[key.to_sym] = value
  134. end
  135. end
  136. end
  137. creator = PostCreator.new(user, params)
  138. post = creator.create
  139. if creator.errors.present?
  140. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  141. else
  142. if topic_custom_fields.present?
  143. topic_custom_fields.each do |k, v|
  144. post.topic.custom_fields[k] = v
  145. end
  146. post.topic.save_custom_fields(true)
  147. end
  148. updater.result = { topic_id: post.topic.id }
  149. end
  150. end
  151. end
  152. if a['type'] === 'send_message' && submission
  153. title = submission[a['title']]
  154. post = submission[a['post']]
  155. if title && post
  156. creator = PostCreator.new(user,
  157. title: title,
  158. raw: post,
  159. archetype: Archetype.private_message,
  160. target_usernames: a['username'])
  161. post = creator.create
  162. if creator.errors.present?
  163. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  164. else
  165. updater.result = { topic_id: post.topic_id }
  166. end
  167. end
  168. end
  169. if a['type'] === 'update_profile' && a['profile_updates'].length && submission
  170. user_updater = UserUpdater.new(user, user)
  171. attributes = {}
  172. a['profile_updates'].each do |pu|
  173. attributes[pu['key'].to_sym] = submission[pu['value']]
  174. end
  175. user_updater.update(attributes) if attributes.present?
  176. end
  177. end
  178. end
  179. if @wizard.save_submissions && updater.errors.empty?
  180. @submissions.pop(1) if submission && submission['completed'] === false
  181. submission['user_id'] = @wizard.user.id
  182. submission['completed'] = updater.step.next.nil?
  183. if step_input
  184. step_input.each do |key, value|
  185. submission[key] = value
  186. end
  187. end
  188. @submissions.push(submission)
  189. PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
  190. end
  191. end
  192. end
  193. end
  194. end
  195. @wizard
  196. end
  197. end