|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class NotificationCategory. |
|
11
|
|
|
* |
|
12
|
|
|
* @property string $name |
|
13
|
|
|
* @property string $text |
|
14
|
|
|
* @method Builder byName($name) |
|
15
|
|
|
*/ |
|
16
|
|
|
class NotificationCategory extends Model |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $table = 'notification_categories'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $fillable = [ |
|
27
|
|
|
'name', |
|
28
|
|
|
'text', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $appends = [ |
|
35
|
|
|
'template_body', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public $timestamps = false; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(array $attributes = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$table = app('notifynder.resolver.model')->getTable(get_class($this)); |
|
46
|
|
|
if (! empty($table)) { |
|
47
|
|
|
$this->setTable($table); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$attributes = array_merge([ |
|
51
|
|
|
'text' => '', |
|
52
|
|
|
], $attributes); |
|
53
|
|
|
|
|
54
|
|
|
parent::__construct($attributes); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
59
|
|
|
*/ |
|
60
|
|
|
public function notifications() |
|
61
|
|
|
{ |
|
62
|
|
|
$model = app('notifynder.resolver.model')->getModel(Notification::class); |
|
63
|
|
|
|
|
64
|
|
|
return $this->hasMany($model, 'category_id'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setNameAttribute($value) |
|
68
|
|
|
{ |
|
69
|
|
|
$parts = explode('.', $value); |
|
70
|
|
|
foreach ($parts as $i => $part) { |
|
71
|
|
|
$parts[$i] = Str::slug(preg_replace('/[^a-z0-9_]/', '_', strtolower($part)), '_'); |
|
72
|
|
|
} |
|
73
|
|
|
$this->attributes['name'] = implode('.', $parts); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return \Symfony\Component\Translation\TranslatorInterface|string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getTemplateBodyAttribute() |
|
80
|
|
|
{ |
|
81
|
|
|
if (notifynder_config()->isTranslated()) { |
|
82
|
|
|
$key = notifynder_config()->getTranslationDomain().'.'.$this->name; |
|
83
|
|
|
$trans = trans($key); |
|
84
|
|
|
if ($trans != $key) { |
|
85
|
|
|
return $trans; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->text; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param Builder $query |
|
94
|
|
|
* @param $name |
|
95
|
|
|
* @return Builder |
|
96
|
|
|
*/ |
|
97
|
|
|
public function scopeByName(Builder $query, $name) |
|
98
|
|
|
{ |
|
99
|
|
|
return $query->where('name', $name); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function getIdByCategory($category) |
|
107
|
|
|
{ |
|
108
|
|
|
$categoryId = $category; |
|
109
|
|
|
if ($category instanceof self) { |
|
110
|
|
|
$categoryId = $category->getKey(); |
|
111
|
|
|
} elseif (! is_numeric($category)) { |
|
112
|
|
|
$categoryId = self::byName($category)->firstOrFail()->getKey(); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $categoryId; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: