text_module.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. class TextModule < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksHtmlSanitized
  5. include CanCsvImport
  6. validates :name, presence: true
  7. validates :content, presence: true
  8. before_create :validate_content
  9. before_update :validate_content
  10. sanitized_html :content, :note
  11. csv_delete_possible true
  12. has_and_belongs_to_many :groups, after_add: :cache_update, after_remove: :cache_update, class_name: 'Group'
  13. association_attributes_ignored :user
  14. =begin
  15. load text modules from online
  16. TextModule.load('de-de', overwrite_existing_item) # e. g. 'en-us' or 'de-de'
  17. =end
  18. def self.load(locale, overwrite_existing_item = false)
  19. raise 'Got no locale' if locale.blank?
  20. locale = locale.split(',').first.downcase # in case of accept_language header is given
  21. url = "https://i18n.zammad.com/api/v1/text_modules/#{locale}"
  22. result = UserAgent.get(
  23. url,
  24. {},
  25. {
  26. json: true,
  27. verify_ssl: true,
  28. }
  29. )
  30. raise "Can't load text modules from #{url}" if !result
  31. raise "Can't load text modules from #{url}: #{result.error}" if !result.success?
  32. ActiveRecord::Base.transaction do
  33. result.data.each do |text_module|
  34. exists = TextModule.find_by(foreign_id: text_module['foreign_id'])
  35. if exists
  36. next if !overwrite_existing_item
  37. exists.update!(text_module.symbolize_keys!)
  38. else
  39. text_module[:updated_by_id] = 1
  40. text_module[:created_by_id] = 1
  41. TextModule.create(text_module.symbolize_keys!)
  42. end
  43. end
  44. end
  45. true
  46. end
  47. =begin
  48. push text_modules to online
  49. TextModule.push(locale)
  50. =end
  51. def self.push(locale)
  52. # only push changed text_modules
  53. text_modules = TextModule.all # where(locale: locale)
  54. text_modules_to_push = []
  55. text_modules.each do |text_module|
  56. next if !text_module.active
  57. text_modules_to_push.push text_module
  58. end
  59. return true if text_modules_to_push.blank?
  60. url = 'https://i18n.zammad.com/api/v1/text_modules/thanks_for_your_support'
  61. translator_key = Setting.get('translator_key')
  62. result = UserAgent.post(
  63. url,
  64. {
  65. locale: locale,
  66. text_modules: text_modules_to_push,
  67. fqdn: Setting.get('fqdn'),
  68. translator_key: translator_key,
  69. },
  70. {
  71. json: true,
  72. open_timeout: 6,
  73. read_timeout: 16,
  74. verify_ssl: true,
  75. }
  76. )
  77. raise "Can't push text_modules to #{url}: #{result.error}" if !result.success?
  78. # set new translator_key if given
  79. if result.data['translator_key']
  80. Setting.set('translator_key', result.data['translator_key'])
  81. end
  82. true
  83. end
  84. private
  85. def validate_content
  86. return true if content.blank?
  87. return true if content.match?(%r{<.+?>})
  88. content.gsub!(%r{(\r\n|\n\r|\r)}, "\n")
  89. self.content = content.text2html
  90. true
  91. end
  92. end