text_module.rb 2.4 KB

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