builder.rb 8.2 KB

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