knowledge_base_helper.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module KnowledgeBaseHelper
  3. def effective_layout_name(knowledge_base, object)
  4. layout_prefix = object.present? ? :category : :homepage
  5. knowledge_base.send(:"#{layout_prefix}_layout")
  6. end
  7. def custom_path_if_needed(path, knowledge_base, full: false)
  8. return path if !knowledge_base.custom_address_matches?(request)
  9. custom_path(path, knowledge_base, full:)
  10. end
  11. def custom_path(path, knowledge_base, full: false)
  12. custom_address = knowledge_base.custom_address_uri
  13. return path if !custom_address
  14. custom_path = knowledge_base.custom_address_path(path)
  15. prefix = full ? knowledge_base.custom_address_prefix(request) : ''
  16. "#{prefix}#{custom_path}"
  17. end
  18. def translation_locale_code(translation)
  19. translation.kb_locale.system_locale.locale
  20. end
  21. def edit_kb_link_label(object)
  22. suffix = case object
  23. when KnowledgeBase::Answer
  24. 'answer'
  25. when KnowledgeBase::Category
  26. 'category'
  27. when KnowledgeBase
  28. 'knowledge base'
  29. end
  30. "edit #{suffix}"
  31. end
  32. def build_kb_link(object)
  33. locale = params.fetch(:locale, object.translation.kb_locale)
  34. path = case object
  35. when KnowledgeBase::Answer
  36. "knowledge_base/#{object.category.knowledge_base.id}/locale/#{locale}/answer/#{object.id}/edit"
  37. when KnowledgeBase::Category
  38. "knowledge_base/#{object.knowledge_base.id}/locale/#{locale}/category/#{object.id}/edit"
  39. when KnowledgeBase
  40. "knowledge_base/#{object.id}/locale/#{locale}/edit"
  41. end
  42. build_zammad_link path
  43. end
  44. def build_zammad_link(path)
  45. host, port = Setting.get('fqdn').split ':'
  46. scheme = Setting.get('http_type')
  47. URI::Generic
  48. .build(host: host, scheme: scheme, port: port, fragment: path)
  49. .to_s
  50. end
  51. def dropdown_menu_direction
  52. system_locale_via_uri.dir == 'ltr' ? 'right' : 'left'
  53. end
  54. def canonical_link_tag(knowledge_base, *objects)
  55. path = kb_public_system_path(*objects)
  56. tag.link(rel: 'canonical', href: knowledge_base.canonical_url(path))
  57. end
  58. def kb_public_system_path(*objects)
  59. objects
  60. .compact
  61. .map { |elem| elem.is_a?(HasTranslations) ? elem.translation.to_param : elem }
  62. .unshift(help_root_path)
  63. .join('/')
  64. end
  65. def feeds_available(knowledge_base, parent_category, current_object)
  66. feeds = [{
  67. title: knowledge_base.translations.first.title,
  68. url: custom_path_if_needed(help_root_feed_url, knowledge_base)
  69. }]
  70. effective_category = [current_object, parent_category].find { |elem| elem.is_a? KnowledgeBase::Category }
  71. if effective_category
  72. feeds << {
  73. title: effective_category.translations.first.title,
  74. url: custom_path_if_needed(help_category_feed_url(system_locale_via_uri.locale, effective_category), knowledge_base)
  75. }
  76. end
  77. feeds
  78. end
  79. end