GroupManager.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\UserBundle\Entity;
  11. use FOS\UserBundle\Entity\GroupManager as BaseGroupManager;
  12. use Sonata\DatagridBundle\Pager\Doctrine\Pager;
  13. use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
  14. use Sonata\UserBundle\Model\GroupManagerInterface;
  15. /**
  16. * @author Hugo Briand <briand@ekino.com>
  17. */
  18. class GroupManager extends BaseGroupManager implements GroupManagerInterface
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function findGroupsBy(array $criteria = null, array $orderBy = null, $limit = null, $offset = null)
  24. {
  25. return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getPager(array $criteria, $page, $limit = 10, array $sort = array())
  31. {
  32. $query = $this->repository
  33. ->createQueryBuilder('g')
  34. ->select('g');
  35. $fields = $this->objectManager->getClassMetadata($this->class)->getFieldNames();
  36. foreach ($sort as $field => $direction) {
  37. if (!in_array($field, $fields)) {
  38. throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class));
  39. }
  40. }
  41. if (count($sort) == 0) {
  42. $sort = array('name' => 'ASC');
  43. }
  44. foreach ($sort as $field => $direction) {
  45. $query->orderBy(sprintf('g.%s', $field), strtoupper($direction));
  46. }
  47. $parameters = array();
  48. if (isset($criteria['enabled'])) {
  49. $query->andWhere('g.enabled = :enabled');
  50. $parameters['enabled'] = $criteria['enabled'];
  51. }
  52. $query->setParameters($parameters);
  53. $pager = new Pager();
  54. $pager->setMaxPerPage($limit);
  55. $pager->setQuery(new ProxyQuery($query));
  56. $pager->setPage($page);
  57. $pager->init();
  58. return $pager;
  59. }
  60. }