categories_controller.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Public::CategoriesController < KnowledgeBase::Public::BaseController
  3. skip_before_action :load_kb, only: :forward_root
  4. def index
  5. @categories = categories_filter(@knowledge_base.categories.root)
  6. @object_locales = find_locales(@knowledge_base)
  7. authorize(@categories, policy_class: Controllers::KnowledgeBase::Public::CategoriesControllerPolicy)
  8. rescue Pundit::NotAuthorizedError
  9. raise ActiveRecord::RecordNotFound
  10. end
  11. def show
  12. @object = find_category(params[:category])
  13. render_alternatives && return if @object.nil? || !policy(@object).show_public?
  14. @categories = categories_filter(@object.children)
  15. @object_locales = find_locales(@object)
  16. @answers = answers_filter(@object.answers)
  17. render :index
  18. end
  19. def forward_root
  20. knowledge_base = policy_scope(KnowledgeBase).first!
  21. primary_locale = KnowledgeBase::Locale
  22. .system_with_kb_locales(knowledge_base)
  23. .where(knowledge_base_locales: { primary: true })
  24. .first!
  25. path = help_root_path(locale: primary_locale.locale)
  26. redirect_to custom_path_if_needed(path, knowledge_base, full: true), allow_other_host: true
  27. end
  28. private
  29. def render_alternatives
  30. @page_title_error = :alternatives
  31. @object = nil
  32. @alternative = @knowledge_base
  33. .categories
  34. .eager_load(translations: :kb_locale)
  35. .find_by(id: params[:category])
  36. if @alternative.nil? || @alternative.translations.none? || !policy(@alternative).show?
  37. raise ActiveRecord::RecordNotFound
  38. end
  39. @object_locales = @alternative.translations.map { |x| x.kb_locale.system_locale }
  40. render 'knowledge_base/public/show_alternatives', locals: { name: 'Category' }
  41. end
  42. end