builder.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. class CustomWizard::Builder
  2. def initialize(user, wizard_id)
  3. data = PluginStore.get('custom_wizard', wizard_id)
  4. @custom_wizard = CustomWizard::Wizard.new(data)
  5. @wizard = Wizard.new(user)
  6. @wizard.id = wizard_id
  7. @wizard.save_submissions = data['save_submissions']
  8. @wizard.background = data["background"]
  9. end
  10. def self.sorted_handlers
  11. @sorted_handlers ||= []
  12. end
  13. def self.step_handlers
  14. sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
  15. end
  16. def self.add_step_handler(priority = 0, wizard_id, &block)
  17. sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
  18. @sorted_handlers.sort_by! { |h| -h[:priority] }
  19. end
  20. def build
  21. @custom_wizard.steps.each do |s|
  22. @wizard.append_step(s['id']) do |step|
  23. step.title = s['title'] if s['title']
  24. step.description = s['description'] if s['description']
  25. step.banner = s['banner'] if s['banner']
  26. step.translation_key = s['translation_key'] if s['translation_key']
  27. s['fields'].each do |f|
  28. params = {
  29. id: f['id'],
  30. type: f['type'],
  31. required: f['required']
  32. }
  33. params[:label] = f['label'] if f['label']
  34. params[:description] = f['description'] if f['description']
  35. params[:translation_key] = f['translation_key'] if f['translation_key']
  36. field = step.add_field(params)
  37. if f['type'] == 'dropdown'
  38. f['choices'].each do |c|
  39. field.add_choice(c['id'], label: c['label'])
  40. end
  41. end
  42. end
  43. step.on_update do |updater|
  44. @updater = updater
  45. input = updater.fields
  46. user = @wizard.user
  47. if @wizard.save_submissions
  48. store_key = @wizard.id
  49. submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", store_key))
  50. submission = {}
  51. if submissions.last && submissions.last['completed'] === false
  52. submission = submissions.last
  53. submissions.pop(1)
  54. end
  55. submission['user_id'] = @wizard.user.id
  56. submission['completed'] = updater.step.next.nil?
  57. input.each do |key, value|
  58. submission[key] = value
  59. end
  60. submissions.push(submission)
  61. PluginStore.set('custom_wizard_submissions', store_key, submissions)
  62. end
  63. if s['actions'] && s['actions'].length
  64. s['actions'].each do |a|
  65. if a['type'] === 'create_topic'
  66. creator = PostCreator.new(user,
  67. title: input[a['title']],
  68. raw: input[a['post']],
  69. category: a['category_id'],
  70. skip_validations: true)
  71. post = creator.create
  72. if creator.errors.present?
  73. raise StandardError, creator.errors.full_messages.join(" ")
  74. end
  75. updater.result = { topic_id: post.topic.id }
  76. end
  77. if a['type'] === 'send_message'
  78. creator = PostCreator.new(user,
  79. title: input[a['title']],
  80. raw: input[a['post']],
  81. archetype: Archetype.private_message,
  82. target_usernames: a['username'])
  83. post = creator.create
  84. if creator.errors.present?
  85. raise StandardError, creator.errors.full_messages.join(" ")
  86. end
  87. updater.result = { topic_id: post.topic.id }
  88. end
  89. end
  90. end
  91. CustomWizard::Builder.step_handlers.each do |handler|
  92. if handler[:wizard_id] == @wizard.id
  93. handler[:block].call(self)
  94. end
  95. end
  96. end
  97. end
  98. end
  99. @wizard
  100. end
  101. end