Datagrid.php 7.4 KB

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