Example #1
0
 public function removeComment(\juzz\CommentsBundle\Entity\Comentarios $comment)
 {
     if ($this->comments->contains($comment)) {
         $this->comments->removeElement($comment);
     }
     return $this;
 }
Example #2
0
 public function newAction(Request $request)
 {
     //recogemos datos de la petición.
     $data = $request->request->get('data');
     try {
         $em = $this->getDoctrine()->getManager();
         $comment = new Comment();
         $comment->setFecha(new \DateTime('now'));
         $comment->setContenido($data['content']);
         $comment->setTarget($data['target']);
         if ($data['target'] != $data['owner']) {
             //Comprobamos Política de comentarios.
             $target = $em->getRepository('juzzUsuariosBundle:Usuarios')->find($data['target']);
             if ($target->getPoliticaComentarios()->getId() == 4) {
                 $comment->setValido(false);
             }
         }
         $parent = null;
         if (isset($data['parent']) && is_numeric($data['parent'])) {
             $parent = $em->getRepository('juzzCommentsBundle:Comentarios')->find($data['parent']);
         }
         $comment->setParent($parent);
         $owner = null;
         if (isset($data['owner']) && is_numeric($data['owner'])) {
             $owner = $em->getRepository('juzzUsuariosBundle:Usuarios')->find($data['owner']);
         }
         $comment->setPropietario($owner);
         //Validamos entidad.
         $validator = $this->get('validator');
         $errors = $validator->validate($comment);
         if (count($errors) == 0) {
             $em->persist($comment);
             $em->flush();
             $serializer = $this->get('jms_serializer');
             $response = $serializer->serialize(['success' => true, 'data' => $comment], 'json');
         } else {
             $response = array('success' => false, 'message' => "Post inválido");
         }
         return new JsonResponse($response);
     } catch (\Exception $exception) {
         return new JsonResponse(['success' => false, 'code' => $exception->getCode(), 'message' => $exception->getMessage()]);
     }
 }