0
  5   ˆl–Ü4ý( ©¤ýÔâ�fàŸ¸TÅ£�þB gmé@Ô¢/óÈƒh¹ÇÀÅ      0
  E   ˆl–Ãa¾”*i?u8 …p/Üiå1hß�þBÜm¬ã€²2óD®ÿ?Éƒm¶ÝÈ_�uÐb&=LyZËcõÂÇ µ    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class KnowledgeBase::Public::BaseController < ApplicationController
  before_action :authenticate_with_preview_token
  before_action :load_kb
  helper_method :system_locale_via_uri, :fallback_locale, :current_user, :find_category,
                :filter_primary_kb_locale, :menu_items, :all_locales, :can_preview?

  layout 'knowledge_base'

  include KnowledgeBaseHelper

  private

  def load_kb
    @knowledge_base = policy_scope(KnowledgeBase)
                      .localed(guess_locale_via_uri)
                      .first!
  end

  def all_locales
    @all_locales ||= KnowledgeBase::Locale.system_with_kb_locales(@knowledge_base)
  end

  def menu_items
    @menu_items ||= KnowledgeBase::MenuItem.using_locale(guess_locale_via_uri || filter_primary_kb_locale)
  end

  def system_locale_via_uri
    @system_locale_via_uri ||= guess_locale_via_uri || filter_primary_kb_locale || all_locales.first
  end

  def fallback_locale
    return system_locale_via_uri if all_locales.find { |locale| locale.id == system_locale_via_uri&.id }

    filter_primary_kb_locale || all_locales.first
  end

  def filter_primary_kb_locale
    all_locales.find(&:primary_locale?)
  end

  def guess_locale_via_uri
    @guess_locale_via_uri ||= params[:locale].present? ? ::Locale.find_by(locale: params[:locale]) : nil
  end

  def find_category(id)
    @knowledge_base.load_category(system_locale_via_uri, id)
  end

  def find_locales(object)
    return [] if object.blank?

    system_locale_ids = KnowledgeBase::Locale.available_for(object).select(:system_locale_id).pluck(:system_locale_id)
    all_locales.select { |locale| system_locale_ids.include? locale.id }
  end

  # `sorting_node` is the knowledge base or category whose categories are being listed, and
  #   therefore the one holding their sorting mode. Nil where the listing spans categories and no
  #   single mode applies (the tag page), which leaves it on the hand-arranged order.
  def categories_filter(list, sorting_node = nil)
    list
      .localed(system_locale_via_uri)
      .sorted_by_mode(sorting_node&.category_sorting_mode, system_locale_or_id: system_locale_via_uri)
      .select { |category| policy(category).show_public? }
  end

  # `sorting_node` is the category whose answers are being listed, holding their own sorting mode â€”
  #   not the one its subcategories are listed in. Nil on the tag page, as above.
  #
  # The category is preloaded for the tag listing: `KnowledgeBase::Answer.tag_objects` is a bare
  #   `where`, so nothing presets the answer's category there the way `category.answers` does
  #   (`inverse_of: :answers`), and the link the view builds per answer
  #   (`answer.category.translation`) then fetches it one answer at a time. Measured on a tag page
  #   listing ten answers from ten categories: ten queries saved, for anonymous visitors as much as
  #   for editors. On a category page the preload is a no-op, the association already being loaded.
  def answers_filter(list, sorting_node = nil)
    list
      .includes(:category)
      .localed(system_locale_via_uri)
      # Not `internal`: this site never shows the internal publication date, so it must
      #   not order by it either.
      .sorted_by_mode(sorting_node&.answer_sorting_mode, system_locale_or_id: system_locale_via_uri, internal: false)
      .visible_on_help_site_to_user(current_user)
  end

  # Asks the policy rather than `.visible_on_help_site_to_user`: for one answer, that is cheaper
  #   than resolving a granular editor's access to every category, which any answer save resets.
  def find_answer(scope, id, locale: system_locale_via_uri)
    return if scope.nil?

    scope = scope.include_contents
    scope = scope.localed(locale) if locale

    if !current_user&.permissions?('knowledge_base.editor')
      return scope.published.find_by(id: id)
    end

    if 