mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-05-18 13:10:08 -07:00
Add Search Repositories and class
This commit is contained in:
parent
c10a2d8f17
commit
5c2421d5fb
@ -1,52 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace AppBundle\Form\Filter\Model;
|
|
||||||
|
|
||||||
use AppBundle\Entity\LDraw\Category;
|
|
||||||
use AppBundle\Repository\LDraw\CategoryRepository;
|
|
||||||
use Lexik\Bundle\FormFilterBundle\Filter\Form\Type as Filters;
|
|
||||||
use Symfony\Component\Form\AbstractType;
|
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
||||||
|
|
||||||
class CategoryFilterType extends AbstractType
|
|
||||||
{
|
|
||||||
private $categoryRepository;
|
|
||||||
|
|
||||||
public function __construct(CategoryRepository $categoryRepository)
|
|
||||||
{
|
|
||||||
$this->categoryRepository = $categoryRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
|
||||||
{
|
|
||||||
$builder->add('id', Filters\ChoiceFilterType::class, [
|
|
||||||
'choices' => $this->categoryRepository->findAll(),
|
|
||||||
'choice_label' => 'name',
|
|
||||||
'label' => 'filter.model.category',
|
|
||||||
// 'attr' => [
|
|
||||||
// 'class' => 'ui dropdown search selection'
|
|
||||||
// ]
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getParent()
|
|
||||||
{
|
|
||||||
return Filters\SharedableFilterType::class; // this allow us to use the "add_shared" option
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getBlockPrefix()
|
|
||||||
{
|
|
||||||
return 'category_filter';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setDefaultOptions(OptionsResolver $resolver)
|
|
||||||
{
|
|
||||||
$resolver->setDefaults([
|
|
||||||
'data_class' => Category::class,
|
|
||||||
'csrf_protection' => false,
|
|
||||||
'validation_groups' => ['filtering'], // avoid NotBlank() constraint-related message
|
|
||||||
'method' => 'GET',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
37
src/AppBundle/Form/NumberRangeType.php
Normal file
37
src/AppBundle/Form/NumberRangeType.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Form;
|
||||||
|
|
||||||
|
use AppBundle\Model\NumberRange;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
class NumberRangeType extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$attr = $options['attr'];
|
||||||
|
|
||||||
|
$builder
|
||||||
|
->add('from', HiddenType::class, [
|
||||||
|
'required' => false,
|
||||||
|
'data' => $attr['min'],
|
||||||
|
'empty_data' => $attr['min'],
|
||||||
|
])
|
||||||
|
->add('to', HiddenType::class, [
|
||||||
|
'required' => false,
|
||||||
|
'data' => $attr['max'],
|
||||||
|
'empty_data' => $attr['max'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'csrf_protection' => false,
|
||||||
|
'data_class' => NumberRange::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
60
src/AppBundle/Form/Search/ModelSearchType.php
Normal file
60
src/AppBundle/Form/Search/ModelSearchType.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Form\Search;
|
||||||
|
|
||||||
|
use AppBundle\Entity\LDraw\Category;
|
||||||
|
use AppBundle\Model\ModelSearch;
|
||||||
|
use AppBundle\Repository\LDraw\CategoryRepository;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
class ModelSearchType extends AbstractType
|
||||||
|
{
|
||||||
|
/** @var CategoryRepository */
|
||||||
|
private $categoryRepository;
|
||||||
|
|
||||||
|
public function __construct(CategoryRepository $categoryRepository)
|
||||||
|
{
|
||||||
|
$this->categoryRepository = $categoryRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder
|
||||||
|
->add('query', TextType::class, [
|
||||||
|
'required' => false,
|
||||||
|
'label' => 'model.form.search',
|
||||||
|
'attr' => [
|
||||||
|
'placeholder' => 'model.form.search',
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->add('category', ChoiceType::class, [
|
||||||
|
'label' => 'model.form.category',
|
||||||
|
'choices' => $this->categoryRepository->findAll(),
|
||||||
|
'placeholder' => 'model.form.category.all',
|
||||||
|
'choice_label' => 'name',
|
||||||
|
'choice_value' => 'id',
|
||||||
|
'required' => false,
|
||||||
|
'attr' => [
|
||||||
|
// 'class' => 'ui dropdown search selection'
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'csrf_protection' => false,
|
||||||
|
'data_class' => ModelSearch::class,
|
||||||
|
'method' => 'GET',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'model_search_type';
|
||||||
|
}
|
||||||
|
}
|
85
src/AppBundle/Form/Search/SetSearchType.php
Normal file
85
src/AppBundle/Form/Search/SetSearchType.php
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Form\Search;
|
||||||
|
|
||||||
|
use AppBundle\Entity\Rebrickable\Theme;
|
||||||
|
use AppBundle\Form\NumberRangeType;
|
||||||
|
use AppBundle\Model\SetSearch;
|
||||||
|
use AppBundle\Repository\Rebrickable\SetRepository;
|
||||||
|
use AppBundle\Repository\Rebrickable\ThemeRepository;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
class SetSearchType extends AbstractType
|
||||||
|
{
|
||||||
|
/** @var ThemeRepository */
|
||||||
|
private $themeRepository;
|
||||||
|
|
||||||
|
/** @var SetRepository */
|
||||||
|
private $setRepository;
|
||||||
|
|
||||||
|
public function __construct(ThemeRepository $themeRepository, SetRepository $setRepository)
|
||||||
|
{
|
||||||
|
$this->themeRepository = $themeRepository;
|
||||||
|
$this->setRepository = $setRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder
|
||||||
|
->add('query', TextType::class, [
|
||||||
|
'required' => false,
|
||||||
|
'label' => 'set.form.search',
|
||||||
|
'attr' => [
|
||||||
|
'placeholder' => 'set.form.search',
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->add('year', NumberRangeType::class, [
|
||||||
|
'label' => 'set.form.year',
|
||||||
|
'attr' => [
|
||||||
|
'step' => 1,
|
||||||
|
'class' => 'slider',
|
||||||
|
'min' => $this->setRepository->getMinYear(),
|
||||||
|
'max' => $this->setRepository->getMaxYear(),
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->add('partCount', NumberRangeType::class, [
|
||||||
|
'label' => 'set.form.partCount',
|
||||||
|
'attr' => [
|
||||||
|
'class' => 'slider',
|
||||||
|
'step' => 50,
|
||||||
|
'min' => 0,
|
||||||
|
'max' => $this->setRepository->getMaxPartCount(),
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->add('theme', ChoiceType::class, [
|
||||||
|
'label' => 'set.form.theme',
|
||||||
|
'choices' => $this->themeRepository->findAll(),
|
||||||
|
'choice_label' => 'fullName',
|
||||||
|
'choice_value' => 'id',
|
||||||
|
'placeholder' => 'set.form.theme.all',
|
||||||
|
'required' => false,
|
||||||
|
'attr' => [
|
||||||
|
'placeholder' => 'set.form.theme.placeholder',
|
||||||
|
'class' => 'select2 dropdown ui',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'csrf_protection' => false,
|
||||||
|
'data_class' => SetSearch::class,
|
||||||
|
'method' => 'GET'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'set_search_type';
|
||||||
|
}
|
||||||
|
}
|
46
src/AppBundle/Model/ModelSearch.php
Normal file
46
src/AppBundle/Model/ModelSearch.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Model;
|
||||||
|
|
||||||
|
use AppBundle\Entity\LDraw\Category;
|
||||||
|
|
||||||
|
class ModelSearch
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
|
protected $query;
|
||||||
|
|
||||||
|
/** @var Category */
|
||||||
|
protected $category;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getQuery()
|
||||||
|
{
|
||||||
|
return $this->query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
*/
|
||||||
|
public function setQuery($query)
|
||||||
|
{
|
||||||
|
$this->query = $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Category
|
||||||
|
*/
|
||||||
|
public function getCategory()
|
||||||
|
{
|
||||||
|
return $this->category;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Category $category
|
||||||
|
*/
|
||||||
|
public function setCategory($category)
|
||||||
|
{
|
||||||
|
$this->category = $category;
|
||||||
|
}
|
||||||
|
}
|
44
src/AppBundle/Model/NumberRange.php
Normal file
44
src/AppBundle/Model/NumberRange.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Model;
|
||||||
|
|
||||||
|
class NumberRange
|
||||||
|
{
|
||||||
|
/** @var int */
|
||||||
|
private $from;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $to;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getFrom()
|
||||||
|
{
|
||||||
|
return $this->from;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $from
|
||||||
|
*/
|
||||||
|
public function setFrom($from)
|
||||||
|
{
|
||||||
|
$this->from = $from;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getTo()
|
||||||
|
{
|
||||||
|
return $this->to;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $to
|
||||||
|
*/
|
||||||
|
public function setTo($to)
|
||||||
|
{
|
||||||
|
$this->to = $to;
|
||||||
|
}
|
||||||
|
}
|
84
src/AppBundle/Model/SetSearch.php
Normal file
84
src/AppBundle/Model/SetSearch.php
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Model;
|
||||||
|
|
||||||
|
use AppBundle\Entity\Rebrickable\Theme;
|
||||||
|
|
||||||
|
class SetSearch
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
|
protected $query;
|
||||||
|
|
||||||
|
/** @var NumberRange */
|
||||||
|
protected $year;
|
||||||
|
|
||||||
|
/** @var NumberRange */
|
||||||
|
protected $partCount;
|
||||||
|
|
||||||
|
/** @var Theme */
|
||||||
|
protected $theme;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getQuery()
|
||||||
|
{
|
||||||
|
return $this->query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
*/
|
||||||
|
public function setQuery($query)
|
||||||
|
{
|
||||||
|
$this->query = $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return NumberRange
|
||||||
|
*/
|
||||||
|
public function getYear()
|
||||||
|
{
|
||||||
|
return $this->year;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NumberRange $year
|
||||||
|
*/
|
||||||
|
public function setYear($year)
|
||||||
|
{
|
||||||
|
$this->year = $year;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return NumberRange
|
||||||
|
*/
|
||||||
|
public function getPartCount()
|
||||||
|
{
|
||||||
|
return $this->partCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param NumberRange $partCount
|
||||||
|
*/
|
||||||
|
public function setPartCount($partCount)
|
||||||
|
{
|
||||||
|
$this->partCount = $partCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Theme
|
||||||
|
*/
|
||||||
|
public function getTheme()
|
||||||
|
{
|
||||||
|
return $this->theme;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Theme $theme
|
||||||
|
*/
|
||||||
|
public function setTheme($theme)
|
||||||
|
{
|
||||||
|
$this->theme = $theme;
|
||||||
|
}
|
||||||
|
}
|
40
src/AppBundle/Repository/Search/ModelRepository.php
Normal file
40
src/AppBundle/Repository/Search/ModelRepository.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Repository\Search;
|
||||||
|
|
||||||
|
use AppBundle\Model\ModelSearch;
|
||||||
|
use FOS\ElasticaBundle\Repository;
|
||||||
|
|
||||||
|
class ModelRepository extends Repository
|
||||||
|
{
|
||||||
|
public function getSearchQuery(ModelSearch $modelSearch) {
|
||||||
|
$boolQuery = new \Elastica\Query\BoolQuery();
|
||||||
|
|
||||||
|
if ($searchQuery = $modelSearch->getQuery()) {
|
||||||
|
$query = new \Elastica\Query\MultiMatch();
|
||||||
|
|
||||||
|
$query->setFields(['name', 'id', 'aliases.id', 'keywords.name']);
|
||||||
|
$query->setQuery($searchQuery);
|
||||||
|
$query->setFuzziness(0.7);
|
||||||
|
$query->setMinimumShouldMatch('80%');
|
||||||
|
} else {
|
||||||
|
$query = new \Elastica\Query\MatchAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
$boolQuery->addMust($query);
|
||||||
|
|
||||||
|
if ($modelSearch->getCategory()) {
|
||||||
|
$categoryQuery = new \Elastica\Query\Match();
|
||||||
|
$categoryQuery->setField('category.id', $modelSearch->getCategory()->getId());
|
||||||
|
$boolQuery->addFilter($categoryQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $boolQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function search(ModelSearch $modelSearch)
|
||||||
|
{
|
||||||
|
$query = $this->getSearchQuery($modelSearch);
|
||||||
|
return $this->find($query, 500);
|
||||||
|
}
|
||||||
|
}
|
64
src/AppBundle/Repository/Search/SetRepository.php
Normal file
64
src/AppBundle/Repository/Search/SetRepository.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Repository\Search;
|
||||||
|
|
||||||
|
use AppBundle\Model\SetSearch;
|
||||||
|
use Elastica\Query\BoolQuery;
|
||||||
|
use Elastica\Query\Match;
|
||||||
|
use Elastica\Query\MultiMatch;
|
||||||
|
use Elastica\Query\Range;
|
||||||
|
use FOS\ElasticaBundle\Repository;
|
||||||
|
|
||||||
|
class SetRepository extends Repository
|
||||||
|
{
|
||||||
|
public function getSearchQuery(SetSearch $setSearch) {
|
||||||
|
$boolQuery = new BoolQuery();
|
||||||
|
|
||||||
|
if ($searchQuery = $setSearch->getQuery()) {
|
||||||
|
$query = new MultiMatch();
|
||||||
|
|
||||||
|
$query->setFields(['name', 'id']);
|
||||||
|
$query->setQuery($searchQuery);
|
||||||
|
$query->setFuzziness(0.7);
|
||||||
|
$query->setMinimumShouldMatch('80%');
|
||||||
|
} else {
|
||||||
|
$query = new \Elastica\Query\MatchAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
$boolQuery->addMust($query);
|
||||||
|
|
||||||
|
if ($setSearch->getTheme()) {
|
||||||
|
$themeQuery = new Match();
|
||||||
|
$themeQuery->setField('theme.id', $setSearch->getTheme()->getId());
|
||||||
|
$boolQuery->addFilter($themeQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($setSearch->getPartCount()) {
|
||||||
|
$range = new Range();
|
||||||
|
$range->addField('partCount', [
|
||||||
|
'gte' => $setSearch->getPartCount()->getFrom(),
|
||||||
|
'lte' => $setSearch->getPartCount()->getTo(),
|
||||||
|
]);
|
||||||
|
$boolQuery->addFilter($range);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($setSearch->getYear()) {
|
||||||
|
$range = new Range();
|
||||||
|
$range->addField('year', [
|
||||||
|
'gte' => $setSearch->getYear()->getFrom(),
|
||||||
|
'lte' => $setSearch->getYear()->getTo(),
|
||||||
|
]);
|
||||||
|
$boolQuery->addFilter($range);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $boolQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function search(SetSearch $setSearch)
|
||||||
|
{
|
||||||
|
$query = $this->getSearchQuery($setSearch);
|
||||||
|
|
||||||
|
return $this->find($query,500);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user