/** * @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())); }
/** * @Route("/notebook/bookmarks/createBookmark/", name="bookmarksCreateBookmark") * @Method("POST") */ public function createBookmarkAction(Request $request) { $project = $request->request->get('project'); $folder = $request->request->get('folder'); $date = new \DateTime("now"); $user = $this->get('security.token_storage')->getToken()->getUser(); $userId = $user->getId(); $bookmark = new Bookmark(); $bookmark->setUserId($userId); $bookmark->setDateCreated($date); $bookmark->setDateModified($date); $bookmark->setName(""); $bookmark->setUrl(""); $bookmark->setNotes(""); $bookmark->setLabels(""); $bookmark->setProject($project); $bookmark->setFolder($folder); $em = $this->getDoctrine()->getManager(); $em->persist($bookmark); $em->flush(); $response = new JsonResponse(array('id' => $bookmark->getId(), 'project' => $bookmark->getProject(), 'folder' => $bookmark->getFolder())); return $response; }
/** * @Route("/ajax/bookmark/create", name="ajax_create_bookmark") */ public function ajaxCreateAction(Request $request) { $em = $this->getDoctrine()->getManager(); $repository = $em->getRepository('AppBundle:Bookmark'); $tagRepository = $em->getRepository('AppBundle:Tag'); $openGraph = $this->container->get('open_graph'); $url = $request->request->get('url'); $tags = $request->request->get('tags'); $user = $this->get('security.context')->getToken()->getUser(); if (!$url) { $data = array('error' => 'invalid_url', 'errorMsg' => 'URL is required'); $json = \json_encode($data); $response = new Response($json, 404); $response->headers->set('Content-Type', 'application/json'); return $response; } $tags = explode(',', $tags); try { $data = $openGraph->read($url); } catch (RequestException $e) { $context = $e->getHandlerContext(); $data = array('error' => 'invalid_url'); if (isset($context['error'])) { $data['errorMsg'] = $context['error']; } $json = \json_encode($data); $response = new Response($json, 404); $response->headers->set('Content-Type', 'application/json'); return $response; } catch (ConnectException $e) { $data = array('error' => 'invalid_page', 'exception' => $e->getMessage()); $json = \json_encode($data); $response = new Response($json, 404); $response->headers->set('Content-Type', 'application/json'); return $response; } catch (\Exception $e) { $data = array('error' => 'exception', 'exception' => $e->getMessage()); $json = \json_encode($data); $response = new Response($json, 500); $response->headers->set('Content-Type', 'application/json'); return $response; } $title = $data['title']; $description = $data['description']; if (isset($data['og:title']) && $data['og:title']) { $title = $data['og:title']; } if (isset($data['og:description']) && $data['og:description']) { $description = $data['og:description']; } $bookmark = new Bookmark(); $bookmark->setUrl($url); $bookmark->setTitle($title); $bookmark->setUser($user); $bookmark->setDescription($description); if (isset($data['og:image'])) { $bookmark->setImage($data['og:image']); } $bookmarkTags = array(); foreach ($tags as $tagName) { $tag = new Tag(); $tag->setName($tagName); $tag->setUser($user); $em->persist($tag); $bookmarkTags[] = $tag; } $bookmark->setTags($bookmarkTags); $em->persist($bookmark); $em->flush(); $normalizer = new ObjectNormalizer(); $normalizer->setCircularReferenceHandler(function ($object) { return $object->getId(); }); $json = \json_encode($bookmark); $response = new Response($json); $response->headers->set('Content-Type', 'application/json'); return $response; }