Added tag filtering by category

This commit is contained in:
Marcin Kurczewski 2014-10-19 15:51:57 +02:00
parent 462708c1d8
commit 368ca1ae3f
4 changed files with 68 additions and 1 deletions

View file

@ -158,6 +158,13 @@ class TagDao extends AbstractDao implements ICrudDao
return; return;
} }
elseif ($requirement->getType() === TagFilter::REQUIREMENT_CATEGORY)
{
$sql = 'IFNULL(category, \'default\') = ?';
$requirement->setType($sql);
return parent::decorateQueryFromRequirement($query, $requirement);
}
parent::decorateQueryFromRequirement($query, $requirement); parent::decorateQueryFromRequirement($query, $requirement);
} }

View file

@ -9,6 +9,7 @@ class TagFilter extends BasicFilter implements IFilter
const ORDER_USAGE_COUNT = 'usages'; const ORDER_USAGE_COUNT = 'usages';
const REQUIREMENT_PARTIAL_TAG_NAME = 'partialTagName'; const REQUIREMENT_PARTIAL_TAG_NAME = 'partialTagName';
const REQUIREMENT_CATEGORY = 'category';
public function __construct() public function __construct()
{ {

View file

@ -26,6 +26,16 @@ class TagSearchParser extends AbstractSearchParser
protected function decorateFilterFromNamedToken(IFilter $filter, NamedSearchToken $namedToken) protected function decorateFilterFromNamedToken(IFilter $filter, NamedSearchToken $namedToken)
{ {
if ($namedToken->getKey() === 'category')
{
$this->addRequirementFromToken(
$filter,
$namedToken,
TagFilter::REQUIREMENT_CATEGORY,
self::ALLOW_COMPOSITE);
}
else
throw new NotSupportedException(); throw new NotSupportedException();
} }

View file

@ -0,0 +1,49 @@
<?php
namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\TagDao;
use Szurubooru\Entities\Tag;
use Szurubooru\SearchServices\Filters\TagFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\SearchServices\Result;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class TagDaoFilterTest extends AbstractDatabaseTestCase
{
public function testCategories()
{
$tag1 = $this->getTestTag('test 1');
$tag2 = $this->getTestTag('test 2');
$tag3 = $this->getTestTag('test 3');
$tag1->setCategory(null);
$tag2->setCategory('misc');
$tag3->setCategory('other');
$tagDao = $this->getTagDao();
$tagDao->save($tag1);
$tagDao->save($tag2);
$tagDao->save($tag3);
$searchFilter = new TagFilter();
$requirement = new Requirement();
$requirement->setType(TagFilter::REQUIREMENT_CATEGORY);
$requirement->setValue(new RequirementSingleValue('misc'));
$requirement->setNegated(true);
$searchFilter->addRequirement($requirement);
$result = $tagDao->findFiltered($searchFilter);
$this->assertEquals(2, $result->getTotalRecords());
$this->assertEntitiesEqual([$tag3, $tag1], array_values($result->getEntities()));
}
private function getTagDao()
{
return new TagDao($this->databaseConnection);
}
private function getTestTag($name)
{
$tag = new Tag();
$tag->setName($name);
$tag->setCreationTime(date('c'));
return $tag;
}
}