Datagrid.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Datagrid;
  11. use Sonata\AdminBundle\Datagrid\PagerInterface;
  12. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  13. use Sonata\AdminBundle\Filter\FilterInterface;
  14. use Symfony\Component\Form\FormFactory;
  15. class Datagrid implements DatagridInterface
  16. {
  17. /**
  18. *
  19. * The filter instances
  20. * @var array
  21. */
  22. protected $filters = array();
  23. protected $values;
  24. protected $columns;
  25. protected $pager;
  26. protected $bound = false;
  27. protected $query;
  28. protected $formFactory;
  29. protected $results;
  30. public function __construct(ProxyQueryInterface $query, ListCollection $columns, PagerInterface $pager, FormFactory $formFactory, array $values = array())
  31. {
  32. $this->pager = $pager;
  33. $this->query = $query;
  34. $this->values = $values;
  35. $this->columns = $columns;
  36. $this->formFactory = $formFactory;
  37. }
  38. /**
  39. * @return \Sonata\AdminBundle\Datagrid\PagerInterface
  40. */
  41. public function getPager()
  42. {
  43. return $this->pager;
  44. }
  45. public function getResults()
  46. {
  47. $this->buildPager();
  48. if (!$this->results) {
  49. $this->results = $this->pager->getResults();
  50. }
  51. return $this->results;
  52. }
  53. public function buildPager()
  54. {
  55. if ($this->bound) {
  56. return;
  57. }
  58. foreach ($this->getFilters() as $name => $filter) {
  59. $value = isset($this->values[$name]) ? $this->values[$name] : null;
  60. $filter->getField()->bind($value);
  61. $filter->apply($this->query, $value);
  62. }
  63. $this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null);
  64. $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
  65. $this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1);
  66. $this->pager->setQuery($this->query);
  67. $this->pager->init();
  68. $this->bound = true;
  69. }
  70. /**
  71. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  72. * @return void
  73. */
  74. public function addFilter(FilterInterface $filter)
  75. {
  76. $filter->defineFieldBuilder($this->formFactory);
  77. $this->filters[$filter->getName()] = $filter;
  78. }
  79. public function getFilters()
  80. {
  81. return $this->filters;
  82. }
  83. public function getValues()
  84. {
  85. return $this->values;
  86. }
  87. public function getColumns()
  88. {
  89. return $this->columns;
  90. }
  91. public function getQuery()
  92. {
  93. return $this->query;
  94. }
  95. }