From d194a8313a00737dc47d389be6f60aa2f7f563a1 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sun, 19 Apr 2020 21:02:14 +1000 Subject: [PATCH] Add user field options as dropdown option --- .../components/wizard-custom-field.js.es6 | 3 +- .../components/wizard-mapper-selector.js.es6 | 56 ++++++++++++------- .../components/wizard-text-editor.js.es6 | 2 +- .../discourse/lib/wizard-mapper.js.es6 | 1 + .../routes/admin-wizards-wizard.js.es6 | 8 +-- .../components/wizard-text-editor.hbs | 4 +- config/locales/client.en.yml | 8 ++- lib/custom_wizard/builder.rb | 11 +++- lib/custom_wizard/mapper.rb | 12 +++- 9 files changed, 69 insertions(+), 36 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 7496336a..767258fd 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -62,7 +62,8 @@ export default Component.extend({ if (this.isDropdown) { options.wizardFieldSelection = 'key,value'; - options.inputTypes = 'association'; + options.userFieldOptionsSelection = 'output'; + options.inputTypes = 'association,assignment'; options.pairConnector = 'association'; options.keyPlaceholder = 'admin.wizard.key'; options.valuePlaceholder = 'admin.wizard.value'; diff --git a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 index 4b1daad1..34f6a819 100644 --- a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 +++ b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 @@ -2,33 +2,37 @@ import { alias, or, gt } from "@ember/object/computed"; import { computed } from "@ember/object"; import { default as discourseComputed, observes, on } from "discourse-common/utils/decorators"; import { getOwner } from 'discourse-common/lib/get-owner'; -import { defaultSelectionType, selectionTypes } from '../lib/wizard-mapper'; -import { snakeCase } from '../lib/wizard'; +import { defaultSelectionType, selectionTypes } from '../lib/wizard-mapper'; +import { snakeCase, generateName, userProperties } from '../lib/wizard'; import Component from "@ember/component"; import { bind, later } from "@ember/runloop"; export default Component.extend({ classNameBindings: [':mapper-selector', 'activeType'], - groups: alias('site.groups'), - categories: alias('site.categories'), + showText: computed('activeType', function() { return this.showInput('text') }), showWizardField: computed('activeType', function() { return this.showInput('wizardField') }), showUserField: computed('activeType', function() { return this.showInput('userField') }), + showUserFieldOptions: computed('activeType', function() { return this.showInput('userFieldOptions') }), showCategory: computed('activeType', function() { return this.showInput('category') }), showTag: computed('activeType', function() { return this.showInput('tag') }), showGroup: computed('activeType', function() { return this.showInput('group') }), showUser: computed('activeType', function() { return this.showInput('user') }), showList: computed('activeType', function() { return this.showInput('list') }), - showComboBox: or('showWizardField', 'showUserField'), - showMultiSelect: or('showCategory', 'showGroup'), textEnabled: computed('options.textSelection', 'inputType', function() { return this.optionEnabled('textSelection') }), wizardFieldEnabled: computed('options.wizardFieldSelection', 'inputType', function() { return this.optionEnabled('wizardFieldSelection') }), userFieldEnabled: computed('options.userFieldSelection', 'inputType', function() { return this.optionEnabled('userFieldSelection') }), + userFieldOptionsEnabled: computed('options.userFieldOptionsSelection', 'inputType', function() { return this.optionEnabled('userFieldOptionsSelection') }), categoryEnabled: computed('options.categorySelection', 'inputType', function() { return this.optionEnabled('categorySelection') }), tagEnabled: computed('options.tagSelection', 'inputType', function() { return this.optionEnabled('tagSelection') }), groupEnabled: computed('options.groupSelection', 'inputType', function() { return this.optionEnabled('groupSelection') }), userEnabled: computed('options.userSelection', 'inputType', function() { return this.optionEnabled('userSelection') }), listEnabled: computed('options.listSelection', 'inputType', function() { return this.optionEnabled('listSelection') }), + + groups: alias('site.groups'), + categories: alias('site.categories'), + showComboBox: or('showWizardField', 'showUserField', 'showUserFieldOptions'), + showMultiSelect: or('showCategory', 'showGroup'), hasTypes: gt('selectorTypes.length', 1), showTypes: false, @@ -81,21 +85,35 @@ export default Component.extend({ @discourseComputed('activeType') comboBoxContent(activeType) { const controller = getOwner(this).lookup('controller:admin-wizards-wizard-show'); - let content = controller[`${activeType}s`]; - - // you can't select the current field in the field context - if (activeType === 'wizardField' && this.options.context === 'field') { - content = content.filter(field => field.id !== controller.currentField.id); + const wizardFields = controller.wizardFields; + const userFields = controller.userFields; + let content; + + if (activeType === 'wizardField') { + content = wizardFields; + + if (this.options.context === 'field') { + content = content.filter(field => field.id !== controller.currentField.id); + } } - // updating certain user fields via the profile update action is not supported - if (activeType === 'userField' && - this.options.context === 'action' && - this.inputType === 'association' && - this.selectorType === 'key') { + if (activeType ===' userField') { + content = userProperties.map((f) => ({ + id: f, + name: generateName(f) + })).concat((userFields || [])); - const excludedFields = ['username','email', 'trust_level']; - content = content.filter(userField => excludedFields.indexOf(userField.id) === -1); + if (this.options.context === 'action' && + this.inputType === 'association' && + this.selectorType === 'key') { + + const excludedFields = ['username','email', 'trust_level']; + content = content.filter(userField => excludedFields.indexOf(userField.id) === -1); + } + } + + if (activeType === 'userFieldOptions') { + content = userFields; } return content; @@ -139,7 +157,7 @@ export default Component.extend({ const option = options[type]; if (option === true) return true; if (typeof option !== 'string') return false; - + return option.split(',').filter(option => { return [this.selectorType, this.inputType].indexOf(option) !== -1; }).length; diff --git a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 index 4cbb7efb..2b54b56c 100644 --- a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 +++ b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 @@ -36,7 +36,7 @@ export default Component.extend({ }, @discourseComputed() - userFieldList() { + userPropertyList() { return userProperties.map((f) => ` u{${f}}`); }, diff --git a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 index 514619a4..ceafa5f1 100644 --- a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 @@ -79,6 +79,7 @@ const selectionTypes = [ 'list', 'wizardField', 'userField', + 'userFieldOptions', 'group', 'category', 'tag', diff --git a/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 index 5f2ba5af..0ba204f3 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 @@ -1,5 +1,4 @@ import DiscourseRoute from "discourse/routes/discourse"; -import { userProperties, generateName } from '../lib/wizard'; import { buildFieldTypes } from '../lib/wizard-schema'; import { set } from "@ember/object"; import { all } from "rsvp"; @@ -44,12 +43,7 @@ export default DiscourseRoute.extend({ result.content.map((f) => ({ id: `user_field_${f.id}`, name: f.name - })).concat( - userProperties.map((f) => ({ - id: f, - name: generateName(f) - })) - ) + })) ); } }); diff --git a/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs b/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs index 5638ac70..f9f6cc9b 100644 --- a/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs @@ -18,8 +18,8 @@ {{#if showPopover}}
{{#if hasWizardFields}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 29cc9e77..dd5f9930 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -83,6 +83,7 @@ en: text: "text" wizard_field: "wizard field" user_field: "user field" + user_field_options: "user field options" user: "user" category: "category" tag: "tag" @@ -94,6 +95,7 @@ en: property: "Select property" wizard_field: "Select field" user_field: "Select field" + user_field_options: "Select field" user: "Select user" category: "Select category" tag: "Select tag" @@ -182,9 +184,9 @@ en: post_builder: checkbox: "Post Builder" label: "Builder" - 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{}." + user_properties: "User Properties" + wizard_fields: "Wizard Fields" + placeholder: "Insert wizard fields using the field_id in w{}. Insert user properties using property in u{}." add_to_group: label: "Add to Group" route_to: diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index 63fdf75a..aaa00470 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -256,7 +256,7 @@ class CustomWizard::Builder with_type: true } ).perform - + if content.present? if content[:type] == 'association' content[:result] = content[:result].map do |item| @@ -267,6 +267,15 @@ class CustomWizard::Builder end end + if content[:type] == 'assignment' && field_template['type'] === 'dropdown' + content[:result] = content[:result].map do |item| + { + id: item, + name: item + } + end + end + params[:content] = content[:result] end end diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 3c16ef70..31232c31 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -37,7 +37,7 @@ class CustomWizard::Mapper output_type = input['output_type'] result = build_result(map_field(output, output_type), input_type) - + if multiple perform_result.push(result) else @@ -169,7 +169,7 @@ class CustomWizard::Mapper end def map_user_field(value) - if value.include?('user_field_') + if value.include?(User::USER_FIELD_PREFIX) UserCustomField.where(user_id: user.id, name: value).pluck(:value).first elsif PROFILE_FIELDS.include?(value) UserProfile.find_by(user_id: user.id).send(value) @@ -178,6 +178,14 @@ class CustomWizard::Mapper end end + def map_user_field_options(value) + if value.include?(User::USER_FIELD_PREFIX) + if field = UserField.find(value.split('_').last) + field.user_field_options.map(&:value) + end + end + end + def interpolate(string, opts={ user: true, wizard: true, value: true }) return string if string.blank?