Jelajahi Sumber

Add post builder

Angus McLeod 7 tahun lalu
induk
melakukan
f5ffff35d8

+ 16 - 7
assets/javascripts/discourse/components/wizard-custom-action.js.es6

@@ -8,18 +8,15 @@ const ACTION_TYPES = [
 
 const PROFILE_FIELDS = [
   'name',
-  'email',
-  'username',
-  'title',
   'date_of_birth',
-  'muted_usernames',
-  'theme_key',
+  'title',
   'locale',
-  'bio_raw',
   'location',
   'website',
+  'bio_raw',
   'profile_background',
-  'card_background'
+  'card_background',
+  'theme_key'
 ];
 
 export default Ember.Component.extend({
@@ -54,5 +51,17 @@ export default Ember.Component.extend({
     });
 
     return fields;
+  },
+
+  @computed('availableFields')
+  builderWizardFields(fields) {
+    return fields.map((f) => ` w{${f.id}}`);
+  },
+
+  @computed()
+  builderUserFields() {
+    const noThemeKey = PROFILE_FIELDS.filter((f) => f !== 'theme_key');
+    const fields = noThemeKey.concat(['email', 'username']);
+    return fields.map((f) => ` u{${f}}`);
   }
 });

+ 47 - 3
assets/javascripts/discourse/templates/components/wizard-custom-action.hbs

@@ -40,12 +40,34 @@
       <h3>{{i18n "admin.wizard.action.post"}}</h3>
     </div>
     <div class="setting-value">
-      {{combo-box value=action.post content=availableFields nameProperty="label" none='admin.wizard.select_field'}}
+      {{combo-box value=action.post content=availableFields
+                                    nameProperty='label'
+                                    none='admin.wizard.select_field'
+                                    isDisabled=action.post_builder}}
+      <div>
+        {{input type='checkbox' checked=action.post_builder}}
+        <span>{{i18n 'admin.wizard.action.post_builder.checkbox'}}</span>
+      </div>
     </div>
   </div>
 
+  {{#if action.post_builder}}
+    <div class="setting full">
+      <div class="setting-label">
+        <h3>{{i18n 'admin.wizard.action.post_builder.label'}}</h3>
+      </div>
+      <div class="setting-value">
+        <label>{{i18n 'admin.wizard.action.post_builder.user_fields'}}{{builderUserFields}}</label>
+        <label>{{i18n 'admin.wizard.action.post_builder.wizard_fields'}}{{builderWizardFields}}</label>
+        {{d-editor value=action.post
+                   placeholder='admin.wizard.action.post_builder.placeholder'
+                   classNames='post-builder-editor'}}
+      </div>
+    </div>
+  {{/if}}
+
   <div class="setting full">
-    <label>{{i18n "admin.wizard.action.add_fields" type='Topic'}}</label>
+    <label>{{i18n 'admin.wizard.action.add_fields' type='Topic'}}</label>
     {{wizard-custom-input inputs=action.add_fields
                           valueContent=availableFields
                           inputKey='admin.wizard.action.topic_attr'
@@ -68,10 +90,32 @@
       <h3>{{i18n "admin.wizard.action.post"}}</h3>
     </div>
     <div class="setting-value">
-      {{combo-box value=action.post content=availableFields nameProperty='label' none='admin.wizard.select_field'}}
+      {{combo-box value=action.post content=availableFields
+                                    nameProperty='label'
+                                    none='admin.wizard.select_field'
+                                    isDisabled=action.post_builder}}
+      <div>
+        {{input type='checkbox' checked=action.post_builder}}
+        <span>{{i18n 'admin.wizard.action.post_builder.checkbox'}}</span>
+      </div>
     </div>
   </div>
 
+  {{#if action.post_builder}}
+    <div class="setting full">
+      <div class="setting-label">
+        <h3>{{i18n 'admin.wizard.action.post_builder.label'}}</h3>
+      </div>
+      <div class="setting-value">
+        <label>{{i18n 'admin.wizard.action.post_builder.user_fields'}}{{builderUserFields}}</label>
+        <label>{{i18n 'admin.wizard.action.post_builder.wizard_fields'}}{{builderWizardFields}}</label>
+        {{d-editor value=action.post
+                   placeholder='admin.wizard.action.post_builder.placeholder'
+                   classNames='post-builder-editor'}}
+      </div>
+    </div>
+  {{/if}}
+
   <div class="setting">
     <div class="setting-label">
       <h3>{{i18n "admin.wizard.action.send_message.recipient"}}</h3>

+ 4 - 0
assets/stylesheets/wizard_custom_admin.scss

@@ -236,3 +236,7 @@
     }
   }
 }
+
+.post-builder-editor {
+  min-height: 220px;
+}

+ 7 - 1
config/locales/client.en.yml

@@ -89,7 +89,7 @@ en:
           include: "Include Fields"
           title: "Title"
           post: "Post"
-          add_fields: "Add Fields To {{type}}"
+          add_fields: "{{type}} Fields"
           available_fields: "* If 'Save wizard submissions' is disabled, only the fields of the current step are available to the current step's actions."
           topic_attr: "Topic Attribute"
           send_message:
@@ -101,6 +101,12 @@ en:
           update_profile:
             label: "Update Profile"
             profile_field: "Profile Field"
+          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{}."
 
   wizard_js:
     location:

+ 35 - 2
lib/builder.rb

@@ -34,6 +34,29 @@ class CustomWizard::Builder
     @sorted_handlers.sort_by! { |h| -h[:priority] }
   end
 
+  USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale']
+  PROFILE_FIELDS = ['location', 'website', 'bio_raw', 'profile_background', 'card_background']
+
+  def self.build_post(template, user, data)
+    post = template.gsub(/u\{(.*?)\}/) do |match|
+      result = ''
+
+      if USER_FIELDS.include?($1)
+        result = user.send($1)
+        if result.blank? && $1 === 'name'
+          result = user.send('username')
+        end
+      end
+
+      if PROFILE_FIELDS.include?($1)
+        result = user.user_profile.send($1)
+      end
+
+      result
+    end
+    post.gsub!(/w\{(.*?)\}/) { |match| data[$1.to_sym] }
+  end
+
   def build
     unless (@wizard.completed? && !@wizard.multiple_submissions) || !@steps
       @steps.each do |s|
@@ -161,7 +184,12 @@ class CustomWizard::Builder
               s['actions'].each do |a|
                 if a['type'] === 'create_topic' && data
                   title = data[a['title']]
-                  post = data[a['post']]
+
+                  if a['post_builder']
+                    post = CustomWizard::Builder.build_post(a['post'], user, data)
+                  else
+                    post = data[a['post']]
+                  end
 
                   if title
                     params = {
@@ -218,7 +246,12 @@ class CustomWizard::Builder
 
                 if a['type'] === 'send_message' && data
                   title = data[a['title']]
-                  post = data[a['post']]
+
+                  if a['post_builder']
+                    post = CustomWizard::Builder.build_post(a['post'], user, data)
+                  else
+                    post = data[a['post']]
+                  end
 
                   if title && post
                     creator = PostCreator.new(user,