CommentController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. namespace Muzich\CommentBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Muzich\CoreBundle\Managers\CommentsManager;
  5. use Muzich\CoreBundle\Propagator\EventElement;
  6. use Muzich\CoreBundle\Security\Context as SecurityContext;
  7. class CommentController extends Controller
  8. {
  9. /**
  10. * Action d'ajouter un commentaire.
  11. *
  12. * @param int $element_id
  13. * @param string $token
  14. * @return \Symfony\Component\HttpFoundation\Response
  15. */
  16. public function addAction($element_id, $token)
  17. {
  18. if (($response = $this->mustBeConnected(true)))
  19. {
  20. return $response;
  21. }
  22. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  23. ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
  24. {
  25. return $this->jsonResponse(array(
  26. 'status' => 'error',
  27. 'errors' => array('NotFound')
  28. ));
  29. }
  30. $length = strlen((($comment = $this->getRequest()->request->get('comment'))));
  31. // TODO: Faire un objet pour le formulaire ajout de commentaire
  32. if ($length < $this->container->getParameter('comment_add_min_length'))
  33. {
  34. return $this->jsonResponse(array(
  35. 'status' => 'error',
  36. 'errors' => array($this->trans(
  37. 'element.comments.errors.min',
  38. array(
  39. '%limit%' => $this->container->getParameter('comment_add_min_length')
  40. ),
  41. 'elements'
  42. )
  43. )));
  44. }
  45. if ($length > $this->container->getParameter('comment_add_max_length'))
  46. {
  47. return $this->jsonResponse(array(
  48. 'status' => 'error',
  49. 'errors' => array($this->trans(
  50. 'element.comments.errors.max',
  51. array(
  52. '%limit%' => $this->container->getParameter('comment_add_max_length')
  53. ),
  54. 'elements'
  55. )
  56. )));
  57. }
  58. $follow = false;
  59. if ($this->getRequest()->request->get('follow') == true)
  60. {
  61. $follow = true;
  62. }
  63. // On met a jour les commentaires
  64. $cm = new CommentsManager($element->getComments());
  65. $cm->add($this->getUser(), $comment, $follow);
  66. $element->setComments($cm->get());
  67. $event = new EventElement($this->container);
  68. // Event pour user d'un nouveau comment
  69. $event->commentAdded($element, $this->getUser());
  70. $this->getDoctrine()->getEntityManager()->persist($element);
  71. $this->getDoctrine()->getEntityManager()->flush();
  72. // On récupère le html du li avec le comment pour la réponse
  73. $html = $this->render('MuzichCommentBundle:Comment:li.comment.html.twig', array(
  74. 'comment' => $cm->getLast(),
  75. 'element_id' => $element->getId()
  76. ))->getContent();
  77. return $this->jsonResponse(array(
  78. 'status' => 'success',
  79. 'html' => $html
  80. ));
  81. }
  82. /**
  83. * Suppression d'un commentaire
  84. *
  85. * @param type $element_id
  86. * @param type $date
  87. * @param type $token
  88. * @return \Symfony\Component\HttpFoundation\Response
  89. */
  90. public function deleteAction($element_id, $date, $token)
  91. {
  92. if (($response = $this->mustBeConnected(true)))
  93. {
  94. return $response;
  95. }
  96. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  97. ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
  98. {
  99. return $this->jsonResponse(array(
  100. 'status' => 'error',
  101. 'errors' => array('NotFound')
  102. ));
  103. }
  104. // On met a jour les commentaires
  105. $cm = new CommentsManager($element->getComments());
  106. // On utilise le comment manager pour supprimer le commentaire de la liste
  107. if (!$cm->delete($this->getUserId(), $date))
  108. {
  109. // Si il n'a pas été trouvé on répond une erreur
  110. return $this->jsonResponse(array(
  111. 'status' => 'error',
  112. 'errors' => array($this->trans(
  113. 'element.comments.errors.unknow',
  114. array(),
  115. 'elements'
  116. )
  117. )));
  118. }
  119. // Si tout c'est bien passé on met a jour l'attribut de l'élément
  120. $element->setComments($cm->get());
  121. $this->getDoctrine()->getEntityManager()->persist($element);
  122. $this->getDoctrine()->getEntityManager()->flush();
  123. return $this->jsonResponse(array(
  124. 'status' => 'success'
  125. ));
  126. }
  127. /**
  128. * Modification d'un commentaire, ouverture du formulaire
  129. *
  130. * @param int $element_id
  131. * @param string $date (Y-m-d H:i:s u)
  132. * @param string $token
  133. * @return Response
  134. */
  135. public function editAction($element_id, $date, $token)
  136. {
  137. if (($response = $this->mustBeConnected(true)))
  138. {
  139. return $response;
  140. }
  141. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  142. ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
  143. {
  144. return $this->jsonResponse(array(
  145. 'status' => 'error',
  146. 'errors' => array('NotFound')
  147. ));
  148. }
  149. // On utilise le gestionnaire de commentaire
  150. $cm = new CommentsManager($element->getComments());
  151. // On récupére le commentaire visé
  152. $comment = $cm->get($cm->getIndex($this->getUserId(), $date));
  153. // On rpépare la réponse html (formulaire)
  154. $html = $this->render('MuzichCommentBundle:Comment:edit.html.twig', array(
  155. 'comment' => $comment,
  156. 'element_id' => $element->getId(),
  157. 'date' => $date,
  158. 'following' => $element->userFollowComments($this->getUserId()),
  159. 'own' => ($this->getUserId() == $element->getOwner()->getId())
  160. ))->getContent();
  161. // On retourne le tout
  162. return $this->jsonResponse(array(
  163. 'status' => 'success',
  164. 'html' => $html
  165. ));
  166. }
  167. /**
  168. * Mise a jour du commentaire. On précise dom_id pour retrouver facilement le
  169. * commentaire dans le dom lorsque js récupére la réponse.
  170. *
  171. * @param int $element_id
  172. * @param string $date (Y-m-d H:i:s u)
  173. * @param string $token
  174. * @param string $dom_id
  175. * @return type
  176. */
  177. public function updateAction($element_id, $date, $token, $dom_id)
  178. {
  179. if (($response = $this->mustBeConnected(true)))
  180. {
  181. return $response;
  182. }
  183. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  184. ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
  185. {
  186. return $this->jsonResponse(array(
  187. 'status' => 'error',
  188. 'errors' => array('NotFound')
  189. ));
  190. }
  191. // TODO: Faire un objet pour le formulaire ajout de commentaire
  192. $length = strlen((($comment = $this->getRequest()->request->get('comment'))));
  193. // TODO: Faire un objet pour le formulaire ajout de commentaire
  194. if ($length < $this->container->getParameter('comment_add_min_length'))
  195. {
  196. return $this->jsonResponse(array(
  197. 'status' => 'error',
  198. 'dom_id' => $dom_id,
  199. 'errors' => array($this->trans(
  200. 'element.comments.errors.min',
  201. array(
  202. '%limit%' => $this->container->getParameter('comment_add_min_length')
  203. ),
  204. 'elements'
  205. )
  206. )));
  207. }
  208. if ($length > $this->container->getParameter('comment_add_max_length'))
  209. {
  210. return $this->jsonResponse(array(
  211. 'status' => 'error',
  212. 'dom_id' => $dom_id,
  213. 'errors' => array($this->trans(
  214. 'element.comments.errors.max',
  215. array(
  216. '%limit%' => $this->container->getParameter('comment_add_max_length')
  217. ),
  218. 'elements'
  219. )
  220. )));
  221. }
  222. $follow = false;
  223. if ($this->getRequest()->request->get('follow') == true)
  224. {
  225. $follow = true;
  226. }
  227. // On met a jour les commentaires
  228. $cm = new CommentsManager($element->getComments());
  229. $cm->update($this->getUser(), $date, $comment, $follow);
  230. $element->setComments($cm->get());
  231. $this->getDoctrine()->getEntityManager()->persist($element);
  232. $this->getDoctrine()->getEntityManager()->flush();
  233. if (null === ($comment_index = $cm->getIndex($this->getUserId(), $date)))
  234. {
  235. return $this->jsonResponse(array(
  236. 'status' => 'error',
  237. 'dom_id' => $dom_id,
  238. 'errors' => array($this->trans(
  239. 'element.comments.errors.unknow',
  240. array(),
  241. 'elements'
  242. )
  243. )));
  244. }
  245. // On récupère le html du li avec le comment pour la réponse
  246. $html = $this->render('MuzichCommentBundle:Comment:comment.html.twig', array(
  247. 'comment' => $cm->get($comment_index),
  248. 'element_id' => $element->getId()
  249. ))->getContent();
  250. return $this->jsonResponse(array(
  251. 'status' => 'success',
  252. 'dom_id' => $dom_id,
  253. 'html' => $html
  254. ));
  255. }
  256. /**
  257. * Signalement d'un commentaire
  258. *
  259. * @param int $element_id
  260. * @param date $date
  261. * @param string $token
  262. * @param string $dom_id
  263. * @return Response
  264. */
  265. public function alertAction($element_id, $date, $token)
  266. {
  267. if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_COMMENT_ALERT)) !== false)
  268. {
  269. return $this->jsonResponseError($non_condition);
  270. }
  271. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  272. ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
  273. {
  274. return $this->jsonResponse(array(
  275. 'status' => 'error',
  276. 'errors' => array('NotFound')
  277. ));
  278. }
  279. // Création de l'objet de gestion des commentaires
  280. $cm = new CommentsManager($element->getComments());
  281. $cm->alertComment($this->getUserId(), $date);
  282. $element->setComments($cm->get());
  283. $element->setCountCommentReport($cm->countCommentAlert());
  284. $this->getDoctrine()->getEntityManager()->persist($element);
  285. $this->getDoctrine()->getEntityManager()->flush();
  286. return $this->jsonResponse(array(
  287. 'status' => 'success'
  288. ));
  289. }
  290. /**
  291. * Signalement d'un commentaire
  292. *
  293. * @param int $element_id
  294. * @param date $date
  295. * @param string $token
  296. * @param string $dom_id
  297. * @return Response
  298. */
  299. public function unAlertAction($element_id, $date, $token)
  300. {
  301. if (($response = $this->mustBeConnected(true)))
  302. {
  303. return $response;
  304. }
  305. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  306. ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
  307. {
  308. return $this->jsonResponse(array(
  309. 'status' => 'error',
  310. 'errors' => array('NotFound')
  311. ));
  312. }
  313. // Création de l'objet de gestion des commentaires
  314. $cm = new CommentsManager($element->getComments());
  315. $cm->unAlertComment($this->getUserId(), $date);
  316. $element->setComments($cm->get());
  317. $element->setCountCommentReport($cm->countCommentAlert());
  318. $this->getDoctrine()->getEntityManager()->persist($element);
  319. $this->getDoctrine()->getEntityManager()->flush();
  320. return $this->jsonResponse(array(
  321. 'status' => 'success'
  322. ));
  323. }
  324. }