Datagrid.php 5.9 KB

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