123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
-
- namespace Muzich\CommentBundle\Controller;
-
- use Muzich\CoreBundle\lib\Controller;
- use Muzich\CoreBundle\Managers\CommentsManager;
- use Muzich\CoreBundle\Propagator\EventElement;
-
- class CommentController extends Controller
- {
-
- /**
- * Action d'ajouter un commentaire.
- *
- * @param int $element_id
- * @param string $token
- * @return \Symfony\Component\HttpFoundation\Response
- */
- public function addAction($element_id, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- $length = strlen((($comment = $this->getRequest()->request->get('comment'))));
-
- // TODO: Faire un objet pour le formulaire ajout de commentaire
- if ($length < $this->container->getParameter('comment_add_min_length'))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array($this->trans(
- 'element.comments.errors.min',
- array(
- '%limit%' => $this->container->getParameter('comment_add_min_length')
- ),
- 'elements'
- )
- )));
- }
- if ($length > $this->container->getParameter('comment_add_max_length'))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array($this->trans(
- 'element.comments.errors.max',
- array(
- '%limit%' => $this->container->getParameter('comment_add_max_length')
- ),
- 'elements'
- )
- )));
- }
-
- $follow = false;
- if ($this->getRequest()->request->get('follow') == true)
- {
- $follow = true;
- }
-
- // On met a jour les commentaires
- $cm = new CommentsManager($element->getComments());
- $cm->add($this->getUser(), $comment, $follow);
- $element->setComments($cm->get());
- $event = new EventElement($this->container);
-
- // Event pour user d'un nouveau comment
- $event->commentAdded($element, $this->getUser());
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- // On récupère le html du li avec le comment pour la réponse
- $html = $this->render('MuzichCommentBundle:Comment:li.comment.html.twig', array(
- 'comment' => $cm->getLast(),
- 'element_id' => $element->getId()
- ))->getContent();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'html' => $html
- ));
-
- }
-
- /**
- * Suppression d'un commentaire
- *
- * @param type $element_id
- * @param type $date
- * @param type $token
- * @return \Symfony\Component\HttpFoundation\Response
- */
- public function deleteAction($element_id, $date, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // On met a jour les commentaires
- $cm = new CommentsManager($element->getComments());
-
- // On utilise le comment manager pour supprimer le commentaire de la liste
- if (!$cm->delete($this->getUserId(), $date))
- {
- // Si il n'a pas été trouvé on répond une erreur
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array($this->trans(
- 'element.comments.errors.unknow',
- array(),
- 'elements'
- )
- )));
- }
-
- // Si tout c'est bien passé on met a jour l'attribut de l'élément
- $element->setComments($cm->get());
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success'
- ));
- }
-
- /**
- * Modification d'un commentaire, ouverture du formulaire
- *
- * @param int $element_id
- * @param string $date (Y-m-d H:i:s u)
- * @param string $token
- * @return Response
- */
- public function editAction($element_id, $date, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // On utilise le gestionnaire de commentaire
- $cm = new CommentsManager($element->getComments());
- // On récupére le commentaire visé
- $comment = $cm->get($cm->getIndex($this->getUserId(), $date));
- // On rpépare la réponse html (formulaire)
- $html = $this->render('MuzichCommentBundle:Comment:edit.html.twig', array(
- 'comment' => $comment,
- 'element_id' => $element->getId(),
- 'date' => $date,
- 'following' => $element->userFollowComments($this->getUserId()),
- 'own' => ($this->getUserId() == $element->getOwner()->getId())
- ))->getContent();
- // On retourne le tout
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'html' => $html
- ));
- }
-
- /**
- * Mise a jour du commentaire. On précise dom_id pour retrouver facilement le
- * commentaire dans le dom lorsque js récupére la réponse.
- *
- * @param int $element_id
- * @param string $date (Y-m-d H:i:s u)
- * @param string $token
- * @param string $dom_id
- * @return type
- */
- public function updateAction($element_id, $date, $token, $dom_id)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // TODO: Faire un objet pour le formulaire ajout de commentaire
- $length = strlen((($comment = $this->getRequest()->request->get('comment'))));
-
- // TODO: Faire un objet pour le formulaire ajout de commentaire
- if ($length < $this->container->getParameter('comment_add_min_length'))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'dom_id' => $dom_id,
- 'errors' => array($this->trans(
- 'element.comments.errors.min',
- array(
- '%limit%' => $this->container->getParameter('comment_add_min_length')
- ),
- 'elements'
- )
- )));
- }
- if ($length > $this->container->getParameter('comment_add_max_length'))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'dom_id' => $dom_id,
- 'errors' => array($this->trans(
- 'element.comments.errors.max',
- array(
- '%limit%' => $this->container->getParameter('comment_add_max_length')
- ),
- 'elements'
- )
- )));
- }
-
- $follow = false;
- if ($this->getRequest()->request->get('follow') == true)
- {
- $follow = true;
- }
-
- // On met a jour les commentaires
- $cm = new CommentsManager($element->getComments());
- $cm->update($this->getUser(), $date, $comment, $follow);
- $element->setComments($cm->get());
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- if (null === ($comment_index = $cm->getIndex($this->getUserId(), $date)))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'dom_id' => $dom_id,
- 'errors' => array($this->trans(
- 'element.comments.errors.unknow',
- array(),
- 'elements'
- )
- )));
- }
-
- // On récupère le html du li avec le comment pour la réponse
- $html = $this->render('MuzichCommentBundle:Comment:comment.html.twig', array(
- 'comment' => $cm->get($comment_index),
- 'element_id' => $element->getId()
- ))->getContent();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'dom_id' => $dom_id,
- 'html' => $html
- ));
-
- }
-
- /**
- * Signalement d'un commentaire
- *
- * @param int $element_id
- * @param date $date
- * @param string $token
- * @param string $dom_id
- * @return Response
- */
- public function alertAction($element_id, $date, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // Création de l'objet de gestion des commentaires
- $cm = new CommentsManager($element->getComments());
- $cm->alertComment($this->getUserId(), $date);
- $element->setComments($cm->get());
- $element->setCountCommentReport($cm->countCommentAlert());
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success'
- ));
- }
-
- /**
- * Signalement d'un commentaire
- *
- * @param int $element_id
- * @param date $date
- * @param string $token
- * @param string $dom_id
- * @return Response
- */
- public function unAlertAction($element_id, $date, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // Création de l'objet de gestion des commentaires
- $cm = new CommentsManager($element->getComments());
- $cm->unAlertComment($this->getUserId(), $date);
- $element->setComments($cm->get());
- $element->setCountCommentReport($cm->countCommentAlert());
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success'
- ));
- }
-
- }
|