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 4545cd3ef65..5e787b6bbbc 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 @@ -6,11 +6,23 @@ export default Ember.Controller.extend(bufferedProperty('emailTemplate'), { actions: { saveChanges() { - const model = this.get('emailTemplate'); const buffered = this.get('buffered'); - model.save(buffered.getProperties('subject', 'body')).then(() => { + this.get('emailTemplate').save(buffered.getProperties('subject', 'body')).then(() => { this.set('saved', true); }).catch(popupAjaxError); + }, + + revertChanges() { + this.set('saved', false); + bootbox.confirm(I18n.t('admin.customize.email_templates.revert_confirm'), result => { + if (result) { + this.get('emailTemplate').revert().then(props => { + const buffered = this.get('buffered'); + buffered.setProperties(props); + this.commitBuffer(); + }).catch(popupAjaxError); + } + }); } } }); diff --git a/app/assets/javascripts/admin/models/email-template.js.es6 b/app/assets/javascripts/admin/models/email-template.js.es6 new file mode 100644 index 00000000000..3da44800778 --- /dev/null +++ b/app/assets/javascripts/admin/models/email-template.js.es6 @@ -0,0 +1,12 @@ +import RestModel from 'discourse/models/rest'; + +const { getProperties } = Ember; +console.log(getProperties); + +export default RestModel.extend({ + revert() { + return Discourse.ajax(`/admin/customize/email_templates/${this.get('id')}`, { + method: 'DELETE' + }).then(result => getProperties(result.email_template, 'subject', 'body', 'can_revert')); + } +}); diff --git a/app/assets/javascripts/admin/templates/components/save-controls.hbs b/app/assets/javascripts/admin/templates/components/save-controls.hbs index e54a090d796..e8e13cb9c05 100644 --- a/app/assets/javascripts/admin/templates/components/save-controls.hbs +++ b/app/assets/javascripts/admin/templates/components/save-controls.hbs @@ -1,2 +1,5 @@ -{{d-button action="saveChanges" disabled=buttonDisabled label=savingText}} -{{#if saved}}{{i18n 'saved'}}{{/if}} +{{d-button action="saveChanges" disabled=buttonDisabled label=savingText class="btn-primary"}} +{{yield}} +
diff --git a/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs b/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs index 8f264cf88bf..97ceb083cd3 100644 --- a/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs +++ b/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs @@ -9,5 +9,9 @@ {{d-editor value=buffered.body}} - {{save-controls model=emailTemplate action="saveChanges" saved=saved}} + {{#save-controls model=emailTemplate action="saveChanges" saved=saved}} + {{#if emailTemplate.can_revert}} + {{d-button action="revertChanges" label="admin.customize.email_templates.revert"}} + {{/if}} + {{/save-controls}} diff --git a/app/assets/stylesheets/common/admin/admin_base.scss b/app/assets/stylesheets/common/admin/admin_base.scss index ee6ba203672..8a3bdd297e7 100644 --- a/app/assets/stylesheets/common/admin/admin_base.scss +++ b/app/assets/stylesheets/common/admin/admin_base.scss @@ -122,6 +122,10 @@ td.flaggers td { .admin-container .controls { @include clearfix; + + .save-messages { + margin-top: 1em; + } } .admin-title { diff --git a/app/controllers/admin/email_templates_controller.rb b/app/controllers/admin/email_templates_controller.rb index 27fd11d7fe8..9d872c99842 100644 --- a/app/controllers/admin/email_templates_controller.rb +++ b/app/controllers/admin/email_templates_controller.rb @@ -35,7 +35,6 @@ class Admin::EmailTemplatesController < Admin::AdminController def update et = params[:email_template] key = params[:id] - raise Discourse::NotFound unless self.class.email_keys.include?(params[:id]) TranslationOverride.upsert!(I18n.locale, "#{key}.subject_template", et[:subject]) @@ -44,6 +43,13 @@ class Admin::EmailTemplatesController < Admin::AdminController render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true) 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") + render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true) + end + def index render_serialized(self.class.email_keys, AdminEmailTemplateSerializer, root: 'email_templates', rest_serializer: true) end diff --git a/app/models/translation_override.rb b/app/models/translation_override.rb index 27914abbdcc..a433605e8e4 100644 --- a/app/models/translation_override.rb +++ b/app/models/translation_override.rb @@ -6,8 +6,19 @@ class TranslationOverride < ActiveRecord::Base params = { locale: locale, translation_key: key } row_count = where(params).update_all(value: value) create!(params.merge(value: value)) if row_count == 0 - - MessageBus.publish('/i18n-flush', { refresh: true }) + i18n_changed end + def self.revert!(locale, *keys) + TranslationOverride.where(locale: locale, translation_key: keys).delete_all + i18n_changed + end + + protected + + def self.i18n_changed + I18n.reload! + MessageBus.publish('/i18n-flush', { refresh: true }) + end + end diff --git a/app/serializers/admin_email_template_serializer.rb b/app/serializers/admin_email_template_serializer.rb index 1c8618cfec8..9ac8838b3e3 100644 --- a/app/serializers/admin_email_template_serializer.rb +++ b/app/serializers/admin_email_template_serializer.rb @@ -1,5 +1,5 @@ class AdminEmailTemplateSerializer < ApplicationSerializer - attributes :id, :title, :subject, :body + attributes :id, :title, :subject, :body, :can_revert? def id object @@ -10,10 +10,19 @@ class AdminEmailTemplateSerializer < ApplicationSerializer end def subject - I18n.t("#{object}.subject_template") + @subject ||= I18n.t("#{object}.subject_template") end def body - I18n.t("#{object}.text_body_template") + @body ||= I18n.t("#{object}.text_body_template") + end + + def can_revert? + current_body, current_subject = body, subject + + I18n.overrides_disabled do + return I18n.t("#{object}.subject_template") != current_subject || + I18n.t("#{object}.text_body_template") != current_body + end end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 24b64591c26..7974d558019 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2093,6 +2093,8 @@ en: subject: "Subject" body: "Body" none_selected: "Select an email template to begin editing." + revert: "Revert Changes" + revert_confirm: "Are you sure you want to revert your changes?" css_html: title: "CSS/HTML" diff --git a/config/routes.rb b/config/routes.rb index bf0ecb589b1..df8e88d4152 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -163,6 +163,7 @@ Discourse::Application.routes.draw do get 'email_templates' => 'email_templates#index' match 'email_templates/(:id)' => 'email_templates#show', :constraints => { :id => /[0-9a-z\_\.]+/ }, via: :get match 'email_templates/(:id)' => 'email_templates#update', :constraints => { :id => /[0-9a-z\_\.]+/ }, via: :put + match 'email_templates/(:id)' => 'email_templates#revert', :constraints => { :id => /[0-9a-z\_\.]+/ }, via: :delete end resources :embeddable_hosts, constraints: AdminConstraint.new