Datagrid.php 6.3 KB

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