Skip to content

Beautiful Open-Source Form Builder

Slack-Discord extra feature (#176)

formsdev662088e20f78
diff --git a/app/Http/Requests/UserFormRequest.php b/app/Http/Requests/UserFormRequest.php
index a0d777c01..79d863319 100644
--- a/app/Http/Requests/UserFormRequest.php
+++ b/app/Http/Requests/UserFormRequest.php
@@ -43,6 +43,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'use_captcha' => 'boolean',
'slack_webhook_url' => 'url|nullable',
'discord_webhook_url' => 'url|nullable',
+ 'notification_settings' => 'nullable',
// Customization
'theme' => ['required',Rule::in(Form::THEMES)],
diff --git a/app/Http/Resources/FormResource.php b/app/Http/Resources/FormResource.php
index 49857ad75..9cdc44c0b 100644
--- a/app/Http/Resources/FormResource.php
+++ b/app/Http/Resources/FormResource.php
@@ -47,6 +47,7 @@ class FormResource extends JsonResource
'notification_emails' => $this->notification_emails,
'slack_webhook_url' => $this->slack_webhook_url,
'discord_webhook_url' => $this->discord_webhook_url,
+ 'notification_settings' => $this->notification_settings,
'removed_properties' => $this->removed_properties,
'last_edited_human' => $this->updated_at?->diffForHumans(),
'seo_meta' => $this->seo_meta
diff --git a/app/Models/Forms/Form.php b/app/Models/Forms/Form.php
index c1458283c..fe2f16396 100644
--- a/app/Models/Forms/Form.php
+++ b/app/Models/Forms/Form.php
@@ -41,6 +41,7 @@ class Form extends Model
'notifications_include_submission',
'slack_webhook_url',
'discord_webhook_url',
+ 'notification_settings',
// integrations
'webhook_url',
@@ -83,10 +84,7 @@ class Form extends Model
// Security & Privacy
'can_be_indexed',
- 'password',
-
- // Custom SEO
- 'seo_meta'
+ 'password'
];
protected $casts = [
@@ -95,7 +93,8 @@ class Form extends Model
'closes_at' => 'datetime',
'tags' => 'array',
'removed_properties' => 'array',
- 'seo_meta' => 'object'
+ 'seo_meta' => 'object',
+ 'notification_settings' => 'object'
];
protected $appends = [
diff --git a/app/Service/Forms/Webhooks/DiscordHandler.php b/app/Service/Forms/Webhooks/DiscordHandler.php
index 446f21794..55ebf2d8d 100644
--- a/app/Service/Forms/Webhooks/DiscordHandler.php
+++ b/app/Service/Forms/Webhooks/DiscordHandler.php
@@ -3,7 +3,8 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
-use Illuminate\Support\Str;
+use Vinkla\Hashids\Facades\Hashids;
+use Illuminate\Support\Arr;
class DiscordHandler extends AbstractWebhookHandler
{
@@ -20,58 +21,60 @@ class DiscordHandler extends AbstractWebhookHandler
protected function getWebhookData(): array
{
- $submissionString = "";
- $formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
+ $settings = (array) Arr::get((array)$this->form->notification_settings, 'discord', []);
+ $externalLinks = [];
+ if(Arr::get($settings, 'link_open_form', true)){
+ $externalLinks[] = '[**πŸ”— Open Form**](' . $this->form->share_url . ')';
+ }
+ if(Arr::get($settings, 'link_edit_form', true)){
+ $editFormURL = url('forms/' . $this->form->slug . '/show');
+ $externalLinks[] = '[**✍️ Edit Form**](' . $editFormURL . ')';
+ }
+ if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
+ $submissionId = Hashids::encode($this->data['submission_id']);
+ $externalLinks[] = '[**✍️ ' . $this->form->editable_submissions_button_text . '**](' . $this->form->share_url . '?submission_id=' . $submissionId . ')';
+ }
- foreach ($formatter->getFieldsWithValue() as $field) {
- $tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
- $submissionString .= "**" . ucfirst($field['name']) . "**: `" . $tmpVal . "`\n";
+ $color = hexdec(str_replace('#', '', $this->form->color));
+ $blocks = [];
+ if(Arr::get($settings, 'include_submission_data', true)){
+ $submissionString = "";
+ $formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
+ foreach ($formatter->getFieldsWithValue() as $field) {
+ $tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
+ $submissionString .= "**" . ucfirst($field['name']) . "**: " . $tmpVal . "\n";
+ }
+ $blocks[] = [
+ "type" => "rich",
+ "color" => $color,
+ "description" => $submissionString
+ ];
}
- $form_name = $this->form->title;
- $formURL = url("forms/" . $this->form->slug . "/show/submissions");
+ if(Arr::get($settings, 'views_submissions_count', true)){
+ $countString = '**πŸ‘€ Views**: ' . (string)$this->form->views_count . " \n";
+ $countString .= '**πŸ–ŠοΈ Submissions**: ' . (string)$this->form->submissions_count;
+ $blocks[] = [
+ "type" => "rich",
+ "color" => $color,
+ "description" => $countString
+ ];
+ }
+ if(count($externalLinks) > 0){
+ $blocks[] = [
+ "type" => "rich",
+ "color" => $color,
+ "description" => implode(' - ', $externalLinks)
+ ];
+ }
+
return [
- "content" => "@here We have received a new submission for **$form_name**",
- "username" => config('app.name'),
- "avatar_url" => asset('img/logo.png'),
- "tts" => false,
- "embeds" => [
- [
- "title" => "πŸ”— Go to $form_name",
-
- "type" => "rich",
-
- "description" => $submissionString,
-
- "url" => $formURL,
-
- "color" => hexdec(str_replace('#', '', $this->form->color)),
-
- "footer" => [
- "text" => config('app.name'),
- "icon_url" => asset('img/logo.png'),
- ],
-
- "author" => [
- "name" => config('app.name'),
- "url" => config('app.url'),
- ],
-
- "fields" => [
- [
- "name" => "Views πŸ‘€",
- "value" => (string)$this->form->views_count,
- "inline" => true
- ],
- [
- "name" => "Submissions πŸ–ŠοΈ",
- "value" => (string)$this->form->submissions_count,
- "inline" => true
- ]
- ]
- ]
- ]
+ 'content' => 'New submission for your form **' . $this->form->title . '**',
+ 'tts' => false,
+ 'username' => config('app.name'),
+ 'avatar_url' => asset('img/logo.png'),
+ 'embeds' => $blocks
];
}
diff --git a/app/Service/Forms/Webhooks/SlackHandler.php b/app/Service/Forms/Webhooks/SlackHandler.php
index 2ed292db2..5b2faf607 100644
--- a/app/Service/Forms/Webhooks/SlackHandler.php
+++ b/app/Service/Forms/Webhooks/SlackHandler.php
@@ -3,8 +3,8 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
-use Illuminate\Support\Str;
use Vinkla\Hashids\Facades\Hashids;
+use Illuminate\Support\Arr;
class SlackHandler extends AbstractWebhookHandler
{
@@ -21,48 +21,70 @@ class SlackHandler extends AbstractWebhookHandler
protected function getWebhookData(): array
{
- $submissionString = '';
- $formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
- foreach ($formatter->getFieldsWithValue() as $field) {
- $tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
- $submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
+ $settings = (array) Arr::get((array)$this->form->notification_settings, 'slack', []);
+ $externalLinks = [];
+ if(Arr::get($settings, 'link_open_form', true)){
+ $externalLinks[] = '*<' . $this->form->share_url . '|πŸ”— Open Form>*';
+ }
+ if(Arr::get($settings, 'link_edit_form', true)){
+ $editFormURL = url('forms/' . $this->form->slug . '/show');
+ $externalLinks[] = '*<' . $editFormURL . '|✍️ Edit Form>*';
+ }
+ if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
+ $submissionId = Hashids::encode($this->data['submission_id']);
+ $externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
}
- $formURL = url('forms/' . $this->form->slug);
- $editFormURL = url('forms/' . $this->form->slug . '/show');
- $submissionId = Hashids::encode($this->data['submission_id']);
- $externalLinks = [
- '*<' . $formURL . '|πŸ”— Open Form>*',
- '*<' . $editFormURL . '|✍️ Edit Form>*'
+ $blocks = [
+ [
+ 'type' => 'section',
+ 'text' => [
+ 'type' => 'mrkdwn',
+ 'text' => 'New submission for your form *' . $this->form->title . '*',
+ ]
+ ]
];
- if ($this->form->editable_submissions) {
- $externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
+
+ if(Arr::get($settings, 'include_submission_data', true)){
+ $submissionString = '';
+ $formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
+ foreach ($formatter->getFieldsWithValue() as $field) {
+ $tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
+ $submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
+ }
+ $blocks[] = [
+ 'type' => 'section',
+ 'text' => [
+ 'type' => 'mrkdwn',
+ 'text' => $submissionString,
+ ]
+ ];
+ }
+
+ if(Arr::get($settings, 'views_submissions_count', true)){
+ $countString = '*πŸ‘€ Views*: ' . (string)$this->form->views_count . " \n";
+ $countString .= '*πŸ–ŠοΈ Submissions*: ' . (string)$this->form->submissions_count;
+ $blocks[] = [
+ 'type' => 'section',
+ 'text' => [
+ 'type' => 'mrkdwn',
+ 'text' => $countString,
+ ]
+ ];
+ }
+
+ if(count($externalLinks) > 0){
+ $blocks[] = [
+ 'type' => 'section',
+ 'text' => [
+ 'type' => 'mrkdwn',
+ 'text' => implode(' ', $externalLinks),
+ ]
+ ];
}
return [
- 'blocks' => [
- [
- 'type' => 'section',
- 'text' => [
- 'type' => 'mrkdwn',
- 'text' => 'New submission for your form *<' . $formURL . '|' . $this->form->title . ':>*',
- ],
- ],
- [
- 'type' => 'section',
- 'text' => [
- 'type' => 'mrkdwn',
- 'text' => $submissionString,
- ],
- ],
- [
- 'type' => 'section',
- 'text' => [
- 'type' => 'mrkdwn',
- 'text' => implode(' ', $externalLinks),
- ],
- ],
- ],
+ 'blocks' => $blocks
];
}
diff --git a/database/factories/FormFactory.php b/database/factories/FormFactory.php
index c7a831e7f..dab01d3e7 100644
--- a/database/factories/FormFactory.php
+++ b/database/factories/FormFactory.php
@@ -84,6 +84,8 @@ class FormFactory extends Factory
'password' => false,
'tags' => [],
'slack_webhook_url' => null,
+ 'discord_webhook_url' => null,
+ 'notification_settings' => [],
'editable_submissions_button_text' => 'Edit submission',
'confetti_on_submission' => false,
'seo_meta' => [],
diff --git a/database/migrations/2023_08_23_100710_add_notification_settings_to_forms.php b/database/migrations/2023_08_23_100710_add_notification_settings_to_forms.php
new file mode 100644
index 000000000..b091f6b7f
--- /dev/null
+++ b/database/migrations/2023_08_23_100710_add_notification_settings_to_forms.php
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::table('forms', function (Blueprint $table) {
+ $table->json('notification_settings')->default('{}')->nullable(true);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('forms', function (Blueprint $table) {
+ $table->dropColumn('notification_settings');
+ });
+ }
+};
diff --git a/resources/js/components/open/forms/components/form-components/components/FormNotificationsDiscord.vue b/resources/js/components/open/forms/components/form-components/components/FormNotificationsDiscord.vue
index f8dc0de62..3ebca14b7 100644
--- a/resources/js/components/open/forms/components/form-components/components/FormNotificationsDiscord.vue
+++ b/resources/js/components/open/forms/components/form-components/components/FormNotificationsDiscord.vue
@@ -28,23 +28,29 @@
<toggle-switch-input name="notifies_discord" :form="form" class="mt-4"
label="Receive a Discord notification on submission"
/>
- <text-input v-if="form.notifies_discord" name="discord_webhook_url" :form="form" class="mt-4"
- label="Discord webhook url" help="help"
- >
- <template #help>
- Receive a discord message on each form submission.
- <a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks" target="_blank">Click here</a> to learn how to get a discord webhook url.
- </template>
- </text-input>
+ <template v-if="form.notifies_discord">
+ <text-input name="discord_webhook_url" :form="form" class="mt-4"
+ label="Discord webhook url" help="help"
+ >
+ <template #help>
+ Receive a discord message on each form submission.
+ <a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks" target="_blank">Click
+ here</a> to learn how to get a discord webhook url.
+ </template>
+ </text-input>
+ <h4 class="font-bold mt-4">Discord message actions</h4>
+ <form-notifications-message-actions v-model="form.notification_settings.discord" />
+ </template>
</modal>
</div>
</template>
<script>
import ProTag from '../../../../../common/ProTag.vue'
+import FormNotificationsMessageActions from './FormNotificationsMessageActions.vue'
export default {
- components: { ProTag },
+ components: { ProTag, FormNotificationsMessageActions },
props: {},
data () {
return {
diff --git a/resources/js/components/open/forms/components/form-components/components/FormNotificationsMessageActions.vue b/resources/js/components/open/forms/components/form-components/components/FormNotificationsMessageActions.vue
new file mode 100644
index 000000000..135e54d0b
--- /dev/null
+++ b/resources/js/components/open/forms/components/form-components/components/FormNotificationsMessageActions.vue
@@ -0,0 +1,64 @@
+<template>
+ <div>
+ <toggle-switch-input name="include_submission_data" v-model="compVal.include_submission_data" class="mt-4"
+ label="Include submission data"
+ help="With form submission answers"
+ />
+ <toggle-switch-input name="link_open_form" v-model="compVal.link_open_form" class="mt-4"
+ label="Open Form"
+ help="Link to the form public page"
+ />
+ <toggle-switch-input name="link_edit_form" v-model="compVal.link_edit_form" class="mt-4"
+ label="Edit Form"
+ help="Link to the form admin page"
+ />
+ <toggle-switch-input name="views_submissions_count" v-model="compVal.views_submissions_count" class="mt-4"
+ label="Analytics (views & submissions)"
+ />
+ <toggle-switch-input name="link_edit_submission" v-model="compVal.link_edit_submission" class="mt-4"
+ label="Link to the Edit Submission Record"
+ />
+ </div>
+</template>
+
+<script>
+export default {
+ name: 'FormNotificationsMessageActions',
+ components: { },
+ props: {
+ value: { required: false }
+ },
+ data () {
+ return {
+ content: this.value
+ }
+ },
+
+ computed: {
+ compVal: {
+ set (val) {
+ this.content = val
+ this.$emit('input', this.compVal)
+ },
+ get () {
+ return this.content
+ }
+ }
+ },
+
+ watch: {},
+
+ mounted () {
+ if(this.compVal === undefined || this.compVal === null){
+ this.compVal = {}
+ }
+ ['include_submission_data', 'link_open_form', 'link_edit_form', 'views_submissions_count', 'link_edit_submission'].forEach((keyname) => {
+ if (this.compVal[keyname] === undefined) {
+ this.compVal[keyname] = true
+ }
+ })
+ },
+
+ methods: { }
+}
+</script>
diff --git a/resources/js/components/open/forms/components/form-components/components/FormNotificationsSlack.vue b/resources/js/components/open/forms/components/form-components/components/FormNotificationsSlack.vue
index 2e7cb7edc..0de50b8be 100644
--- a/resources/js/components/open/forms/components/form-components/components/FormNotificationsSlack.vue
+++ b/resources/js/components/open/forms/components/form-components/components/FormNotificationsSlack.vue
@@ -28,22 +28,30 @@
<toggle-switch-input name="notifies_slack" :form="form" class="mt-4"
label="Receive a Slack notification on submission"
/>
- <text-input v-if="form.notifies_slack" name="slack_webhook_url" :form="form" class="mt-4"
- label="Slack webhook url" help="help"
- >
- <template #help>
- Receive slack message on each form submission. <a href="https://api.slack.com/messaging/webhooks" target="_blank">Click here</a> to learn how to get a slack webhook url
- </template>
- </text-input>
+ <template v-if="form.notifies_slack">
+ <text-input name="slack_webhook_url" :form="form" class="mt-4"
+ label="Slack webhook url" help="help"
+ >
+ <template #help>
+ Receive slack message on each form submission. <a href="https://api.slack.com/messaging/webhooks"
+ target="_blank"
+ >Click here</a> to learn how to get a slack
+ webhook url
+ </template>
+ </text-input>
+ <h4 class="font-bold mt-4">Slack message actions</h4>
+ <form-notifications-message-actions v-model="form.notification_settings.slack" />
+ </template>
</modal>
</div>
</template>
<script>
import ProTag from '../../../../../common/ProTag.vue'
+import FormNotificationsMessageActions from './FormNotificationsMessageActions.vue'
export default {
- components: { ProTag },
+ components: { ProTag, FormNotificationsMessageActions },
props: {},
data () {
return {
diff --git a/resources/js/mixins/form_editor/initForm.js b/resources/js/mixins/form_editor/initForm.js
index cc38ba84d..74d61932a 100644
--- a/resources/js/mixins/form_editor/initForm.js
+++ b/resources/js/mixins/form_editor/initForm.js
@@ -14,6 +14,7 @@ export default {
slack_notifies: false,
send_submission_confirmation: false,
webhook_url: null,
+ notification_settings: {},
// Customization
theme: 'default',
diff --git a/tests/Feature/Forms/FormStatTest.php b/tests/Feature/Forms/FormStatTest.php
index 19222cc02..af83211fa 100644
--- a/tests/Feature/Forms/FormStatTest.php
+++ b/tests/Feature/Forms/FormStatTest.php
@@ -38,8 +38,6 @@ it('check formstat chart data', function () {
}
// Now check chart data
- $response = $this->getJson(route('open.workspaces.form.stats', [$workspace->id, $form->id]));
-
$this->getJson(route('open.workspaces.form.stats', [$workspace->id, $form->id]))
->assertSuccessful()
->assertJson(function (\Illuminate\Testing\Fluent\AssertableJson $json) use ($views, $submissions) {