Datagrid.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. $this->formBuilder->add('_sort_by', 'hidden');
  109. $this->formBuilder->get('_sort_by')->addViewTransformer(new CallbackTransformer(
  110. function ($value) {
  111. return $value;
  112. },
  113. function ($value) {
  114. return $value instanceof FieldDescriptionInterface ? $value->getName() : $value;
  115. }
  116. ));
  117. $this->formBuilder->add('_sort_order', 'hidden');
  118. $this->formBuilder->add('_page', 'hidden');
  119. $this->formBuilder->add('_per_page', 'hidden');
  120. $this->form = $this->formBuilder->getForm();
  121. $this->form->submit($this->values);
  122. $data = $this->form->getData();
  123. foreach ($this->getFilters() as $name => $filter) {
  124. $this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null;
  125. $filter->apply($this->query, $data[$filter->getFormName()]);
  126. }
  127. if (isset($this->values['_sort_by'])) {
  128. if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
  129. throw new UnexpectedTypeException($this->values['_sort_by'], 'FieldDescriptionInterface');
  130. }
  131. if ($this->values['_sort_by']->isSortable()) {
  132. $this->query->setSortBy($this->values['_sort_by']->getSortParentAssociationMapping(), $this->values['_sort_by']->getSortFieldMapping());
  133. $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
  134. }
  135. }
  136. $maxPerPage = 25;
  137. if (isset($this->values['_per_page'])) {
  138. // check for `is_array` can be safely removed if php 5.3 support will be dropped
  139. if (is_array($this->values['_per_page'])) {
  140. if (isset($this->values['_per_page']['value'])) {
  141. $maxPerPage = $this->values['_per_page']['value'];
  142. }
  143. } else {
  144. $maxPerPage = $this->values['_per_page'];
  145. }
  146. }
  147. $this->pager->setMaxPerPage($maxPerPage);
  148. $page = 1;
  149. if (isset($this->values['_page'])) {
  150. // check for `is_array` can be safely removed if php 5.3 support will be dropped
  151. if (is_array($this->values['_page'])) {
  152. if (isset($this->values['_page']['value'])) {
  153. $page = $this->values['_page']['value'];
  154. }
  155. } else {
  156. $page = $this->values['_page'];
  157. }
  158. }
  159. $this->pager->setPage($page);
  160. $this->pager->setQuery($this->query);
  161. $this->pager->init();
  162. $this->bound = true;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function addFilter(FilterInterface $filter)
  168. {
  169. $this->filters[$filter->getName()] = $filter;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function hasFilter($name)
  175. {
  176. return isset($this->filters[$name]);
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function removeFilter($name)
  182. {
  183. unset($this->filters[$name]);
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function getFilter($name)
  189. {
  190. return $this->hasFilter($name) ? $this->filters[$name] : null;
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function getFilters()
  196. {
  197. return $this->filters;
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function reorderFilters(array $keys)
  203. {
  204. $this->filters = array_merge(array_flip($keys), $this->filters);
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function getValues()
  210. {
  211. return $this->values;
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function setValue($name, $operator, $value)
  217. {
  218. $this->values[$name] = array(
  219. 'type' => $operator,
  220. 'value' => $value,
  221. );
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function hasActiveFilters()
  227. {
  228. foreach ($this->filters as $name => $filter) {
  229. if ($filter->isActive()) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. /**
  236. * {@inheritdoc}
  237. */
  238. public function hasDisplayableFilters()
  239. {
  240. foreach ($this->filters as $name => $filter) {
  241. $showFilter = $filter->getOption('show_filter', null);
  242. if (($filter->isActive() && $showFilter === null) || ($showFilter === true)) {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function getColumns()
  252. {
  253. return $this->columns;
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function getQuery()
  259. {
  260. return $this->query;
  261. }
  262. /**
  263. * {@inheritdoc}
  264. */
  265. public function getForm()
  266. {
  267. $this->buildPager();
  268. return $this->form;
  269. }
  270. }