Datagrid.php 7.7 KB

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