Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/custom_wizard/admin/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
class CustomWizard::UserController < ::Admin::AdminController
before_action :ensure_admin
requires_plugin "discourse-custom-wizard"

def clear_redirect
user = User.find_by(id: params[:id])

if user
user.custom_fields["redirect_to_wizard"] = nil
user.save_custom_fields(true)
render json: success_json
else
render json: failed_json
end
end
end
1 change: 1 addition & 0 deletions app/controllers/custom_wizard/admin/wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def save_wizard_params
:resume_on_revisit,
:theme_id,
permitted: mapped_params,
after_time_groups: [],
steps: [
:id,
:index,
Expand Down
22 changes: 17 additions & 5 deletions app/jobs/regular/set_after_time_wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@ module Jobs
class SetAfterTimeWizard < ::Jobs::Base
def execute(args)
if SiteSetting.custom_wizard_enabled
wizard = CustomWizard::Wizard.create(args[:wizard_id])
@wizard = CustomWizard::Wizard.create(args[:wizard_id])

if wizard && wizard.after_time
if @wizard && @wizard.after_time
user_ids = []

User.human_users.each do |user|
user_ids.push(user.id) if CustomWizard::Wizard.set_user_redirect(wizard.id, user)
target_users.each do |user|
user_ids.push(user.id) if CustomWizard::Wizard.set_after_time_redirect(@wizard.id, user)
end

CustomWizard::Template.clear_cache_keys

MessageBus.publish "/redirect_to_wizard", wizard.id, user_ids: user_ids
MessageBus.publish "/redirect_to_wizard", @wizard.id, user_ids: user_ids
end
end
end

def target_users
users = []

if @wizard.after_time_groups.exists?
@wizard.after_time_groups.each { |group| users += group.users }
else
users = User.human_users
end

users
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<section class="details">
<h1>{{i18n "admin.wizard.user.label"}}</h1>

<div class="display-row">
<div class="field">{{i18n "admin.wizard.user.redirect.label"}}</div>
<div class="value">
{{#if model.redirect_to_wizard}}
<LinkTo
@route="adminWizardsWizardShow"
@model={{dasherize model.redirect_to_wizard}}
>
{{model.redirect_to_wizard}}
</LinkTo>
{{else}}
&mdash;
{{/if}}
</div>
<div class="controls">
{{#if model.redirect_to_wizard}}
<DButton
@action={{fn model.clearWizardRedirect model}}
@label="admin.wizard.user.redirect.remove_label"
@title="admin.wizard.user.redirect.remove_title"
/>
{{/if}}
</div>
</div>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import Controller from "@ember/controller";
import copyText from "discourse/lib/copy-text";
import I18n from "I18n";
import { filterValues } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
import { action } from "@ember/object";

export default Controller.extend({
modal: service(),
site: service(),
hasName: notEmpty("wizard.name"),

@observes("currentStep")
Expand Down Expand Up @@ -91,6 +93,24 @@ export default Controller.extend({
return I18n.t(`admin.wizard.error.${errorType}`, errorParams);
},

setAfterTimeGroupIds() {
const groups = this.site.groups.filter((g) =>
this.wizard.after_time_groups.includes(g.name)
);
this.setProperties({
afterTimeGroupIds: groups.map((g) => g.id),
});
},

@action
setAfterTimeGroups(groupIds) {
const groups = this.site.groups.filter((g) => groupIds.includes(g.id));
this.setProperties({
afterTimeGroupIds: groups.map((g) => g.id),
"wizard.after_time_groups": groups.map((g) => g.name),
});
},

actions: {
save() {
this.setProperties({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import DiscourseURL from "discourse/lib/url";
import { withPluginApi } from "discourse/lib/plugin-api";
import getUrl from "discourse-common/lib/get-url";
import { observes } from "discourse-common/utils/decorators";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { ajax } from "discourse/lib/ajax";

export default {
name: "custom-wizard-edits",
Expand Down Expand Up @@ -105,6 +107,24 @@ export default {
route: "adminWizardsWizard",
icon: "hat-wizard",
});

if (api.getCurrentUser()?.admin) {
api.modifyClass("model:admin-user", {
pluginId: "custom-wizard",

clearWizardRedirect(user) {
return ajax(`/admin/users/${user.id}/wizards/clear_redirect`, {
type: "PUT",
})
.then(() => {
user.setProperties({
redirect_to_wizard: null,
});
})
.catch(popupAjaxError);
},
});
}
});
},
};
2 changes: 2 additions & 0 deletions assets/javascripts/discourse/lib/wizard-schema.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ const wizard = {
resume_on_revisit: null,
theme_id: null,
permitted: null,
after_time_groups: null,
},
mapped: ["permitted"],
required: ["id"],
dependent: {
after_time: "after_time_scheduled",
after_time: "after_time_groups",
},
objectArrays: {
step: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export default DiscourseRoute.extend({
};

controller.setProperties(props);
controller.setAfterTimeGroupIds();
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@
}}
</div>
</div>

<div class="setting full">
<div class="setting-label">
<label>{{i18n "admin.wizard.after_time_groups.label"}}</label>
</div>
<div class="setting-value">
<GroupChooser
@content={{this.site.groups}}
@value={{this.afterTimeGroupIds}}
@onChange={{this.setAfterTimeGroups}}
/>
<div class="setting-gutter">
{{i18n "admin.wizard.after_time_groups.description"}}
</div>
</div>
</div>
{{/wizard-subscription-container}}
</div>

Expand Down
7 changes: 6 additions & 1 deletion assets/stylesheets/common/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,16 @@ $error: #ef1700;
}

.input .select-kit,
> .select-kit {
> .select-kit:not(.group-chooser) {
max-width: 250px !important;
min-width: 250px !important;
}

.group-chooser {
max-width: 400px !important;
min-width: 400px !important;
}

&.force-final {
padding: 1em;
background-color: var(--primary-very-low);
Expand Down
9 changes: 9 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ en:
time: "Time"
done: "Set Time"
clear: "Clear"
after_time_groups:
label: Time Groups
description: Groups directed to wizard after start time.
required: "Required"
required_label: "Users cannot skip the wizard."
prompt_completion: "Prompt"
Expand Down Expand Up @@ -587,6 +590,12 @@ en:
community:
label: Support
title: There is a Custom Wizard Community subscription active on this forum.
user:
label: Wizards
redirect:
label: Redirect
remove_label: Remove
remove_title: Remove wizard redirect

wizard_js:
group:
Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ en:
after_signup: "You can only have one 'after signup' wizard at a time. %{wizard_id} has 'after signup' enabled."
after_signup_after_time: "You can't use 'after time' and 'after signup' on the same wizard."
after_time: "After time setting is invalid."
after_time_group: "After time group does not exist: %{group_name}"
liquid_syntax_error: "Liquid syntax error in %{attribute}: %{message}"
subscription: "%{type} %{property} usage is not supported on your subscription"
not_permitted_for_guests: "%{object_id} is not permitted when guests can access the wizard"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@
post "admin/wizards/manager/import" => "admin_manager#import"
delete "admin/wizards/manager/destroy" => "admin_manager#destroy"
end

put "/admin/users/:id/wizards/clear_redirect" => "custom_wizard/user#clear_redirect"
end
6 changes: 6 additions & 0 deletions lib/custom_wizard/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def self.attributes
business: ["*"],
community: ["*"],
},
after_time_groups: {
none: [],
standard: [],
business: ["*"],
community: [],
},
},
step: {
condition: {
Expand Down
4 changes: 2 additions & 2 deletions lib/custom_wizard/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def schedule_save_jobs
end

if enqueue_wizard_at
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard_id)
self.class.clear_user_wizard_redirect(wizard_id, after_time: true)
Jobs.enqueue_at(enqueue_wizard_at, :set_after_time_wizard, wizard_id: wizard_id)
elsif old_data && old_data[:after_time]
clear_user_wizard_redirect(wizard_id, after_time: true)
self.class.clear_user_wizard_redirect(wizard_id, after_time: true)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/custom_wizard/validators/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ def validate_after_time
if invalid_time || active_time.blank? || active_time < Time.now.utc
errors.add :base, I18n.t("wizard.validation.after_time")
end

group_names = @data[:after_time_groups]
if group_names.present?
group_names.each do |group_name|
unless Group.exists?(name: group_name)
errors.add :base, I18n.t("wizard.validation.after_time_group", group_name: group_name)
end
end
end
end

def validate_liquid_template(object, type)
Expand Down
21 changes: 21 additions & 0 deletions lib/custom_wizard/wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CustomWizard::Wizard
:multiple_submissions,
:after_time,
:after_time_scheduled,
:after_time_group_names,
:after_signup,
:required,
:prompt_completion,
Expand Down Expand Up @@ -58,6 +59,7 @@ def initialize(attrs = {}, user = nil, guest_id = nil)
@after_signup = cast_bool(attrs["after_signup"])
@after_time = cast_bool(attrs["after_time"])
@after_time_scheduled = attrs["after_time_scheduled"]
@after_time_group_names = attrs["after_time_groups"]
@required = cast_bool(attrs["required"])
@permitted = attrs["permitted"] || nil
@theme_id = attrs["theme_id"]
Expand Down Expand Up @@ -251,6 +253,10 @@ def can_access?(always_allow_admin: true)
permitted?(always_allow_admin: always_allow_admin) && can_submit?
end

def should_redirect?
can_access?(always_allow_admin: false) && after_time_target?
end

def reset
return nil unless actor_id

Expand Down Expand Up @@ -329,6 +335,16 @@ def remove_user_redirect
end
end

def after_time_groups
@after_time_groups ||= Group.where(name: after_time_group_names)
end

def after_time_target?
return true if after_time_group_names.blank? || !after_time_groups.exists?
return true if after_time_groups.joins(:users).where(users: { username: user.username }).exists?
false
end

def self.create(wizard_id, user = nil, guest_id = nil)
if template = CustomWizard::Template.find(wizard_id)
new(template.to_h, user, guest_id)
Expand Down Expand Up @@ -372,6 +388,11 @@ def self.prompt_completion(user)
end
end

def self.set_after_time_redirect(wizard_id, user)
wizard = self.create(wizard_id, user)
set_user_redirect(wizard_id, user) if wizard.after_time_target?
end

def self.set_user_redirect(wizard_id, user)
wizard = self.create(wizard_id, user)

Expand Down
6 changes: 4 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# name: discourse-custom-wizard
# about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more.
# version: 2.8.13
# version: 2.9.0
# authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos
# url: https://github.com/paviliondev/discourse-custom-wizard
# contact_emails: [email protected]
Expand Down Expand Up @@ -46,6 +46,7 @@
require_relative "app/controllers/custom_wizard/admin/logs.rb"
require_relative "app/controllers/custom_wizard/admin/manager.rb"
require_relative "app/controllers/custom_wizard/admin/custom_fields.rb"
require_relative "app/controllers/custom_wizard/admin/user.rb"
require_relative "app/controllers/custom_wizard/wizard_client.rb"
require_relative "app/controllers/custom_wizard/wizard.rb"
require_relative "app/controllers/custom_wizard/steps.rb"
Expand Down Expand Up @@ -146,6 +147,7 @@
end

add_to_serializer(:current_user, :redirect_to_wizard) { object.redirect_to_wizard }
add_to_serializer(:admin_user_list, :redirect_to_wizard) { object.redirect_to_wizard }

on(:user_approved) do |user|
if wizard = CustomWizard::Wizard.after_signup(user)
Expand All @@ -168,7 +170,7 @@
end

wizard = CustomWizard::Wizard.create(wizard_id, current_user)
redirect_to "/w/#{wizard_id.dasherize}" if wizard.can_access?(always_allow_admin: false)
redirect_to "/w/#{wizard_id.dasherize}" if wizard.should_redirect?
end
end
end
Expand Down
Loading
Loading