From 229ccf89f06f2c18415c4721fb53f50ede8142ed Mon Sep 17 00:00:00 2001 From: Stefano Vena Date: Wed, 14 Nov 2018 16:56:48 +0100 Subject: [PATCH 1/3] ciccio --- composer.json | 2 +- ...migration_cartalyst_tags_create_tables.php | 3 +- src/IlluminateTag.php | 1 + src/TaggableInterface.php | 24 ++++++++------- src/TaggableTrait.php | 30 +++++++++++-------- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 18c1718..c30beae 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "cartalyst/tags", + "name": "wetfire2k/tags", "description": "Easily add tags to your Eloquent models.", "keywords": [ "php", 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 240940b..9bee04e 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 7795608..22148ea 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 b63e8d5..65ae065 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 a88ceba..24ae918 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); From 8da7ceffdf55a16b4c3b81d5c214f4cdcf391b68 Mon Sep 17 00:00:00 2001 From: Stefano Vena Date: Wed, 14 Nov 2018 17:28:19 +0100 Subject: [PATCH 2/3] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c30beae..18c1718 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "wetfire2k/tags", + "name": "cartalyst/tags", "description": "Easily add tags to your Eloquent models.", "keywords": [ "php", From 34065faec2082a3ca441a3fb565734032d2a96a7 Mon Sep 17 00:00:00 2001 From: Stefano Vena Date: Fri, 2 Aug 2019 19:47:31 +0200 Subject: [PATCH 3/3] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 18c1718..0900204 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=7.1.3", - "illuminate/database": "5.7.*" + "illuminate/database": "5.8.*" }, "require-dev": { "orchestra/testbench": "^3.7",