diff --git a/resources/migrations/2014_10_29_202547_migration_cartalyst_tags_create_tables.php b/resources/migrations/2014_10_29_202547_migration_cartalyst_tags_create_tables.php index e4015d5..164a80e 100644 --- a/resources/migrations/2014_10_29_202547_migration_cartalyst_tags_create_tables.php +++ b/resources/migrations/2014_10_29_202547_migration_cartalyst_tags_create_tables.php @@ -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'; @@ -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'; diff --git a/src/IlluminateTag.php b/src/IlluminateTag.php index 92cd356..3f769c9 100644 --- a/src/IlluminateTag.php +++ b/src/IlluminateTag.php @@ -41,6 +41,7 @@ class IlluminateTag extends Model protected $fillable = [ 'name', 'slug', + 'lang', 'count', 'namespace', ]; diff --git a/src/TaggableInterface.php b/src/TaggableInterface.php index 6088d6c..843477a 100644 --- a/src/TaggableInterface.php +++ b/src/TaggableInterface.php @@ -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(); @@ -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 */ @@ -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 */ @@ -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. diff --git a/src/TaggableTrait.php b/src/TaggableTrait.php index e33c58e..b865ac3 100644 --- a/src/TaggableTrait.php +++ b/src/TaggableTrait.php @@ -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); }); } @@ -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; @@ -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); @@ -184,13 +188,13 @@ 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); @@ -198,12 +202,12 @@ public function setTags($tags, $type = 'name') // 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; @@ -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(), ]); @@ -237,7 +242,7 @@ public function addTag($name) /** * {@inheritdoc} */ - public function removeTag($name) + public function removeTag($name, $lang = null) { $slug = $this->generateTagSlug($name); @@ -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) @@ -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);