builder.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. data = @wizard.save_submissions ? submission : step_input
  104. if s['actions'] && s['actions'].length
  105. s['actions'].each do |a|
  106. if a['type'] === 'create_topic' && data
  107. title = data[a['title']]
  108. post = data[a['post']]
  109. if title
  110. params = {
  111. title: title,
  112. raw: post,
  113. skip_validations: true
  114. }
  115. params[:category] = a['category_id'] if a['category_id']
  116. topic_custom_fields = {}
  117. if a['add_fields']
  118. a['add_fields'].each do |f|
  119. value = data[f['value']]
  120. key = f['key']
  121. if key.include?('custom_fields')
  122. keyArr = key.split('.')
  123. if keyArr.length === 3
  124. custom_key = keyArr.last
  125. type = keyArr.first
  126. if type === 'topic'
  127. topic_custom_fields[custom_key] = value
  128. elsif type === 'post'
  129. params[:custom_fields] ||= {}
  130. params[:custom_fields][custom_key.to_sym] = value
  131. end
  132. end
  133. else
  134. params[key.to_sym] = value
  135. end
  136. end
  137. end
  138. creator = PostCreator.new(user, params)
  139. post = creator.create
  140. if creator.errors.present?
  141. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  142. else
  143. if topic_custom_fields.present?
  144. topic_custom_fields.each do |k, v|
  145. post.topic.custom_fields[k] = v
  146. end
  147. post.topic.save_custom_fields(true)
  148. end
  149. updater.result = { topic_id: post.topic.id }
  150. end
  151. end
  152. end
  153. if a['type'] === 'send_message' && data
  154. title = data[a['title']]
  155. post = data[a['post']]
  156. if title && post
  157. creator = PostCreator.new(user,
  158. title: title,
  159. raw: post,
  160. archetype: Archetype.private_message,
  161. target_usernames: a['username'])
  162. post = creator.create
  163. if creator.errors.present?
  164. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  165. else
  166. updater.result = { topic_id: post.topic_id }
  167. end
  168. end
  169. end
  170. if a['type'] === 'update_profile' && a['profile_updates'].length && data
  171. user_updater = UserUpdater.new(user, user)
  172. attributes = {}
  173. a['profile_updates'].each do |pu|
  174. attributes[pu['key'].to_sym] = data[pu['value']]
  175. end
  176. user_updater.update(attributes) if attributes.present?
  177. end
  178. end
  179. end
  180. if @wizard.save_submissions && updater.errors.empty?
  181. @submissions.pop(1) if submission && submission['completed'] === false
  182. submission['user_id'] = @wizard.user.id
  183. submission['completed'] = updater.step.next.nil?
  184. if step_input
  185. step_input.each do |key, value|
  186. submission[key] = value
  187. end
  188. end
  189. @submissions.push(submission)
  190. PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
  191. end
  192. end
  193. end
  194. end
  195. end
  196. @wizard
  197. end
  198. end