2015-08-07 14:05:08 -04:00
|
|
|
class Admin::SiteTextsController < Admin::AdminController
|
|
|
|
|
2015-11-23 16:45:05 -05: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-11-30 15:22:58 -05:00
|
|
|
overridden = params[:overridden] == 'true'
|
|
|
|
extras = {}
|
|
|
|
|
2015-12-08 12:49:37 -05:00
|
|
|
query = params[:q] || ""
|
|
|
|
if query.blank? && !overridden
|
2015-11-30 15:22:58 -05:00
|
|
|
extras[:recommended] = true
|
2017-07-27 21:20:09 -04:00
|
|
|
results = self.class.preferred_keys.map { |k| record_for(k) }
|
2015-11-23 16:45:05 -05:00
|
|
|
else
|
|
|
|
results = []
|
2015-12-08 12:49:37 -05:00
|
|
|
translations = I18n.search(query, overridden: overridden)
|
2015-11-23 16:45:05 -05:00
|
|
|
translations.each do |k, v|
|
2016-04-08 14:49:50 -04:00
|
|
|
results << record_for(k, v)
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
2015-11-30 15:31:30 -05:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
results.sort! do |x, y|
|
2015-12-08 12:49:37 -05:00
|
|
|
if x[:value].casecmp(query) == 0
|
2015-11-30 15:31:30 -05:00
|
|
|
-1
|
2015-12-08 12:49:37 -05:00
|
|
|
elsif y[:value].casecmp(query) == 0
|
2015-11-30 15:31:30 -05:00
|
|
|
1
|
|
|
|
else
|
|
|
|
(x[:id].size + x[:value].size) <=> (y[:id].size + y[:value].size)
|
|
|
|
end
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 15:22:58 -05:00
|
|
|
render_serialized(results[0..50], SiteTextSerializer, root: 'site_texts', rest_serializer: true, extras: extras)
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
|
2015-08-07 14:05:08 -04:00
|
|
|
def show
|
2015-11-23 16:45:05 -05:00
|
|
|
site_text = find_site_text
|
|
|
|
render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true)
|
2015-08-07 14:05:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-11-23 16:45:05 -05:00
|
|
|
site_text = find_site_text
|
2017-06-15 04:08:23 -04: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-07 14:05:08 -04:00
|
|
|
|
2017-06-15 04:08:23 -04: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-23 16:45:05 -05:00
|
|
|
end
|
2015-08-07 14:05:08 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
def revert
|
|
|
|
site_text = find_site_text
|
2015-12-18 08:31:04 -05:00
|
|
|
old_text = I18n.t(site_text[:id])
|
2015-11-23 16:45:05 -05:00
|
|
|
TranslationOverride.revert!(I18n.locale, site_text[:id])
|
|
|
|
site_text = find_site_text
|
2015-12-18 08:31:04 -05:00
|
|
|
StaffActionLogger.new(current_user).log_site_text_change(site_text[:id], site_text[:value], old_text)
|
2015-11-23 16:45:05 -05:00
|
|
|
render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true)
|
2015-08-07 14:05:08 -04:00
|
|
|
end
|
2015-11-23 16:45:05 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def record_for(k, value = nil)
|
2016-04-08 14:49:50 -04: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-27 21:20:09 -04:00
|
|
|
{ id: k, value: value }
|
2016-04-08 14:49:50 -04:00
|
|
|
end
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
def find_site_text
|
|
|
|
raise Discourse::NotFound unless I18n.exists?(params[:id])
|
2016-04-08 14:49:50 -04:00
|
|
|
record_for(params[:id])
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
|
2015-08-07 14:05:08 -04:00
|
|
|
end
|