2016-09-20 17:01:54 -04:00
|
|
|
require_dependency 'introduction_updater'
|
2016-09-21 11:22:46 -04:00
|
|
|
require_dependency 'emoji_set_site_setting'
|
2016-09-20 17:01:54 -04:00
|
|
|
|
2016-09-09 16:51:07 -04:00
|
|
|
class Wizard
|
|
|
|
class Builder
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@wizard = Wizard.new(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build
|
2016-09-14 16:36:08 -04:00
|
|
|
return @wizard unless SiteSetting.wizard_enabled? && @wizard.user.try(:staff?)
|
|
|
|
|
2016-09-09 16:51:07 -04:00
|
|
|
@wizard.append_step('locale') do |step|
|
2016-09-20 13:25:56 -04:00
|
|
|
step.banner = "welcome.png"
|
|
|
|
|
2016-09-09 16:51:07 -04:00
|
|
|
languages = step.add_field(id: 'default_locale',
|
|
|
|
type: 'dropdown',
|
|
|
|
required: true,
|
|
|
|
value: SiteSetting.default_locale)
|
|
|
|
|
|
|
|
LocaleSiteSetting.values.each do |locale|
|
|
|
|
languages.add_choice(locale[:value], label: locale[:name])
|
|
|
|
end
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2016-09-09 16:51:07 -04:00
|
|
|
old_locale = SiteSetting.default_locale
|
2016-09-12 14:43:00 -04:00
|
|
|
updater.apply_setting(:default_locale)
|
|
|
|
updater.refresh_required = true if old_locale != updater.fields[:default_locale]
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('forum-title') do |step|
|
|
|
|
step.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title)
|
|
|
|
step.add_field(id: 'site_description', type: 'text', required: true, value: SiteSetting.site_description)
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2016-09-15 16:01:44 -04:00
|
|
|
updater.ensure_changed(:title)
|
|
|
|
|
|
|
|
if updater.errors.blank?
|
|
|
|
updater.apply_settings(:title, :site_description)
|
|
|
|
end
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-20 17:01:54 -04:00
|
|
|
@wizard.append_step('introduction') do |step|
|
|
|
|
introduction = IntroductionUpdater.new(@wizard.user)
|
|
|
|
|
|
|
|
step.add_field(id: 'welcome', type: 'textarea', required: true, value: introduction.get_summary)
|
|
|
|
|
|
|
|
step.on_update do |updater|
|
|
|
|
value = updater.fields[:welcome].strip
|
|
|
|
|
|
|
|
if value.index("\n")
|
|
|
|
updater.errors.add(:welcome, I18n.t("wizard.step.introduction.fields.welcome.one_paragraph"))
|
|
|
|
else
|
|
|
|
introduction.update_summary(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-09 16:51:07 -04:00
|
|
|
@wizard.append_step('privacy') do |step|
|
|
|
|
locked = SiteSetting.login_required? && SiteSetting.invite_only?
|
|
|
|
privacy = step.add_field(id: 'privacy',
|
|
|
|
type: 'radio',
|
|
|
|
required: true,
|
|
|
|
value: locked ? 'restricted' : 'open')
|
|
|
|
privacy.add_choice('open', icon: 'unlock')
|
|
|
|
privacy.add_choice('restricted', icon: 'lock')
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
|
|
|
updater.update_setting(:login_required, updater.fields[:privacy] == 'restricted')
|
|
|
|
updater.update_setting(:invite_only, updater.fields[:privacy] == 'restricted')
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('contact') do |step|
|
|
|
|
step.add_field(id: 'contact_email', type: 'text', required: true, value: SiteSetting.contact_email)
|
|
|
|
step.add_field(id: 'contact_url', type: 'text', value: SiteSetting.contact_url)
|
2016-09-15 16:14:47 -04: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)
|
|
|
|
|
2017-08-14 15:31:57 -04:00
|
|
|
User.human_users.where(admin: true).pluck(:username).each do |c|
|
|
|
|
contact.add_choice(c) unless reserved_usernames.include?(c.downcase)
|
|
|
|
end
|
2017-08-11 15:29:07 -04:00
|
|
|
contact.add_choice(Discourse.system_user.username)
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2016-09-15 16:14:47 -04:00
|
|
|
updater.apply_settings(:contact_email, :contact_url)
|
|
|
|
updater.update_setting(:site_contact_username, updater.fields[:site_contact])
|
2016-09-12 14:43:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('corporate') do |step|
|
|
|
|
step.add_field(id: 'company_short_name', type: 'text', value: SiteSetting.company_short_name)
|
|
|
|
step.add_field(id: 'company_full_name', type: 'text', value: SiteSetting.company_full_name)
|
|
|
|
step.add_field(id: 'company_domain', type: 'text', value: SiteSetting.company_domain)
|
|
|
|
|
|
|
|
step.on_update do |updater|
|
|
|
|
|
|
|
|
tos_post = Post.where(topic_id: SiteSetting.tos_topic_id, post_number: 1).first
|
|
|
|
if tos_post.present?
|
|
|
|
raw = tos_post.raw.dup
|
|
|
|
|
|
|
|
replace_company(updater, raw, 'company_full_name')
|
|
|
|
replace_company(updater, raw, 'company_short_name')
|
|
|
|
replace_company(updater, raw, 'company_domain')
|
|
|
|
|
|
|
|
revisor = PostRevisor.new(tos_post)
|
|
|
|
revisor.revise!(@wizard.user, raw: raw)
|
|
|
|
end
|
|
|
|
|
|
|
|
updater.apply_settings(:company_short_name, :company_full_name, :company_domain)
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('colors') do |step|
|
2017-05-03 11:31:16 -04:00
|
|
|
default_theme = Theme.find_by(key: SiteSetting.default_theme_key)
|
|
|
|
scheme_id = default_theme&.color_scheme&.base_scheme_id || 'default'
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
themes = step.add_field(id: 'base_scheme_id', type: 'dropdown', required: true, value: scheme_id)
|
|
|
|
ColorScheme.base_color_scheme_colors.each do |t|
|
|
|
|
with_hash = t[:colors].dup
|
2017-07-27 21:20:09 -04:00
|
|
|
with_hash.map { |k, v| with_hash[k] = "##{v}" }
|
|
|
|
themes.add_choice(t[:id], data: { colors: with_hash })
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2016-09-09 16:51:07 -04:00
|
|
|
step.add_field(id: 'theme_preview', type: 'component')
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2017-04-12 10:52:52 -04:00
|
|
|
scheme_name = updater.fields[:base_scheme_id]
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2017-05-03 11:31:16 -04:00
|
|
|
theme = nil
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2017-05-03 11:31:16 -04:00
|
|
|
if scheme_name == "dark"
|
|
|
|
scheme = ColorScheme.find_by(base_scheme_id: 'dark', via_wizard: true)
|
|
|
|
|
|
|
|
name = I18n.t("wizard.step.colors.fields.theme_id.choices.dark.label")
|
|
|
|
scheme ||= ColorScheme.create_from_base(name: name, via_wizard: true, base_scheme_id: "dark")
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2017-05-03 11:31:16 -04:00
|
|
|
theme = Theme.find_by(color_scheme_id: scheme.id)
|
|
|
|
name = I18n.t('color_schemes.dark_theme_name')
|
2017-05-03 23:44:23 -04:00
|
|
|
theme ||= Theme.create(name: name, color_scheme_id: scheme.id, user_id: @wizard.user.id)
|
2016-09-09 16:51:07 -04:00
|
|
|
else
|
2017-05-03 11:31:16 -04:00
|
|
|
themes = Theme.where(color_scheme_id: nil).order(:id).to_a
|
|
|
|
theme = themes.find(&:default?)
|
|
|
|
theme ||= themes.first
|
|
|
|
|
|
|
|
name = I18n.t('color_schemes.light_theme_name')
|
2017-05-03 23:44:23 -04:00
|
|
|
theme ||= Theme.create(name: name, user_id: @wizard.user.id)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2017-05-03 11:31:16 -04:00
|
|
|
|
|
|
|
theme.set_default!
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('logos') do |step|
|
|
|
|
step.add_field(id: 'logo_url', type: 'image', value: SiteSetting.logo_url)
|
|
|
|
step.add_field(id: 'logo_small_url', type: 'image', value: SiteSetting.logo_small_url)
|
2016-09-16 12:13:54 -04:00
|
|
|
|
|
|
|
step.on_update do |updater|
|
|
|
|
updater.apply_settings(:logo_url, :logo_small_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('icons') do |step|
|
2016-09-09 16:51:07 -04:00
|
|
|
step.add_field(id: 'favicon_url', type: 'image', value: SiteSetting.favicon_url)
|
|
|
|
step.add_field(id: 'apple_touch_icon_url', type: 'image', value: SiteSetting.apple_touch_icon_url)
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2016-09-16 12:13:54 -04:00
|
|
|
updater.apply_settings(:favicon_url, :apple_touch_icon_url)
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-21 16:09:18 -04:00
|
|
|
@wizard.append_step('homepage') do |step|
|
2016-09-21 17:34:15 -04:00
|
|
|
|
|
|
|
current = SiteSetting.top_menu.starts_with?("categories") ? "categories" : "latest"
|
|
|
|
|
|
|
|
style = step.add_field(id: 'homepage_style', type: 'dropdown', required: true, value: current)
|
2016-09-21 16:09:18 -04:00
|
|
|
style.add_choice('latest')
|
|
|
|
style.add_choice('categories')
|
|
|
|
step.add_field(id: 'homepage_preview', type: 'component')
|
|
|
|
|
|
|
|
step.on_update do |updater|
|
|
|
|
top_menu = "latest|new|unread|top|categories"
|
|
|
|
top_menu = "categories|latest|new|unread|top" if updater.fields[:homepage_style] == 'categories'
|
|
|
|
updater.update_setting(:top_menu, top_menu)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-21 11:22:46 -04:00
|
|
|
@wizard.append_step('emoji') do |step|
|
2017-07-27 21:20:09 -04:00
|
|
|
sets = step.add_field(id: 'emoji_set',
|
|
|
|
type: 'radio',
|
|
|
|
required: true,
|
|
|
|
value: SiteSetting.emoji_set)
|
2016-09-21 11:22:46 -04:00
|
|
|
|
|
|
|
emoji = ["smile", "+1", "tada", "poop"]
|
|
|
|
|
|
|
|
EmojiSetSiteSetting.values.each do |set|
|
|
|
|
imgs = emoji.map do |e|
|
|
|
|
"<img src='/images/emoji/#{set[:value]}/#{e}.png'>"
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
sets.add_choice(set[:value], label: I18n.t("js.#{set[:name]}"),
|
|
|
|
extra_label: "<span class='emoji-preview'>#{imgs.join}</span>")
|
2016-09-21 11:22:46 -04:00
|
|
|
|
|
|
|
step.on_update do |updater|
|
|
|
|
updater.apply_settings(:emoji_set)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 15:14:17 -04:00
|
|
|
@wizard.append_step('invites') do |step|
|
2016-09-21 17:15:57 -04:00
|
|
|
|
2017-08-14 22:17:41 -04:00
|
|
|
staff_count = User.staff.human_users.where('username_lower not in (?)', reserved_usernames).count
|
2016-09-21 17:15:57 -04:00
|
|
|
step.add_field(id: 'staff_count', type: 'component', value: staff_count)
|
|
|
|
|
2016-09-13 15:14:17 -04:00
|
|
|
step.add_field(id: 'invite_list', type: 'component')
|
|
|
|
|
|
|
|
step.on_update do |updater|
|
|
|
|
users = JSON.parse(updater.fields[:invite_list])
|
|
|
|
|
|
|
|
users.each do |u|
|
2016-09-20 13:12:00 -04:00
|
|
|
args = {}
|
|
|
|
args[:moderator] = true if u['role'] == 'moderator'
|
2017-04-21 09:37:24 -04:00
|
|
|
begin
|
|
|
|
Invite.create_invite_by_email(u['email'], @wizard.user, args)
|
|
|
|
rescue => e
|
|
|
|
updater.errors.add(:invite_list, e.message.concat("<br>"))
|
|
|
|
end
|
2016-09-13 15:14:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
DiscourseEvent.trigger(:build_wizard, @wizard)
|
|
|
|
|
2016-09-20 13:25:56 -04:00
|
|
|
@wizard.append_step('finished') do |step|
|
|
|
|
step.banner = "finished.png"
|
|
|
|
end
|
2016-09-09 16:51:07 -04:00
|
|
|
@wizard
|
|
|
|
end
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def replace_company(updater, raw, field_name)
|
|
|
|
old_value = SiteSetting.send(field_name)
|
|
|
|
old_value = field_name if old_value.blank?
|
|
|
|
|
|
|
|
new_value = updater.fields[field_name.to_sym]
|
|
|
|
new_value = field_name if new_value.blank?
|
|
|
|
|
|
|
|
raw.gsub!(old_value, new_value)
|
|
|
|
end
|
2017-08-14 22:17:41 -04:00
|
|
|
|
|
|
|
def reserved_usernames
|
|
|
|
@reserved_usernames ||= SiteSetting.defaults[:reserved_usernames].split('|')
|
|
|
|
end
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|