text_module.rb 2.9 KB

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