2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-10 04:51:07 +08:00
|
|
|
class Wizard
|
|
|
|
class Builder
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@wizard = Wizard.new(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build
|
2016-09-15 04:36:08 +08:00
|
|
|
return @wizard unless SiteSetting.wizard_enabled? && @wizard.user.try(:staff?)
|
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
@wizard.append_step('introduction') do |step|
|
2022-09-12 23:34:15 +08:00
|
|
|
step.banner = "welcome-illustration"
|
2022-07-27 09:23:01 +08:00
|
|
|
|
2022-10-06 14:32:48 +08:00
|
|
|
step.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title == SiteSetting.defaults[:title] ? "" : SiteSetting.title)
|
2022-07-27 09:23:01 +08:00
|
|
|
step.add_field(id: 'site_description', type: 'text', required: false, value: SiteSetting.site_description)
|
|
|
|
step.add_field(id: 'contact_email', type: 'text', required: true, value: SiteSetting.contact_email)
|
2016-09-21 01:25:56 +08:00
|
|
|
|
2016-09-10 04:51:07 +08:00
|
|
|
languages = step.add_field(id: 'default_locale',
|
|
|
|
type: 'dropdown',
|
2022-11-17 05:53:45 +08:00
|
|
|
required: false,
|
2016-09-10 04:51:07 +08:00
|
|
|
value: SiteSetting.default_locale)
|
|
|
|
|
|
|
|
LocaleSiteSetting.values.each do |locale|
|
|
|
|
languages.add_choice(locale[:value], label: locale[:name])
|
|
|
|
end
|
|
|
|
|
2016-09-13 02:43:00 +08:00
|
|
|
step.on_update do |updater|
|
2022-07-27 09:23:01 +08:00
|
|
|
updater.ensure_changed(:title)
|
|
|
|
|
|
|
|
if updater.errors.blank?
|
|
|
|
updater.apply_settings(:title, :site_description, :contact_email)
|
|
|
|
end
|
|
|
|
|
2016-09-10 04:51:07 +08:00
|
|
|
old_locale = SiteSetting.default_locale
|
2016-09-13 02:43:00 +08:00
|
|
|
updater.apply_setting(:default_locale)
|
2019-03-19 04:09:13 +08:00
|
|
|
|
|
|
|
if old_locale != updater.fields[:default_locale]
|
|
|
|
Scheduler::Defer.later "Reseed" do
|
|
|
|
SeedData::Categories.with_default_locale.update(skip_changed: true)
|
|
|
|
SeedData::Topics.with_default_locale.update(skip_changed: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
updater.refresh_required = true
|
|
|
|
end
|
2016-09-10 04:51:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('privacy') do |step|
|
2022-09-12 23:34:15 +08:00
|
|
|
step.banner = "members-illustration"
|
2022-07-27 09:23:01 +08:00
|
|
|
step.add_field(
|
|
|
|
id: 'login_required',
|
|
|
|
type: 'checkbox',
|
|
|
|
icon: 'unlock',
|
|
|
|
value: SiteSetting.login_required
|
|
|
|
)
|
2016-09-16 04:14:47 +08:00
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
step.add_field(
|
|
|
|
id: 'invite_only',
|
|
|
|
type: 'checkbox',
|
|
|
|
icon: 'user-plus',
|
|
|
|
value: SiteSetting.invite_only
|
|
|
|
)
|
2016-09-16 04:14:47 +08:00
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
step.add_field(
|
|
|
|
id: 'must_approve_users',
|
|
|
|
type: 'checkbox',
|
|
|
|
icon: 'user-shield',
|
|
|
|
value: SiteSetting.must_approve_users
|
|
|
|
)
|
2016-09-10 04:51:07 +08:00
|
|
|
|
2016-09-13 02:43:00 +08:00
|
|
|
step.on_update do |updater|
|
2022-07-27 09:23:01 +08:00
|
|
|
updater.update_setting(:login_required, updater.fields[:login_required])
|
|
|
|
updater.update_setting(:invite_only, updater.fields[:invite_only])
|
|
|
|
updater.update_setting(:must_approve_users, updater.fields[:must_approve_users])
|
2016-09-13 02:43:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
@wizard.append_step('ready') do |step|
|
|
|
|
# no form on this page, just info.
|
2022-09-12 23:34:15 +08:00
|
|
|
step.banner = "finished-illustration"
|
2016-09-10 04:51:07 +08:00
|
|
|
end
|
|
|
|
|
2021-08-26 05:10:12 +08:00
|
|
|
@wizard.append_step('styling') do |step|
|
2018-07-12 12:18:21 +08:00
|
|
|
default_theme = Theme.find_by(id: SiteSetting.default_theme_id)
|
2019-05-09 17:22:28 +08:00
|
|
|
default_theme_override = SiteSetting.exists?(name: "default_theme_id")
|
|
|
|
|
2019-10-25 20:29:51 +08:00
|
|
|
base_scheme = default_theme&.color_scheme&.base_scheme_id
|
|
|
|
color_scheme_name = default_theme&.color_scheme&.name
|
|
|
|
|
|
|
|
scheme_id = default_theme_override ? (base_scheme || color_scheme_name) : ColorScheme::LIGHT_THEME_ID
|
2019-05-09 17:22:28 +08:00
|
|
|
|
|
|
|
themes = step.add_field(
|
2021-08-26 05:10:12 +08:00
|
|
|
id: 'color_scheme',
|
|
|
|
type: 'dropdown',
|
2019-05-09 17:22:28 +08:00
|
|
|
required: !default_theme_override,
|
2021-08-26 05:10:12 +08:00
|
|
|
value: scheme_id || ColorScheme::LIGHT_THEME_ID,
|
|
|
|
show_in_sidebar: true
|
2019-05-09 17:22:28 +08:00
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-10-25 20:29:51 +08:00
|
|
|
# fix for the case when base_scheme is nil
|
|
|
|
if scheme_id && default_theme_override && base_scheme.nil?
|
|
|
|
scheme = default_theme.color_scheme
|
2021-08-26 05:10:12 +08:00
|
|
|
themes.add_choice(scheme_id, data: { colors: scheme.colors_hashes })
|
2019-10-25 20:29:51 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
ColorScheme.base_color_scheme_colors.each do |t|
|
2021-08-26 05:10:12 +08:00
|
|
|
themes.add_choice(t[:id], data: { colors: t[:colors] })
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2016-09-10 04:51:07 +08:00
|
|
|
|
2021-08-26 05:10:12 +08:00
|
|
|
body_font = step.add_field(
|
|
|
|
id: 'body_font',
|
|
|
|
type: 'dropdown',
|
|
|
|
value: SiteSetting.base_font,
|
|
|
|
show_in_sidebar: true
|
|
|
|
)
|
|
|
|
|
|
|
|
heading_font = step.add_field(
|
|
|
|
id: 'heading_font',
|
|
|
|
type: 'dropdown',
|
|
|
|
value: SiteSetting.heading_font,
|
|
|
|
show_in_sidebar: true
|
|
|
|
)
|
|
|
|
|
|
|
|
DiscourseFonts.fonts.each do |font|
|
|
|
|
body_font.add_choice(font[:key], label: font[:name])
|
|
|
|
heading_font.add_choice(font[:key], label: font[:name])
|
|
|
|
end
|
|
|
|
|
|
|
|
current = SiteSetting.top_menu.starts_with?("categories") ? SiteSetting.desktop_category_page_style : "latest"
|
2022-11-17 05:53:45 +08:00
|
|
|
style = step.add_field(id: 'homepage_style', type: 'dropdown', required: false, value: current, show_in_sidebar: true)
|
2021-08-26 05:10:12 +08:00
|
|
|
style.add_choice('latest')
|
|
|
|
CategoryPageStyle.values.each do |page|
|
|
|
|
style.add_choice(page[:value])
|
|
|
|
end
|
|
|
|
|
|
|
|
step.add_field(
|
|
|
|
id: 'styling_preview',
|
|
|
|
type: 'component'
|
|
|
|
)
|
|
|
|
|
2016-09-13 02:43:00 +08:00
|
|
|
step.on_update do |updater|
|
2021-08-26 05:10:12 +08:00
|
|
|
updater.update_setting(:base_font, updater.fields[:body_font])
|
|
|
|
updater.update_setting(:heading_font, updater.fields[:heading_font])
|
|
|
|
|
2022-08-02 21:59:34 +08:00
|
|
|
top_menu = SiteSetting.top_menu.split("|")
|
|
|
|
if updater.fields[:homepage_style] == 'latest' && top_menu[0] != "latest"
|
|
|
|
top_menu.delete("latest")
|
|
|
|
top_menu.insert(0, "latest")
|
|
|
|
elsif updater.fields[:homepage_style] != 'latest' && top_menu[0] != "categories"
|
|
|
|
top_menu.delete("categories")
|
|
|
|
top_menu.insert(0, "categories")
|
2021-09-03 02:55:38 +08:00
|
|
|
updater.update_setting(:desktop_category_page_style, updater.fields[:homepage_style])
|
|
|
|
end
|
2022-08-02 21:59:34 +08:00
|
|
|
updater.update_setting(:top_menu, top_menu.join("|"))
|
2021-09-03 02:55:38 +08:00
|
|
|
|
2019-05-09 17:22:28 +08:00
|
|
|
scheme_name = (
|
2021-08-26 05:10:12 +08:00
|
|
|
(updater.fields[:color_scheme] || "") ||
|
2019-05-09 17:22:28 +08:00
|
|
|
ColorScheme::LIGHT_THEME_ID
|
|
|
|
)
|
|
|
|
|
2019-10-25 20:29:51 +08:00
|
|
|
next unless scheme_name.present? && ColorScheme.is_base?(scheme_name)
|
2019-05-09 17:22:28 +08:00
|
|
|
|
2018-07-24 21:00:20 +08:00
|
|
|
name = I18n.t("color_schemes.#{scheme_name.downcase.gsub(' ', '_')}_theme_name")
|
2020-10-14 22:18:02 +08:00
|
|
|
|
2018-07-24 21:00:20 +08:00
|
|
|
scheme = ColorScheme.find_by(base_scheme_id: scheme_name, via_wizard: true)
|
|
|
|
scheme ||= ColorScheme.create_from_base(name: name, via_wizard: true, base_scheme_id: scheme_name)
|
2020-04-20 21:31:43 +08:00
|
|
|
|
2020-10-14 22:18:02 +08:00
|
|
|
if default_theme
|
|
|
|
default_theme.color_scheme_id = scheme.id
|
|
|
|
default_theme.save!
|
|
|
|
else
|
|
|
|
theme = Theme.create!(
|
2021-08-26 05:10:12 +08:00
|
|
|
name: I18n.t("color_schemes.default_theme_name"),
|
2020-10-14 22:18:02 +08:00
|
|
|
user_id: @wizard.user.id,
|
|
|
|
color_scheme_id: scheme.id
|
|
|
|
)
|
|
|
|
|
|
|
|
theme.set_default!
|
2020-04-20 21:31:43 +08:00
|
|
|
end
|
2020-10-06 01:40:41 +08:00
|
|
|
|
2021-09-03 02:55:38 +08:00
|
|
|
if scheme.is_dark?
|
|
|
|
updater.update_setting(:default_dark_mode_color_scheme_id, -1)
|
2021-08-26 05:10:12 +08:00
|
|
|
end
|
2022-08-29 17:40:47 +08:00
|
|
|
updater.refresh_required = true
|
2020-08-31 18:14:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
@wizard.append_step('branding') do |step|
|
2018-11-14 15:03:02 +08:00
|
|
|
step.add_field(id: 'logo', type: 'image', value: SiteSetting.site_logo_url)
|
|
|
|
step.add_field(id: 'logo_small', type: 'image', value: SiteSetting.site_logo_small_url)
|
2016-09-17 00:13:54 +08:00
|
|
|
|
|
|
|
step.on_update do |updater|
|
2019-05-01 21:44:45 +08:00
|
|
|
if SiteSetting.site_logo_url != updater.fields[:logo] ||
|
|
|
|
SiteSetting.site_logo_small_url != updater.fields[:logo_small]
|
|
|
|
updater.apply_settings(:logo, :logo_small)
|
|
|
|
updater.refresh_required = true
|
|
|
|
end
|
2016-09-17 00:13:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
@wizard.append_step('corporate') do |step|
|
|
|
|
step.description_vars = { base_path: Discourse.base_path }
|
|
|
|
step.add_field(id: 'company_name', type: 'text', value: SiteSetting.company_name)
|
|
|
|
step.add_field(id: 'governing_law', type: 'text', value: SiteSetting.governing_law)
|
|
|
|
step.add_field(id: 'contact_url', type: 'text', value: SiteSetting.contact_url)
|
|
|
|
step.add_field(id: 'city_for_disputes', type: 'text', value: SiteSetting.city_for_disputes)
|
2016-09-22 05:15:57 +08:00
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
username = SiteSetting.site_contact_username
|
|
|
|
username = Discourse.system_user.username if username.blank?
|
|
|
|
contact = step.add_field(id: 'site_contact', type: 'dropdown', value: username)
|
2016-09-22 05:15:57 +08:00
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
User.human_users.where(admin: true).pluck(:username).each do |c|
|
|
|
|
contact.add_choice(c) unless reserved_usernames.include?(c.downcase)
|
|
|
|
end
|
|
|
|
contact.add_choice(Discourse.system_user.username)
|
2018-11-14 20:05:32 +08:00
|
|
|
|
2022-07-27 09:23:01 +08:00
|
|
|
step.on_update do |updater|
|
|
|
|
update_tos do |raw|
|
|
|
|
replace_setting_value(updater, raw, 'company_name')
|
|
|
|
replace_setting_value(updater, raw, 'governing_law')
|
|
|
|
replace_setting_value(updater, raw, 'city_for_disputes')
|
2016-09-14 03:14:17 +08:00
|
|
|
end
|
2022-07-27 09:23:01 +08:00
|
|
|
|
|
|
|
updater.apply_settings(:company_name, :governing_law, :city_for_disputes, :contact_url)
|
|
|
|
updater.update_setting(:site_contact_username, updater.fields[:site_contact])
|
2016-09-14 03:14:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 02:43:00 +08:00
|
|
|
DiscourseEvent.trigger(:build_wizard, @wizard)
|
2016-09-10 04:51:07 +08:00
|
|
|
@wizard
|
|
|
|
end
|
|
|
|
|
2018-11-21 07:18:30 +08:00
|
|
|
protected
|
2016-09-13 02:43:00 +08:00
|
|
|
|
2019-03-19 04:09:13 +08:00
|
|
|
def replace_setting_value(updater, raw, field_name)
|
2019-05-07 09:00:09 +08:00
|
|
|
old_value = SiteSetting.get(field_name)
|
2016-09-13 02:43:00 +08:00
|
|
|
old_value = field_name if old_value.blank?
|
|
|
|
|
|
|
|
new_value = updater.fields[field_name.to_sym]
|
|
|
|
new_value = field_name if new_value.blank?
|
|
|
|
|
2019-03-19 04:09:13 +08:00
|
|
|
raw.gsub!("<ins>#{old_value}</ins>", new_value) || raw.gsub!(old_value, new_value)
|
2016-09-13 02:43:00 +08:00
|
|
|
end
|
2017-08-15 10:17:41 +08:00
|
|
|
|
|
|
|
def reserved_usernames
|
|
|
|
@reserved_usernames ||= SiteSetting.defaults[:reserved_usernames].split('|')
|
|
|
|
end
|
2018-11-21 07:18:30 +08:00
|
|
|
|
|
|
|
def update_tos
|
|
|
|
tos_post = Post.find_by(topic_id: SiteSetting.tos_topic_id, post_number: 1)
|
|
|
|
|
|
|
|
if tos_post.present?
|
|
|
|
raw = tos_post.raw.dup
|
|
|
|
|
|
|
|
yield(raw)
|
|
|
|
|
|
|
|
revisor = PostRevisor.new(tos_post)
|
|
|
|
revisor.revise!(@wizard.user, raw: raw)
|
|
|
|
end
|
|
|
|
end
|
2016-09-10 04:51:07 +08:00
|
|
|
end
|
|
|
|
end
|