admin.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. class CustomWizard::AdminController < ::ApplicationController
  2. before_action :ensure_logged_in
  3. before_action :ensure_admin
  4. def index
  5. render nothing: true
  6. end
  7. def field_types
  8. render json: { types: CustomWizard::Field.types }
  9. end
  10. def save
  11. params.require(:wizard)
  12. wizard = ::JSON.parse(params[:wizard])
  13. existing = PluginStore.get('custom_wizard', wizard['id']) || {}
  14. new_time = false
  15. error = nil
  16. if wizard["id"].blank?
  17. error = 'id_required'
  18. elsif wizard["name"].blank?
  19. error = 'name_required'
  20. elsif wizard["steps"].blank?
  21. error = 'steps_required'
  22. elsif wizard["after_time"]
  23. if !wizard["after_time_scheduled"] && !existing["after_time_scheduled"]
  24. error = 'after_time_need_time'
  25. else
  26. after_time_scheduled = Time.parse(wizard["after_time_scheduled"]).utc
  27. new_time = existing['after_time_scheduled'] ?
  28. after_time_scheduled != Time.parse(existing['after_time_scheduled']).utc :
  29. true
  30. begin
  31. if new_time && after_time_scheduled < Time.now.utc
  32. error = 'after_time_invalid'
  33. end
  34. rescue ArgumentError
  35. error = 'after_time_invalid'
  36. end
  37. end
  38. end
  39. return render json: { error: error } if error
  40. wizard["steps"].each do |s|
  41. if s["id"].blank?
  42. error = 'id_required'
  43. break
  44. end
  45. if s["fields"] && s["fields"].present?
  46. s["fields"].each do |f|
  47. if f["id"].blank?
  48. error = 'id_required'
  49. break
  50. end
  51. if f["type"].blank?
  52. error = 'type_required'
  53. break
  54. end
  55. if f["type"] === 'dropdown'
  56. choices = f["choices"]
  57. if (!choices || choices.length < 1) && !f["choices_key"] && !f["choices_preset"]
  58. error = 'field.need_choices'
  59. break
  60. end
  61. end
  62. end
  63. end
  64. if s["actions"] && s["actions"].present?
  65. s["actions"].each do |a|
  66. if a["id"].blank?
  67. error = 'id_required'
  68. break
  69. end
  70. end
  71. end
  72. end
  73. return render json: { error: error } if error
  74. ## end of error checks
  75. wizard['steps'].each do |s|
  76. s['description'] = PrettyText.cook(s['raw_description']) if s['raw_description']
  77. end
  78. if wizard['after_time'] && new_time
  79. Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
  80. Jobs.enqueue_at(after_time_scheduled, :set_after_time_wizard, wizard_id: wizard['id'])
  81. end
  82. if existing['after_time'] && !wizard['after_time']
  83. Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard['id'])
  84. Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
  85. end
  86. PluginStore.set('custom_wizard', wizard["id"], wizard)
  87. render json: success_json
  88. end
  89. def remove
  90. params.require(:id)
  91. wizard = PluginStore.get('custom_wizard', params[:id])
  92. if wizard['after_time']
  93. Jobs.cancel_scheduled_job(:set_after_time_wizard)
  94. Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard['id'])
  95. end
  96. PluginStore.remove('custom_wizard', params[:id])
  97. render json: success_json
  98. end
  99. def find_wizard
  100. params.require(:wizard_id)
  101. wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
  102. render json: success_json.merge(wizard: wizard)
  103. end
  104. def custom_wizards
  105. rows = PluginStoreRow.where(plugin_name: 'custom_wizard').order(:id)
  106. wizards = [*rows].map { |r| CustomWizard::Template.new(r.value) }
  107. render json: success_json.merge(wizards: wizards)
  108. end
  109. def submissions
  110. params.require(:wizard_id)
  111. rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order('id DESC')
  112. all_submissions = [*rows].map do |r|
  113. submissions = ::JSON.parse(r.value)
  114. if user = User.find_by(id: r.key)
  115. username = user.username
  116. else
  117. username = I18n.t('admin.wizard.submissions.no_user', id: r.key)
  118. end
  119. submissions.map { |s| { username: username }.merge!(s.except("redirect_to")) }
  120. end.flatten
  121. render json: success_json.merge(submissions: all_submissions)
  122. end
  123. end