knowledge_base_helper.rb 2.8 KB

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