Skip to content

Commit

Permalink
Allow searching by translated slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rchavik committed Nov 5, 2020
1 parent 1a94b8e commit ec2053a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 27 deletions.
1 change: 1 addition & 0 deletions Core/src/Croogo.php
Expand Up @@ -10,6 +10,7 @@
use Cake\Http\ServerRequest;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Croogo\Core\Routing\Router;

/**
* Croogo
Expand Down
34 changes: 34 additions & 0 deletions Core/src/Model/Behavior/SluggableBehavior.php
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace Croogo\Core\Model\Behavior;

use Cake\ORM\Behavior;
use Cake\ORM\Query;

class SluggableBehavior extends Behavior
{

protected $_defaultConfig = [
'slugField' => 'slug',
];

public function findBySlug(Query $query, array $options = [])
{
$slugField = $this->_config['slugField'];
if ($this->_table->behaviors()->has('Translate')) {
$query->where([
'OR' => [
$this->_table->aliasField('slug') . ' LIKE'=> '%' . $options['search']['slug'] . '%',
$this->_table->translationField($slugField) . ' LIKE'=> '%' . $options['search']['slug'] . '%',
],
]);
} else {
$query->where([
$this->_table->aliasField($slugField) . ' LIKE' => '%' . $options['search']['slug'] . '%',
]);
}
return $query;
}

}
7 changes: 3 additions & 4 deletions Nodes/src/Model/Table/NodesTable.php
Expand Up @@ -27,6 +27,7 @@ public function initialize(array $config): void
],
]);
$this->addBehavior('Croogo/Core.Publishable');
$this->addBehavior('Croogo/Core.Sluggable');
$this->addBehavior('Croogo/Core.Url', [
'url' => [
'plugin' => 'Croogo/Nodes',
Expand Down Expand Up @@ -82,10 +83,8 @@ public function searchManager()
'before' => true,
'after' => true
])
->add('slug', 'Search.Like', [
'fields' => $this->aliasField('slug'),
'before' => true,
'after' => true
->add('slug', 'Search.Finder', [
'finder' => 'bySlug',
])
->add('type', 'Search.Like', [
'fields' => $this->aliasField('type'),
Expand Down
15 changes: 9 additions & 6 deletions Taxonomy/src/Model/Behavior/TaxonomizableBehavior.php
Expand Up @@ -4,6 +4,7 @@
namespace Croogo\Taxonomy\Model\Behavior;

use ArrayObject;
use Cake\Core\Plugin;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\EventInterface;
use Cake\I18n\I18n;
Expand Down Expand Up @@ -325,9 +326,10 @@ public function findWithTerm(Query $query, array $options)
$locale = I18n::getLocale();
$cacheKeys = ['term', $locale, $term];
$cacheKey = implode('_', $cacheKeys);
$entity = $this->_table->Taxonomies->Terms->find()
->where([
'Terms.slug' => $term
$entity = $this->_table->Taxonomies->Terms->find('bySlug', [
'options' => [
'search' => $term,
],
])
->cache($cacheKey, 'nodes_term')
->first();
Expand Down Expand Up @@ -365,9 +367,10 @@ public function findWithVocabulary(Query $query, array $options)
$locale = I18n::getLocale();
$cacheKeys = ['term', $locale, $vocab];
$cacheKey = implode('_', $cacheKeys);
$entity = $this->_table->Taxonomies->Vocabularies->find()
->where([
'Vocabularies.alias' => $vocab
$entity = $this->_table->Taxonomies->Vocabularies->find('bySlug', [
'options' => [
'slug' => $vocab,
],
])
->cache($cacheKey, 'croogo_vocabularies')
->first();
Expand Down
21 changes: 4 additions & 17 deletions Taxonomy/src/Model/Table/TermsTable.php
Expand Up @@ -35,6 +35,7 @@ public function initialize(array $config): void
$this->addBehavior('Search.Search');
$this->addBehavior('Timestamp');
$this->addBehavior('Croogo/Core.Trackable');
$this->addBehavior('Croogo/Core.Sluggable');

$this->belongsToMany('Croogo/Taxonomy.Vocabularies', [
'through' => 'Croogo/Taxonomy.Taxonomies',
Expand All @@ -53,7 +54,9 @@ public function initialize(array $config): void
});
},
])
->value('slug');
->add('slug', 'Search.Finder', [
'finder' => 'bySlug',
]);
}

protected function _initializeSchema(TableSchemaInterface $table): TableSchemaInterface
Expand Down Expand Up @@ -212,22 +215,6 @@ public function findByVocabulary(Query $query, array $options)
return $query;
}

public function findBySlug(Query $query, array $options)
{
$defaults = ['slug' => null];
$options += $defaults;

if ($this->hasBehavior('Translate')) {
$conditions['OR'] = [
$this->aliasField('slug') => $options['slug'],
$this->translationField('slug') => $options['slug'],
];
} else {
$conditions[$this->aliasField('slug')] = $options['slug'];
}

return $query->where($conditions);
}

/**
* Save new/updated term data
Expand Down
3 changes: 3 additions & 0 deletions Taxonomy/src/Model/Table/VocabulariesTable.php
Expand Up @@ -21,6 +21,9 @@ public function initialize(array $config): void

$this->addBehavior('Timestamp');
$this->addBehavior('Search.Search');
$this->addBehavior('Croogo/Core.Sluggable', [
'slugField' => 'alias',
]);
$this->addBehavior('Croogo/Core.Trackable');
$this->addBehavior('Croogo/Core.Cached', [
'groups' => ['taxonomy']
Expand Down

0 comments on commit ec2053a

Please sign in to comment.