private function copyParent(\Club\TeamBundle\Entity\Schedule $old, \Club\TeamBundle\Entity\Schedule $schedule)
 {
     $em = $this->getDoctrine()->getManager();
     $schedule->setSchedule(null);
     $rep = new \Club\TeamBundle\Entity\Repetition();
     $rep->setType($old->getRepetition()->getType());
     $rep->setFirstDate($old->getRepetition()->getFirstDate());
     $rep->setLastDate($old->getRepetition()->getLastDate());
     $rep->setEndOccurrences($old->getRepetition()->getEndOccurrences());
     $rep->setRepeatEvery($old->getRepetition()->getRepeatEvery());
     $rep->setDaysInWeek($old->getRepetition()->getDaysInWeek());
     $rep->setDayOfMonth($old->getRepetition()->getDayOfMonth());
     $rep->setWeek($old->getRepetition()->getWeek());
     $rep->setSchedule($schedule);
     $em->persist($rep);
     $schedule->setRepetition($rep);
     $em->persist($schedule);
     return $schedule;
 }
 public function generateSchedules(\Club\TeamBundle\Entity\Schedule $schedule, \DateTime $start)
 {
     // set end time
     if ($schedule->getRepetition()->getLastDate() != '') {
         $end = $schedule->getRepetition()->getLastDate();
     } else {
         $end = new \DateTime();
         $end->add(new \DateInterval('P10Y'));
     }
     // get diff interval
     $diff = $schedule->getFirstDate()->diff($schedule->getEndDate());
     while ($start->getTimestamp() <= $end->getTimestamp()) {
         switch ($schedule->getRepetition()->getType()) {
             case 'daily':
                 $this->addSchedule($start, $diff, $schedule);
                 for ($i = 1; $i < $schedule->getRepetition()->getRepeatEvery(); $i++) {
                     $start->add(new \DateInterval('P1D'));
                     $this->deleteSchedule($start, $schedule);
                 }
                 $start->add(new \DateInterval('P1D'));
                 break;
             case 'weekly':
                 $curr_day = $start->format('N');
                 while ($curr_day < 8) {
                     if (in_array($start->format('N'), $schedule->getRepetition()->getDaysInWeek())) {
                         $this->addSchedule($start, $diff, $schedule);
                     } else {
                         $this->deleteSchedule($start, $schedule);
                     }
                     $curr_day++;
                     $start->add(new \DateInterval('P1D'));
                 }
                 for ($i = 0; $i < ($schedule->getRepetition()->getRepeatEvery() - 1) * 7; $i++) {
                     $start->add(new \DateInterval('P1D'));
                     $this->deleteSchedule($start, $schedule);
                 }
                 break;
             case 'monthly':
                 if ($schedule->getRepetition()->getDayOfMonth()) {
                     $this->addSchedule($start, $diff, $schedule);
                     $t1 = clone $start;
                     $start->add(new \DateInterval('P' . $schedule->getRepetition()->getRepeatEvery() . 'M'));
                     $t2 = clone $start;
                 } else {
                     if (!isset($init_time)) {
                         $init_time = $start->getTimestamp();
                         $temp = new \DateTime($schedule->getRepetition()->getWeek() . ' ' . $schedule->getFirstDate()->format('D') . ' of ' . $start->format('F Y'));
                         $start = new \DateTime($temp->format('Y-m-d') . ' ' . $start->format('H:i:s'));
                         if ($init_time > $start->getTimestamp()) {
                             $start->add(new \DateInterval('P1M'));
                             $temp = new \DateTime($schedule->getRepetition()->getWeek() . ' ' . $schedule->getFirstDate()->format('D') . ' of ' . $start->format('F Y'));
                             $start = new \DateTime($temp->format('Y-m-d') . ' ' . $start->format('H:i:s'));
                         }
                     } else {
                         $temp = new \DateTime($schedule->getRepetition()->getWeek() . ' ' . $schedule->getFirstDate()->format('D') . ' of ' . $start->format('F Y'));
                         $start = new \DateTime($temp->format('Y-m-d') . ' ' . $start->format('H:i:s'));
                     }
                     $this->addSchedule($start, $diff, $schedule);
                     $t1 = clone $start;
                     $start->add(new \DateInterval('P' . $schedule->getRepetition()->getRepeatEvery() . 'M'));
                     $temp = new \DateTime($schedule->getRepetition()->getWeek() . ' ' . $schedule->getFirstDate()->format('D') . ' of ' . $start->format('F Y'));
                     $start = new \DateTime($temp->format('Y-m-d') . ' ' . $start->format('H:i:s'));
                     $t2 = clone $start;
                 }
                 $this->deleteBetween($t1, $schedule, $t2);
                 break;
             case 'yearly':
                 $this->addSchedule($start, $diff, $schedule);
                 $t1 = clone $start;
                 $start->add(new \DateInterval('P' . $schedule->getRepetition()->getRepeatEvery() . 'Y'));
                 $t2 = clone $start;
                 $this->deleteBetween($t1, $schedule, $t2);
                 break;
         }
         if ($this->future_occur >= $this->future_occurs) {
             break;
         }
         if ($schedule->getRepetition()->getEndOccurrences() > 0 & $this->occur >= $schedule->getRepetition()->getEndOccurrences()) {
             break;
         }
     }
     $this->deleteBetween($start, $schedule);
     $this->em->flush();
 }