public function reverseTransform($urlString) { if (!$urlString) { return null; } $url = $this->em->getRepository('AppBundle:Url')->findOneBy(array('urlKey' => md5($urlString))); if (!$url instanceof Url) { $url = new Url(); $url->setUrl($urlString); } return $url; }
/** * @Route("/add", name="bookmark_add") */ public function addAction(Request $request) { $em = $this->getDoctrine()->getManager(); $bookmark = new Bookmark(); $bookmark->setUser($this->getUser()); // Url-Parameter $urlParameter = $request->get('url', null); $titleParameter = $request->get('title', null); if (!is_null($urlParameter)) { $url = new Url(); $url->setUrl($urlParameter); $bookmark->setUrl($url); $validator = $this->get('validator'); $errors = $validator->validateValue($urlParameter, new Constraints\Url()); if ($errors->count() != 0) { foreach ($errors as $error) { $this->addFlash('danger', $error); } } else { $tagTransformer = new TagTransformer($em); $bookmark->setTitle($titleParameter); $url->setUrl($urlParameter); $tags = $tagTransformer->reverseTransform($tagTransformer->titleToTags($titleParameter)); if (count($tags) != 0) { foreach ($tags as $tag) { $bookmark->addTag($tag); } } } } $bookmarkForm = $this->createForm('app_bookmark', $bookmark, array('action' => $this->generateUrl('bookmark_add', array('nowindow' => $request->get('nowindow', null))), 'nowindow' => $request->get('nowindow', null)))->handleRequest($request); if ($bookmarkForm->isValid()) { $em->persist($bookmark); $em->flush(); if ($bookmarkForm->has('nowindow') && !is_null($bookmarkForm->get('nowindow')->getData())) { return new Response('<script>window.close();</script>'); } $this->addFlash('success', 'flashbag_bookmark_add_success'); return $this->redirectToRoute('homepage'); } return $this->render('Default/add.html.twig', array('bookmarkForm' => $bookmarkForm->createView())); }