builder.rb 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. end
  14. def self.sorted_handlers
  15. @sorted_handlers ||= []
  16. end
  17. def self.step_handlers
  18. sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
  19. end
  20. def self.add_step_handler(priority = 0, wizard_id, &block)
  21. sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
  22. @sorted_handlers.sort_by! { |h| -h[:priority] }
  23. end
  24. def build
  25. unless (@wizard.completed? && !@template.respond_to?(:multiple_submissions)) ||
  26. !@template.steps
  27. @template.steps.each do |s|
  28. @wizard.append_step(s['id']) do |step|
  29. step.title = s['title'] if s['title']
  30. step.description = s['description'] if s['description']
  31. step.banner = s['banner'] if s['banner']
  32. step.key = s['key'] if s['key']
  33. if s['fields'] && s['fields'].length
  34. s['fields'].each do |f|
  35. params = {
  36. id: f['id'],
  37. type: f['type'],
  38. required: f['required']
  39. }
  40. params[:label] = f['label'] if f['label']
  41. params[:description] = f['description'] if f['description']
  42. params[:key] = f['key'] if f['key']
  43. submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", @wizard.id))
  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. input = updater.fields
  83. user = @wizard.user
  84. if s['fields'] && s['fields'].length
  85. s['fields'].each do |f|
  86. value = input[f['id']]
  87. min_length = f['min_length']
  88. if min_length && value.is_a?(String) && value.length < min_length.to_i
  89. label = f['label'] || I18n.t("#{f['key']}.label")
  90. updater.errors.add(f['id'].to_s, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
  91. end
  92. end
  93. end
  94. next if updater.errors.any?
  95. CustomWizard::Builder.step_handlers.each do |handler|
  96. if handler[:wizard_id] == @wizard.id
  97. handler[:block].call(self)
  98. end
  99. end
  100. next if updater.errors.any?
  101. if s['actions'] && s['actions'].length
  102. s['actions'].each do |a|
  103. if a['type'] === 'create_topic'
  104. title = input[a['title']]
  105. post = input[a['post']]
  106. if title && post
  107. params = {
  108. title: title,
  109. raw: post,
  110. skip_validations: true
  111. }
  112. params[:category] = a['category_id'] if a['category_id']
  113. params[:featured_link] = input[a['featured_link']] if input[a['featured_link']]
  114. creator = PostCreator.new(user, params)
  115. post = creator.create
  116. if creator.errors.present?
  117. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  118. else
  119. updater.result = { topic_id: post.topic.id }
  120. end
  121. end
  122. end
  123. if a['type'] === 'send_message'
  124. title = input[a['title']]
  125. post = input[a['post']]
  126. if title && post
  127. creator = PostCreator.new(user,
  128. title: title,
  129. raw: post,
  130. archetype: Archetype.private_message,
  131. target_usernames: a['username'])
  132. post = creator.create
  133. if creator.errors.present?
  134. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  135. else
  136. updater.result = { topic_id: post.topic.id }
  137. end
  138. end
  139. end
  140. if a['type'] === 'update_profile' && a['profile_updates'].length
  141. updater = UserUpdater.new(user, user)
  142. attributes = a['profile_updates'].map do |pu|
  143. { pu['profile_field'].to_sym => input[pu['wizard_field']] }
  144. end
  145. updater.update(attributes)
  146. end
  147. end
  148. end
  149. if @wizard.save_submissions && updater.errors.empty?
  150. store_key = @wizard.id
  151. submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", store_key))
  152. submission = {}
  153. if submissions.last && submissions.last['completed'] === false
  154. submission = submissions.last
  155. submissions.pop(1)
  156. end
  157. submission['user_id'] = @wizard.user.id
  158. submission['completed'] = updater.step.next.nil?
  159. if input
  160. input.each do |key, value|
  161. submission[key] = value
  162. end
  163. end
  164. submissions.push(submission)
  165. PluginStore.set('custom_wizard_submissions', store_key, submissions)
  166. end
  167. end
  168. end
  169. end
  170. end
  171. puts "BUILDER: #{@wizard.respond_to?(:multiple_submissions)}"
  172. @wizard
  173. end
  174. end