Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.0 #32

Open
wants to merge 4 commits into
base: 8.0
Choose a base branch
from
Open

8.0 #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function up()
$table->increments('id');
$table->string('taggable_type');
$table->integer('taggable_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->integer('tag_id')->unsigned();

$table->engine = 'InnoDB';

Expand All @@ -47,6 +47,7 @@ public function up()
$table->string('namespace');
$table->string('slug');
$table->string('name');
$table->string('lang');
$table->integer('count')->default(0)->unsigned();

$table->engine = 'InnoDB';
Expand Down
1 change: 1 addition & 0 deletions src/IlluminateTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class IlluminateTag extends Model
protected $fillable = [
'name',
'slug',
'lang',
'count',
'namespace',
];
Expand Down
24 changes: 14 additions & 10 deletions src/TaggableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ public static function setSlugGenerator($name);

/**
* Returns the entity Eloquent tag model object.
*
* @param string $lang
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags();

/**
* Returns all the tags under the entity namespace.
*
* Returns all the tags under the entity namespace.
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function allTags();
Expand All @@ -87,7 +86,7 @@ public static function allTags();
* Returns the entities with only the given tags.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|array $tags
* @param string|array $tags
* @param string $type
* @return \Illuminate\Database\Eloquent\Builder
*/
Expand All @@ -107,7 +106,7 @@ public static function scopeWithTag(Builder $query, $tags, $type = 'slug');
* Returns the entities that do not have one of the given tags.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|array $tags
* @param string|array $tags
* @param string $type
* @return \Illuminate\Database\Eloquent\Builder
*/
Expand All @@ -117,43 +116,48 @@ public static function scopeWithoutTag(Builder $query, $tags, $type = 'slug');
* Attaches multiple tags to the entity.
*
* @param string|array $tags
* @param string $lang
* @return bool
*/
public function tag($tags);
public function tag($tags, $lang = null);

/**
* Detaches multiple tags from the entity or if no tags are
* passed, removes all the attached tags from the entity.
*
* @param string|array|null $tags
* @param string $lang
* @return bool
*/
public function untag($tags = null);
public function untag($tags = null, $lang = null);

/**
* Attaches or detaches the given tags.
*
* @param string|array $tags
* @param string $lang
* @param string $type
* @return bool
*/
public function setTags($tags, $type = 'name');
public function setTags($tags, $lang = null, $type = 'name');

/**
* Attaches the given tag to the entity.
*
* @param string $name
* @param string $lang
* @return void
*/
public function addTag($name);
public function addTag($name, $lang = null);

/**
* Detaches the given tag from the entity.
*
* @param string $name
* @param string $lang
* @return void
*/
public function removeTag($name);
public function removeTag($name, $lang = null);

/**
* Prepares the given tags before being saved.
Expand Down
30 changes: 18 additions & 12 deletions src/TaggableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function scopeWhereTag(Builder $query, $tags, $type = 'slug')

foreach ($tags as $tag) {
$query->whereHas('tags', function ($query) use ($type, $tag) {
$query->where($type, $tag);
$query->where($type, $tag);
});
}

Expand Down Expand Up @@ -158,10 +158,10 @@ public static function scopeWithoutTag(Builder $query, $tags, $type = 'slug')
/**
* {@inheritdoc}
*/
public function tag($tags)
public function tag($tags, $lang=null)
{
foreach ($this->prepareTags($tags) as $tag) {
$this->addTag($tag);
$this->addTag($tag, $lang);
}

return true;
Expand All @@ -170,9 +170,13 @@ public function tag($tags)
/**
* {@inheritdoc}
*/
public function untag($tags = null)
public function untag($tags = null, $lang = null)
{
$tags = $tags ?: $this->tags->pluck('name')->all();
if( empty($lang) ){
$tags = $tags ?: $this->tags->pluck('name')->all();
}else{
$tags = $tags ?: $this->tags()->where('lang', $lang)->pluck('name')->all();
}

foreach ($this->prepareTags($tags) as $tag) {
$this->removeTag($tag);
Expand All @@ -184,26 +188,26 @@ public function untag($tags = null)
/**
* {@inheritdoc}
*/
public function setTags($tags, $type = 'name')
public function setTags($tags, $lang = null, $type = 'name')
{
// Prepare the tags
$tags = $this->prepareTags($tags);

// Get the current entity tags
$entityTags = $this->tags->pluck($type)->all();
$entityTags = $this->tags()->where('lang', $lang)->pluck($type)->all();

// Prepare the tags to be added and removed
$tagsToAdd = array_diff($tags, $entityTags);
$tagsToDel = array_diff($entityTags, $tags);

// Detach the tags
if (! empty($tagsToDel)) {
$this->untag($tagsToDel);
$this->untag($tagsToDel, $lang);
}

// Attach the tags
if (! empty($tagsToAdd)) {
$this->tag($tagsToAdd);
$this->tag($tagsToAdd, $lang);
}

return true;
Expand All @@ -212,10 +216,11 @@ public function setTags($tags, $type = 'name')
/**
* {@inheritdoc}
*/
public function addTag($name)
public function addTag($name, $lang = null)
{
$tag = $this->createTagsModel()->firstOrNew([
'slug' => $this->generateTagSlug($name),
'lang' => $lang,
'namespace' => $this->getEntityClassName(),
]);

Expand All @@ -237,7 +242,7 @@ public function addTag($name)
/**
* {@inheritdoc}
*/
public function removeTag($name)
public function removeTag($name, $lang = null)
{
$slug = $this->generateTagSlug($name);

Expand All @@ -246,6 +251,7 @@ public function removeTag($name)
$tag = $this
->createTagsModel()
->whereNamespace($namespace)
->where("lang", $lang)
->where(function ($query) use ($name, $slug) {
$query
->orWhere('name', '=', $name)
Expand All @@ -255,7 +261,7 @@ public function removeTag($name)
->first()
;

if ($tag && $this->tags()->get()->contains($tag->id)) {
if ($tag && $this->tags()->where("lang", $lang)->get()->contains($tag->id)) {
$tag->update([ 'count' => $tag->count - 1 ]);

$this->tags()->detach($tag);
Expand Down