text_module.rb 2.7 KB

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