123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Datagrid;
- use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Filter\FilterInterface;
- use Symfony\Component\Form\CallbackTransformer;
- use Symfony\Component\Form\Exception\UnexpectedTypeException;
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\Form\FormInterface;
- /**
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class Datagrid implements DatagridInterface
- {
- /**
- * The filter instances.
- *
- * @var array
- */
- protected $filters = array();
- /**
- * @var array
- */
- protected $values;
- /**
- * @var FieldDescriptionCollection
- */
- protected $columns;
- /**
- * @var PagerInterface
- */
- protected $pager;
- /**
- * @var bool
- */
- protected $bound = false;
- /**
- * @var ProxyQueryInterface
- */
- protected $query;
- /**
- * @var FormBuilderInterface
- */
- protected $formBuilder;
- /**
- * @var FormInterface
- */
- protected $form;
- /**
- * @var array
- */
- protected $results;
- /**
- * @param ProxyQueryInterface $query
- * @param FieldDescriptionCollection $columns
- * @param PagerInterface $pager
- * @param FormBuilderInterface $formBuilder
- * @param array $values
- */
- public function __construct(ProxyQueryInterface $query, FieldDescriptionCollection $columns, PagerInterface $pager, FormBuilderInterface $formBuilder, array $values = array())
- {
- $this->pager = $pager;
- $this->query = $query;
- $this->values = $values;
- $this->columns = $columns;
- $this->formBuilder = $formBuilder;
- }
- /**
- * {@inheritdoc}
- */
- public function getPager()
- {
- return $this->pager;
- }
- /**
- * {@inheritdoc}
- */
- public function getResults()
- {
- $this->buildPager();
- if (!$this->results) {
- $this->results = $this->pager->getResults();
- }
- return $this->results;
- }
- /**
- * {@inheritdoc}
- */
- public function buildPager()
- {
- if ($this->bound) {
- return;
- }
- foreach ($this->getFilters() as $name => $filter) {
- list($type, $options) = $filter->getRenderSettings();
- $this->formBuilder->add($filter->getFormName(), $type, $options);
- }
- // NEXT_MAJOR: Remove BC trick when bumping Symfony requirement to 2.8+
- $hiddenType = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
- ? 'Symfony\Component\Form\Extension\Core\Type\HiddenType'
- : 'hidden';
- $this->formBuilder->add('_sort_by', $hiddenType);
- $this->formBuilder->get('_sort_by')->addViewTransformer(new CallbackTransformer(
- function ($value) {
- return $value;
- },
- function ($value) {
- return $value instanceof FieldDescriptionInterface ? $value->getName() : $value;
- }
- ));
- $this->formBuilder->add('_sort_order', $hiddenType);
- $this->formBuilder->add('_page', $hiddenType);
- $this->formBuilder->add('_per_page', $hiddenType);
- $this->form = $this->formBuilder->getForm();
- $this->form->submit($this->values);
- $data = $this->form->getData();
- foreach ($this->getFilters() as $name => $filter) {
- $this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null;
- $filter->apply($this->query, $data[$filter->getFormName()]);
- }
- if (isset($this->values['_sort_by'])) {
- if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
- throw new UnexpectedTypeException($this->values['_sort_by'], 'FieldDescriptionInterface');
- }
- if ($this->values['_sort_by']->isSortable()) {
- $this->query->setSortBy($this->values['_sort_by']->getSortParentAssociationMapping(), $this->values['_sort_by']->getSortFieldMapping());
- $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
- }
- }
- $maxPerPage = 25;
- if (isset($this->values['_per_page'])) {
- // check for `is_array` can be safely removed if php 5.3 support will be dropped
- if (is_array($this->values['_per_page'])) {
- if (isset($this->values['_per_page']['value'])) {
- $maxPerPage = $this->values['_per_page']['value'];
- }
- } else {
- $maxPerPage = $this->values['_per_page'];
- }
- }
- $this->pager->setMaxPerPage($maxPerPage);
- $page = 1;
- if (isset($this->values['_page'])) {
- // check for `is_array` can be safely removed if php 5.3 support will be dropped
- if (is_array($this->values['_page'])) {
- if (isset($this->values['_page']['value'])) {
- $page = $this->values['_page']['value'];
- }
- } else {
- $page = $this->values['_page'];
- }
- }
- $this->pager->setPage($page);
- $this->pager->setQuery($this->query);
- $this->pager->init();
- $this->bound = true;
- }
- /**
- * {@inheritdoc}
- */
- public function addFilter(FilterInterface $filter)
- {
- $this->filters[$filter->getName()] = $filter;
- }
- /**
- * {@inheritdoc}
- */
- public function hasFilter($name)
- {
- return isset($this->filters[$name]);
- }
- /**
- * {@inheritdoc}
- */
- public function removeFilter($name)
- {
- unset($this->filters[$name]);
- }
- /**
- * {@inheritdoc}
- */
- public function getFilter($name)
- {
- return $this->hasFilter($name) ? $this->filters[$name] : null;
- }
- /**
- * {@inheritdoc}
- */
- public function getFilters()
- {
- return $this->filters;
- }
- /**
- * {@inheritdoc}
- */
- public function reorderFilters(array $keys)
- {
- $this->filters = array_merge(array_flip($keys), $this->filters);
- }
- /**
- * {@inheritdoc}
- */
- public function getValues()
- {
- return $this->values;
- }
- /**
- * {@inheritdoc}
- */
- public function setValue($name, $operator, $value)
- {
- $this->values[$name] = array(
- 'type' => $operator,
- 'value' => $value,
- );
- }
- /**
- * {@inheritdoc}
- */
- public function hasActiveFilters()
- {
- foreach ($this->filters as $name => $filter) {
- if ($filter->isActive()) {
- return true;
- }
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function hasDisplayableFilters()
- {
- foreach ($this->filters as $name => $filter) {
- $showFilter = $filter->getOption('show_filter', null);
- if (($filter->isActive() && $showFilter === null) || ($showFilter === true)) {
- return true;
- }
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public function getColumns()
- {
- return $this->columns;
- }
- /**
- * {@inheritdoc}
- */
- public function getQuery()
- {
- return $this->query;
- }
- /**
- * {@inheritdoc}
- */
- public function getForm()
- {
- $this->buildPager();
- return $this->form;
- }
- }
|