Datagrid.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public function __construct(ProxyQueryInterface $query, ListCollection $columns, PagerInterface $pager, FormFactory $formFactory, array $values = array())
  30. {
  31. $this->pager = $pager;
  32. $this->query = $query;
  33. $this->values = $values;
  34. $this->columns = $columns;
  35. $this->formFactory = $formFactory;
  36. }
  37. /**
  38. * @return \Sonata\AdminBundle\Datagrid\PagerInterface
  39. */
  40. public function getPager()
  41. {
  42. return $this->pager;
  43. }
  44. public function getResults()
  45. {
  46. $this->buildPager();
  47. return $this->pager->getResults();
  48. }
  49. public function buildPager()
  50. {
  51. if ($this->bound) {
  52. return;
  53. }
  54. foreach ($this->getFilters() as $name => $filter) {
  55. $value = isset($this->values[$name]) ? $this->values[$name] : null;
  56. $filter->getField()->bind($value);
  57. $filter->apply($this->query, $value);
  58. }
  59. $this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null);
  60. $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
  61. $this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1);
  62. $this->pager->setQuery($this->query);
  63. $this->pager->init();
  64. $this->bound = true;
  65. }
  66. /**
  67. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  68. * @return void
  69. */
  70. public function addFilter(FilterInterface $filter)
  71. {
  72. $filter->defineFieldBuilder($this->formFactory);
  73. $this->filters[$filter->getName()] = $filter;
  74. }
  75. public function getFilters()
  76. {
  77. return $this->filters;
  78. }
  79. public function getValues()
  80. {
  81. return $this->values;
  82. }
  83. public function getColumns()
  84. {
  85. return $this->columns;
  86. }
  87. public function getQuery()
  88. {
  89. return $this->query;
  90. }
  91. }