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

support slug taxonomy #29

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,28 @@ 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:

```php
$vocabulary = Taxonomy::getVocabulary(1); // Using ID
$vocabulary = Taxonomy::getVocabularyByName('Cars'); // Using Name
$vocabulary = Taxonomy::getVocabularyBySlug('cars-slug'); // Using Slug
```

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:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Vocabulary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Vocabulary extends \Eloquent {

protected $fillable = [
'name',
'name', 'slug',
];

protected $table = 'vocabularies';
Expand Down
114 changes: 109 additions & 5 deletions src/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
*
Expand All @@ -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
*
Expand All @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
39 changes: 39 additions & 0 deletions src/TaxonomyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/lang/en/vocabulary.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'header' => 'Create a new Vocabulary',
'label' => array(
'name' => 'Name',
'slug' => 'Slug',
),
'button' => array(
'create' => 'Create',
Expand All @@ -39,4 +40,4 @@
),
),
'vocabularies' => 'Vocabularies',
);
);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function up()
Schema::create('vocabularies', function($table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
Expand All @@ -28,4 +29,4 @@ public function down()
Schema::dropIfExists('vocabularies');
}

}
}
7 changes: 6 additions & 1 deletion src/views/vocabulary/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
{!! $errors->has('name') ? Form::label('error', $errors->first('name'), array('class' => 'control-label')) : '' !!}
{!! $errors->has('name') ? '<span class="glyphicon glyphicon-remove form-control-feedback"></span>' : '' !!}
</div>

<div class="form-group{!! $errors->has('slug') ? ' has-error has-feedback' : '' !!}">
{!! 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') ? '<span class="glyphicon glyphicon-remove form-control-feedback"></span>' : '' !!}
</div>
</div>

<div class="box-footer">
Expand Down
7 changes: 6 additions & 1 deletion src/views/vocabulary/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
{!! $errors->has('name') ? Form::label('error', $errors->first('name'), array('class' => 'control-label')) : '' !!}
{!! $errors->has('name') ? '<span class="glyphicon glyphicon-remove form-control-feedback"></span>' : '' !!}
</div>

<div class="form-group{!! $errors->has('slug') ? ' has-error has-feedback' : '' !!}">
{!! 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') ? '<span class="glyphicon glyphicon-remove form-control-feedback"></span>' : '' !!}
</div>
</div>

<div class="box-footer">
Expand Down