Skip to content

Commit 3db4870

Browse files
committed
add locale in Tag model
1 parent bcce530 commit 3db4870

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('tagging_tags', function (Blueprint $table) {
17+
$table->string('locale', 5)->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('tagging_tags', function (Blueprint $table) {
29+
$table->dropColumn('locale');
30+
});
31+
}
32+
};

src/Contracts/TaggableContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface TaggableContract {
1212
*
1313
* @param string|array $tagNames
1414
*/
15-
public function addTags($tagNames);
15+
public function addTags($tagNames, $locale);
1616

1717
/**
1818
* Return array of the tag names related to the current model

src/Model/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Tag extends Model
2323
{
2424
protected $table = 'tagging_tags';
2525
public $timestamps = false;
26-
public $fillable = ['name', 'description'];
26+
public $fillable = ['name', 'description', 'locale'];
2727

2828
/**
2929
* @param array $attributes

src/Taggable.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ public function getTagNamesAttribute(): array
9393
*
9494
* @param string|array $tagNames
9595
*/
96-
public function addTags($tagNames)
96+
public function addTags($tagNames, $locale)
9797
{
9898
$tagNames = TaggingUtility::makeTagArray($tagNames);
9999

100-
foreach($tagNames as $tagName) {
101-
$this->addSingleTag($tagName);
100+
foreach ($tagNames as $tagName) {
101+
$this->addSingleTag($tagName, $locale);
102102
}
103103
}
104104

@@ -107,9 +107,9 @@ public function addTags($tagNames)
107107
*
108108
* @param string|array $tagNames
109109
*/
110-
public function tag($tagNames)
110+
public function tag($tagNames, $locale = 'en')
111111
{
112-
return $this->addTags($tagNames);
112+
return $this->addTags($tagNames, $locale);
113113
}
114114

115115
/**
@@ -250,29 +250,32 @@ public function scopeWithoutTags(Builder $query, $tagNames): Builder
250250
*
251251
* @param string $tagName
252252
*/
253-
private function addSingleTag($tagName)
253+
private function addSingleTag($tagName, $locale)
254254
{
255255
$tagName = trim($tagName);
256256

257-
if(strlen($tagName) == 0) {
257+
if (strlen($tagName) == 0) {
258258
return;
259259
}
260260

261261
$tagSlug = TaggingUtility::normalize($tagName);
262262

263263
$previousCount = $this->tagged()->where('tag_slug', '=', $tagSlug)->take(1)->count();
264-
if($previousCount >= 1) { return; }
264+
if ($previousCount >= 1) {
265+
return;
266+
}
265267

266268
$model = TaggingUtility::taggedModelString();
267269

268270
$tagged = new $model([
269271
'tag_name' => TaggingUtility::displayize($tagName),
270272
'tag_slug' => $tagSlug,
273+
'locale' => $locale,
271274
]);
272275

273276
$this->tagged()->save($tagged);
274277

275-
TaggingUtility::incrementCount($tagName, $tagSlug, 1);
278+
TaggingUtility::incrementCount($tagName, $tagSlug, 1, $locale);
276279

277280
unset($this->relations['tagged']);
278281

src/TaggingUtility.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,21 @@ public static function slug($str)
201201
* @param string $tagSlug
202202
* @param integer $count
203203
*/
204-
public static function incrementCount($tagString, $tagSlug, $count)
204+
public static function incrementCount($tagString, $tagSlug, $count, $locale)
205205
{
206-
if($count <= 0) { return; }
206+
if ($count <= 0) {
207+
return;
208+
}
207209
$model = static::tagModelString();
208210

209211
$tag = $model::where('slug', '=', $tagSlug)->first();
210212

211-
if(!$tag) {
213+
if (!$tag) {
212214
$tag = new $model;
213215
$tag->name = $tagString;
214216
$tag->slug = $tagSlug;
215217
$tag->suggest = false;
218+
$tag->locale = $locale;
216219
$tag->save();
217220
}
218221

0 commit comments

Comments
 (0)