/** * @param Goal $value * @return mixed|void */ public function transform($value) { if ($value === null) { return ""; } return $value->getId(); }
/** * @Rest\View * @Rest\Post("/goals") */ public function postGoalAction(Request $request) { $currentUser = $this->container->get('security.context')->getToken()->getUser(); $form = $this->get('form.factory')->createNamedBuilder('', 'form', null, array('csrf_protection' => false))->add('name', 'text', array('label' => 'goal.new.short_label'))->add('images', 'text', array('label' => 'goal.new.short_label'))->getForm(); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $existingGoal = $this->getDoctrine()->getRepository('AcmeEdelaBundle:Goal')->findOneBy(array('name' => $form->getData()['name'], 'user' => $currentUser, 'isDeleted' => 0)); if (!$existingGoal) { $newGoal = new Goal(); $newGoal->setName($form->getData()['name'])->setUser($this->container->get('security.context')->getToken()->getUser()); $em->persist($newGoal); $em->flush(); if (isset($form->getData()['images']) && count($form->getData()['images']) > 0) { $image = $this->getDoctrine()->getRepository('AcmeEdelaBundle:GoalImage')->find($form->getData()['images'][0]['id']); $image->setGoal($newGoal); $em->persist($image); $em->flush(); } return $newGoal; } return ['error' => ['message' => 'existing goal']]; } return ['errors' => $form->getErrors()]; }
public function listAction($user_id) { if ($user_id == 0) { $user = $this->container->get('security.context')->getToken()->getUser(); return new RedirectResponse($this->container->get('router')->generate('goals_list', array('user_id' => $user->getId()))); } $user = $this->getDoctrine()->getRepository('AcmeUserBundle:User')->find($user_id); if (!$user) { throw $this->createNotFoundException('user.not_found'); } $formView = null; if ($user == $this->container->get('security.context')->getToken()->getUser()) { $form = $this->createFormBuilder()->add('name', 'text', array('label' => 'goal.new.short_label'))->getForm(); $request = $this->container->get('request_stack')->getCurrentRequest(); $form->handleRequest($request); if ($form->isValid()) { $existingGoal = $this->getDoctrine()->getRepository('AcmeEdelaBundle:Goal')->findOneBy(array('name' => $form->getData()['name'], 'user' => $user)); if (!$existingGoal) { $newGoal = new Goal(); $newGoal->setName($form->getData()['name'])->setUser($this->container->get('security.context')->getToken()->getUser()); $this->getDoctrine()->getManager()->persist($newGoal); $this->getDoctrine()->getManager()->flush(); return new RedirectResponse($this->container->get('router')->generate('goals_edit', array('goal_id' => $newGoal->getId()))); } $form->get('name')->addError(new FormError('goal.new.existing')); } $formView = $form->createView(); } /** @var $user User */ $goals = $user->getGoals(); if ($user_id != 0 && $user != $this->container->get('security.context')->getToken()->getUser()) { $goals = $goals->matching(Criteria::create()->where(Criteria::expr()->eq('isPrivate', 0))); } return $this->render('AcmeEdelaBundle:Goals:list.html.twig', array('goals' => $goals, 'form' => $formView)); }