public function executesubAction2($id) { /** @var Request $request */ $request = $this->container->get('request_stack')->getCurrentRequest(); $em = $this->getDoctrine()->getManager(); $user = $this->container->get('security.context')->getToken()->getUser(); $subaction = $em->find('AcmeEdelaBundle:Subaction', $id); $action = $subaction->getAction(); $userAction = $em->getRepository('AcmeEdelaBundle:UserAction')->findOneBy(array('user' => $user, 'action' => $action)); $success = false; try { if (!$userAction || !$userAction->getIsDayIncluded(new \DateTime())) { throw new \Exception(); } $existingSubprogress = $em->getRepository('AcmeEdelaBundle:UserSubactionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('user', $user))->andWhere(Criteria::expr()->eq('subaction', $subaction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingSubprogress) { throw new \Exception(); } $existingProgress = $em->getRepository('AcmeEdelaBundle:UserActionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('userAction', $userAction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingProgress > 0) { throw new \Exception(); } $subprogress = new UserSubactionProgress(); $subprogress->setUser($user)->setSubaction($subaction); $em->persist($subprogress); $em->flush(); if ($action->getSubactions()->count() == count($em->getRepository('AcmeEdelaBundle:Action')->getSubactions($action, $user, null, true))) { $progress = new UserActionProgress(); $progress->setUserAction($userAction); $progress->setResult($request->get('result', 1)); $em->persist($progress); } $em->flush(); return ['success' => true]; } catch (\Exception $e) { return ['success' => false]; } }
public function executeSubaction(Subaction $subaction, User $user) { $em = $this->getEntityManager(); $userTime = $user->getCurrentDateTime(); $dayStart = clone $userTime; $dayFinish = clone $userTime; $dayStart->modify('today midnight'); $dayFinish->modify('tomorrow midnight'); $existingSubprogress = $em->getRepository('AcmeEdelaBundle:UserSubactionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('user', $user))->andWhere(Criteria::expr()->eq('subaction', $subaction))->andWhere(Criteria::expr()->gte('createdAt', $dayStart))->andWhere(Criteria::expr()->lte('createdAt', $dayFinish))); if ($existingSubprogress->count()) { $em->remove($existingSubprogress->first()); $progress = false; } else { $subprogress = new UserSubactionProgress(); $subprogress->setUser($user)->setSubaction($subaction); $em->persist($subprogress); $progress = true; } $em->flush(); return $progress; }
public function executesubAction($subaction_id) { /** @var Request $request */ $request = $this->container->get('request_stack')->getCurrentRequest(); $em = $this->getDoctrine()->getManager(); $user = $this->container->get('security.context')->getToken()->getUser(); $subaction = $em->find('AcmeEdelaBundle:Subaction', $subaction_id); $action = $subaction->getAction(); $userAction = $em->getRepository('AcmeEdelaBundle:UserAction')->findOneBy(array('user' => $user, 'action' => $action)); $success = false; try { if (!$userAction || !$userAction->getIsDayIncluded(new \DateTime())) { throw new \Exception(); } $existingSubprogress = $em->getRepository('AcmeEdelaBundle:UserSubactionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('user', $user))->andWhere(Criteria::expr()->eq('subaction', $subaction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingSubprogress) { throw new \Exception(); } $existingProgress = $em->getRepository('AcmeEdelaBundle:UserActionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('userAction', $userAction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingProgress > 0) { throw new \Exception(); } $subprogress = new UserSubactionProgress(); $subprogress->setUser($user)->setSubaction($subaction); $em->persist($subprogress); $em->flush(); if ($action->getSubactions()->count() == count($em->getRepository('AcmeEdelaBundle:Action')->getSubactions($action, $user, null, true))) { $progress = new UserActionProgress(); $progress->setUserAction($userAction); $progress->setResult($request->get('result', 1)); $em->persist($progress); } $em->flush(); $success = true; } catch (\Exception $e) { $success = false; } if ($success) { if ($request->isXmlHttpRequest()) { return new JsonResponse(['success' => true, 'text' => $this->renderView('AcmeEdelaBundle:Actions:_one_block.html.twig', array('action' => $action, 'progress' => $progress))]); } else { return new RedirectResponse($this->container->get('router')->generate('tasks_edit', array('task_id' => $action->getId()))); } } else { return new JsonResponse(['success' => false]); } }