2017-04-12 10:52:52 -04:00
|
|
|
require_dependency 'distributed_cache'
|
|
|
|
require_dependency 'stylesheet/compiler'
|
|
|
|
require_dependency 'stylesheet/manager'
|
2018-03-04 19:04:23 -05:00
|
|
|
require_dependency 'theme_settings_parser'
|
|
|
|
require_dependency 'theme_settings_manager'
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
class Theme < ActiveRecord::Base
|
|
|
|
|
|
|
|
@cache = DistributedCache.new('theme')
|
|
|
|
|
|
|
|
belongs_to :color_scheme
|
|
|
|
has_many :theme_fields, dependent: :destroy
|
2018-03-04 19:04:23 -05:00
|
|
|
has_many :theme_settings, dependent: :destroy
|
2017-04-12 10:52:52 -04:00
|
|
|
has_many :child_theme_relation, class_name: 'ChildTheme', foreign_key: 'parent_theme_id', dependent: :destroy
|
|
|
|
has_many :child_themes, through: :child_theme_relation, source: :child_theme
|
2017-04-17 15:56:13 -04:00
|
|
|
has_many :color_schemes
|
2017-04-12 10:52:52 -04:00
|
|
|
belongs_to :remote_theme
|
|
|
|
|
|
|
|
before_create do
|
|
|
|
self.key ||= SecureRandom.uuid
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
def notify_color_change(color)
|
|
|
|
changed_colors << color
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
after_save do
|
2017-04-17 15:56:13 -04:00
|
|
|
changed_colors.each(&:save!)
|
|
|
|
changed_colors.clear
|
2017-04-12 10:52:52 -04:00
|
|
|
changed_fields.each(&:save!)
|
|
|
|
changed_fields.clear
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
Theme.expire_site_cache! if saved_change_to_user_selectable? || saved_change_to_name?
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
@dependant_themes = nil
|
|
|
|
@included_themes = nil
|
|
|
|
|
|
|
|
remove_from_cache!
|
2018-03-04 19:04:23 -05:00
|
|
|
clear_cached_settings!
|
2017-08-31 00:06:56 -04:00
|
|
|
notify_scheme_change if saved_change_to_color_scheme_id?
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after_destroy do
|
|
|
|
remove_from_cache!
|
2018-03-04 19:04:23 -05:00
|
|
|
clear_cached_settings!
|
2017-04-12 10:52:52 -04:00
|
|
|
if SiteSetting.default_theme_key == self.key
|
|
|
|
Theme.clear_default!
|
|
|
|
end
|
2017-05-04 14:03:07 -04:00
|
|
|
|
|
|
|
if self.id
|
|
|
|
ColorScheme
|
|
|
|
.where(theme_id: self.id)
|
|
|
|
.where("id NOT IN (SELECT color_scheme_id FROM themes where color_scheme_id IS NOT NULL)")
|
2017-07-27 21:20:09 -04:00
|
|
|
.destroy_all
|
2017-05-04 14:03:07 -04:00
|
|
|
|
|
|
|
ColorScheme
|
|
|
|
.where(theme_id: self.id)
|
|
|
|
.update_all(theme_id: nil)
|
|
|
|
end
|
2017-09-04 07:27:58 -04:00
|
|
|
|
|
|
|
Theme.expire_site_cache!
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after_commit ->(theme) do
|
|
|
|
theme.notify_theme_change
|
|
|
|
end, on: :update
|
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
def self.theme_keys
|
|
|
|
if keys = @cache["theme_keys"]
|
|
|
|
return keys
|
|
|
|
end
|
|
|
|
@cache["theme_keys"] = Set.new(Theme.pluck(:key))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.user_theme_keys
|
|
|
|
if keys = @cache["user_theme_keys"]
|
|
|
|
return keys
|
|
|
|
end
|
2018-02-23 01:58:13 -05:00
|
|
|
@cache["user_theme_keys"] = Set.new(
|
2017-04-14 13:35:12 -04:00
|
|
|
Theme
|
|
|
|
.where('user_selectable OR key = ?', SiteSetting.default_theme_key)
|
|
|
|
.pluck(:key)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def self.expire_site_cache!
|
|
|
|
Site.clear_anon_cache!
|
|
|
|
ApplicationSerializer.expire_cache_fragment!("user_themes")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_default!
|
|
|
|
SiteSetting.default_theme_key = ""
|
|
|
|
expire_site_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default!
|
|
|
|
SiteSetting.default_theme_key = key
|
|
|
|
Theme.expire_site_cache!
|
|
|
|
end
|
|
|
|
|
2017-05-03 11:31:16 -04:00
|
|
|
def default?
|
|
|
|
SiteSetting.default_theme_key == key
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def self.lookup_field(key, target, field)
|
|
|
|
return if key.blank?
|
|
|
|
|
|
|
|
cache_key = "#{key}:#{target}:#{field}:#{ThemeField::COMPILER_VERSION}"
|
|
|
|
lookup = @cache[cache_key]
|
|
|
|
return lookup.html_safe if lookup
|
|
|
|
|
|
|
|
target = target.to_sym
|
|
|
|
theme = find_by(key: key)
|
|
|
|
|
|
|
|
val = theme.resolve_baked_field(target, field) if theme
|
|
|
|
|
|
|
|
(@cache[cache_key] = val || "").html_safe
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.remove_from_cache!(themes = nil)
|
2017-04-12 10:52:52 -04:00
|
|
|
clear_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache!
|
|
|
|
@cache.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.targets
|
2018-03-04 19:04:23 -05:00
|
|
|
@targets ||= Enum.new(common: 0, desktop: 1, mobile: 2, settings: 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_target(target_id)
|
|
|
|
self.targets.invert[target_id]
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def notify_scheme_change(clear_manager_cache = true)
|
2017-04-12 10:52:52 -04:00
|
|
|
Stylesheet::Manager.cache.clear if clear_manager_cache
|
2017-04-18 16:48:15 -04:00
|
|
|
message = refresh_message_for_targets(["desktop", "mobile", "admin"], self)
|
2017-04-12 10:52:52 -04:00
|
|
|
MessageBus.publish('/file-change', message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_theme_change
|
|
|
|
Stylesheet::Manager.clear_theme_cache!
|
|
|
|
|
|
|
|
themes = [self] + dependant_themes
|
|
|
|
|
|
|
|
message = themes.map do |theme|
|
2017-07-27 21:20:09 -04:00
|
|
|
refresh_message_for_targets([:mobile_theme, :desktop_theme], theme)
|
2017-04-12 10:52:52 -04:00
|
|
|
end.compact.flatten
|
|
|
|
MessageBus.publish('/file-change', message)
|
|
|
|
end
|
|
|
|
|
2017-04-18 16:48:15 -04:00
|
|
|
def refresh_message_for_targets(targets, theme)
|
2017-04-12 10:52:52 -04:00
|
|
|
targets.map do |target|
|
2017-04-18 16:48:15 -04:00
|
|
|
href = Stylesheet::Manager.stylesheet_href(target.to_sym, theme.key)
|
|
|
|
if href
|
2017-04-12 10:52:52 -04:00
|
|
|
{
|
2017-04-18 16:48:15 -04:00
|
|
|
target: target,
|
|
|
|
new_href: href,
|
|
|
|
theme_key: theme.key
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependant_themes
|
|
|
|
@dependant_themes ||= resolve_dependant_themes(:up)
|
|
|
|
end
|
|
|
|
|
|
|
|
def included_themes
|
|
|
|
@included_themes ||= resolve_dependant_themes(:down)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_dependant_themes(direction)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
select_field, where_field = nil
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
if direction == :up
|
|
|
|
select_field = "parent_theme_id"
|
|
|
|
where_field = "child_theme_id"
|
|
|
|
elsif direction == :down
|
|
|
|
select_field = "child_theme_id"
|
|
|
|
where_field = "parent_theme_id"
|
|
|
|
else
|
|
|
|
raise "Unknown direction"
|
|
|
|
end
|
|
|
|
|
|
|
|
themes = []
|
|
|
|
return [] unless id
|
|
|
|
|
|
|
|
uniq = Set.new
|
|
|
|
uniq << id
|
|
|
|
|
|
|
|
iterations = 0
|
|
|
|
added = [id]
|
|
|
|
|
|
|
|
while added.length > 0 && iterations < 5
|
|
|
|
|
|
|
|
iterations += 1
|
|
|
|
|
|
|
|
new_themes = Theme.where("id in (SELECT #{select_field}
|
|
|
|
FROM child_themes
|
|
|
|
WHERE #{where_field} in (?))", added).to_a
|
|
|
|
|
|
|
|
added = []
|
|
|
|
new_themes.each do |theme|
|
|
|
|
unless uniq.include?(theme.id)
|
|
|
|
added << theme.id
|
|
|
|
uniq << theme.id
|
|
|
|
themes << theme
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
themes
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_baked_field(target, name)
|
2017-07-27 21:20:09 -04:00
|
|
|
list_baked_fields(target, name).map { |f| f.value_baked || f.value }.join("\n")
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_baked_fields(target, name)
|
|
|
|
|
|
|
|
target = target.to_sym
|
|
|
|
|
|
|
|
theme_ids = [self.id] + (included_themes.map(&:id) || [])
|
2017-05-02 16:01:01 -04:00
|
|
|
fields = ThemeField.where(target_id: [Theme.targets[target], Theme.targets[:common]])
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(name: name.to_s)
|
|
|
|
.includes(:theme)
|
|
|
|
.joins("
|
|
|
|
JOIN (
|
|
|
|
SELECT #{theme_ids.map.with_index { |id, idx| "#{id} AS theme_id, #{idx} AS sort_column" }.join(" UNION ALL SELECT ")}
|
|
|
|
) as X ON X.theme_id = theme_fields.theme_id"
|
|
|
|
)
|
|
|
|
.order('sort_column, target_id')
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
fields.each(&:ensure_baked!)
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_cache!
|
|
|
|
self.class.remove_from_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
def changed_fields
|
|
|
|
@changed_fields ||= []
|
|
|
|
end
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
def changed_colors
|
|
|
|
@changed_colors ||= []
|
|
|
|
end
|
|
|
|
|
2017-05-08 11:38:48 -04:00
|
|
|
def set_field(target:, name:, value: nil, type: nil, type_id: nil, upload_id: nil)
|
2017-04-12 10:52:52 -04:00
|
|
|
name = name.to_s
|
|
|
|
|
|
|
|
target_id = Theme.targets[target.to_sym]
|
|
|
|
raise "Unknown target #{target} passed to set field" unless target_id
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
type_id ||= type ? ThemeField.types[type.to_sym] : ThemeField.guess_type(name)
|
|
|
|
raise "Unknown type #{type} passed to set field" unless type_id
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
value ||= ""
|
2017-05-08 11:38:48 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
field = theme_fields.find { |f| f.name == name && f.target_id == target_id && f.type_id == type_id }
|
2017-04-12 10:52:52 -04:00
|
|
|
if field
|
2017-05-09 17:20:28 -04:00
|
|
|
if value.blank? && !upload_id
|
2017-04-13 17:24:15 -04:00
|
|
|
theme_fields.delete field.destroy
|
2017-04-12 10:52:52 -04:00
|
|
|
else
|
2017-05-09 17:20:28 -04:00
|
|
|
if field.value != value || field.upload_id != upload_id
|
2017-04-12 10:52:52 -04:00
|
|
|
field.value = value
|
2017-05-09 17:20:28 -04:00
|
|
|
field.upload_id = upload_id
|
2017-04-12 10:52:52 -04:00
|
|
|
changed_fields << field
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2017-05-08 11:38:48 -04:00
|
|
|
theme_fields.build(target_id: target_id, value: value, name: name, type_id: type_id, upload_id: upload_id) if value.present? || upload_id.present?
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-13 23:22:59 -05:00
|
|
|
def all_theme_variables
|
|
|
|
fields = {}
|
|
|
|
([self] + (included_themes || [])).each do |theme|
|
|
|
|
theme&.theme_fields.each do |field|
|
|
|
|
next unless ThemeField.theme_var_type_ids.include?(field.type_id)
|
|
|
|
next if fields.key?(field.name)
|
|
|
|
fields[field.name] = field
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fields.values
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def add_child_theme!(theme)
|
|
|
|
child_theme_relation.create!(child_theme_id: theme.id)
|
|
|
|
@included_themes = nil
|
|
|
|
child_themes.reload
|
|
|
|
save!
|
|
|
|
end
|
2018-03-04 19:04:23 -05:00
|
|
|
|
|
|
|
def settings
|
|
|
|
field = theme_fields.where(target_id: Theme.targets[:settings], name: "yaml").first
|
|
|
|
return [] unless field && field.error.nil?
|
|
|
|
|
|
|
|
settings = []
|
|
|
|
ThemeSettingsParser.new(field).load do |name, default, type, opts|
|
|
|
|
settings << ThemeSettingsManager.create(name, default, type, self, opts)
|
|
|
|
end
|
|
|
|
settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_settings
|
|
|
|
Rails.cache.fetch("settings_for_theme_#{self.key}", expires_in: 30.minutes) do
|
|
|
|
hash = {}
|
|
|
|
self.settings.each do |setting|
|
|
|
|
hash[setting.name] = setting.value
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cached_settings!
|
|
|
|
Rails.cache.delete("settings_for_theme_#{self.key}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def included_settings
|
|
|
|
hash = {}
|
|
|
|
|
|
|
|
self.included_themes.each do |theme|
|
|
|
|
hash.merge!(theme.cached_settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
hash.merge!(self.cached_settings)
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.settings_for_client(key)
|
|
|
|
theme = Theme.find_by(key: key)
|
|
|
|
return {}.to_json unless theme
|
|
|
|
|
|
|
|
theme.included_settings.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_setting(setting_name, new_value)
|
|
|
|
target_setting = settings.find { |setting| setting.name == setting_name }
|
|
|
|
raise Discourse::NotFound unless target_setting
|
|
|
|
|
|
|
|
target_setting.value = new_value
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: themes
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2018-02-20 01:28:58 -05:00
|
|
|
# name :string not null
|
2017-04-12 10:52:52 -04:00
|
|
|
# user_id :integer not null
|
2018-02-20 01:28:58 -05:00
|
|
|
# key :string not null
|
2017-04-12 10:52:52 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# compiler_version :integer default(0), not null
|
|
|
|
# user_selectable :boolean default(FALSE), not null
|
|
|
|
# hidden :boolean default(FALSE), not null
|
|
|
|
# color_scheme_id :integer
|
|
|
|
# remote_theme_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_themes_on_key (key)
|
|
|
|
# index_themes_on_remote_theme_id (remote_theme_id) UNIQUE
|
|
|
|
#
|