2019-07-30 15:05:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class EmailStyleUpdater
|
|
|
|
|
|
|
|
attr_reader :errors
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(attrs)
|
2019-10-23 15:41:58 -04:00
|
|
|
if attrs.has_key?(:html) && !attrs[:html].include?('%{email_content}')
|
|
|
|
@errors << I18n.t(
|
|
|
|
'email_style.html_missing_placeholder',
|
|
|
|
placeholder: '%{email_content}'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if attrs.has_key?(:css)
|
|
|
|
begin
|
|
|
|
compiled_css = SassC::Engine.new(attrs[:css], style: :compressed).render
|
|
|
|
rescue SassC::SyntaxError => e
|
|
|
|
# @errors << I18n.t('email_style.css_syntax_error')
|
|
|
|
@errors << e.message[0...(e.message.index("\n"))]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false unless @errors.empty?
|
|
|
|
|
2019-07-30 15:05:08 -04:00
|
|
|
if attrs.has_key?(:html)
|
|
|
|
if attrs[:html] == EmailStyle.default_template
|
|
|
|
SiteSetting.remove_override!(:email_custom_template)
|
|
|
|
else
|
2019-10-23 15:41:58 -04:00
|
|
|
SiteSetting.email_custom_template = attrs[:html]
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if attrs.has_key?(:css)
|
|
|
|
if attrs[:css] == EmailStyle.default_css
|
|
|
|
SiteSetting.remove_override!(:email_custom_css)
|
2019-10-23 15:41:58 -04:00
|
|
|
SiteSetting.remove_override!(:email_custom_css_compiled)
|
2019-07-30 15:05:08 -04:00
|
|
|
else
|
|
|
|
SiteSetting.email_custom_css = attrs[:css]
|
2019-10-23 15:41:58 -04:00
|
|
|
SiteSetting.email_custom_css_compiled = compiled_css
|
2019-07-30 15:05:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@errors.empty?
|
|
|
|
end
|
|
|
|
end
|