public function friendRequestAction($type) { $request = Request::createFromGlobals(); if (!$request->isXmlHttpRequest()) { throw new AccessDeniedException("Access denied!"); } $em = $this->container->get('doctrine')->getManager(); # get user to accept $userid = $request->request->get('_userid'); $user = $em->getRepository('MaximCMSBundle:User')->findOneBy(array("id" => $userid)); if (!$user) { throw new NotFoundHttpException("User could not be found."); } # get the friendrequest $frequest = $em->getRepository('MaximCMSBundle:FriendRequest')->findOneBy(array("recipient" => $this->container->get('security.context')->getToken()->getUser(), "user" => $user)); if (!$frequest || in_array($frequest->getState(), array(FriendRequest::STATE_ACCEPT, FriendRequest::STATE_DENY))) { return new Response(json_encode(array("success" => false, "userid" => $user->getId(), "message" => "Could not find the request, possibly the friend has already been accepted or denied."))); } try { if (strtoupper($type) == "APPROVE") { $frequest->setState(FriendRequest::STATE_ACCEPT); $frequest->setChangedOn(new \DateTime("now")); # add the friend to the receiving user $friend = new UserFriend(); $friend->setUser($this->container->get('security.context')->getToken()->getUser()); $friend->setFriend($user); $em->persist($friend); # add the friend to the requesting user $friend = new UserFriend(); $friend->setUser($user); $friend->setFriend($this->container->get('security.context')->getToken()->getUser()); $em->persist($friend); # notify the user who sent the request $notification = new UserNotification(); $notification->setReceiver($frequest->getUser()); $notification->setUser($frequest->getRecipient()); $notification->setType($notification::TYPE_FRIENDREQUEST); $notification->addData('accepted', true); $em->persist($notification); } else { $frequest->setState(FriendRequest::STATE_DENY); $frequest->setChangedOn(new \DateTime("now")); } $em->flush(); } catch (\Exception $ex) { $this->container->get('logger')->error($ex->getMessage()); return new Response(json_encode(array("success" => false, "userid" => $user->getId(), "message" => "An error occured, please try again later"))); } return new Response(json_encode(array("success" => true, "userid" => $user->getId()))); }