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