Datagrid.php 6.9 KB

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