builder.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. @custom_wizard = CustomWizard::Wizard.new(data)
  6. @wizard = Wizard.new(user,
  7. id: wizard_id,
  8. save_submissions: data['save_submissions'],
  9. multiple_submissions: data['multiple_submissions'],
  10. background: data["background"],
  11. custom: true
  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? && !@custom_wizard.respond_to?(:multiple_submissions)) ||
  26. !@custom_wizard.steps
  27. @custom_wizard.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. creator = PostCreator.new(user,
  105. title: input[a['title']],
  106. raw: input[a['post']],
  107. category: a['category_id'],
  108. skip_validations: true)
  109. post = creator.create
  110. if creator.errors.present?
  111. updater.errors.add(:create_topic, creator.errors.full_messages.join(" "))
  112. else
  113. updater.result = { topic_id: post.topic.id }
  114. end
  115. end
  116. if a['type'] === 'send_message'
  117. creator = PostCreator.new(user,
  118. title: input[a['title']],
  119. raw: input[a['post']],
  120. archetype: Archetype.private_message,
  121. target_usernames: a['username'])
  122. post = creator.create
  123. if creator.errors.present?
  124. updater.errors.add(:send_message, creator.errors.full_messages.join(" "))
  125. else
  126. updater.result = { topic_id: post.topic.id }
  127. end
  128. end
  129. end
  130. end
  131. if @wizard.save_submissions && updater.errors.empty?
  132. store_key = @wizard.id
  133. submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", store_key))
  134. submission = {}
  135. if submissions.last && submissions.last['completed'] === false
  136. submission = submissions.last
  137. submissions.pop(1)
  138. end
  139. submission['user_id'] = @wizard.user.id
  140. submission['completed'] = updater.step.next.nil?
  141. if input
  142. input.each do |key, value|
  143. submission[key] = value
  144. end
  145. end
  146. submissions.push(submission)
  147. PluginStore.set('custom_wizard_submissions', store_key, submissions)
  148. end
  149. end
  150. end
  151. end
  152. end
  153. @wizard
  154. end
  155. end