2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-10-22 13:00:26 +00:00
|
|
|
class TextModule < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksClientNotification
|
2017-07-20 13:58:03 +00:00
|
|
|
include ChecksHtmlSanitized
|
2018-02-20 04:29:30 +00:00
|
|
|
include CanCsvImport
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :content, presence: true
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
before_create :validate_content
|
|
|
|
before_update :validate_content
|
|
|
|
|
2021-04-12 09:49:26 +00:00
|
|
|
sanitized_html :content, :note
|
2017-07-20 13:58:03 +00:00
|
|
|
|
2018-06-12 20:58:59 +00:00
|
|
|
csv_delete_possible true
|
|
|
|
|
2019-07-03 16:14:28 +00:00
|
|
|
has_and_belongs_to_many :groups, after_add: :cache_update, after_remove: :cache_update, class_name: 'Group'
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
association_attributes_ignored :user
|
|
|
|
|
2016-07-25 20:13:38 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
load text modules from online
|
|
|
|
|
|
|
|
TextModule.load('de-de', overwrite_existing_item) # e. g. 'en-us' or 'de-de'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.load(locale, overwrite_existing_item = false)
|
2017-11-15 14:06:53 +00:00
|
|
|
raise 'Got no locale' if locale.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
locale = locale.split(',').first.downcase # in case of accept_language header is given
|
2016-07-25 20:13:38 +00:00
|
|
|
url = "https://i18n.zammad.com/api/v1/text_modules/#{locale}"
|
|
|
|
|
|
|
|
result = UserAgent.get(
|
|
|
|
url,
|
|
|
|
{},
|
|
|
|
{
|
2021-07-20 13:31:46 +00:00
|
|
|
json: true,
|
|
|
|
verify_ssl: true,
|
2016-07-25 20:13:38 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
raise "Can't load text modules from #{url}" if !result
|
|
|
|
raise "Can't load text modules from #{url}: #{result.error}" if !result.success?
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2017-10-01 12:25:52 +00:00
|
|
|
result.data.each do |text_module|
|
2016-07-25 20:13:38 +00:00
|
|
|
exists = TextModule.find_by(foreign_id: text_module['foreign_id'])
|
|
|
|
if exists
|
|
|
|
next if !overwrite_existing_item
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-09-11 11:16:08 +00:00
|
|
|
exists.update!(text_module.symbolize_keys!)
|
2016-07-25 20:13:38 +00:00
|
|
|
else
|
|
|
|
text_module[:updated_by_id] = 1
|
|
|
|
text_module[:created_by_id] = 1
|
|
|
|
TextModule.create(text_module.symbolize_keys!)
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-07-25 20:13:38 +00:00
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
push text_modules to online
|
|
|
|
|
|
|
|
TextModule.push(locale)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.push(locale)
|
|
|
|
|
|
|
|
# only push changed text_modules
|
2021-07-16 13:44:10 +00:00
|
|
|
text_modules = TextModule.all # where(locale: locale)
|
2016-07-25 20:13:38 +00:00
|
|
|
text_modules_to_push = []
|
2017-10-01 12:25:52 +00:00
|
|
|
text_modules.each do |text_module|
|
2016-07-25 20:13:38 +00:00
|
|
|
next if !text_module.active
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-07-25 20:13:38 +00:00
|
|
|
text_modules_to_push.push text_module
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
return true if text_modules_to_push.blank?
|
2016-07-25 20:13:38 +00:00
|
|
|
|
|
|
|
url = 'https://i18n.zammad.com/api/v1/text_modules/thanks_for_your_support'
|
|
|
|
|
|
|
|
translator_key = Setting.get('translator_key')
|
|
|
|
|
|
|
|
result = UserAgent.post(
|
|
|
|
url,
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
locale: locale,
|
|
|
|
text_modules: text_modules_to_push,
|
|
|
|
fqdn: Setting.get('fqdn'),
|
2016-07-25 20:13:38 +00:00
|
|
|
translator_key: translator_key,
|
|
|
|
},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
json: true,
|
2016-07-25 20:13:38 +00:00
|
|
|
open_timeout: 6,
|
|
|
|
read_timeout: 16,
|
2021-07-20 13:31:46 +00:00
|
|
|
verify_ssl: true,
|
2016-07-25 20:13:38 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
raise "Can't push text_modules to #{url}: #{result.error}" if !result.success?
|
|
|
|
|
|
|
|
# set new translator_key if given
|
|
|
|
if result.data['translator_key']
|
2019-06-28 11:38:49 +00:00
|
|
|
Setting.set('translator_key', result.data['translator_key'])
|
2016-07-25 20:13:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_content
|
|
|
|
return true if content.blank?
|
2021-05-12 11:37:44 +00:00
|
|
|
return true if content.match?(%r{<.+?>})
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
content.gsub!(%r{(\r\n|\n\r|\r)}, "\n")
|
2018-02-20 04:29:30 +00:00
|
|
|
self.content = content.text2html
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|