UserManager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Doctrine\UserManager as BaseUserManager;
  12. use Sonata\CoreBundle\Model\ManagerInterface;
  13. use Sonata\DatagridBundle\Pager\Doctrine\Pager;
  14. use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
  15. use Sonata\UserBundle\Model\UserManagerInterface;
  16. /**
  17. * @author Hugo Briand <briand@ekino.com>
  18. */
  19. class UserManager extends BaseUserManager implements UserManagerInterface, ManagerInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function findUsersBy(array $criteria = null, array $orderBy = null, $limit = null, $offset = null)
  25. {
  26. return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function find($id)
  32. {
  33. return $this->repository->find($id);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function findAll()
  39. {
  40. return $this->repository->findAll();
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  46. {
  47. return parent::findUsers($criteria, $orderBy, $limit, $offset);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function findOneBy(array $criteria, array $orderBy = null)
  53. {
  54. return parent::findUserBy($criteria);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function create()
  60. {
  61. return parent::createUser();
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function save($entity, $andFlush = true)
  67. {
  68. parent::updateUser($entity, $andFlush);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function delete($entity, $andFlush = true)
  74. {
  75. parent::deleteUser($entity);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getTableName()
  81. {
  82. return $this->objectManager->getClassMetadata($this->class)->table['name'];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getConnection()
  88. {
  89. return $this->objectManager->getConnection();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getPager(array $criteria, $page, $limit = 10, array $sort = array())
  95. {
  96. $query = $this->repository
  97. ->createQueryBuilder('u')
  98. ->select('u');
  99. $fields = $this->objectManager->getClassMetadata($this->class)->getFieldNames();
  100. foreach ($sort as $field => $direction) {
  101. if (!in_array($field, $fields)) {
  102. throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class));
  103. }
  104. }
  105. if (count($sort) == 0) {
  106. $sort = array('username' => 'ASC');
  107. }
  108. foreach ($sort as $field => $direction) {
  109. $query->orderBy(sprintf('u.%s', $field), strtoupper($direction));
  110. }
  111. if (isset($criteria['enabled'])) {
  112. $query->andWhere('u.enabled = :enabled');
  113. $query->setParameter('enabled', $criteria['enabled']);
  114. }
  115. if (isset($criteria['locked'])) {
  116. $query->andWhere('u.locked = :locked');
  117. $query->setParameter('locked', $criteria['locked']);
  118. }
  119. $pager = new Pager();
  120. $pager->setMaxPerPage($limit);
  121. $pager->setQuery(new ProxyQuery($query));
  122. $pager->setPage($page);
  123. $pager->init();
  124. return $pager;
  125. }
  126. }