2015-08-08 02:05:08 +08:00
|
|
|
class Admin::SiteTextsController < Admin::AdminController
|
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
def self.preferred_keys
|
|
|
|
['system_messages.usage_tips.text_body_template',
|
|
|
|
'education.new-topic',
|
|
|
|
'education.new-reply',
|
|
|
|
'login_required.welcome_message']
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2015-12-01 04:22:58 +08:00
|
|
|
overridden = params[:overridden] == 'true'
|
|
|
|
extras = {}
|
|
|
|
|
2015-12-09 01:49:37 +08:00
|
|
|
query = params[:q] || ""
|
|
|
|
if query.blank? && !overridden
|
2015-12-01 04:22:58 +08:00
|
|
|
extras[:recommended] = true
|
2017-07-28 09:20:09 +08:00
|
|
|
results = self.class.preferred_keys.map { |k| record_for(k) }
|
2015-11-24 05:45:05 +08:00
|
|
|
else
|
|
|
|
results = []
|
2015-12-09 01:49:37 +08:00
|
|
|
translations = I18n.search(query, overridden: overridden)
|
2015-11-24 05:45:05 +08:00
|
|
|
translations.each do |k, v|
|
2016-04-09 02:49:50 +08:00
|
|
|
results << record_for(k, v)
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
2015-12-01 04:31:30 +08:00
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
results.sort! do |x, y|
|
2015-12-09 01:49:37 +08:00
|
|
|
if x[:value].casecmp(query) == 0
|
2015-12-01 04:31:30 +08:00
|
|
|
-1
|
2015-12-09 01:49:37 +08:00
|
|
|
elsif y[:value].casecmp(query) == 0
|
2015-12-01 04:31:30 +08:00
|
|
|
1
|
|
|
|
else
|
|
|
|
(x[:id].size + x[:value].size) <=> (y[:id].size + y[:value].size)
|
|
|
|
end
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-01 04:22:58 +08:00
|
|
|
render_serialized(results[0..50], SiteTextSerializer, root: 'site_texts', rest_serializer: true, extras: extras)
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
|
|
|
|
2015-08-08 02:05:08 +08:00
|
|
|
def show
|
2015-11-24 05:45:05 +08:00
|
|
|
site_text = find_site_text
|
|
|
|
render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true)
|
2015-08-08 02:05:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-11-24 05:45:05 +08:00
|
|
|
site_text = find_site_text
|
2017-06-15 16:08:23 +08:00
|
|
|
value = site_text[:value] = params[:site_text][:value]
|
|
|
|
id = site_text[:id]
|
|
|
|
old_value = I18n.t(id)
|
|
|
|
translation_override = TranslationOverride.upsert!(I18n.locale, id, value)
|
2015-08-08 02:05:08 +08:00
|
|
|
|
2017-06-15 16:08:23 +08:00
|
|
|
if translation_override.errors.empty?
|
|
|
|
StaffActionLogger.new(current_user).log_site_text_change(id, value, old_value)
|
|
|
|
render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true)
|
|
|
|
else
|
|
|
|
render json: failed_json.merge(
|
|
|
|
message: translation_override.errors.full_messages.join("\n\n")
|
|
|
|
), status: 422
|
|
|
|
end
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
2015-08-08 02:05:08 +08:00
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
def revert
|
|
|
|
site_text = find_site_text
|
2015-12-18 21:31:04 +08:00
|
|
|
old_text = I18n.t(site_text[:id])
|
2015-11-24 05:45:05 +08:00
|
|
|
TranslationOverride.revert!(I18n.locale, site_text[:id])
|
|
|
|
site_text = find_site_text
|
2015-12-18 21:31:04 +08:00
|
|
|
StaffActionLogger.new(current_user).log_site_text_change(site_text[:id], site_text[:value], old_text)
|
2015-11-24 05:45:05 +08:00
|
|
|
render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true)
|
2015-08-08 02:05:08 +08:00
|
|
|
end
|
2015-11-24 05:45:05 +08:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def record_for(k, value = nil)
|
2016-04-09 02:49:50 +08:00
|
|
|
if k.ends_with?("_MF")
|
|
|
|
ovr = TranslationOverride.where(translation_key: k).pluck(:value)
|
|
|
|
value = ovr[0] if ovr.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
value ||= I18n.t(k)
|
2017-07-28 09:20:09 +08:00
|
|
|
{ id: k, value: value }
|
2016-04-09 02:49:50 +08:00
|
|
|
end
|
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
def find_site_text
|
|
|
|
raise Discourse::NotFound unless I18n.exists?(params[:id])
|
2016-04-09 02:49:50 +08:00
|
|
|
record_for(params[:id])
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
|
|
|
|
2015-08-08 02:05:08 +08:00
|
|
|
end
|