Datagrid.php 8.1 KB

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