Datagrid.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\FormBuilder;
  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 $formBuilder;
  30. protected $form;
  31. protected $results;
  32. /**
  33. * @param ProxyQueryInterface $query
  34. * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $columns
  35. * @param PagerInterface $pager
  36. * @param \Symfony\Component\Form\FormBuilder $formBuilder
  37. * @param array $values
  38. */
  39. public function __construct(ProxyQueryInterface $query, FieldDescriptionCollection $columns, PagerInterface $pager, FormBuilder $formBuilder, array $values = array())
  40. {
  41. $this->pager = $pager;
  42. $this->query = $query;
  43. $this->values = $values;
  44. $this->columns = $columns;
  45. $this->formBuilder = $formBuilder;
  46. }
  47. /**
  48. * @return \Sonata\AdminBundle\Datagrid\PagerInterface
  49. */
  50. public function getPager()
  51. {
  52. return $this->pager;
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function getResults()
  58. {
  59. $this->buildPager();
  60. if (!$this->results) {
  61. $this->results = $this->pager->getResults();
  62. }
  63. return $this->results;
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function buildPager()
  69. {
  70. if ($this->bound) {
  71. return;
  72. }
  73. foreach ($this->getFilters() as $name => $filter) {
  74. list($type, $options) = $filter->getRenderSettings();
  75. $this->formBuilder->add($filter->getFormName(), $type, $options);
  76. }
  77. $this->formBuilder->add('_sort_by', 'hidden');
  78. $this->formBuilder->add('_sort_order', 'hidden');
  79. $this->formBuilder->add('_page', 'hidden');
  80. $this->form = $this->formBuilder->getForm();
  81. $this->form->bind($this->values);
  82. $data = $this->form->getData();
  83. foreach ($this->getFilters() as $name => $filter) {
  84. $this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null;
  85. $filter->apply($this->query, $data[$filter->getFormName()]);
  86. }
  87. if (isset($this->values['_sort_by'])) {
  88. foreach ($this->getColumns()->getElements() as $fieldDescription){
  89. if ($fieldDescription->isSortable() && $fieldDescription->getName() == $this->values['_sort_by']) {
  90. $this->query->setSortBy($fieldDescription->getSortParentAssociationMapping(), $fieldDescription->getSortFieldMapping());
  91. $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
  92. break;
  93. }
  94. }
  95. }
  96. $this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1);
  97. $this->pager->setQuery($this->query);
  98. $this->pager->init();
  99. $this->bound = true;
  100. }
  101. /**
  102. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  103. *
  104. * @return void
  105. */
  106. public function addFilter(FilterInterface $filter)
  107. {
  108. $this->filters[$filter->getName()] = $filter;
  109. }
  110. /**
  111. * @param string $name
  112. *
  113. * @return bool
  114. */
  115. public function hasFilter($name)
  116. {
  117. return isset($this->filters[$name]);
  118. }
  119. /**
  120. * @param string $name
  121. */
  122. public function removeFilter($name)
  123. {
  124. unset($this->filters[$name]);
  125. }
  126. /**
  127. * @param string $name
  128. *
  129. * @return null
  130. */
  131. public function getFilter($name)
  132. {
  133. return $this->hasFilter($name) ? $this->filters[$name] : null;
  134. }
  135. /**
  136. * @return FilterInterface[]
  137. */
  138. public function getFilters()
  139. {
  140. return $this->filters;
  141. }
  142. public function reorderFilters(array $keys)
  143. {
  144. $this->filters = array_merge(array_flip($keys), $this->filters);
  145. }
  146. /**
  147. * @return array
  148. */
  149. public function getValues()
  150. {
  151. return $this->values;
  152. }
  153. /**
  154. * @param string $name
  155. * @param string $operator
  156. * @param mixed $value
  157. */
  158. public function setValue($name, $operator, $value)
  159. {
  160. $this->values[$name] = array(
  161. 'type' => $operator,
  162. 'value' => $value
  163. );
  164. }
  165. /**
  166. * @return boolean
  167. */
  168. public function hasActiveFilters()
  169. {
  170. foreach ($this->filters as $name => $filter) {
  171. if ($filter->isActive()) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. /**
  178. * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
  179. */
  180. public function getColumns()
  181. {
  182. return $this->columns;
  183. }
  184. /**
  185. * @return ProxyQueryInterface
  186. */
  187. public function getQuery()
  188. {
  189. return $this->query;
  190. }
  191. /**
  192. * @return \Symfony\Component\Form\Form
  193. */
  194. public function getForm()
  195. {
  196. $this->buildPager();
  197. return $this->form;
  198. }
  199. }