Datagrid.php 7.7 KB

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