示例#1
0
 public function createShortAction()
 {
     /** @var Request $request */
     $request = $this->container->get('request_stack')->getCurrentRequest();
     $action = new Action();
     $user = $this->container->get('security.context')->getToken()->getUser();
     $action->setUser($user);
     $form = $this->createForm(new ActionCreateShortFormType(), $action, array('em' => $this->getDoctrine()->getManager()));
     $form->handleRequest($request);
     $success = false;
     if ($form->isValid()) {
         $userAction = new UserAction();
         $userAction->setUser($user);
         $userAction->setIsDeleted(false);
         $action->addUserAction($userAction);
         $em = $this->getDoctrine()->getManager();
         $em->persist($action);
         $em->flush();
         $success = true;
     }
     if ($success) {
         if ($request->isXmlHttpRequest()) {
             return new JsonResponse(['success' => true, 'text' => $this->renderView('AcmeEdelaBundle:Actions:_one_block.html.twig', array('action' => $action, 'progress' => null))]);
         } else {
             return new RedirectResponse($this->container->get('router')->generate('tasks_edit', array('task_id' => $action->getId())));
         }
     } else {
         return new JsonResponse(['success' => false]);
     }
 }
示例#2
0
 /**
  * @Rest\View
  * @Rest\Post("/actions")
  */
 public function postActionAction(Request $request)
 {
     $action = new Action();
     $user = $this->container->get('security.context')->getToken()->getUser();
     $action->setUser($user);
     $form = $this->createForm(new ActionCreateShortFormType(), $action, array('em' => $this->getDoctrine()->getManager(), 'csrf_protection' => false));
     $em = $this->getDoctrine()->getManager();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $userAction = new UserAction();
         $userAction->setUser($user);
         $userAction->setStartAt($form->get('start_at')->getData());
         $userAction->setPosition($form->get('position')->getData());
         $userAction->setIsDeleted(0);
         $action->addUserAction($userAction);
         $actionType = $em->getRepository('AcmeEdelaBundle:ActionType')->findOneBy(['tkey' => 'done']);
         $action->setActionType($actionType);
         $dynamicType = $em->getRepository('AcmeEdelaBundle:ActionDynamicType')->findOneBy(['tkey' => 'up']);
         $action->setActionDynamicType($dynamicType);
         $em->persist($action);
         $em->flush();
         $event = new ActionEvent($action, $user, false);
         $dispatcher = $this->get('event_dispatcher');
         $dispatcher->dispatch('event.action.add', $event);
         return ['id' => $action->getId(), 'title' => $action->getTitle(), 'progress' => 0, 'repeat_amount' => $action->getRepeatAmount(), 'start_time' => $userAction->getStartAt(), 'position' => $userAction->getPosition(), 'periodicity' => $userAction->getPeriodicity(), 'periodicity_interval' => $userAction->getPeriodicityInterval(), 'is_private' => $userAction->getIsPrivate(), 'done' => 0, 'action_type_id' => $actionType->getId(), 'action_type_title' => $action->getActionTypeTitle(), 'dynamic_type_id' => $dynamicType->getId(), 'goal' => ['id' => $action->getGoal() ? $action->getGoal()->getId() : null, 'title' => $action->getGoal() ? $action->getGoal()->getName() : null]];
     }
     return $form->getErrors();
 }