2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://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
|
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
import text modules from i18n/text_modules/*.yml if no text modules exist yet.
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
TextModule.load('de-de') # e. g. 'en-us' or 'de-de'
|
2016-07-25 20:13:38 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
def self.load(locale)
|
2021-11-15 15:58:19 +00:00
|
|
|
raise __('Got no locale') if locale.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
return if !TextModule.count.zero?
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
locale = locale.split(',').first.downcase # in case of accept_language header is given
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
# First check the full locale, e.g. 'de-de'.
|
|
|
|
filename = Rails.root.join("i18n/text_modules/#{locale}.yml")
|
|
|
|
if !File.exist?(filename)
|
|
|
|
# Fall back to the more generic language if needed, e.g. 'de'.
|
|
|
|
locale = locale.split('-').first
|
|
|
|
filename = Rails.root.join("i18n/text_modules/#{locale}.yml")
|
2016-07-25 20:13:38 +00:00
|
|
|
end
|
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
if !File.exist?(filename)
|
|
|
|
# No text modules available for current locale data.
|
|
|
|
return
|
|
|
|
end
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
file_content = File.read(filename)
|
|
|
|
result = Psych.load(file_content)
|
2016-07-25 20:13:38 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
raise "Can't load text modules from #{filename}" if result.length.zero?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-12-14 10:17:47 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
result.each do |text_module|
|
|
|
|
text_module[:updated_by_id] = 1
|
|
|
|
text_module[:created_by_id] = 1
|
|
|
|
TextModule.create(text_module.symbolize_keys!)
|
|
|
|
end
|
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
|