-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #983 from MadeWilson/feature/2.3-blog-improvement
Fix/filter bundle
- Loading branch information
Showing
7 changed files
with
199 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
Bundle/FilterBundle/Domain/FilterFormFieldQueryHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Victoire\Bundle\FilterBundle\Domain; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ORM\EntityManager; | ||
use Victoire\Bundle\QueryBundle\Helper\QueryHelper; | ||
use Victoire\Bundle\WidgetBundle\Entity\Widget; | ||
|
||
/** | ||
* Class FilterFormFieldQueryHandler. | ||
*/ | ||
class FilterFormFieldQueryHandler | ||
{ | ||
/** | ||
* @var QueryHelper | ||
*/ | ||
private $queryHelper; | ||
/** | ||
* @var EntityManager | ||
*/ | ||
private $entityManager; | ||
|
||
/** | ||
* FilterFormFieldQueryHandler constructor. | ||
* | ||
* @param QueryHelper $queryHelper | ||
* @param EntityManager $entityManager | ||
*/ | ||
public function __construct( | ||
QueryHelper $queryHelper, | ||
EntityManager $entityManager | ||
) { | ||
$this->queryHelper = $queryHelper; | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
* @param Widget $widgetFilter | ||
* @param $entity | ||
* | ||
* @return array | ||
*/ | ||
public function handle(Widget $widgetFilter, $entity) | ||
{ | ||
$widgetListing = $widgetFilter->getListing(); | ||
|
||
/* first we generate the query for the listing widget */ | ||
$queryBuilder = $this->queryHelper->getQueryBuilder($widgetListing, $this->entityManager); | ||
|
||
$mode = $widgetListing->getMode(); | ||
|
||
if ($mode == 'query') { | ||
/* if listing widget is in query mode we had a subquery with here parameter */ | ||
$queryBuilder = $this->queryHelper->buildWithSubQuery($widgetListing, $queryBuilder, $this->entityManager); | ||
} | ||
|
||
$subQuery = $queryBuilder->getQuery(); | ||
$subQueryParameters = $queryBuilder->getParameters(); | ||
|
||
$filterEntityRepo = $this->entityManager->getRepository($entity); | ||
|
||
$alias = explode('\\', $entity); | ||
$alias = end($alias); | ||
|
||
/* we build a new query for the filter entity */ | ||
$queryBuilder = $filterEntityRepo->createQueryBuilder($alias); | ||
/* and give the query for the listing entity has subquery */ | ||
$queryBuilder->andWhere($queryBuilder->expr()->in($alias, $subQuery->getDQL())); | ||
|
||
$parameters = new ArrayCollection( | ||
array_merge($subQueryParameters->toArray(), $queryBuilder->getParameters()->toArray()) | ||
); | ||
$queryBuilder->setParameters($parameters); | ||
|
||
$entities = $queryBuilder->getQuery()->getResult(); | ||
|
||
return $entities; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
Allows to filter any list's results | ||
|
||
**MORE DETAILS SOON** | ||
To create a new filter you have to build a Filter class extending the abstract Victoire\FilterBundle\Filter\BaseFilter class | ||
|
||
```php | ||
<?php | ||
|
||
namespace AppBundle\Filter; | ||
|
||
use Victoire\FilterBundle\Filter\BaseFilter; | ||
|
||
class TagFilter extends BaseFilter { | ||
|
||
/* will be the entry point to generate the result query of a filter */ | ||
public function buildQuery(){} | ||
|
||
/* BaseFilter is an extension of symfony AbstractType so you can generate a form as usual */ | ||
public function buildForm(){} | ||
|
||
/* Mandatory since when a filter is apply the WidgetListingContentResolver will identify the good filter with this method */ | ||
public function getName(){} | ||
|
||
/* Method used in the WidgetListingContentResolver to recover the selected entity */ | ||
public function getFilters(){} | ||
} | ||
``` | ||
And you have to declare it in your services | ||
|
||
```yaml | ||
victoire_blog.tag_filters.form.type: | ||
class: AppBundle\Filter\TagFilter | ||
parent: victoire_filter_bundle.abstract_base_filter | ||
tags: | ||
- { name: form.type } | ||
- { name: victoire_core.filter } | ||
``` | ||
If your list has been created in query mode you can use the FilterFormFieldQueryHandler service | ||
wich is accessible in the Base Filter to build your query. | ||
```php | ||
/* he takes the WidgetFilter and the class name of the filtred entity and return an array of that entities */ | ||
$this->filterQueryHandler->handle($options['widget'], Tag::class); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
services: | ||
victoire_core.filter_chain: | ||
class: Victoire\Bundle\FilterBundle\Filter\Chain\FilterChain | ||
|
||
############################# | ||
######## DOMAIN | ||
############################ | ||
|
||
victoire_filter_bundle.abstract_base_filter: | ||
class: Victoire\Bundle\FilterBundle\Filter\BaseFilter | ||
abstract: true | ||
arguments: | ||
- "@doctrine.orm.entity_manager" | ||
- "@request_stack" | ||
- "@victoire_filter_bundle.filter_form_field_query.handler" | ||
|
||
victoire_filter_bundle.filter_form_field_query.handler: | ||
class: Victoire\Bundle\FilterBundle\Domain\FilterFormFieldQueryHandler | ||
arguments: | ||
- "@victoire_query.query_helper" | ||
- "@doctrine.orm.entity_manager" |