|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Conner\Tagging\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Conner\Tagging\TaggingUtility; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @mixin Model |
|
12
|
|
|
* @mixin Builder |
|
13
|
|
|
* |
|
14
|
|
|
* @property string id |
|
15
|
|
|
* @property string name |
|
16
|
|
|
* @property string slug |
|
17
|
|
|
* @property bool suggest |
|
18
|
|
|
* @property string locale |
|
19
|
|
|
* @property int count |
|
20
|
|
|
* @property int tag_group_id |
|
21
|
|
|
* @property TagGroup group |
|
22
|
|
|
* @property string description |
|
23
|
|
|
* |
|
24
|
|
|
* @method static suggested() |
|
25
|
|
|
* @method static inGroup(string $group) |
|
26
|
|
|
*/ |
|
27
|
|
|
class Tag extends Model |
|
28
|
|
|
{ |
|
29
|
|
|
protected $table = 'tagging_tags'; |
|
30
|
|
|
|
|
31
|
|
|
public $timestamps = false; |
|
32
|
|
|
|
|
33
|
|
|
public $fillable = ['name', 'description', 'locale']; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(array $attributes = []) |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct($attributes); |
|
38
|
|
|
|
|
39
|
|
|
$this->connection = config('tagging.connection'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function save(array $options = []) |
|
46
|
|
|
{ |
|
47
|
|
|
if (strlen($this->name) < 1) { |
|
48
|
|
|
throw new RuntimeException('Cannot save a tag with an empty name'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->slug = TaggingUtility::normalize($this->name); |
|
52
|
|
|
|
|
53
|
|
|
return parent::save($options); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Tag group setter |
|
58
|
|
|
* |
|
59
|
|
|
* @return Tag |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setGroup(string $group) |
|
62
|
|
|
{ |
|
63
|
|
|
$model = TaggingUtility::tagGroupModelString(); |
|
64
|
|
|
|
|
65
|
|
|
$tagGroup = $model::query() |
|
66
|
|
|
->where('slug', TaggingUtility::normalize($group)) |
|
67
|
|
|
->first(); |
|
68
|
|
|
|
|
69
|
|
|
if ($tagGroup) { |
|
70
|
|
|
$this->group()->associate($tagGroup); |
|
71
|
|
|
$this->save(); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} else { |
|
75
|
|
|
throw new RuntimeException('No Tag Group found: '.$group); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Tag group remove |
|
81
|
|
|
* |
|
82
|
|
|
* @return Tag |
|
83
|
|
|
*/ |
|
84
|
|
|
public function removeGroup() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->group()->dissociate(); |
|
87
|
|
|
$this->save(); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Tag group helper function |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $groupName |
|
96
|
|
|
*/ |
|
97
|
|
|
public function isInGroup($groupName): bool |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->group && ($this->group->slug == TaggingUtility::normalize($groupName))) { |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Tag group relationship |
|
108
|
|
|
*/ |
|
109
|
|
|
public function group() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->belongsTo(TaggingUtility::tagGroupModelString(), 'tag_group_id'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get suggested tags |
|
116
|
|
|
*/ |
|
117
|
|
|
public function scopeSuggested($query) |
|
118
|
|
|
{ |
|
119
|
|
|
return $query->where('suggest', true); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get suggested tags |
|
124
|
|
|
* |
|
125
|
|
|
* @return Builder |
|
126
|
|
|
*/ |
|
127
|
|
|
public function scopeInGroup(Builder $query, $groupName) |
|
128
|
|
|
{ |
|
129
|
|
|
$groupSlug = TaggingUtility::normalize($groupName); |
|
130
|
|
|
|
|
131
|
|
|
return $query->whereHas('group', function (Builder $query) use ($groupSlug) { |
|
|
|
|
|
|
132
|
|
|
$query->where('slug', $groupSlug); |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set the name of the tag : $tag->name = 'myname'; |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setNameAttribute(string $value) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->attributes['name'] = TaggingUtility::displayize($value); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Look at the tags table and delete any tags that are no longer in use by any taggable database rows. |
|
146
|
|
|
* Does not delete tags where 'suggest' value is true |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function deleteUnused() |
|
151
|
|
|
{ |
|
152
|
|
|
return (new static)->newQuery() |
|
153
|
|
|
->where('count', '=', 0) |
|
154
|
|
|
->where('suggest', false) |
|
155
|
|
|
->delete(); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|