diff --git a/README.md b/README.md index 14842fa..2b3da0a 100644 --- a/README.md +++ b/README.md @@ -95,10 +95,12 @@ class Car extends \Eloquent { ## Usage -Creating a vocabulary: +Creating a vocabulary (by default, the separator is "-"): ```php -Taxonomy::createVocabulary('Cars'); +Taxonomy::createVocabulary('Cars'); // Taxonomy Object [Cars,cars] +Taxonomy::createVocabulary('Cars','cars-slug'); // Taxonomy Object [Cars,cars-slug] +Taxonomy::createVocabulary('Cars','Cars Slug','-'); // Taxonomy Object [Cars,cars-slug] ``` Retrieving a Vocabulary: @@ -106,6 +108,7 @@ Retrieving a Vocabulary: ```php $vocabulary = Taxonomy::getVocabulary(1); // Using ID $vocabulary = Taxonomy::getVocabularyByName('Cars'); // Using Name +$vocabulary = Taxonomy::getVocabularyBySlug('cars-slug'); // Using Slug ``` Deleting a Vocabulary: @@ -113,6 +116,7 @@ Deleting a Vocabulary: ```php Taxonomy::deleteVocabulary(1); // Using ID Taxonomy::deleteVocabularyByName('Cars'); // Using Name +Taxonomy::deleteVocabularyBySlug('cars-slug'); // Using Slug ``` Adding a Term to a vocabulary: 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.*", 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'); 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'; diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 1fb741e..52d2ccd 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -14,7 +14,7 @@ public function __construct(Vocabulary $vocabulary, Term $term) { $this->vocabulary = $vocabulary; $this->term = $term; } - + /** * Create a new Vocabulary with the given name * @@ -25,14 +25,42 @@ 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,$separator="-") { + if(is_null($slug)) + $slug=str_slug($name,$separator); + else + $slug=str_slug($slug,$separator); + 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]); + } + + /** + * Auto create slug + * + * @param string $separator + * The separator for blank spaces + * + * @return + * Return TRUE + */ + public function autoCreateSlugs($separator="-") { + + $vocabularies = $this->vocabulary->where('slug',null)->get(); - return $this->vocabulary->create(['name' => $name]); - } - + foreach($vocabularies as $vocabulary){ + $vocabulary->slug = str_slug($vocabulary->name,$separator); + $vocabulary->save(); + } + return true; + } + /** * Get a Vocabulary by ID * @@ -59,6 +87,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 * @@ -78,6 +118,24 @@ public function getVocabularyByNameAsArray($name) { return []; } + /** + * Get a Vocabulary by slug + * + * @param string $slug + * The name of the Vocabulary to fetch + * + * @return + * The Vocabulary Model object, otherwise NULL + */ + public function getVocabularyBySlugAsArray($slug) { + $vocabulary = $this->vocabulary->where('slug', $slug)->first(); + + if (!is_null($vocabulary)) { + return $vocabulary->terms->pluck('name', 'id')->toArray(); + } + + return []; + } /** * Get a Vocabulary by name as an options array for dropdowns * @@ -107,7 +165,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 * @@ -163,6 +249,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 * diff --git a/src/TaxonomyTrait.php b/src/TaxonomyTrait.php index 05cd07a..290ed0e 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 getTermsByVocabularySlugAsArray($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 diff --git a/src/lang/en/vocabulary.php b/src/lang/en/vocabulary.php index a948a28..a1afe0a 100644 --- a/src/lang/en/vocabulary.php +++ b/src/lang/en/vocabulary.php @@ -23,6 +23,7 @@ 'header' => 'Create a new Vocabulary', 'label' => array( 'name' => 'Name', + 'slug' => 'Slug', ), 'button' => array( 'create' => 'Create', @@ -39,4 +40,4 @@ ), ), 'vocabularies' => 'Vocabularies', -); \ No newline at end of file +); 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 +} 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') ? '' : '' !!} - +