Skip to content

Commit 214f604

Browse files
authored
Merge pull request #106 from invokemedia/tag-groups
Tag groups testing and tweaks
2 parents b19b041 + 76a8110 commit 214f604

9 files changed

+460
-82
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ Article::existingTags(); // return collection of all existing tags on any articl
8484

8585
[More examples in the documentation](docs/usage-examples.md)
8686

87+
88+
### Tag Groups
89+
90+
You can create groups with the following artisan command
91+
92+
```php
93+
php artisan tagging:create-group MyTagGroup
94+
```
95+
96+
Set the tag group for a tag
97+
98+
```php
99+
$tag->setGroup('MyTagGroup');
100+
```
101+
102+
To get all the tags in a certain group
103+
104+
```php
105+
Tag::inGroup('MyTagGroup')->get()
106+
```
107+
108+
Check if a tag is in a group
109+
110+
```php
111+
$tag->isInGroup('MyTagGroup');
112+
```
113+
114+
87115
### Configure
88116

89117
[See config/tagging.php](config/tagging.php) for configuration options.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class CreateTagGroupsTable extends Migration {
7+
8+
public function up()
9+
{
10+
Schema::create('tagging_tag_groups', function(Blueprint $table) {
11+
$table->increments('id');
12+
$table->string('slug', 255)->index();
13+
$table->string('name', 255);
14+
});
15+
}
16+
17+
public function down()
18+
{
19+
Schema::drop('tagging_tag_groups');
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class UpdateTagsTable extends Migration {
7+
8+
public function up()
9+
{
10+
11+
Schema::table('tagging_tags', function ($table) {
12+
$table->integer('tag_group_id')->unsigned()->nullable()->after('id');
13+
$table->foreign('tag_group_id')->references('id')->on('tagging_tag_groups');
14+
});
15+
16+
}
17+
18+
19+
public function down()
20+
{
21+
Schema::table('tagging_tags', function ($table) {
22+
$table->dropColumn('tag_group_id');
23+
});
24+
}
25+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Conner\Tagging\Console\Commands;
4+
5+
use Conner\Tagging\Contracts\TaggingUtility;
6+
use Conner\Tagging\Model\TagGroup;
7+
use Illuminate\Console\Command;
8+
9+
10+
class GenerateTagGroup extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'tagging:create-group {group_name}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create a laravel tag group';
25+
26+
27+
protected $taggingUtility;
28+
29+
30+
31+
public function __construct(TaggingUtility $taggingUtility)
32+
{
33+
parent::__construct();
34+
35+
$this->taggingUtility = $taggingUtility;
36+
}
37+
38+
39+
40+
/**
41+
* Execute the console command.
42+
*
43+
* @return mixed
44+
*/
45+
public function handle()
46+
{
47+
$group_name = $this->argument('group_name');
48+
49+
$tag_group = new TagGroup();
50+
$tag_group->name = $group_name;
51+
$tag_group->slug = $this->taggingUtility->slug($group_name);
52+
53+
$tag_group->save();
54+
55+
56+
$this->info('Created tag group: ' . $group_name);
57+
}
58+
}

src/Model/Tag.php

+141-77
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,145 @@
88
*/
99
class Tag extends Eloquent
1010
{
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-
* Get suggested tags
55-
*/
56-
public function scopeSuggested($query)
57-
{
58-
return $query->where('suggest', true);
59-
}
60-
61-
/**
62-
* Set the name of the tag : $tag->name = 'myname';
63-
*
64-
* @param string $value
65-
*/
66-
public function setNameAttribute($value)
67-
{
68-
$displayer = config('tagging.displayer');
69-
$displayer = empty($displayer) ? '\Illuminate\Support\Str::title' : $displayer;
70-
71-
$this->attributes['name'] = call_user_func($displayer, $value);
72-
}
73-
74-
/**
75-
* Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
76-
* Does not delete tags where 'suggest'value is true
77-
*
78-
* @return int
79-
*/
80-
public static function deleteUnused()
81-
{
82-
return (new static)->newQuery()
83-
->where('count', '=', 0)
84-
->where('suggest', false)
85-
->delete();
86-
}
87-
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+
}
88152
}

0 commit comments

Comments
 (0)