ModerateController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace Muzich\AdminBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. //use Muzich\CoreBundle\Util\TagLike;
  6. use Muzich\CoreBundle\Entity\UsersTagsFavorites;
  7. use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
  8. use Muzich\CoreBundle\Managers\TagManager;
  9. use Muzich\CoreBundle\Propagator\EventElement;
  10. use Muzich\CoreBundle\Managers\CommentsManager;
  11. class ModerateController extends Controller
  12. {
  13. /**
  14. *
  15. * @Template()
  16. */
  17. public function indexAction()
  18. {
  19. $count_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  20. ->countToModerate();
  21. $count_elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  22. ->countToModerate();
  23. $count_comments = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  24. ->countForCommentToModerate();
  25. return array(
  26. 'count_tags' => $count_tags,
  27. 'count_elements' => $count_elements,
  28. 'count_comments' => $count_comments
  29. );
  30. }
  31. /**
  32. *
  33. * @Template()
  34. */
  35. public function tagsAction()
  36. {
  37. // Récupération des tags
  38. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  39. ->getToModerate();
  40. // TODO: Ajouter a chaque tag la liste des tags ressemblant
  41. return array(
  42. 'tags' => $tags
  43. );
  44. }
  45. public function tagAcceptAction($tag_id)
  46. {
  47. if (($response = $this->mustBeConnected(true)))
  48. {
  49. return $response;
  50. }
  51. if (!($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
  52. 'id' => $tag_id,
  53. 'tomoderate' => true
  54. ))))
  55. {
  56. return $this->jsonResponse(array(
  57. 'status' => 'error',
  58. 'message' => 'NotFound'
  59. ));
  60. }
  61. $tagManager = new TagManager();
  62. if (!$tagManager->moderateTag($this->getDoctrine(), $tag, true))
  63. {
  64. return $this->jsonResponse(array(
  65. 'status' => 'error',
  66. 'message' => 'NotFound'
  67. ));
  68. }
  69. return $this->jsonResponse(array(
  70. 'status' => 'success'
  71. ));
  72. }
  73. public function tagRefuseAction($tag_id)
  74. {
  75. if (($response = $this->mustBeConnected(true)))
  76. {
  77. return $response;
  78. }
  79. if (!($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
  80. 'id' => $tag_id,
  81. 'tomoderate' => true
  82. ))))
  83. {
  84. return $this->jsonResponse(array(
  85. 'status' => 'error',
  86. 'message' => 'NotFound'
  87. ));
  88. }
  89. $tagManager = new TagManager();
  90. if (!$tagManager->moderateTag($this->getDoctrine(), $tag, false))
  91. {
  92. return $this->jsonResponse(array(
  93. 'status' => 'error',
  94. 'message' => 'NotFound'
  95. ));
  96. }
  97. // Tout c'est bien passé, on incremente ceci dit le compteur
  98. // de tag refusés par la modération pour le ou les utilisateurs
  99. // ayant fait la demande d'ajout
  100. $uids = json_decode($tag->getPrivateids(), true);
  101. $users = $this->getDoctrine()->getEntityManager()
  102. ->createQuery('
  103. SELECT u FROM MuzichCoreBundle:User u
  104. WHERE u.id IN (:uids)'
  105. )
  106. ->setParameter('uids', $uids)
  107. ->getResult()
  108. ;
  109. // Pour chacun on augmente le compteur
  110. foreach ($users as $user)
  111. {
  112. $user->addModeratedTagCount();
  113. $this->getDoctrine()->getEntityManager()->persist($user);
  114. }
  115. $this->getDoctrine()->getEntityManager()->flush();
  116. return $this->jsonResponse(array(
  117. 'status' => 'success'
  118. ));
  119. }
  120. /**
  121. * Cette action est plus délicate, elle consiste a remplacer le tag en question
  122. * par un autre.
  123. *
  124. * @param int $tag_id
  125. * @param int $tag_new_id
  126. * @return view
  127. */
  128. public function tagReplaceAction($tag_id, $tag_new_id)
  129. {
  130. if (($response = $this->mustBeConnected(true)))
  131. {
  132. return $response;
  133. }
  134. if (!($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
  135. 'id' => $tag_id,
  136. 'tomoderate' => true
  137. ))))
  138. {
  139. return $this->jsonResponse(array(
  140. 'status' => 'error',
  141. 'message' => 'NotFound'
  142. ));
  143. }
  144. $tag_array = json_decode($tag_new_id);
  145. if (!array_key_exists(0, $tag_array))
  146. {
  147. return $this->jsonResponse(array(
  148. 'status' => 'error',
  149. 'message' => 'netTagError'
  150. ));
  151. }
  152. $tag_new_id = $tag_array[0];
  153. $tagManager = new TagManager();
  154. if (!$tagManager->moderateTag($this->getDoctrine(), $tag, false, $tag_new_id))
  155. {
  156. return $this->jsonResponse(array(
  157. 'status' => 'error',
  158. 'message' => 'NotFound'
  159. ));
  160. }
  161. return $this->jsonResponse(array(
  162. 'status' => 'success'
  163. ));
  164. }
  165. /**
  166. *
  167. * @Template()
  168. */
  169. public function elementsAction()
  170. {
  171. // Récupération des elements
  172. $elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  173. ->getToModerate();
  174. return array(
  175. 'elements' => $elements
  176. );
  177. }
  178. /**
  179. *
  180. */
  181. public function deleteElementAction($element_id)
  182. {
  183. if (($response = $this->mustBeConnected(true)))
  184. {
  185. return $response;
  186. }
  187. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  188. ->findOneById($element_id))
  189. )
  190. {
  191. return $this->jsonResponse(array(
  192. 'status' => 'error',
  193. 'errors' => array('NotFound')
  194. ));
  195. }
  196. $event = new EventElement($this->container);
  197. $event->elementRemoved($element);
  198. $element->getOwner()->addModeratedElementCount();
  199. $this->getDoctrine()->getEntityManager()->persist($element->getOwner());
  200. $this->getDoctrine()->getEntityManager()->remove($element);
  201. $this->getDoctrine()->getEntityManager()->flush();
  202. return $this->jsonResponse(array(
  203. 'status' => 'success'
  204. ));
  205. }
  206. public function cleanElementAction($element_id)
  207. {
  208. if (($response = $this->mustBeConnected(true)))
  209. {
  210. return $response;
  211. }
  212. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  213. ->findOneById($element_id))
  214. )
  215. {
  216. return $this->jsonResponse(array(
  217. 'status' => 'error',
  218. 'errors' => array('NotFound')
  219. ));
  220. }
  221. $user_ids = $element->getReportIds();
  222. $element->setReportIds(null);
  223. $element->setCountReport(null);
  224. $this->getDoctrine()->getEntityManager()->persist($element);
  225. $users = $this->getDoctrine()->getEntityManager()
  226. ->createQuery('
  227. SELECT u FROM MuzichCoreBundle:User u
  228. WHERE u.id IN (:uids)'
  229. )
  230. ->setParameter('uids', $user_ids)
  231. ->getResult()
  232. ;
  233. foreach ($users as $user)
  234. {
  235. $user->addBadReport();
  236. $this->getDoctrine()->getEntityManager()->persist($user);
  237. }
  238. $this->getDoctrine()->getEntityManager()->flush();
  239. return $this->jsonResponse(array(
  240. 'status' => 'success'
  241. ));
  242. }
  243. /**
  244. *
  245. * @Template()
  246. */
  247. public function commentsAction()
  248. {
  249. // Récupération des elements
  250. $elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  251. ->getForCommentToModerate();
  252. $comments = array();
  253. foreach ($elements as $element)
  254. {
  255. $cm = new CommentsManager($element->getComments());
  256. foreach ($cm->getAlertedComments() as $comment)
  257. {
  258. $comments[] = array(
  259. 'element_id' => $element->getId(),
  260. 'comment' => $comment
  261. );
  262. }
  263. }
  264. return array(
  265. 'comments' => $comments
  266. );
  267. }
  268. /**
  269. * Considérer le commentaire signalé comme étant tout a fait acceptable
  270. *
  271. * Ceci induit:
  272. * * Que le commentaire en question ne soit plus signalé
  273. * * Que le ou les ids d'utilisateurs qui l'ont signalé comme soit "pénalisé
  274. *
  275. * @param int $element_id
  276. * @param date $date
  277. *
  278. * @return Response
  279. */
  280. public function commentCleanAction($element_id, $date)
  281. {
  282. if (($response = $this->mustBeConnected(true)))
  283. {
  284. return $response;
  285. }
  286. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  287. ->findOneById($element_id))
  288. )
  289. {
  290. return $this->jsonResponse(array(
  291. 'status' => 'error',
  292. 'errors' => array('NotFound')
  293. ));
  294. }
  295. $cm = new CommentsManager($element->getComments());
  296. // On nettoie le commentaire et on récupère les ids des "signaleurs"
  297. $ids = $cm->cleanAlertsOnComment($date);
  298. $element->setComments($cm->get());
  299. $element->setCountCommentReport($cm->countCommentAlert());
  300. $this->getDoctrine()->getEntityManager()->persist($element);
  301. // On récupère les user qui ont signalés ce commentaire
  302. $users = $this->getDoctrine()->getEntityManager()
  303. ->createQuery('
  304. SELECT u FROM MuzichCoreBundle:User u
  305. WHERE u.id IN (:uids)'
  306. )
  307. ->setParameter('uids', $ids)
  308. ->getResult()
  309. ;
  310. // Pour chacun on augmente le compteur de signalements inutiles
  311. foreach ($users as $user)
  312. {
  313. $user->addBadReport();
  314. $this->getDoctrine()->getEntityManager()->persist($user);
  315. }
  316. $this->getDoctrine()->getEntityManager()->flush();
  317. return $this->jsonResponse(array(
  318. 'status' => 'success'
  319. ));
  320. }
  321. /**
  322. * Considérer le commentaire signalé comme étant tout a fait acceptable
  323. *
  324. * Ceci induit:
  325. * * Que le commentaire en question ne soit plus signalé
  326. * * Que le ou les ids d'utilisateurs qui l'ont signalé comme soit "pénalisé
  327. *
  328. * @param int $element_id
  329. * @param date $date
  330. *
  331. * @return Response
  332. */
  333. public function commentRefuseAction($element_id, $date)
  334. {
  335. if (($response = $this->mustBeConnected(true)))
  336. {
  337. return $response;
  338. }
  339. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  340. ->findOneById($element_id))
  341. )
  342. {
  343. return $this->jsonResponse(array(
  344. 'status' => 'error',
  345. 'errors' => array('NotFound')
  346. ));
  347. }
  348. $cm = new CommentsManager($element->getComments());
  349. $comment = $cm->get($cm->getIndexWithDate($date));
  350. // On supprime le commentaire
  351. $cm->deleteWithDate($date);
  352. $element->setComments($cm->get());
  353. $element->setCountCommentReport($cm->countCommentAlert());
  354. // On récupère l'auteur du commentaire pour lui incrémenté son compteur
  355. // de contenu modéré
  356. $user = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:User')
  357. ->findOneBy(array(
  358. 'id' => $comment['u']['i']
  359. ));
  360. $user->addModeratedCommentCount();
  361. $this->getDoctrine()->getEntityManager()->persist($user);
  362. $this->getDoctrine()->getEntityManager()->persist($element);
  363. $this->getDoctrine()->getEntityManager()->flush();
  364. return $this->jsonResponse(array(
  365. 'status' => 'success'
  366. ));
  367. }
  368. }