Example #1
0
 /**
  * @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);
 }