Datagrid.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. $maxPerPage = 25;
  105. if (isset($this->values['_per_page']['value'])) {
  106. $maxPerPage = $this->values['_per_page']['value'];
  107. } elseif (isset($this->values['_per_page'])) {
  108. $maxPerPage = $this->values['_per_page'];
  109. }
  110. $this->pager->setMaxPerPage($maxPerPage);
  111. $page = 1;
  112. if (isset($this->values['_page']['value'])) {
  113. $page = $this->values['_page']['value'];
  114. } elseif (isset($this->values['_page'])) {
  115. $page = $this->values['_page'];
  116. }
  117. $this->pager->setPage($page);
  118. $this->pager->setQuery($this->query);
  119. $this->pager->init();
  120. $this->bound = true;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function addFilter(FilterInterface $filter)
  126. {
  127. $this->filters[$filter->getName()] = $filter;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function hasFilter($name)
  133. {
  134. return isset($this->filters[$name]);
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function removeFilter($name)
  140. {
  141. unset($this->filters[$name]);
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function getFilter($name)
  147. {
  148. return $this->hasFilter($name) ? $this->filters[$name] : null;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function getFilters()
  154. {
  155. return $this->filters;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function reorderFilters(array $keys)
  161. {
  162. $this->filters = array_merge(array_flip($keys), $this->filters);
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getValues()
  168. {
  169. return $this->values;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function setValue($name, $operator, $value)
  175. {
  176. $this->values[$name] = array(
  177. 'type' => $operator,
  178. 'value' => $value
  179. );
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function hasActiveFilters()
  185. {
  186. foreach ($this->filters as $name => $filter) {
  187. if ($filter->isActive()) {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function getColumns()
  197. {
  198. return $this->columns;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function getQuery()
  204. {
  205. return $this->query;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function getForm()
  211. {
  212. $this->buildPager();
  213. return $this->form;
  214. }
  215. }