/** * @Route("/ajax", name="oktothek_tag_ajax") * @Method({"GET", "POST"}) */ public function ajaxTagAction(Request $request) { if ($request->isXmlHttpRequest()) { $em = $this->getDoctrine()->getManager(); if ($request->getMethod() == "GET") { $tags = $em->getRepository('AppBundle:Tag')->findAll(); $json = []; foreach ($tags as $tag) { $json[] = $tag->getText(); } return new Response(json_encode($json)); } else { //Posts tag $action = $request->request->get('action'); if ($action = "add") { // add new tag $tag = $em->getRepository('AppBundle:Tag')->findOneBy(['text' => $request->request->get('text')]); if (!$tag) { $tag = new Tag(); $tag->setText($request->request->get('text')); $em->persist($tag); $em->flush(); } return new Response(); } else { // delete tag $tag = $em->getRepository('AppBundle:Tag')->findOneBy(['text' => $request->request->get('text')]); $em->remove($tag); $em->flush(); return new Response(); } } } return $this->redirect('oktothek_tag_index'); }