opnform-host-nginx/api/app/Models/Forms/Form.php

327 lines
8.0 KiB
PHP
Raw Normal View History

2022-09-20 21:59:52 +02:00
<?php
namespace App\Models\Forms;
use App\Events\Models\FormCreated;
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
use App\Models\Integration\FormIntegration;
2022-09-20 21:59:52 +02:00
use App\Models\Integration\FormZapierWebhook;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
2022-09-20 21:59:52 +02:00
use App\Models\User;
use App\Models\Workspace;
use Database\Factories\FormFactory;
2024-02-23 11:54:12 +01:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2022-09-20 21:59:52 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
2024-02-23 11:54:12 +01:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2022-09-20 21:59:52 +02:00
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify;
use Carbon\Carbon;
2022-09-20 21:59:52 +02:00
class Form extends Model implements CachableAttributes
2022-09-20 21:59:52 +02:00
{
use CachesAttributes;
2024-02-23 11:54:12 +01:00
use HasFactory;
use HasSlug;
use SoftDeletes;
public const DARK_MODE_VALUES = ['auto', 'light', 'dark'];
public const SIZES = ['sm', 'md', 'lg'];
public const BORDER_RADIUS = ['none', 'small', 'full'];
2024-02-23 11:54:12 +01:00
public const THEMES = ['default', 'simple', 'notion'];
2022-09-20 21:59:52 +02:00
2024-02-23 11:54:12 +01:00
public const WIDTHS = ['centered', 'full'];
public const VISIBILITY = ['public', 'draft', 'closed'];
2022-09-20 21:59:52 +02:00
public const LANGUAGES = ['en', 'fr', 'hi', 'es', 'ar', 'zh', 'ja', 'bn', 'pt', 'ru', 'ur', 'pa', 'de', 'jv', 'ko', 'vi', 'te', 'mr', 'ta', 'tr'];
2022-09-20 21:59:52 +02:00
protected $fillable = [
'workspace_id',
'creator_id',
'properties',
'removed_properties',
'title',
'tags',
'visibility',
2022-09-20 21:59:52 +02:00
// Customization
'language',
'font_family',
'custom_domain',
'size',
'border_radius',
2022-09-20 21:59:52 +02:00
'theme',
'width',
'layout_rtl',
2022-09-20 21:59:52 +02:00
'cover_picture',
'logo_picture',
'dark_mode',
'color',
'uppercase_labels',
'no_branding',
'hide_title',
'transparent_background',
// Custom Code
'custom_code',
// Submission
'submit_button_text',
'database_fields_update',
're_fillable',
're_fill_button_text',
'submitted_text',
'redirect_url',
'use_captcha',
'closes_at',
'closed_text',
'max_submissions_count',
'max_submissions_reached_text',
'editable_submissions',
'editable_submissions_button_text',
2023-04-12 13:17:05 +02:00
'confetti_on_submission',
'show_progress_bar',
'auto_save',
'auto_focus',
2022-09-20 21:59:52 +02:00
// Security & Privacy
'can_be_indexed',
'password',
// Custom SEO
2024-02-23 11:54:12 +01:00
'seo_meta',
2022-09-20 21:59:52 +02:00
];
protected function casts(): array
{
return [
'properties' => 'array',
'database_fields_update' => 'array',
'closes_at' => 'datetime',
'tags' => 'array',
'removed_properties' => 'array',
'seo_meta' => 'object'
];
}
2022-09-20 21:59:52 +02:00
protected $appends = [
'share_url',
];
protected $hidden = [
'workspace_id',
'redirect_url',
'database_fields_update',
'password',
'tags',
2024-02-23 11:54:12 +01:00
'removed_properties',
2022-09-20 21:59:52 +02:00
];
protected $cachableAttributes = [
'is_pro',
'views_count',
2024-02-23 11:54:12 +01:00
'max_file_size',
];
2022-09-20 21:59:52 +02:00
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => FormCreated::class,
];
public function getIsProAttribute()
{
2023-12-04 10:43:24 +01:00
return $this->remember('is_pro', 15 * 60, function (): ?bool {
return $this->workspace?->is_pro === true;
});
2022-09-20 21:59:52 +02:00
}
public function getShareUrlAttribute()
{
if ($this->custom_domain) {
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
return 'https://' . $this->custom_domain . '/forms/' . $this->slug;
}
2024-02-23 11:54:12 +01:00
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
return front_url('/forms/' . $this->slug);
2022-09-20 21:59:52 +02:00
}
public function getSubmissionsUrlAttribute()
{
if ($this->custom_domain) {
return 'https://' . $this->custom_domain . '/forms/' . $this->slug . '/show/submissions';
}
return front_url('/forms/' . $this->slug . '/show/submissions');
}
public function getEditUrlAttribute()
{
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
return front_url('/forms/' . $this->slug . '/show');
}
2022-09-20 21:59:52 +02:00
public function getSubmissionsCountAttribute()
{
return $this->submissions()->count();
2022-09-20 21:59:52 +02:00
}
public function getViewsCountAttribute()
{
return $this->remember('views_count', 15 * 60, function (): int {
if (env('DB_CONNECTION') == 'mysql') {
2024-02-23 11:54:12 +01:00
return (int) ($this->views()->count() +
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")));
}
2024-02-23 11:54:12 +01:00
return $this->views()->count() +
$this->statistics()->sum(DB::raw("cast(data->>'views' as integer)"));
});
2022-09-20 21:59:52 +02:00
}
public function setSubmittedTextAttribute($value)
{
// Strip out unwanted html
$this->attributes['submitted_text'] = Purify::clean($value);
}
2022-10-27 23:31:05 +02:00
public function setTagsAttribute($value)
{
2022-11-01 14:43:14 +01:00
if ($value == '') {
$value = null;
2022-10-27 23:31:05 +02:00
}
$this->attributes['tags'] = json_encode($value);
}
public function setClosesAtAttribute($value)
{
$this->attributes['closes_at'] = ($value) ? Carbon::parse($value)->setTimezone('UTC') : null;
}
public function getClosesAtAttribute($value)
{
if (!$value) {
return $value;
}
// Retrieve the desired timezone from the request or default to 'UTC'
$timezone = request()->get('timezone', 'UTC');
return Carbon::parse($value)->setTimezone($timezone)->toIso8601String();
}
2022-09-20 21:59:52 +02:00
public function getIsClosedAttribute()
{
2024-02-23 11:54:12 +01:00
return $this->closes_at && now()->gt($this->closes_at);
2022-09-20 21:59:52 +02:00
}
public function getFormPendingSubmissionKeyAttribute()
{
if ($this->updated_at?->timestamp) {
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
return 'openform-' . $this->id . '-pending-submission-' . substr($this->updated_at?->timestamp, -6);
}
2024-02-23 11:54:12 +01:00
return null;
}
2022-09-20 21:59:52 +02:00
public function getMaxNumberOfSubmissionsReachedAttribute()
{
2024-02-23 11:54:12 +01:00
return $this->max_submissions_count && $this->max_submissions_count <= $this->submissions_count;
2022-09-20 21:59:52 +02:00
}
public function setClosedTextAttribute($value)
{
$this->attributes['closed_text'] = Purify::clean($value);
}
public function setMaxSubmissionsReachedTextAttribute($value)
{
$this->attributes['max_submissions_reached_text'] = Purify::clean($value);
}
public function getHasPasswordAttribute()
{
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
return !empty($this->password);
2022-09-20 21:59:52 +02:00
}
2023-12-15 12:43:05 +01:00
public function getMaxFileSizeAttribute()
{
2024-02-23 11:54:12 +01:00
return $this->remember('max_file_size', 15 * 60, function (): int {
2023-12-15 12:43:05 +01:00
return $this->workspace->max_file_size;
});
}
protected function removedProperties(): Attribute
{
2023-10-13 11:43:53 +02:00
return Attribute::make(
get: function ($value) {
return $value ? json_decode($value, true) : [];
}
);
2023-10-13 10:05:10 +02:00
}
2022-09-20 21:59:52 +02:00
/**
* Relationships
*/
public function workspace()
{
return $this->belongsTo(Workspace::class);
}
public function creator()
{
return $this->belongsTo(User::class, 'creator_id');
}
public function submissions()
{
return $this->hasMany(FormSubmission::class);
}
public function views()
{
return $this->hasMany(FormView::class);
}
public function statistics()
{
return $this->hasMany(FormStatistic::class);
}
public function zappierHooks()
{
return $this->hasMany(FormZapierWebhook::class);
}
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
public function integrations()
{
return $this->hasMany(FormIntegration::class);
}
2022-09-20 21:59:52 +02:00
/**
* Config/options
*/
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->doNotGenerateSlugsOnUpdate()
->generateSlugsFrom(function (Form $form) {
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
return $form->title . ' ' . Str::random(6);
})
2022-09-20 21:59:52 +02:00
->saveSlugsTo('slug');
}
public static function newFactory()
{
return FormFactory::new();
}
}