text_module.rb 2.7 KB

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