Datagrid.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  15. use Symfony\Component\Form\FormFactory;
  16. class Datagrid implements DatagridInterface
  17. {
  18. /**
  19. *
  20. * The filter instances
  21. * @var array
  22. */
  23. protected $filters = array();
  24. protected $values;
  25. protected $columns;
  26. protected $pager;
  27. protected $bound = false;
  28. protected $query;
  29. protected $formFactory;
  30. protected $results;
  31. public function __construct(ProxyQueryInterface $query, FieldDescriptionCollection $columns, PagerInterface $pager, FormFactory $formFactory, array $values = array())
  32. {
  33. $this->pager = $pager;
  34. $this->query = $query;
  35. $this->values = $values;
  36. $this->columns = $columns;
  37. $this->formFactory = $formFactory;
  38. }
  39. /**
  40. * @return \Sonata\AdminBundle\Datagrid\PagerInterface
  41. */
  42. public function getPager()
  43. {
  44. return $this->pager;
  45. }
  46. public function getResults()
  47. {
  48. $this->buildPager();
  49. if (!$this->results) {
  50. $this->results = $this->pager->getResults();
  51. }
  52. return $this->results;
  53. }
  54. public function buildPager()
  55. {
  56. if ($this->bound) {
  57. return;
  58. }
  59. foreach ($this->getFilters() as $name => $filter) {
  60. $value = isset($this->values[$name]) ? $this->values[$name] : null;
  61. $filter->getField()->bind($value);
  62. $filter->apply($this->query, $value);
  63. }
  64. $this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null);
  65. $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
  66. $this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1);
  67. $this->pager->setQuery($this->query);
  68. $this->pager->init();
  69. $this->bound = true;
  70. }
  71. /**
  72. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  73. * @return void
  74. */
  75. public function addFilter(FilterInterface $filter)
  76. {
  77. $filter->defineFieldBuilder($this->formFactory);
  78. $this->filters[$filter->getName()] = $filter;
  79. }
  80. public function getFilters()
  81. {
  82. return $this->filters;
  83. }
  84. public function getValues()
  85. {
  86. return $this->values;
  87. }
  88. public function getColumns()
  89. {
  90. return $this->columns;
  91. }
  92. public function getQuery()
  93. {
  94. return $this->query;
  95. }
  96. }