Datagrid.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\BaseApplicationBundle\Tool;
  11. use Sonata\BaseApplicationBundle\Tool\DoctrinePager as Pager;
  12. use Sonata\BaseApplicationBundle\Filter\StringFilter;
  13. use Sonata\BaseApplicationBundle\Filter\BooleanFilter;
  14. use Sonata\BaseApplicationBundle\Filter\IntegerFilter;
  15. use Sonata\BaseApplicationBundle\Filter\CallbackFilter;
  16. use Sonata\BaseApplicationBundle\Filter\ChoiceFilter;
  17. use Sonata\BaseApplicationBundle\Admin\EntityAdmin;
  18. use Sonata\BaseApplicationBundle\Admin\FieldDescription;
  19. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  20. class Datagrid
  21. {
  22. protected $classname;
  23. protected $entityManager;
  24. /**
  25. * The filter descriptions
  26. * @var array
  27. */
  28. protected $filterFields = array();
  29. /**
  30. *
  31. * The filter instances
  32. * @var array
  33. */
  34. protected $filters = array();
  35. protected $values;
  36. protected $pager;
  37. protected $maxPerPage = 25;
  38. public function __construct($classname, $entityManager, $values = array())
  39. {
  40. $this->classname = $classname;
  41. $this->entityManager = $entityManager;
  42. $this->values = $values;
  43. }
  44. public function getClassMetaData()
  45. {
  46. return $this->getEntityManager()
  47. ->getClassMetaData($this->getClassname());
  48. }
  49. public function getPager()
  50. {
  51. if (!$this->pager) {
  52. $this->pager = new Pager($this->getClassname(), $this->getMaxPerPage());
  53. $this->pager->setQueryBuilder($this->getQueryBuilder($this->values));
  54. $this->pager->setPage(isset($this->values['page']) ? $this->values['page'] : 1);
  55. $this->pager->init();
  56. }
  57. return $this->pager;
  58. }
  59. public function getResults()
  60. {
  61. $pager = $this->getPager($this->getValues());
  62. return $pager->getResults();
  63. }
  64. public function getBaseQueryBuilder()
  65. {
  66. $em = $this->getEntityManager();
  67. $repository = $em->getRepository($this->getClassname());
  68. $queryBuidler = $repository
  69. ->createQueryBuilder('o');
  70. return $queryBuidler;
  71. }
  72. public function getQueryBuilder($values = array())
  73. {
  74. $queryBuidler = $this->getBaseQueryBuilder();
  75. foreach ($this->getFilters() as $name => $filter) {
  76. $value = isset($values[$name]) ? $values[$name] : null;
  77. $filter->apply($queryBuidler, $value);
  78. }
  79. return $queryBuidler;
  80. }
  81. public function setClassname($classname)
  82. {
  83. $this->classname = $classname;
  84. }
  85. public function getClassname()
  86. {
  87. return $this->classname;
  88. }
  89. public function setEntityManager($entityManager)
  90. {
  91. $this->entityManager = $entityManager;
  92. }
  93. public function getEntityManager()
  94. {
  95. return $this->entityManager;
  96. }
  97. public function setFilterFields($filterFields)
  98. {
  99. $this->filterFields = $filterFields;
  100. }
  101. public function getFilterFields()
  102. {
  103. return $this->filterFields;
  104. }
  105. public function buildFilterFields()
  106. {
  107. $this->filterFields = EntityAdmin::getBaseFields($this->getClassMetaData(), $this->filterFields);
  108. foreach ($this->filterFields as $name => $fieldDescription) {
  109. // set default values
  110. $fieldDescription->setOption('code', $fieldDescription->getOption('code', $name));
  111. $fieldDescription->setOption('label', $fieldDescription->getOption('label', $name));
  112. $fieldDescription->setOption('filter_value', $fieldDescription->getOption('filter_value', null));
  113. $fieldDescription->setOption('filter_options', $fieldDescription->getOption('filter_options', null));
  114. $fieldDescription->setOption('filter_field_options', $fieldDescription->getOption('filter_field_options', null));
  115. $fieldDescription->setOption('name', $fieldDescription->getOption('name', $name));
  116. // set the default type if none is set
  117. if (!$fieldDescription->getType()) {
  118. $fieldDescription->setType('string');
  119. }
  120. if (!$fieldDescription->getTemplate()) {
  121. $fieldDescription->setTemplate(sprintf('SonataBaseApplicationBundle:CRUD:filter_%s.twig.html', $fieldDescription->getType()));
  122. if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_ONE) {
  123. $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:filter_many_to_one.twig.html');
  124. }
  125. if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_MANY) {
  126. $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:filter_many_to_many.twig.html');
  127. }
  128. }
  129. }
  130. $this->configureFilterFields();
  131. }
  132. public function getChoices(FieldDescription $fieldDescription)
  133. {
  134. $targets = $this->getEntityManager()
  135. ->createQueryBuilder()
  136. ->select('t')
  137. ->from($fieldDescription->getTargetEntity(), 't')
  138. ->getQuery()
  139. ->execute();
  140. $choices = array();
  141. foreach ($targets as $target) {
  142. // todo : puts this into a configuration option and use reflection
  143. foreach (array('getTitle', 'getName', '__toString') as $getter) {
  144. if (method_exists($target, $getter)) {
  145. $choices[$target->getId()] = $target->$getter();
  146. break;
  147. }
  148. }
  149. }
  150. return $choices;
  151. }
  152. public function getFilterInstance(FieldDescription $fieldDescription)
  153. {
  154. if (!$fieldDescription->getType()) {
  155. return false;
  156. }
  157. $name = $fieldDescription->getName();
  158. switch($fieldDescription->getType()) {
  159. case ClassMetadataInfo::MANY_TO_MANY:
  160. $options = $fieldDescription->getOption('filter_field_options');
  161. $options['choices'] = $this->getChoices($fieldDescription);
  162. $fieldDescription->setOption('filter_field_options', $options);
  163. $filter = new ChoiceFilter($name, $fieldDescription);
  164. break;
  165. case 'string':
  166. case 'text':
  167. $filter = new StringFilter($name, $fieldDescription);
  168. break;
  169. case 'boolean':
  170. $filter = new BooleanFilter($name, $fieldDescription);
  171. break;
  172. case 'integer':
  173. $filter = new IntegerFilter($name, $fieldDescription);
  174. break;
  175. case 'callback':
  176. $filter = new CallbackFilter($name, $fieldDescription);
  177. break;
  178. default:
  179. return false;
  180. }
  181. return $filter;
  182. }
  183. public function configureFilterFields()
  184. {
  185. }
  186. public function getFilters()
  187. {
  188. if (!$this->filters) {
  189. foreach ($this->filterFields as $name => $description) {
  190. $filter = $this->getFilterInstance($this->filterFields[$name]);
  191. if ($filter) {
  192. $this->filters[$name] = $filter;
  193. }
  194. }
  195. }
  196. return $this->filters;
  197. }
  198. public function setValues($values)
  199. {
  200. $this->values = $values;
  201. }
  202. public function getValues()
  203. {
  204. return $this->values;
  205. }
  206. public function setMaxPerPage($maxPerPage)
  207. {
  208. $this->maxPerPage = $maxPerPage;
  209. }
  210. public function getMaxPerPage()
  211. {
  212. return $this->maxPerPage;
  213. }
  214. }