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'
|
2019-03-18 16:09:13 -04:00
|
|
|
require_dependency 'seed_data/categories'
|
|
|
|
require_dependency 'seed_data/topics'
|
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)
|
2019-03-18 16:09:13 -04: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-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)
|
2018-12-12 05:46:14 -05:00
|
|
|
step.add_field(id: 'short_site_description', type: 'text', required: false, value: SiteSetting.short_site_description)
|
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:01:44 -04:00
|
|
|
updater.ensure_changed(:title)
|
|
|
|
|
|
|
|
if updater.errors.blank?
|
2018-12-12 05:46:14 -05:00
|
|
|
updater.apply_settings(:title, :site_description, :short_site_description)
|
2016-09-15 16:01:44 -04:00
|
|
|
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)
|
|
|
|
|
2018-11-15 06:20:48 -05:00
|
|
|
if @wizard.completed_steps?('introduction') && !introduction.get_summary
|
|
|
|
step.disabled = true
|
2018-12-05 12:05:37 -05:00
|
|
|
step.description_vars = { topic_title: I18n.t("discourse_welcome_topic.title") }
|
2018-11-15 06:20:48 -05:00
|
|
|
else
|
|
|
|
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
|
2016-09-20 17:01:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-09 16:51:07 -04:00
|
|
|
@wizard.append_step('privacy') do |step|
|
|
|
|
privacy = step.add_field(id: 'privacy',
|
|
|
|
type: 'radio',
|
|
|
|
required: true,
|
2019-04-11 10:55:08 -04:00
|
|
|
value: SiteSetting.login_required? ? 'restricted' : 'open')
|
2016-09-09 16:51:07 -04:00
|
|
|
privacy.add_choice('open', icon: 'unlock')
|
|
|
|
privacy.add_choice('restricted', icon: 'lock')
|
|
|
|
|
2019-04-11 10:55:08 -04:00
|
|
|
invite_only = step.add_field(id: 'invite_only',
|
|
|
|
type: 'checkbox',
|
|
|
|
required: false,
|
|
|
|
placeholder: 'wizard.invites.add_user',
|
|
|
|
value: SiteSetting.invite_only?)
|
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
|
|
|
updater.update_setting(:login_required, updater.fields[:privacy] == 'restricted')
|
2019-04-11 10:55:08 -04:00
|
|
|
updater.update_setting(:invite_only, updater.fields[:invite_only])
|
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|
|
2018-11-20 18:18:30 -05:00
|
|
|
update_tos do |raw|
|
2019-03-18 16:09:13 -04:00
|
|
|
replace_setting_value(updater, raw, 'contact_email')
|
2018-11-20 18:18:30 -05:00
|
|
|
end
|
|
|
|
|
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|
|
2018-12-05 12:05:37 -05:00
|
|
|
step.description_vars = { base_path: Discourse.base_path }
|
2018-11-20 18:18:30 -05:00
|
|
|
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: 'city_for_disputes', type: 'text', value: SiteSetting.city_for_disputes)
|
2016-09-12 14:43:00 -04:00
|
|
|
|
|
|
|
step.on_update do |updater|
|
2018-11-20 18:18:30 -05:00
|
|
|
update_tos do |raw|
|
2019-03-18 16:09:13 -04:00
|
|
|
replace_setting_value(updater, raw, 'company_name')
|
|
|
|
replace_setting_value(updater, raw, 'governing_law')
|
|
|
|
replace_setting_value(updater, raw, 'city_for_disputes')
|
2016-09-12 14:43:00 -04:00
|
|
|
end
|
|
|
|
|
2018-11-20 18:18:30 -05:00
|
|
|
updater.apply_settings(:company_name, :governing_law, :city_for_disputes)
|
2016-09-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('colors') do |step|
|
2018-07-12 00:18:21 -04:00
|
|
|
default_theme = Theme.find_by(id: SiteSetting.default_theme_id)
|
2018-07-24 09:00:20 -04:00
|
|
|
scheme_id = default_theme&.color_scheme&.base_scheme_id || 'Light'
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-07-24 09:00:20 -04:00
|
|
|
themes = step.add_field(id: 'theme_previews', type: 'component', required: true, value: scheme_id)
|
2017-04-12 10:52:52 -04:00
|
|
|
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
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2018-07-24 09:00:20 -04:00
|
|
|
scheme_name = updater.fields[:theme_previews] || 'Light'
|
|
|
|
name = I18n.t("color_schemes.#{scheme_name.downcase.gsub(' ', '_')}_theme_name")
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2017-05-03 11:31:16 -04:00
|
|
|
theme = nil
|
2018-07-24 09:00:20 -04: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)
|
|
|
|
themes = Theme.where(color_scheme_id: scheme.id).order(:id).to_a
|
|
|
|
theme = themes.find(&:default?)
|
|
|
|
theme ||= themes.first
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2018-07-24 09:00:20 -04:00
|
|
|
theme ||= Theme.create(name: name, user_id: @wizard.user.id, color_scheme_id: scheme.id)
|
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|
|
2018-11-14 02:03:02 -05: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-16 12:13:54 -04:00
|
|
|
|
|
|
|
step.on_update do |updater|
|
2018-11-14 02:03:02 -05:00
|
|
|
updater.apply_settings(:logo, :logo_small)
|
2016-09-16 12:13:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@wizard.append_step('icons') do |step|
|
2018-11-22 01:19:36 -05:00
|
|
|
step.add_field(id: 'favicon', type: 'image', value: SiteSetting.site_favicon_url)
|
|
|
|
step.add_field(id: 'apple_touch_icon', type: 'image', value: SiteSetting.site_apple_touch_icon_url)
|
2016-09-09 16:51:07 -04:00
|
|
|
|
2016-09-12 14:43:00 -04:00
|
|
|
step.on_update do |updater|
|
2018-11-14 02:03:02 -05:00
|
|
|
updater.apply_settings(:favicon)
|
2018-06-19 11:37:02 -04:00
|
|
|
|
2018-11-14 02:03:02 -05:00
|
|
|
if updater.fields[:apple_touch_icon] != SiteSetting.apple_touch_icon
|
|
|
|
upload = Upload.find_by_url(updater.fields[:apple_touch_icon])
|
2018-06-19 11:37:02 -04:00
|
|
|
dimensions = 180 # for apple touch icon
|
2018-11-14 02:03:02 -05:00
|
|
|
|
2018-06-19 11:37:02 -04:00
|
|
|
if upload && upload.width > dimensions && upload.height > dimensions
|
2018-11-14 02:03:02 -05:00
|
|
|
updater.update_setting(:large_icon, upload)
|
2018-06-19 11:37:02 -04:00
|
|
|
|
2018-08-16 02:32:36 -04:00
|
|
|
apple_touch_icon_optimized = OptimizedImage.create_for(
|
|
|
|
upload,
|
|
|
|
dimensions,
|
|
|
|
dimensions
|
|
|
|
)
|
|
|
|
|
2018-06-19 11:37:02 -04:00
|
|
|
original_file = File.new(Discourse.store.path_for(apple_touch_icon_optimized)) rescue nil
|
2018-11-14 02:03:02 -05:00
|
|
|
|
2018-06-19 11:37:02 -04:00
|
|
|
if original_file
|
|
|
|
apple_touch_icon_upload = UploadCreator.new(original_file, upload.original_filename).create_for(@wizard.user.id)
|
2018-11-14 02:03:02 -05:00
|
|
|
updater.update_setting(:apple_touch_icon, apple_touch_icon_upload)
|
2018-06-19 11:37:02 -04:00
|
|
|
end
|
2018-11-14 02:03:02 -05:00
|
|
|
|
2018-06-19 11:37:02 -04:00
|
|
|
apple_touch_icon_optimized.destroy! if apple_touch_icon_optimized.present?
|
|
|
|
else
|
2018-11-14 02:03:02 -05:00
|
|
|
updater.apply_settings(:apple_touch_icon)
|
2018-06-19 11:37:02 -04:00
|
|
|
end
|
|
|
|
end
|
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
|
|
|
|
2018-06-14 02:31:56 -04:00
|
|
|
current = SiteSetting.top_menu.starts_with?("categories") ? SiteSetting.desktop_category_page_style : "latest"
|
2016-09-21 17:34:15 -04:00
|
|
|
|
|
|
|
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')
|
2018-06-14 02:31:56 -04:00
|
|
|
CategoryPageStyle.values.each do |page|
|
|
|
|
style.add_choice(page[:value])
|
|
|
|
end
|
|
|
|
|
2016-09-21 16:09:18 -04:00
|
|
|
step.add_field(id: 'homepage_preview', type: 'component')
|
|
|
|
|
|
|
|
step.on_update do |updater|
|
2018-06-14 02:31:56 -04:00
|
|
|
if updater.fields[:homepage_style] == 'latest'
|
|
|
|
top_menu = "latest|new|unread|top|categories"
|
|
|
|
else
|
|
|
|
top_menu = "categories|latest|new|unread|top"
|
|
|
|
updater.update_setting(:desktop_category_page_style, updater.fields[:homepage_style])
|
|
|
|
end
|
2016-09-21 16:09:18 -04:00
|
|
|
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|
|
2018-07-10 11:02:22 -04:00
|
|
|
"<img src='#{Discourse.base_uri}/images/emoji/#{set[:value]}/#{e}.png'>"
|
2016-09-21 11:22:46 -04:00
|
|
|
end
|
|
|
|
|
2018-11-20 18:18:30 -05: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|
|
2018-11-14 07:05:32 -05:00
|
|
|
if SiteSetting.enable_local_logins
|
|
|
|
staff_count = User.staff.human_users.where('username_lower not in (?)', reserved_usernames).count
|
|
|
|
step.add_field(id: 'staff_count', type: 'component', value: staff_count)
|
2016-09-21 17:15:57 -04:00
|
|
|
|
2018-11-14 07:05:32 -05:00
|
|
|
step.add_field(id: 'invite_list', type: 'component')
|
2016-09-21 17:15:57 -04:00
|
|
|
|
2018-11-14 07:05:32 -05:00
|
|
|
step.on_update do |updater|
|
|
|
|
users = JSON.parse(updater.fields[:invite_list])
|
|
|
|
|
|
|
|
users.each do |u|
|
|
|
|
args = {}
|
|
|
|
args[:moderator] = true if u['role'] == 'moderator'
|
|
|
|
begin
|
|
|
|
Invite.create_invite_by_email(u['email'], @wizard.user, args)
|
|
|
|
rescue => e
|
|
|
|
updater.errors.add(:invite_list, e.message.concat("<br>"))
|
|
|
|
end
|
2017-04-21 09:37:24 -04:00
|
|
|
end
|
2016-09-13 15:14:17 -04:00
|
|
|
end
|
2018-11-14 07:05:32 -05:00
|
|
|
else
|
|
|
|
step.disabled = true
|
2016-09-13 15:14:17 -04:00
|
|
|
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"
|
2018-12-05 12:05:37 -05:00
|
|
|
step.description_vars = { base_path: Discourse.base_path }
|
2016-09-20 13:25:56 -04:00
|
|
|
end
|
2016-09-09 16:51:07 -04:00
|
|
|
@wizard
|
|
|
|
end
|
|
|
|
|
2018-11-20 18:18:30 -05:00
|
|
|
protected
|
2016-09-12 14:43:00 -04:00
|
|
|
|
2019-03-18 16:09:13 -04:00
|
|
|
def replace_setting_value(updater, raw, field_name)
|
2016-09-12 14:43:00 -04:00
|
|
|
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?
|
|
|
|
|
2019-03-18 16:09:13 -04:00
|
|
|
raw.gsub!("<ins>#{old_value}</ins>", new_value) || raw.gsub!(old_value, new_value)
|
2016-09-12 14:43:00 -04:00
|
|
|
end
|
2017-08-14 22:17:41 -04:00
|
|
|
|
|
|
|
def reserved_usernames
|
|
|
|
@reserved_usernames ||= SiteSetting.defaults[:reserved_usernames].split('|')
|
|
|
|
end
|
2018-11-20 18:18:30 -05: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-09 16:51:07 -04:00
|
|
|
end
|
|
|
|
end
|