From aa85b4274ad76e77398e3c73c3c2b0ea5b8a8b1f Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:33:20 -0500 Subject: [PATCH 01/20] support slug taxonomy --- src/Taxonomy.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 1fb741e..279f5ff 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -25,12 +25,12 @@ public function __construct(Vocabulary $vocabulary, Term $term) { * The Vocabulary object if created, FALSE if error creating, * Exception if the vocabulary name already exists. */ - public function createVocabulary($name) { + public function createVocabulary($name,$slug=null) { if ($this->vocabulary->where('name', $name)->count()) { throw new Exceptions\VocabularyExistsException(); } - return $this->vocabulary->create(['name' => $name]); + return $this->vocabulary->create(['name' => $name,'slug' => $slug]); } /** @@ -59,6 +59,18 @@ public function getVocabularyByName($name) { return $this->vocabulary->where('name', $name)->first(); } + /** + * Get a Vocabulary by slug + * + * @param string $slug + * The name of the Vocabulary to fetch + * + * @return + * The Vocabulary Model object, otherwise NULL + */ + public function getVocabularyBySlug($slug) { + return $this->vocabulary->where('slug', $slug)->first(); + } /** * Get a Vocabulary by name * From 1f6c0aa10683775250838ae543ccc4fbdd4260ae Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:36:55 -0500 Subject: [PATCH 02/20] Update Vocabulary.php --- src/Models/Vocabulary.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Vocabulary.php b/src/Models/Vocabulary.php index 196e6a6..2273501 100644 --- a/src/Models/Vocabulary.php +++ b/src/Models/Vocabulary.php @@ -3,7 +3,7 @@ class Vocabulary extends \Eloquent { protected $fillable = [ - 'name', + 'name', 'slug', ]; protected $table = 'vocabularies'; From 9c2e747f04fd4b8ef89c6621534344a72e79ff8a Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:37:39 -0500 Subject: [PATCH 03/20] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c97103f..4fc8885 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,8 @@ } ], "require": { - "php": ">=5.4", - "illuminate/support": "~5.0" + "php": ">=7.0", + "illuminate/support": "~6.0" }, "require-dev": { "mockery/mockery": "0.9.*", From 573b602618e7def01756f1e6b8d27efe783b5867 Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:40:17 -0500 Subject: [PATCH 04/20] Update 2014_10_29_164909_create_vocabularies_table.php --- src/migrations/2014_10_29_164909_create_vocabularies_table.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/migrations/2014_10_29_164909_create_vocabularies_table.php b/src/migrations/2014_10_29_164909_create_vocabularies_table.php index d21def8..72096b3 100644 --- a/src/migrations/2014_10_29_164909_create_vocabularies_table.php +++ b/src/migrations/2014_10_29_164909_create_vocabularies_table.php @@ -14,6 +14,7 @@ public function up() Schema::create('vocabularies', function($table) { $table->increments('id'); $table->string('name'); + $table->string('slug'); $table->timestamps(); }); } @@ -28,4 +29,4 @@ public function down() Schema::dropIfExists('vocabularies'); } -} \ No newline at end of file +} From e2d58f8d037c65ebd5a339eb1c3319f43e3b8e04 Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:49:17 -0500 Subject: [PATCH 05/20] extended support slugs --- src/Taxonomy.php | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 279f5ff..3cd20b6 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -29,9 +29,12 @@ public function createVocabulary($name,$slug=null) { if ($this->vocabulary->where('name', $name)->count()) { throw new Exceptions\VocabularyExistsException(); } + if (!is_null($slug)&&$this->vocabulary->where('slug', $slug)->count()) { + throw new Exceptions\VocabularyExistsException(); + } - return $this->vocabulary->create(['name' => $name,'slug' => $slug]); - } + return $this->vocabulary->create(['name' => $name,'slug' => $slug]); + } /** * Get a Vocabulary by ID @@ -119,7 +122,35 @@ public function getVocabularyByNameOptionsArray($name) { return $options; } + /** + * Get a Vocabulary by slug as an options array for dropdowns + * + * @param string $slug + * The name of the Vocabulary to fetch + * + * @return + * The Vocabulary Model object, otherwise NULL + */ + public function getVocabularyBySlugOptionsArray($slug) { + $vocabulary = $this->vocabulary->where('slug', $slug)->first(); + + if (is_null($vocabulary)) { + return []; + } + + $parents = $this->term->whereParent(0) + ->whereVocabularyId($vocabulary->id) + ->orderBy('weight', 'ASC') + ->get(); + + $options = []; + foreach ($parents as $parent) { + $options[$parent->id] = $parent->name; + $this->recurse_children($parent, $options); + } + return $options; + } /** * Recursively visit the children of a term and generate the '- ' option array for dropdowns * @@ -175,6 +206,24 @@ public function deleteVocabularyByName($name) { return FALSE; } + /** + * Delete a Vocabulary by slug + * + * @param string $slug + * The name of the Vocabulary to delete + * + * @return bool + * TRUE if Vocabulary is deletes, otherwise FALSE + */ + public function deleteVocabularyBySlug($slug) { + $vocabulary = $this->vocabulary->where('slug', $slug)->first(); + + if (!is_null($vocabulary)) { + return $vocabulary->delete(); + } + + return FALSE; + } /** * Create a new term in a specific vocabulary * From 50d8832e8ca5ee9374d2a916b60e915efc945321 Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:53:01 -0500 Subject: [PATCH 06/20] suport slug added --- src/TaxonomyTrait.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 05cd07a..69427dc 100644 --- a/src/TaxonomyTrait.php +++ b/src/TaxonomyTrait.php @@ -70,6 +70,21 @@ public function getTermsByVocabularyName($name) { return $this->related()->where('vocabulary_id', $vocabulary->id)->get(); } + /** + * Get all the terms for a given vocabulary that are linked to the current + * Model. + * + * @param $slug string + * The name of the vocabulary + * + * @return object + * A collection of TermRelation objects + */ + public function getTermsByVocabularySlug($slug) { + $vocabulary = \Taxonomy::getVocabularyBySlug($slug); + + return $this->related()->where('vocabulary_id', $vocabulary->id)->get(); + } /** * Get all the terms for a given vocabulary that are linked to the current * Model as a key value pair array. @@ -92,6 +107,30 @@ public function getTermsByVocabularyNameAsArray($name) { return $data; } + + + /** + * Get all the terms for a given vocabulary that are linked to the current + * Model as a key value pair array. + * + * @param $slug string + * The name of the vocabulary + * + * @return array + * A key value pair array of the type 'id' => 'name' + */ + public function getTermsByVocabularyNameAsArray($slug) { + $vocabulary = \Taxonomy::getVocabularyBySlug($slug); + + $term_relations = $this->related()->where('vocabulary_id', $vocabulary->id)->get(); + + $data = []; + foreach ($term_relations as $term_relation) { + $data[$term_relation->term->id] = $term_relation->term->name; + } + + return $data; + } /** * Unlink the given term with the current model object From 52579a78497cedfdf012a54a1f28f29bbd5acc57 Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:57:23 -0500 Subject: [PATCH 07/20] support slug added --- src/Controllers/TaxonomyController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controllers/TaxonomyController.php b/src/Controllers/TaxonomyController.php index 27fd3b8..f792016 100644 --- a/src/Controllers/TaxonomyController.php +++ b/src/Controllers/TaxonomyController.php @@ -65,7 +65,7 @@ public function getCreate() { public function postStore(Request $request) { $this->validate($request, isset($this->vocabulary->rules_create) ? $this->vocabulary->rules_create : $this->vocabulary->rules); - Vocabulary::create(Input::only('name')); + Vocabulary::create(Input::only('name','slug')); return Redirect::to(action('\Devfactory\Taxonomy\Controllers\TaxonomyController@getIndex'))->with('success', 'Created'); @@ -97,7 +97,7 @@ public function putUpdate(Request $request, $id) { $this->validate($request, isset($this->vocabulary->rules_create) ? $this->vocabulary->rules_create : $this->vocabulary->rules); $vocabulary = $this->vocabulary->findOrFail($id); - $vocabulary->update(Input::only('name')); + $vocabulary->update(Input::only('name','slug')); return Redirect::to(action('\Devfactory\Taxonomy\Controllers\TaxonomyController@getIndex'))->with('success', 'Updated'); From 7cca383ff7d7b5cfa0023f5b9fdaaa5f090314ae Mon Sep 17 00:00:00 2001 From: isaactmp Date: Wed, 26 Feb 2020 08:59:56 -0500 Subject: [PATCH 08/20] support slug added --- src/views/vocabulary/create.blade.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/views/vocabulary/create.blade.php b/src/views/vocabulary/create.blade.php index 6dcf59c..e0dcae0 100644 --- a/src/views/vocabulary/create.blade.php +++ b/src/views/vocabulary/create.blade.php @@ -22,7 +22,12 @@ {!! $errors->has('name') ? Form::label('error', $errors->first('name'), array('class' => 'control-label')) : '' !!} {!! $errors->has('name') ? '' : '' !!} - +
+ {!! Form::label('slug', Lang::get('taxonomy::vocabulary.create.label.slug'), ['class' => 'control-label']) !!} + {!! Form::text('slug', NULL, ['class' => 'form-control']) !!} + {!! $errors->has('slug') ? Form::label('error', $errors->first('slug'), array('class' => 'control-label')) : '' !!} + {!! $errors->has('slug') ? '' : '' !!} +
- +
+ {!! Form::label('slug', Lang::get('taxonomy::vocabulary.create.label.slug'), ['class' => 'control-label']) !!} + {!! Form::text('slug', NULL, ['class' => 'form-control']) !!} + {!! $errors->has('slug') ? Form::label('error', $errors->first('slug'), array('class' => 'control-label')) : '' !!} + {!! $errors->has('slug') ? '' : '' !!} +