UserRepository.php 7.0KB

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