/** * @Route("/add") * @Method({"POST"}) */ public function addAction() { $uid = $this->getRequest()->get('uid', 0); $user = $this->getUser(); if (false && !$user) { return $this->getJSONError('No user logged in'); } else { $entity = $this->getRepo('DiveEntity')->findOneBy(array('uid' => $uid)); $manager = $this->getDoctrine()->getManager(); if (!$entity) { $entity = new DiveEntity(); $entity->setUID($uid); $manager->persist($entity); } $request = $this->getRequest(); $comment = new Comment(); $comment->setEntity($entity); $body = $request->get('body'); if (!$body) { return $this->getJSONError('Empty comment given'); } $comment->setBody($body); $comment->setOwner($user); $manager->persist($comment); $manager->flush(); $result = array('success' => true, 'data' => $comment->jsonSerialize()); } return $this->getJSONResponse($result); }
/** * @Route("/{id}/add") * @Method({"POST"}) */ public function addAction($id) { $uid = $this->getRequest()->get('uid', false); if ($uid == false) { $uids = $this->getRequest()->get('uids', false); if ($uids) { $uids = explode(",", $uids); } } else { $uids = array($uid); } $user = $this->getUser(); if (!$user) { return $this->getJSONError('No user logged in'); } else { if ($id == 'new') { $collection = new Collection(); $request = $this->getRequest(); $collection->setTitle($request->get('title', 'Unnamed Collection')); $collection->setDescription($request->get('description', '')); $collection->setPublic($request->get('public', true) || $request->get('public') == 'true'); $collection->setOwner($user); $manager = $this->getDoctrine()->getManager(); $manager->persist($collection); $manager->flush(); } else { $collection = $this->getRepo('Collection')->findOneBy(array('id' => $id, 'owner' => $user)); } if (!$collection) { return $this->getJSONError('Collection not found'); } elseif (!$uids) { return $this->getJSONError('No uid specified'); } else { $manager = $this->getDoctrine()->getManager(); foreach ($uids as $uid) { $entity = $this->getRepo('DiveEntity')->findOneBy(array('uid' => $uid)); if (!$entity) { $entity = new DiveEntity(); $entity->setUid($uid); } $request = $this->getRequest(); if (!$collection->getEntities()->contains($entity)) { $collection->addEntity($entity); } $manager->persist($entity); } $manager->persist($collection); $manager->flush(); $result = array('success' => true, 'uid' => $uid, 'data' => $collection->jsonSerialize()); } } return $this->getJSONResponse($result); }