UserRepository.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Muzich\CoreBundle\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\Query;
  5. class UserRepository extends EntityRepository
  6. {
  7. /**
  8. * Retourne une Query construite selon les paramètres fournis.
  9. *
  10. * @param int $user_id
  11. * @param array $join
  12. * @return Doctrine\ORM\Query
  13. */
  14. public function findOneById($user_id, $join_list)
  15. {
  16. $select = 'u';
  17. $join = '';
  18. $where = '';
  19. $parameters = array('uid' => $user_id);
  20. if (in_array('followeds_users', $join_list))
  21. {
  22. $select .= ', fdu, fdu_u';
  23. $join .= ' LEFT JOIN u.followeds_users fdu LEFT JOIN fdu.followed fdu_u';
  24. }
  25. if (in_array('followers_users', $join_list))
  26. {
  27. $select .= ', fru, fru_u';
  28. $join .= ' LEFT JOIN u.followers_users fru LEFT JOIN fru.follower fru_u';
  29. }
  30. if (in_array('followeds_groups', $join_list))
  31. {
  32. $select .= ', fdg, fdg_g';
  33. $join .= ' LEFT JOIN u.followed_groups fdg LEFT JOIN fdg.group fdg_g';
  34. }
  35. if (in_array('favorites_tags', $join_list))
  36. {
  37. $select .= ', tf, tf_t';
  38. $join .= ' LEFT JOIN u.tags_favorites tf LEFT JOIN tf.tag tf_t';
  39. }
  40. if (in_array('groups_owned', $join_list))
  41. {
  42. $select .= ', og';
  43. $join .= ' LEFT JOIN u.groups_owned og';
  44. }
  45. if (in_array('groups_owned_groups_tags', $join_list))
  46. {
  47. $select .= ', og, ogt';
  48. $join .= ' LEFT JOIN u.groups_owned og LEFT JOIN og.tags ogt';
  49. }
  50. return $this->getEntityManager()
  51. ->createQuery("
  52. SELECT $select FROM MuzichCoreBundle:User u
  53. $join
  54. WHERE u.id = :uid
  55. $where
  56. ")
  57. ->setParameters($parameters)
  58. ;
  59. }
  60. /**
  61. * Retourne les tag id préférés de l'user.
  62. *
  63. * @param int $user_id
  64. * @param int $limit
  65. * @return array
  66. */
  67. public function getTagsFavorites($user_id, $limit = null)
  68. {
  69. $tags = array();
  70. foreach ($this->getEntityManager()
  71. ->createQuery('
  72. SELECT t.id, t.name FROM MuzichCoreBundle:Tag t
  73. JOIN t.users_favorites uf
  74. JOIN uf.user u
  75. WHERE u.id = :uid
  76. ORDER BY uf.position ASC'
  77. )
  78. ->setParameter('uid', $user_id)
  79. ->setMaxResults($limit)
  80. ->getArrayResult() as $tag)
  81. {
  82. $tags[$tag['id']] = $tag['name'];
  83. }
  84. return $tags;
  85. }
  86. /**
  87. * Retourne la requete selectionnant les user correspondant a une
  88. * chaine de caractère
  89. * La recherche est effectué sur le username.
  90. *
  91. * @param type $string
  92. * @return Doctrine\ORM\Query
  93. */
  94. public function findByString($string)
  95. {
  96. return $this->getEntityManager()
  97. ->createQuery("
  98. SELECT u FROM MuzichCoreBundle:User u
  99. WHERE UPPER(u.username) LIKE :str
  100. OR UPPER(u.usernameCanonical) LIKE :str
  101. ORDER BY u.username ASC"
  102. )
  103. ->setParameters(array(
  104. 'str' => '%'.strtoupper(trim($string)).'%'
  105. ))
  106. ;
  107. }
  108. /**
  109. * Retourne une Query sleectionnant un User par son slug
  110. *
  111. * @param type string
  112. * @return Doctrine\ORM\Query
  113. */
  114. public function findOneBySlug($slug)
  115. {
  116. return $this->getEntityManager()
  117. ->createQuery("
  118. SELECT u FROM MuzichCoreBundle:User u
  119. WHERE u.slug = :str
  120. ")
  121. ->setParameters(array(
  122. 'str' => $slug
  123. ))
  124. ;
  125. }
  126. /**
  127. * Retourne vrai si follower suis followed
  128. *
  129. * @param int $follower_id
  130. * @param int $followed_id
  131. * @return boolean
  132. */
  133. public function isFollowingUser($follower_id, $followed_id)
  134. {
  135. $result = $this->getEntityManager()
  136. ->createQuery("
  137. SELECT COUNT(fu.id) FROM MuzichCoreBundle:FollowUser fu
  138. WHERE fu.follower = :frid AND fu.followed = :fdid
  139. ")
  140. ->setParameters(array(
  141. 'frid' => $follower_id,
  142. 'fdid' => $followed_id
  143. ))
  144. ->getSingleResult(Query::HYDRATE_ARRAY)
  145. ;
  146. return $result[1];
  147. }
  148. /**
  149. * Retourne vrai si follower suis le groupe
  150. *
  151. * @param int $follower_id
  152. * @param int $group_id
  153. * @return boolean
  154. */
  155. public function isFollowingGroup($follower_id, $group_id)
  156. {
  157. $result = $this->getEntityManager()
  158. ->createQuery("
  159. SELECT COUNT(fg.id) FROM MuzichCoreBundle:FollowGroup fg
  160. WHERE fg.follower = :frid AND fg.group = :fdgid
  161. ")
  162. ->setParameters(array(
  163. 'frid' => $follower_id,
  164. 'fdgid' => $group_id
  165. ))
  166. ->getSingleResult(Query::HYDRATE_ARRAY)
  167. ;
  168. return $result[1];
  169. }
  170. /**
  171. * Retourne tous les tags des elements postés
  172. *
  173. * @return doctrine_collection
  174. */
  175. public function getElementsTags($user_id, $current_user_id)
  176. {
  177. return $this->getEntityManager()
  178. ->createQuery('
  179. SELECT t FROM MuzichCoreBundle:Tag t
  180. LEFT JOIN t.elements e
  181. WHERE e.owner = :uid
  182. AND (t.tomoderate = \'FALSE\' OR t.tomoderate IS NULL
  183. OR t.privateids LIKE :uidt)
  184. ORDER BY t.name ASC'
  185. )
  186. ->setParameters(array('uid' => $user_id, 'uidt' => '%"'.$current_user_id.'"%'))
  187. ->getResult()
  188. ;
  189. }
  190. public function getElementIdsOwned($user_id)
  191. {
  192. $ids = array();
  193. foreach ($this->getEntityManager()
  194. ->createQuery('SELECT e.id FROM MuzichCoreBundle:Element e
  195. WHERE e.owner = :uid')
  196. ->setParameter('uid', $user_id)
  197. ->getScalarResult() as $row)
  198. {
  199. $ids[] = $row['id'];
  200. }
  201. return $ids;
  202. }
  203. public function countFollowers($user_id)
  204. {
  205. $data = $this->getEntityManager()
  206. ->createQuery('SELECT COUNT(f) FROM MuzichCoreBundle:FollowUser f
  207. WHERE f.followed = :uid')
  208. ->setParameter('uid', $user_id)
  209. ->getScalarResult()
  210. ;
  211. if (count($data))
  212. {
  213. if (count($data[0]))
  214. {
  215. return $data[0][1];
  216. }
  217. }
  218. return 0;
  219. }
  220. /**
  221. * Retourne les objets utilisateurs correspondant aux ids transmis.
  222. *
  223. * @param array $ids
  224. * @return Collection
  225. */
  226. public function getUsersWithIds($ids)
  227. {
  228. return $this->getEntityManager()
  229. ->createQuery('SELECT u FROM MuzichCoreBundle:User u'
  230. .' WHERE u.id IN (:uids)')
  231. ->setParameter('uids', $ids)
  232. ->getResult()
  233. ;
  234. }
  235. }