2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Forms;
|
|
|
|
|
|
|
|
use App\Events\Models\FormCreated;
|
|
|
|
use App\Models\Integration\FormZapierWebhook;
|
2023-12-02 13:51:08 +00:00
|
|
|
use App\Models\Traits\CachableAttributes;
|
|
|
|
use App\Models\Traits\CachesAttributes;
|
2022-09-20 19:59:52 +00:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Workspace;
|
|
|
|
use Database\Factories\FormFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2023-06-19 12:50:31 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-09-20 19:59:52 +00:00
|
|
|
use Spatie\Sluggable\HasSlug;
|
|
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
use Stevebauman\Purify\Facades\Purify;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2023-10-13 09:43:53 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2022-09-20 19:59:52 +00:00
|
|
|
|
2023-12-02 13:51:08 +00:00
|
|
|
class Form extends Model implements CachableAttributes
|
2022-09-20 19:59:52 +00:00
|
|
|
{
|
2023-12-02 13:51:08 +00:00
|
|
|
use CachesAttributes;
|
2023-12-02 19:35:44 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
const DARK_MODE_VALUES = ['auto', 'light', 'dark'];
|
|
|
|
const THEMES = ['default', 'simple', 'notion'];
|
|
|
|
const WIDTHS = ['centered', 'full'];
|
2023-02-19 12:11:50 +00:00
|
|
|
const VISIBILITY = ['public', 'draft', 'closed'];
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
use HasFactory, HasSlug, SoftDeletes;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'workspace_id',
|
|
|
|
'creator_id',
|
|
|
|
'properties',
|
|
|
|
'removed_properties',
|
|
|
|
|
|
|
|
// Notifications
|
|
|
|
'notifies',
|
|
|
|
'notification_emails',
|
|
|
|
'send_submission_confirmation',
|
|
|
|
'notification_sender',
|
|
|
|
'notification_subject',
|
|
|
|
'notification_body',
|
|
|
|
'notifications_include_submission',
|
2022-09-21 15:23:37 +00:00
|
|
|
'slack_webhook_url',
|
2023-02-19 12:19:16 +00:00
|
|
|
'discord_webhook_url',
|
2023-08-30 12:20:14 +00:00
|
|
|
'notification_settings',
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
// integrations
|
|
|
|
'webhook_url',
|
|
|
|
|
|
|
|
'title',
|
|
|
|
'description',
|
|
|
|
'tags',
|
2022-10-16 17:27:14 +00:00
|
|
|
'visibility',
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
// Customization
|
2023-11-29 13:53:08 +00:00
|
|
|
'custom_domain',
|
2022-09-20 19:59:52 +00:00
|
|
|
'theme',
|
|
|
|
'width',
|
|
|
|
'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',
|
2023-01-10 13:52:14 +00:00
|
|
|
'editable_submissions',
|
2023-03-15 17:11:38 +00:00
|
|
|
'editable_submissions_button_text',
|
2023-04-12 11:17:05 +00:00
|
|
|
'confetti_on_submission',
|
2023-10-12 11:26:02 +00:00
|
|
|
'auto_save',
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
// Security & Privacy
|
|
|
|
'can_be_indexed',
|
2023-08-31 08:56:14 +00:00
|
|
|
'password',
|
|
|
|
|
|
|
|
// Custom SEO
|
|
|
|
'seo_meta'
|
2022-09-20 19:59:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'properties' => 'array',
|
|
|
|
'database_fields_update' => 'array',
|
|
|
|
'closes_at' => 'datetime',
|
|
|
|
'tags' => 'array',
|
2023-08-30 09:43:11 +00:00
|
|
|
'removed_properties' => 'array',
|
2023-08-30 12:20:14 +00:00
|
|
|
'seo_meta' => 'object',
|
|
|
|
'notification_settings' => 'object'
|
2022-09-20 19:59:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'share_url',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $hidden = [
|
|
|
|
'workspace_id',
|
|
|
|
'notifies',
|
2022-09-21 15:23:37 +00:00
|
|
|
'slack_webhook_url',
|
2023-02-19 12:19:16 +00:00
|
|
|
'discord_webhook_url',
|
2022-09-20 19:59:52 +00:00
|
|
|
'webhook_url',
|
|
|
|
'send_submission_confirmation',
|
|
|
|
'redirect_url',
|
|
|
|
'database_fields_update',
|
|
|
|
'notification_sender',
|
|
|
|
'notification_subject',
|
|
|
|
'notification_body',
|
|
|
|
'notifications_include_submission',
|
|
|
|
'password',
|
|
|
|
'tags',
|
|
|
|
'notification_emails',
|
|
|
|
'removed_properties'
|
|
|
|
];
|
|
|
|
|
2023-12-02 13:51:08 +00:00
|
|
|
protected $cachableAttributes = [
|
|
|
|
'is_pro',
|
|
|
|
'views_count',
|
2023-12-15 11:43:05 +00:00
|
|
|
'max_file_size'
|
2023-12-02 13:51:08 +00:00
|
|
|
];
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
/**
|
|
|
|
* The event map for the model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dispatchesEvents = [
|
|
|
|
'created' => FormCreated::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getIsProAttribute()
|
|
|
|
{
|
2023-12-04 09:43:24 +00:00
|
|
|
return $this->remember('is_pro', 15 * 60, function (): ?bool {
|
|
|
|
return optional($this->workspace)->is_pro === true;
|
2023-12-02 13:51:08 +00:00
|
|
|
});
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getShareUrlAttribute()
|
|
|
|
{
|
2023-11-29 13:53:08 +00:00
|
|
|
if ($this->custom_domain) {
|
|
|
|
return 'https://' . $this->custom_domain . '/forms/' . $this->slug;
|
|
|
|
}
|
2023-12-24 19:19:59 +00:00
|
|
|
return '/forms/' . $this->slug;
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:37:08 +00:00
|
|
|
public function getEditUrlAttribute()
|
|
|
|
{
|
2023-11-29 13:53:08 +00:00
|
|
|
return url('/forms/' . $this->slug . '/show');
|
2023-08-30 10:37:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
public function getSubmissionsCountAttribute()
|
|
|
|
{
|
2023-12-02 19:35:44 +00:00
|
|
|
return $this->submissions()->count();
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getViewsCountAttribute()
|
|
|
|
{
|
2023-12-02 19:35:44 +00:00
|
|
|
return $this->remember('views_count', 15 * 60, function (): int {
|
2023-12-02 13:51:08 +00:00
|
|
|
if (env('DB_CONNECTION') == 'mysql') {
|
|
|
|
return (int)($this->views()->count() +
|
|
|
|
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")));
|
|
|
|
}
|
2023-01-04 12:59:25 +00:00
|
|
|
return $this->views()->count() +
|
2023-11-29 13:53:08 +00:00
|
|
|
$this->statistics()->sum(DB::raw("cast(data->>'views' as integer)"));
|
2023-12-02 13:51:08 +00:00
|
|
|
});
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setDescriptionAttribute($value)
|
|
|
|
{
|
|
|
|
// Strip out unwanted html
|
|
|
|
$this->attributes['description'] = Purify::clean($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSubmittedTextAttribute($value)
|
|
|
|
{
|
|
|
|
// Strip out unwanted html
|
|
|
|
$this->attributes['submitted_text'] = Purify::clean($value);
|
|
|
|
}
|
|
|
|
|
2022-10-27 21:31:05 +00:00
|
|
|
public function setTagsAttribute($value)
|
|
|
|
{
|
2022-11-01 13:43:14 +00:00
|
|
|
if ($value == '') {
|
|
|
|
$value = null;
|
2022-10-27 21:31:05 +00:00
|
|
|
}
|
|
|
|
$this->attributes['tags'] = json_encode($value);
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
public function getIsClosedAttribute()
|
|
|
|
{
|
|
|
|
return ($this->closes_at && now()->gt($this->closes_at));
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:20:28 +00:00
|
|
|
public function getFormPendingSubmissionKeyAttribute()
|
|
|
|
{
|
|
|
|
if ($this->updated_at?->timestamp) {
|
|
|
|
return "openform-" . $this->id . "-pending-submission-" . substr($this->updated_at?->timestamp, -6);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
public function getMaxNumberOfSubmissionsReachedAttribute()
|
|
|
|
{
|
|
|
|
return ($this->max_submissions_count && $this->max_submissions_count <= $this->submissions_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return !empty($this->password);
|
|
|
|
}
|
|
|
|
|
2023-12-15 11:43:05 +00:00
|
|
|
public function getMaxFileSizeAttribute()
|
|
|
|
{
|
|
|
|
return $this->remember('max_file_size', 15 * 60, function(): int {
|
|
|
|
return $this->workspace->max_file_size;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-29 13:53:08 +00:00
|
|
|
protected function removedProperties(): Attribute
|
|
|
|
{
|
2023-10-13 09:43:53 +00:00
|
|
|
return Attribute::make(
|
|
|
|
get: function ($value) {
|
|
|
|
return $value ? json_decode($value, true) : [];
|
|
|
|
}
|
|
|
|
);
|
2023-10-13 08:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config/options
|
|
|
|
*/
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
|
|
{
|
|
|
|
return SlugOptions::create()
|
|
|
|
->doNotGenerateSlugsOnUpdate()
|
2023-06-19 12:50:31 +00:00
|
|
|
->generateSlugsFrom(function (Form $form) {
|
|
|
|
return $form->title . ' ' . Str::random(6);
|
|
|
|
})
|
2022-09-20 19:59:52 +00:00
|
|
|
->saveSlugsTo('slug');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function newFactory()
|
|
|
|
{
|
|
|
|
return FormFactory::new();
|
|
|
|
}
|
2022-09-21 15:23:37 +00:00
|
|
|
|
2023-02-19 12:19:16 +00:00
|
|
|
|
2023-10-26 11:22:16 +00:00
|
|
|
public function getNotifiesWebhookAttribute()
|
|
|
|
{
|
|
|
|
return !empty($this->webhook_url);
|
|
|
|
}
|
2023-11-29 13:53:08 +00:00
|
|
|
|
2023-02-19 12:19:16 +00:00
|
|
|
public function getNotifiesDiscordAttribute()
|
|
|
|
{
|
|
|
|
return !empty($this->discord_webhook_url);
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:23:37 +00:00
|
|
|
public function getNotifiesSlackAttribute()
|
|
|
|
{
|
|
|
|
return !empty($this->slack_webhook_url);
|
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|