Skip to content

Commit

Permalink
Merge pull request #270 from Charlie-Lucas/master
Browse files Browse the repository at this point in the history
use a choice_tree form type for category-filter
  • Loading branch information
Leny BERNARD committed Nov 12, 2015
2 parents 2bee9ac + f3cfa54 commit 13fbfd4
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 41 deletions.
84 changes: 63 additions & 21 deletions Bundle/BlogBundle/Filter/CategoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Victoire\Bundle\BlogBundle\Entity\Category;
use Victoire\Bundle\FilterBundle\Filter\BaseFilter;

Expand All @@ -15,15 +16,18 @@ class CategoryFilter extends BaseFilter
{
protected $em;
protected $request;
protected $translator;

/**
* @param EntityManager $em
* @param Request $request
* @param EntityManager $em
* @param \Victoire\Bundle\FilterBundle\Filter\Request $request
* @param TranslatorInterface $translator
*/
public function __construct(EntityManager $em, $request)
public function __construct(EntityManager $em, $request, TranslatorInterface $translator)
{
$this->em = $em;
$this->request = $request;
$this->translator = $translator;
}

/**
Expand Down Expand Up @@ -57,10 +61,10 @@ public function buildQuery(QueryBuilder $qb, array $parameters)
foreach ($childrenArray as $index => $category) {
$parameter = ':category'.$index;
$subquery = $repository->createQueryBuilder('article_'.$index)
->join('article_'.$index.'.category', 'category_'.$index)
->where('category_'.$index.' = '.$parameter);
->join('article_'.$index.'.category', 'category_'.$index)
->where('category_'.$index.' = '.$parameter);
$qb->andWhere($qb->expr()->in('main_item', $subquery->getDql()))
->setParameter($parameter, $category);
->setParameter($parameter, $category);
}
} else {
$qb = $qb
Expand Down Expand Up @@ -112,16 +116,17 @@ public function buildForm(FormBuilderInterface $builder, array $options)
break;
}
//filter categoriess with right articles
$categoryQb->filterByArticles($articleQb->getInstance('article'));
$categories = $categoryQb->getInstance('c_category')->getQuery()->getResult();
//the blank value

$categoriesChoices = [];

foreach ($categories as $category) {
$categoriesChoices[$category->getId()] = $category->getTitle();
}

$categories = $categoryQb->filterByArticles(
$articleQb->getInstance()
)
->orderByHierarchy()
->getInstance('c_category')
->getQuery()
->getResult();
//build the tree for categories
$tree = $categoryQb->buildTree($categoryQb->getNodesHierarchy());
$categoryQb->clearInstance();
$articleQb->clearInstance();
$data = null;
if ($this->request->query->has('filter') && array_key_exists('category_filter', $this->request->query->get('filter'))) {
if ($options['multiple']) {
Expand All @@ -133,21 +138,58 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$data = $this->request->query->get('filter')['category_filter']['tags'];
}
}

$builder
->add(
'category', 'choice', [
'category', 'infinite_form_choice_tree', [
'label' => false,
'choices' => $categoriesChoices,
'choices' => $this->buildHierarchy($tree, $categories),
'required' => false,
'expanded' => true,
'empty_value' => 'Tous',
'multiple' => $options['multiple'],
'empty_value' => $this->translator->trans('blog.category_filter.empty_value.label'),
'multiple' => false,
'data' => $data,
]
);
}

/**
* @param $categories
* @param $validCategories
*
* @return array
*/
public function buildHierarchy($categories, $validCategories)
{
$hierarchy = [];
foreach ($categories as $category) {
$isValid = false;
$categoryHierarchy = [];
//if we have children we try to build their hierarchy
if (count($children = $category['__children'])) {
$categoryHierarchy = $this->buildHierarchy($children, $validCategories);
}
// try to match with listed categories
foreach ($validCategories as $key => $validCategory) {
if ($validCategory->getId() == $category['id']) {
$isValid = true;
//unset the valid category
unset($validCategories[$key]);
}
}
// if the current category is valid or if a children is valid
if ($isValid || count($categoryHierarchy) > 0) {
//add a node
$node = [];
$node['label'] = $category['title'];
$node['value'] = $category['id'];
$node['choice_list'] = $categoryHierarchy;
$hierarchy[] = $node;
}
}

return $hierarchy;
}

/**
* Get the filters.
*
Expand Down
23 changes: 23 additions & 0 deletions Bundle/BlogBundle/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function getOrderedCategories($blogId)
return $this;
}

/**
* @return $this
*/
public function getAll()
{
$this->qb = $this->getInstance('c_category')
Expand All @@ -36,6 +39,26 @@ public function getAll()
return $this;
}

/**
* Order categories by tree hierarchy.
*
* @return $this
*/
public function orderByHierarchy()
{
$this->qb
->addOrderBy('c_category.root')
->addOrderBy('c_category.lft')
->addOrderBy('c_category.lvl');

return $this;
}

/**
* @param $articles
*
* @return $this
*/
public function filterByArticles($articles)
{
$this->qb
Expand Down
1 change: 1 addition & 0 deletions Bundle/BlogBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ services:
arguments:
- "@doctrine.orm.entity_manager"
- "@request="
- "@translator"
tags:
- { name: form.type, alias: category_filter }
- { name: victoire_core.filter }
Expand Down
4 changes: 4 additions & 0 deletions Bundle/BlogBundle/Resources/translations/messages.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<source>blog.date_filter.label</source>
<target>Date</target>
</trans-unit>
<trans-unit id="4" resname="blog.category_filter.empty_value.label">
<source>blog.category_filter.empty_value.label</source>
<target>All</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions Bundle/BlogBundle/Resources/translations/messages.es.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<source>blog.date_filter.label</source>
<target>Fecha</target>
</trans-unit>
<trans-unit id="4" resname="blog.category_filter.empty_value.label">
<source>blog.category_filter.empty_value.label</source>
<target>Todos</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions Bundle/BlogBundle/Resources/translations/messages.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<source>blog.date_filter.label</source>
<target>Date</target>
</trans-unit>
<trans-unit id="4" resname="blog.category_filter.empty_value.label">
<source>blog.category_filter.empty_value.label</source>
<target>Tous</target>
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"friendsofvictoire/text-widget": "~1.3.0",
"friendsofvictoire/button-widget": "~1.3.0",
"incenteev/composer-parameter-handler" : "~2.0",
"infinite-networks/form-bundle": "dev-master",
"jms/di-extra-bundle" : "1.4.*@dev",
"jms/serializer-bundle": "^1.0",
"jms/translation-bundle" : "1.1.*@dev",
Expand Down Expand Up @@ -134,6 +135,10 @@
{
"type": "vcs",
"url": "https://github.com/lenybernard/mink"
},
{
"type": "vcs",
"url": "https://github.com/Charlie-Lucas/InfiniteFormBundle"
}
],
"extra": {
Expand Down
Loading

0 comments on commit 13fbfd4

Please sign in to comment.