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