1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FormEntries\Models; |
4
|
|
|
|
5
|
|
|
use FormEntries\Database\Factories\FormEntryFactory; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property \FormEntries\Forms\FormContent $content |
12
|
|
|
* @property \JsonFieldCast\Json\SimpleJsonField $meta |
13
|
|
|
*/ |
14
|
|
|
class FormEntry extends Model |
15
|
|
|
{ |
16
|
|
|
use HasFactory; |
17
|
|
|
|
18
|
|
|
protected $guarded = []; |
19
|
|
|
|
20
|
|
|
protected $casts = [ |
21
|
|
|
'notified_at' => 'datetime', |
22
|
|
|
'content' => \FormEntries\Casts\FormContentCast::class, |
23
|
|
|
'meta' => \JsonFieldCast\Casts\SimpleJsonField::class, |
24
|
|
|
]; |
25
|
|
|
|
26
|
2 |
|
protected static function newFactory(): FormEntryFactory |
27
|
|
|
{ |
28
|
2 |
|
return new FormEntryFactory(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
12 |
|
public function getTable(): string |
35
|
|
|
{ |
36
|
12 |
|
return $this->table ?? config('forms-entries.tables.forms-entries', parent::getTable()); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
public function sender() |
40
|
|
|
{ |
41
|
2 |
|
return $this->morphTo('sender'); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function requestIp(): string |
45
|
|
|
{ |
46
|
1 |
|
$value = $this->meta->getAttribute('request_data.ip', ''); |
47
|
|
|
|
48
|
1 |
|
return !is_string($value) ? '' : $value; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function requestIps(): array |
52
|
|
|
{ |
53
|
1 |
|
$ips = $this->meta->getAttribute('request_data.ips', []); |
54
|
|
|
|
55
|
1 |
|
return !is_array($ips) ? [] : $ips; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function requestUserAgent(): string |
59
|
|
|
{ |
60
|
1 |
|
$value = $this->meta->getAttribute('request_data.userAgent', ''); |
61
|
|
|
|
62
|
1 |
|
return !is_string($value) ? '' : $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function getNameAttribute(): string |
66
|
|
|
{ |
67
|
1 |
|
return $this->content->formName(); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function scopeContent(Builder $query, string $contentType) |
71
|
|
|
{ |
72
|
1 |
|
$query->where('content_type', '=', $contentType); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|