translation.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Answer::Translation < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include HasAgentAllowedParams
  5. include HasLinks
  6. include HasSearchIndexBackend
  7. include KnowledgeBase::Search
  8. include KnowledgeBase::HasUniqueTitle
  9. AGENT_ALLOWED_ATTRIBUTES = %i[title kb_locale_id].freeze
  10. AGENT_ALLOWED_NESTED_RELATIONS = %i[content].freeze
  11. belongs_to :kb_locale, class_name: 'KnowledgeBase::Locale', inverse_of: :answer_translations
  12. belongs_to :answer, class_name: 'KnowledgeBase::Answer', inverse_of: :translations, touch: true
  13. belongs_to :content, class_name: 'KnowledgeBase::Answer::Translation::Content', inverse_of: :translation, dependent: :destroy
  14. accepts_nested_attributes_for :content, update_only: true
  15. validates :title, presence: true, length: { maximum: 250 }
  16. validates :kb_locale_id, uniqueness: { case_sensitive: true, scope: :answer_id }
  17. scope :neighbours_of, ->(translation) { joins(:answer).where(knowledge_base_answers: { category_id: translation.answer&.category_id }) }
  18. alias assets_essential assets
  19. def assets(data = {})
  20. return data if assets_added_to?(data)
  21. data = super
  22. answer.assets(data)
  23. ApplicationModel::CanAssets.reduce inline_linked_objects, data
  24. end
  25. def to_param
  26. [answer_id, title.parameterize].join('-')
  27. end
  28. def search_index_attribute_lookup(include_references: true)
  29. attrs = super
  30. attrs.merge('title' => ActionController::Base.helpers.strip_tags(attrs['title']),
  31. 'content' => content&.search_index_attribute_lookup,
  32. 'scope_id' => answer.category_id,
  33. 'attachment' => answer.attachments_for_search_index_attribute_lookup,
  34. 'tags' => answer.tag_list)
  35. end
  36. def inline_linked_objects
  37. output = []
  38. scrubber = Loofah::Scrubber.new do |node|
  39. next if node.name != 'a'
  40. next if !node.key? 'data-target-type'
  41. case node['data-target-type']
  42. when 'knowledge-base-answer'
  43. if (translation = KnowledgeBase::Answer::Translation.find_by(id: node['data-target-id']))
  44. output.push translation
  45. end
  46. end
  47. end
  48. Loofah.scrub_fragment(content.body, scrubber)
  49. output
  50. end
  51. class << self
  52. def search_preferences(current_user)
  53. return false if !KnowledgeBase.exists? || !current_user.permissions?('knowledge_base.*')
  54. {
  55. prio: 1209,
  56. direct_search_index: false,
  57. }
  58. end
  59. def search_es_filter(es_response, _query, kb_locales, options)
  60. return es_response if options[:user]&.permissions?('knowledge_base.editor')
  61. answer_translations_id = es_response.pluck(:id)
  62. allowed_answer_translation_ids = KnowledgeBase::Answer
  63. .internal
  64. .joins(:translations)
  65. .where(knowledge_base_answer_translations: { id: answer_translations_id, kb_locale_id: kb_locales.map(&:id) })
  66. .pluck('knowledge_base_answer_translations.id')
  67. es_response.filter { |elem| allowed_answer_translation_ids.include? elem[:id].to_i }
  68. end
  69. def search_fallback(query, scope = nil, options: {})
  70. fields = %w[title]
  71. fields << KnowledgeBase::Answer::Translation::Content.arel_table[:body]
  72. output = where_or_cis(fields, query)
  73. .joins(:content)
  74. if !options[:user]&.permissions?('knowledge_base.editor')
  75. answer_ids = KnowledgeBase::Answer.internal.pluck(:id)
  76. output = output.where(answer_id: answer_ids)
  77. end
  78. if scope.present?
  79. output = output
  80. .joins(:answer)
  81. .where(knowledge_base_answers: { category_id: scope })
  82. end
  83. output
  84. end
  85. end
  86. end