Datagrid.php 7.0 KB

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