CommentController.php 11KB

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