CommentController.php 11KB

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