From aa1c30f1d3ac4cec404b6aadbd6950909330ded9 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 30 Nov 2017 10:55:15 +0800 Subject: [PATCH] Add custom category and title for create_topic --- .../components/wizard-custom-action.js.es6 | 14 +++++- .../components/wizard-custom-action.hbs | 45 +++++++++++++++++-- .../components/wizard-custom-input.hbs | 9 +++- assets/stylesheets/wizard_custom_admin.scss | 9 ++++ config/locales/client.en.yml | 7 ++- lib/builder.rb | 29 ++++++++++-- 6 files changed, 103 insertions(+), 10 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index 1ecf9184..cff46d04 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -1,4 +1,4 @@ -import { default as computed } from 'ember-addons/ember-computed-decorators'; +import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; const ACTION_TYPES = [ { id: 'create_topic', name: 'Create Topic' }, @@ -63,5 +63,17 @@ export default Ember.Component.extend({ const noThemeKey = PROFILE_FIELDS.filter((f) => f !== 'theme_key'); const fields = noThemeKey.concat(['email', 'username']); return fields.map((f) => ` u{${f}}`); + }, + + @observes('action.custom_category_wizard_field') + toggleCustomCategoryUserField() { + const wizard = this.get('action.custom_category_wizard_field'); + if (wizard) this.set('action.custom_category_user_field', false); + }, + + @observes('action.custom_category_user_field') + toggleCustomCategoryWizardField() { + const user = this.get('action.custom_category_user_field'); + if (user) this.set('action.custom_category_wizard_field', false); } }); diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 5e23a218..dec404bc 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -22,7 +22,32 @@

{{i18n "admin.wizard.action.create_topic.category"}}

- {{category-chooser value=action.category_id}} + {{category-chooser value=action.category_id isDisabled=action.custom_category_enabled}} +
+ {{input type='checkbox' checked=action.custom_category_enabled}} + {{i18n 'admin.wizard.action.custom_category.label'}} + {{#if action.custom_category_enabled}} +
+
+ {{input type='checkbox' checked=action.custom_category_wizard_field}} + {{i18n 'admin.wizard.action.custom_category.wizard_field'}} + {{#if action.custom_category_wizard_field}} + {{combo-box value=action.category_id + content=availableFields + nameProperty="label" + none='admin.wizard.select_field'}} + {{/if}} +
+
+ {{input type='checkbox' checked=action.custom_category_user_field}} + {{i18n 'admin.wizard.action.custom_category.user_field'}} + {{#if action.custom_category_user_field}} + {{input value=action.custom_category_user_field_key}} + {{/if}} +
+
+ {{/if}} +
@@ -31,7 +56,18 @@

{{i18n "admin.wizard.action.title"}}

- {{combo-box value=action.title content=availableFields nameProperty="label" none='admin.wizard.select_field'}} + {{combo-box value=action.title + content=availableFields + nameProperty="label" + none='admin.wizard.select_field' + isDisabled=action.custom_title_enabled}} +
+ {{input type='checkbox' checked=action.custom_title_enabled}} + {{i18n 'admin.wizard.action.custom_title'}} + {{#if action.custom_title_enabled}} + {{input value=action.custom_title}} + {{/if}} +
@@ -44,7 +80,7 @@ nameProperty='label' none='admin.wizard.select_field' isDisabled=action.post_builder}} -
+
{{input type='checkbox' checked=action.post_builder}} {{i18n 'admin.wizard.action.post_builder.checkbox'}}
@@ -71,7 +107,8 @@ {{wizard-custom-input inputs=action.add_fields valueContent=availableFields inputKey='admin.wizard.action.topic_attr' - noneValue='admin.wizard.select_field'}} + noneValue='admin.wizard.select_field' + optionalCustom=true}}
{{/if}} diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs index add8f9eb..95d1c87c 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-input.hbs @@ -6,7 +6,14 @@ {{input type="text" value=in.key placeholder=(i18n inputKey)}} {{/if}} {{#if valueContent}} - {{combo-box value=in.value content=valueContent nameProperty="label" none=noneValue}} + {{combo-box value=in.value + content=valueContent + nameProperty="label" + none=noneValue + isDisabled=(if in.value_custom true false)}} + {{#if optionalCustom}} + {{input type="text" value=in.value_custom placeholder=(i18n inputValue)}} + {{/if}} {{else}} {{input type="text" value=in.value placeholder=(i18n inputValue)}} {{/if}} diff --git a/assets/stylesheets/wizard_custom_admin.scss b/assets/stylesheets/wizard_custom_admin.scss index 5b5b2695..506f73e5 100644 --- a/assets/stylesheets/wizard_custom_admin.scss +++ b/assets/stylesheets/wizard_custom_admin.scss @@ -87,6 +87,10 @@ label { margin: 5px 0; } + + .setting-gutter { + margin-top: 5px; + } } .select-box-kit-header { @@ -104,6 +108,11 @@ .buttons .remove { float: right; } + + .custom-category { + margin-top: 5px; + padding: 5px; + } } .wizard-links { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 86229ec5..65f97310 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -47,7 +47,7 @@ en: custom_text_placeholder: "Overrides translation" type: "Type" none: "Make a selection" - select_field: "Wizard Field" + select_field: "Select Field" error: name_required: "Wizards must have a name." steps_required: "Wizards must have at least one step." @@ -109,6 +109,11 @@ en: user_fields: "User Fields: " wizard_fields: "Wizard Fields: " placeholder: "Insert wizard fields using the field_id in w{}. Insert user fields using field key in u{}." + custom_title: "Custom Title" + custom_category: + label: "Custom Category" + wizard_field: "Wizard Field" + user_field: "User Field" wizard_js: location: diff --git a/lib/builder.rb b/lib/builder.rb index 5b3afcb2..e2a440b1 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -174,7 +174,12 @@ class CustomWizard::Builder if s['actions'] && s['actions'].length s['actions'].each do |a| if a['type'] === 'create_topic' && data - title = data[a['title']] + + if a['custom_title'] + title = a['custom_title'] + else + title = data[a['title']] + end if a['post_builder'] post = CustomWizard::Builder.build_post(a['post_template'], user, data) @@ -188,13 +193,31 @@ class CustomWizard::Builder raw: post, skip_validations: true } - params[:category] = a['category_id'] if a['category_id'] + + if a['custom_category_enabled'] && + !a['custom_category_wizard_field'] && + a['custom_category_user_field_key'] + if a['custom_category_user_field_key'].include?('custom_fields') + field = a['custom_category_user_field_key'].split('.').last + category_id = user.custom_fields[field] + else + category_id = user.send(a['custom_category_user_field_key']) + end + else + category_id = a['category_id'] + end + + params[:category] = category_id topic_custom_fields = {} if a['add_fields'] a['add_fields'].each do |f| - value = data[f['value']] + if f['value_custom'] + value = f['value_custom'] + else + value = data[f['value']] + end key = f['key'] if key && key.include?('custom_fields')