|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Builder\Notification as BuilderNotification; |
|
6
|
|
|
use Fenos\Notifynder\Parsers\NotificationParser; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
|
|
10
|
|
|
class Notification extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
protected $fillable = [ |
|
13
|
|
|
'to_id', |
|
14
|
|
|
'to_type', |
|
15
|
|
|
'from_id', |
|
16
|
|
|
'from_type', |
|
17
|
|
|
'category_id', |
|
18
|
|
|
'read', |
|
19
|
|
|
'url', |
|
20
|
|
|
'extra', |
|
21
|
|
|
'expires_at', |
|
22
|
|
|
'stack_id', |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
protected $appends = [ |
|
26
|
|
|
'text', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
protected $casts = [ |
|
30
|
|
|
'extra' => 'array', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct($attributes = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->fillable($this->mergeFillables()); |
|
36
|
|
|
|
|
37
|
|
|
if ($attributes instanceof BuilderNotification) { |
|
38
|
|
|
$attributes = $attributes->toArray(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct($attributes); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function category() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->belongsTo(NotificationCategory::class, 'category_id'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function from() |
|
50
|
|
|
{ |
|
51
|
|
|
if (notifynder_config()->isPolymorphic()) { |
|
52
|
|
|
return $this->belongsTo(notifynder_config()->getModel(), 'from_id'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->morphTo(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function to() |
|
59
|
|
|
{ |
|
60
|
|
|
if (notifynder_config()->isPolymorphic()) { |
|
61
|
|
|
return $this->belongsTo(notifynder_config()->getModel(), 'to_id'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->morphTo(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getCustomFillableFields() |
|
68
|
|
|
{ |
|
69
|
|
|
return notifynder_config()->getAdditionalFields(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function mergeFillables() |
|
73
|
|
|
{ |
|
74
|
|
|
$fillables = array_unique($this->getFillable() + $this->getCustomFillableFields()); |
|
75
|
|
|
|
|
76
|
|
|
return $fillables; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getTemplateBodyAttribute() |
|
80
|
|
|
{ |
|
81
|
|
|
if (notifynder_config()->isTranslated()) { |
|
82
|
|
|
$key = notifynder_config()->getTranslationDomain().'.'.$this->category->name; |
|
|
|
|
|
|
83
|
|
|
$trans = trans($key); |
|
84
|
|
|
if ($trans != $key) { |
|
85
|
|
|
return $trans; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->category->text; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getTextAttribute() |
|
93
|
|
|
{ |
|
94
|
|
|
if (! array_key_exists('text', $this->attributes)) { |
|
95
|
|
|
$notifynderParse = new NotificationParser(); |
|
96
|
|
|
$this->attributes['text'] = $notifynderParse->parse($this); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->attributes['text']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function read() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->update(['read' => 1]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function unread() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->update(['read' => 0]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function resend() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->updateTimestamps(); |
|
115
|
|
|
$this->read = 0; |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
return $this->save(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function isAnonymous() |
|
121
|
|
|
{ |
|
122
|
|
|
return is_null($this->from_id); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
public function scopeByCategory(Builder $query, $category) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$categoryId = $category; |
|
128
|
|
|
if ($category instanceof NotificationCategory) { |
|
129
|
|
|
$categoryId = $category->getKey(); |
|
130
|
|
|
} elseif (! is_numeric($category)) { |
|
131
|
|
|
$categoryId = NotificationCategory::byName($category)->firstOrFail()->getKey(); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $query->where('category_id', $categoryId); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function scopeByRead(Builder $query, $read = 1) |
|
138
|
|
|
{ |
|
139
|
|
|
return $query->where('read', $read); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.