* * 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\Datagrid\PagerInterface; use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; use Sonata\AdminBundle\Filter\FilterInterface; use Symfony\Component\Form\FormFactory; class Datagrid implements DatagridInterface { /** * * The filter instances * @var array */ protected $filters = array(); protected $values; protected $columns; protected $pager; protected $bound = false; protected $query; protected $formFactory; public function __construct(ProxyQueryInterface $query, ListCollection $columns, PagerInterface $pager, FormFactory $formFactory, array $values = array()) { $this->pager = $pager; $this->query = $query; $this->values = $values; $this->columns = $columns; $this->formFactory = $formFactory; } /** * @return \Sonata\AdminBundle\Datagrid\PagerInterface */ public function getPager() { return $this->pager; } public function getResults() { $this->buildPager(); return $this->pager->getResults(); } public function buildPager() { if ($this->bound) { return; } foreach ($this->getFilters() as $name => $filter) { $value = isset($this->values[$name]) ? $this->values[$name] : null; $filter->getField()->bind($value); $filter->apply($this->query, $value); } $this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null); $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null); $this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1); $this->pager->setQuery($this->query); $this->pager->init(); $this->bound = true; } /** * @param \Sonata\AdminBundle\Filter\FilterInterface $filter * @return void */ public function addFilter(FilterInterface $filter) { $filter->defineFieldBuilder($this->formFactory); $this->filters[$filter->getName()] = $filter; } public function getFilters() { return $this->filters; } public function getValues() { return $this->values; } public function getColumns() { return $this->columns; } public function getQuery() { return $this->query; } }