-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathForm.php
328 lines (276 loc) · 7.91 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace App\Models\Forms;
use App\Events\Models\FormCreated;
use App\Models\Integration\FormZapierWebhook;
use App\Models\Traits\CachableAttributes;
use App\Models\Traits\CachesAttributes;
use App\Models\User;
use App\Models\Workspace;
use Database\Factories\FormFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify;
class Form extends Model implements CachableAttributes
{
use CachesAttributes;
use HasFactory;
use HasSlug;
use SoftDeletes;
public const DARK_MODE_VALUES = ['auto', 'light', 'dark'];
public const THEMES = ['default', 'simple', 'notion'];
public const WIDTHS = ['centered', 'full'];
public const VISIBILITY = ['public', 'draft', 'closed'];
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',
'slack_webhook_url',
'discord_webhook_url',
'notification_settings',
// integrations
'webhook_url',
'title',
'description',
'tags',
'visibility',
// Customization
'custom_domain',
'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',
'editable_submissions',
'editable_submissions_button_text',
'confetti_on_submission',
'show_progress_bar',
'auto_save',
// Security & Privacy
'can_be_indexed',
'password',
// Custom SEO
'seo_meta',
];
protected $casts = [
'properties' => 'array',
'database_fields_update' => 'array',
'closes_at' => 'datetime',
'tags' => 'array',
'removed_properties' => 'array',
'seo_meta' => 'object',
'notification_settings' => 'object',
];
protected $appends = [
'share_url',
];
protected $hidden = [
'workspace_id',
'notifies',
'slack_webhook_url',
'discord_webhook_url',
'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',
];
protected $cachableAttributes = [
'is_pro',
'views_count',
'max_file_size',
];
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => FormCreated::class,
];
public function getIsProAttribute()
{
return $this->remember('is_pro', 15 * 60, function (): ?bool {
return optional($this->workspace)->is_pro === true;
});
}
public function getShareUrlAttribute()
{
if ($this->custom_domain) {
return 'https://'.$this->custom_domain.'/forms/'.$this->slug;
}
return front_url('/forms/'.$this->slug);
}
public function getEditUrlAttribute()
{
return front_url('/forms/'.$this->slug.'/show');
}
public function getSubmissionsCountAttribute()
{
return $this->submissions()->count();
}
public function getViewsCountAttribute()
{
return $this->remember('views_count', 15 * 60, function (): int {
if (env('DB_CONNECTION') == 'mysql') {
return (int) ($this->views()->count() +
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")));
}
return $this->views()->count() +
$this->statistics()->sum(DB::raw("cast(data->>'views' as integer)"));
});
}
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);
}
public function setTagsAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['tags'] = json_encode($value);
}
public function getIsClosedAttribute()
{
return $this->closes_at && now()->gt($this->closes_at);
}
public function getFormPendingSubmissionKeyAttribute()
{
if ($this->updated_at?->timestamp) {
return 'openform-'.$this->id.'-pending-submission-'.substr($this->updated_at?->timestamp, -6);
}
return null;
}
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);
}
public function getMaxFileSizeAttribute()
{
return $this->remember('max_file_size', 15 * 60, function (): int {
return $this->workspace->max_file_size;
});
}
protected function removedProperties(): Attribute
{
return Attribute::make(
get: function ($value) {
return $value ? json_decode($value, true) : [];
}
);
}
/**
* 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()
->generateSlugsFrom(function (Form $form) {
return $form->title.' '.Str::random(6);
})
->saveSlugsTo('slug');
}
public static function newFactory()
{
return FormFactory::new();
}
public function getNotifiesWebhookAttribute()
{
return ! empty($this->webhook_url);
}
public function getNotifiesDiscordAttribute()
{
return ! empty($this->discord_webhook_url);
}
public function getNotifiesSlackAttribute()
{
return ! empty($this->slack_webhook_url);
}
}