diff --git a/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 b/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 index 1c8eb6b0860..4c5a2a28549 100644 --- a/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 @@ -15,6 +15,7 @@ export default Ember.Controller.extend(bufferedProperty('emailTemplate'), { actions: { saveChanges() { + this.set('saved', false); const buffered = this.get('buffered'); this.get('emailTemplate').save(buffered.getProperties('subject', 'body')).then(() => { this.set('saved', true); diff --git a/app/controllers/admin/email_templates_controller.rb b/app/controllers/admin/email_templates_controller.rb index b6fd3b631c3..4c1df06239c 100644 --- a/app/controllers/admin/email_templates_controller.rb +++ b/app/controllers/admin/email_templates_controller.rb @@ -42,16 +42,34 @@ class Admin::EmailTemplatesController < Admin::AdminController key = params[:id] raise Discourse::NotFound unless self.class.email_keys.include?(params[:id]) - TranslationOverride.upsert!(I18n.locale, "#{key}.subject_template", et[:subject]) - TranslationOverride.upsert!(I18n.locale, "#{key}.text_body_template", et[:body]) + subject_result = update_key("#{key}.subject_template", et[:subject]) + body_result = update_key("#{key}.text_body_template", et[:body]) - render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true) + error_messages = [] + if subject_result[:error_messages].present? + TranslationOverride.upsert!(I18n.locale, "#{key}.subject_template", subject_result[:old_value]) + error_messages << format_error_message(subject_result, "subject") + end + if body_result[:error_messages].present? + TranslationOverride.upsert!(I18n.locale, "#{key}.text_body_template", body_result[:old_value]) + error_messages << format_error_message(body_result, "body") + end + + if error_messages.blank? + log_site_text_change(subject_result) + log_site_text_change(body_result) + + render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true) + else + render_json_error(error_messages) + end end def revert key = params[:id] raise Discourse::NotFound unless self.class.email_keys.include?(params[:id]) - TranslationOverride.revert!(I18n.locale, "#{key}.subject_template", "#{key}.text_body_template") + + revert_and_log("#{key}.subject_template", "#{key}.text_body_template") render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true) end @@ -59,4 +77,41 @@ class Admin::EmailTemplatesController < Admin::AdminController render_serialized(self.class.email_keys, AdminEmailTemplateSerializer, root: 'email_templates', rest_serializer: true) end + private + + def update_key(key, value) + old_value = I18n.t(key) + translation_override = TranslationOverride.upsert!(I18n.locale, key, value) + + { + key: key, + old_value: old_value, + error_messages: translation_override.errors.full_messages + } + end + + def revert_and_log(*keys) + old_values = {} + keys.each { |key| old_values[key] = I18n.t(key) } + + TranslationOverride.revert!(I18n.locale, keys) + + keys.each do |key| + old_value = old_values[key] + new_value = I18n.t(key) + StaffActionLogger.new(current_user).log_site_text_change(key, new_value, old_value) + end + end + + def log_site_text_change(update_result) + new_value = I18n.t(update_result[:key]) + StaffActionLogger.new(current_user).log_site_text_change( + update_result[:key], new_value, update_result[:old_value]) + end + + def format_error_message(update_result, attribute_key) + attribute = I18n.t("admin_js.admin.customize.email_templates.#{attribute_key}") + message = update_result[:error_messages].join("
") + I18n.t("errors.format_with_full_message", attribute: attribute, message: message) + end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index f0ea00a3132..11c191568ae 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -83,6 +83,7 @@ en: errors: &errors format: ! '%{attribute} %{message}' + format_with_full_message: '%{attribute}: %{message}' messages: too_long_validation: "is limited to %{max} characters; you entered %{length}." invalid_boolean: "Invalid boolean."