/**
  * @Route("/new/{date}")
  * @ParamConverter("date", options={"format": "d-m-Y"})
  */
 public function newAction(Request $request, DateTime $date)
 {
     $form = $this->createForm(StatisticEntryContainerType::class, null, array('data' => array('date' => $date)));
     if ($request->isMethod(Request::METHOD_POST)) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             /** @var UsernamePasswordToken $token */
             $token = $this->container->get('security.token_storage')->getToken();
             $data = $form->getData();
             /** @var TrainingDayRepository $repository */
             $repository = $this->getDoctrine()->getManager()->getRepository('TrainingScheduleBundle:TrainingDay');
             $trainingDay = $repository->findOneBy(array('date' => $data['date'], 'user' => $token->getUser()));
             if (is_null($trainingDay)) {
                 $trainingDay = new TrainingDay();
                 $trainingDay->setDate($data['date']);
                 $trainingDay->setUser($token->getUser());
                 $this->getDoctrine()->getManager()->persist($trainingDay);
             }
             /** @var StatisticEntry $statEntry */
             $statEntry = $data['statisticEntry'];
             $statEntry->setTrainingDay($trainingDay);
             $this->getDoctrine()->getManager()->persist($trainingDay);
             $this->getDoctrine()->getManager()->persist($trainingDay);
             $this->getDoctrine()->getManager()->persist($data['statisticEntry']);
             $this->getDoctrine()->getManager()->flush();
             return $this->redirectToRoute('trainingschedule_overview_index');
         }
     }
     return $this->render('TrainingScheduleBundle:StatisticEntry:new.html.twig', array('form' => $form->createView()));
 }
 /**
  * Load data fixtures with the passed EntityManager
  *
  * @param \Doctrine\Common\Persistence\ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $user = $this->getReference('user1');
     $trainigDay = new TrainingDay();
     $trainigDay->setDate(new \DateTime());
     $trainigDay->setUser($user);
     $dateMonday = new \DateTime();
     $dateMonday->modify('last monday');
     $trainigMonday = new TrainingDay();
     $trainigMonday->setDate($dateMonday);
     $trainigMonday->setUser($user);
     $enduranceTraining = new EnduranceTraining();
     $enduranceTraining->setName('Joggen');
     $enduranceTraining->setLength(45);
     $enduranceTraining->setNote('Bli');
     $enduranceTraining2 = new EnduranceTraining();
     $enduranceTraining2->setName('Schwimmen');
     $enduranceTraining2->setLength(100);
     $enduranceTraining2->setNote('Bla');
     $enduranceTraining3 = new EnduranceTraining();
     $enduranceTraining3->setName('Laufen');
     $enduranceTraining3->setLength(150);
     $enduranceTraining3->setNote('Blub');
     $enduranceTraining->setTrainingDay($trainigDay);
     $enduranceTraining2->setTrainingDay($trainigDay);
     $enduranceTraining3->setTrainingDay($trainigMonday);
     $manager->persist($trainigDay);
     $manager->persist($trainigMonday);
     $manager->persist($enduranceTraining);
     $manager->persist($enduranceTraining2);
     $manager->persist($enduranceTraining3);
     $manager->flush();
     $this->addReference('trainingDay', $trainigDay);
     $this->addReference('trainingMonday', $trainigMonday);
 }
 /**
  * @Route("/new/{date}")
  * @ParamConverter("date", options={"format": "d-m-Y"})
  * @param Request  $request
  * @param Datetime $date
  *
  * @return \Symfony\Component\HttpFoundation\Request
  */
 public function newAction(Request $request, Datetime $date)
 {
     $form = $this->createForm(TrainingContainerType::class, null, array('data' => array('date' => $date, 'route_back' => $request->getHost() . $this->generateUrl('trainingschedule_overview_index', array(), UrlGeneratorInterface::ABSOLUTE_PATH)), 'required' => false));
     /** @var UsernamePasswordToken $token */
     $token = $this->container->get('security.token_storage')->getToken();
     if ($request->isMethod(Request::METHOD_POST)) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $data = $form->getData();
             /** @var TrainingDayRepository $repository */
             $repository = $this->getDoctrine()->getManager()->getRepository('TrainingScheduleBundle:TrainingDay');
             $trainingDay = $repository->findOneBy(array('date' => $data['date'], 'user' => $token->getUser()));
             if (is_null($trainingDay)) {
                 $trainingDay = new TrainingDay();
                 $trainingDay->setDate($data['date']);
                 $trainingDay->setUser($token->getUser());
                 $this->getDoctrine()->getManager()->persist($trainingDay);
             }
             if ($data['Training']['type'] == EnduranceTraining::__className()) {
                 /** @var EnduranceTraining $enduranceTraining */
                 $enduranceTraining = $data['Training']['enduranceTraining'];
                 $enduranceTraining->setName($data['Training']['name']);
                 $enduranceTraining->setNote($data['Training']['note']);
                 $enduranceTraining->setTrainingDay($trainingDay);
                 $this->getDoctrine()->getManager()->persist($enduranceTraining);
             }
             if ($data['Training']['type'] == StrengthTraining::__className()) {
                 /** @var StrengthTraining $strengthTraining */
                 $strengthTraining = $data['Training']['strengthTraining'];
                 $strengthTraining->setName($data['Training']['name']);
                 $strengthTraining->setNote($data['Training']['note']);
                 $strengthTraining->setTrainingDay($trainingDay);
                 $this->getDoctrine()->getManager()->persist($strengthTraining);
             }
             $this->getDoctrine()->getManager()->flush();
             return $this->redirectToRoute('trainingschedule_overview_index');
         }
     }
     // generate suggestion for last weekday
     $weekDaySuggestions = TrainingHelper::generateWeekDaySuggestion($this->getDoctrine()->getManager(), $date, $token->getUser());
     // generate suggestion for last entries
     $ownTrainingSuggestions = TrainingHelper::generateOwnTrainingSuggestion($this->getDoctrine()->getManager(), $date, $token->getUser());
     return $this->render('TrainingScheduleBundle:Training:new.html.twig', array('form' => $form->createView(), 'weekDaySuggestions' => $weekDaySuggestions, 'ownTrainingSuggestions' => $ownTrainingSuggestions));
 }
 /**
  * Load data fixtures with the passed EntityManager
  *
  * @param \Doctrine\Common\Persistence\ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $user = $this->getReference('user1');
     $currentDate = new \DateTime();
     $currentDate->modify('-10 days');
     for ($i = 0; $i < 55; $i++) {
         $day = new TrainingDay();
         $day->setDate(new \DateTime($currentDate->modify('-6 day')->format('Y-m-d')));
         $day->setUser($user);
         $manager->persist($day);
         $weightEntry = new StatisticEntry();
         $weightEntry->setName('Weight');
         $weightEntry->setValue(rand(70, 82));
         $weightEntry->setTrainingDay($day);
         $fatEntry = new StatisticEntry();
         $fatEntry->setName('Body Fat');
         $fatEntry->setValue(18 * (100 + $i) / 100);
         $fatEntry->setTrainingDay($day);
         $muscleEntry = new StatisticEntry();
         $muscleEntry->setName('Muscle Mass');
         $muscleEntry->setValue(10 * (100 - $i) / 100);
         $muscleEntry->setTrainingDay($day);
         $manager->persist($weightEntry);
         $manager->persist($fatEntry);
         $manager->persist($muscleEntry);
     }
     for ($i = 0; $i < 55; $i++) {
         $day = new TrainingDay();
         $day->setDate(new \DateTime($currentDate->modify('-6 day')->format('Y-m-d')));
         $day->setUser($user);
         $manager->persist($day);
         $weightEntry = new StatisticEntry();
         $weightEntry->setName('Weight');
         $weightEntry->setValue(rand(75, 82));
         $weightEntry->setTrainingDay($day);
         $manager->persist($weightEntry);
     }
     $manager->flush();
 }