base_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Public::BaseController < ApplicationController
  3. before_action :load_kb
  4. helper_method :system_locale_via_uri, :fallback_locale, :current_user, :find_category,
  5. :filter_primary_kb_locale, :menu_items, :all_locales, :can_preview?
  6. layout 'knowledge_base'
  7. include KnowledgeBaseHelper
  8. private
  9. def load_kb
  10. @knowledge_base = policy_scope(KnowledgeBase)
  11. .localed(guess_locale_via_uri)
  12. .first
  13. raise ActiveRecord::RecordNotFound if @knowledge_base.nil?
  14. end
  15. def all_locales
  16. @all_locales ||= KnowledgeBase::Locale.system_with_kb_locales(@knowledge_base)
  17. end
  18. def menu_items
  19. @menu_items ||= KnowledgeBase::MenuItem.using_locale(guess_locale_via_uri || filter_primary_kb_locale)
  20. end
  21. def system_locale_via_uri
  22. @system_locale_via_uri ||= guess_locale_via_uri || filter_primary_kb_locale || all_locales.first
  23. end
  24. def fallback_locale
  25. return system_locale_via_uri if all_locales.find { |locale| locale.id == system_locale_via_uri&.id }
  26. filter_primary_kb_locale || all_locales.first
  27. end
  28. def filter_primary_kb_locale
  29. all_locales.find(&:primary_locale?)
  30. end
  31. def guess_locale_via_uri
  32. @guess_locale_via_uri ||= params[:locale].present? ? ::Locale.find_by(locale: params[:locale]) : nil
  33. end
  34. def find_category(id)
  35. @knowledge_base.load_category(system_locale_via_uri, id)
  36. end
  37. def find_locales(object)
  38. return [] if object.blank?
  39. system_locale_ids = KnowledgeBase::Locale.available_for(object).select(:system_locale_id).pluck(:system_locale_id)
  40. all_locales.select { |locale| system_locale_ids.include? locale.id }
  41. end
  42. def categories_filter(list)
  43. list
  44. .localed(system_locale_via_uri)
  45. .sorted
  46. .select { |category| policy(category).show_public? }
  47. end
  48. def answers_filter(list)
  49. answers = list
  50. .localed(system_locale_via_uri)
  51. .sorted
  52. if current_user&.permissions?('knowledge_base.editor')
  53. answers.filter { |answer| policy(answer).show_public? }
  54. else
  55. answers.published
  56. end
  57. end
  58. def find_answer(scope, id, locale: system_locale_via_uri)
  59. return if scope.nil?
  60. scope = scope.include_contents
  61. scope = scope.localed(locale) if locale
  62. if !current_user&.permissions?('knowledge_base.editor')
  63. return scope.published.find_by(id: id)
  64. end
  65. if (item = scope.find_by(id: id)) && policy(item).show_public?
  66. return item
  67. end
  68. nil
  69. end
  70. def can_preview?
  71. @can_preview ||= policy(@knowledge_base).update?
  72. end
  73. def not_found(e)
  74. @knowledge_base = policy_scope(KnowledgeBase).first
  75. if @knowledge_base.nil?
  76. super
  77. return
  78. end
  79. @page_title_error = :not_found
  80. render 'knowledge_base/public/not_found', status: :not_found
  81. end
  82. end