/**
  * drag
  * Process drag event on scheduleInstance (add to both start and end of event)
  * @Route("/admin/schedule/{id}/drag/{dayDelta}/{minuteDelta}/{isAllDay}", options={"expose"=true})
  * @Template("TSKScheduleBundle:Default:drag.html.twig")
  * @Method({"POST", "GET"})
  */
 public function dragAction(ScheduleInstance $scheduleInstance, $dayDelta, $minuteDelta, $isAllDay)
 {
     $interval = sprintf('P%dDT%dM', $dayDelta, $minuteDelta);
     $start = $scheduleInstance->getStart();
     $end = $scheduleInstance->getEnd();
     $finalStart = $this->addInterval($start, $dayDelta, 'day');
     $finalStart = $this->addInterval($finalStart, $minuteDelta, 'minute');
     $finalEnd = $this->addInterval($end, $dayDelta, 'day');
     $finalEnd = $this->addInterval($finalEnd, $minuteDelta, 'minute');
     $scheduleInstance->setStart($finalStart);
     $scheduleInstance->setEnd($finalEnd);
     $scheduleInstance->setIsAllDay($isAllDay == 1);
     $em = $this->getDoctrine()->getManager();
     $em->persist($scheduleInstance);
     $em->flush();
     return array('response' => '1');
     $view = View::create()->setStatusCode(200)->setData($scheduleInstance)->setTemplate('TSKScheduleBundle:Default:drag.html.twig');
     return $this->get('fos_rest.view_handler')->handle($view);
 }
 /**
  * processEntity 
  * This function is responsible for taking a ScheduleEntity object
  * and generating up to $maxInstances ScheduleInstance objects from it.
  * 
  * @param ScheduleEntity $scheduleEntity 
  * @param mixed $maxInstances 
  * @access public
  * @return void
  */
 public function processEntity(ScheduleEntity $scheduleEntity, $maxInstances = null)
 {
     if (is_null($maxInstances)) {
         $maxInstances = $this->maxInstances;
     }
     if ($maxInstances > $this->maxInstances) {
         $maxInstances = $this->maxInstances;
     }
     // get rrule
     $timezone = 'America/New_York';
     $today = new \DateTime();
     $dateTimeZone = new \DateTimeZone($timezone);
     if ($scheduleEntity->getRrule()) {
         // Handling a recurring event
         $rrule = new RecurrenceRule($scheduleEntity->getRrule(), $scheduleEntity->getStartAt(), $timezone);
         $transformer = new RecurrenceRuleTransformer($rrule);
         $instances = $transformer->getComputedArray($scheduleEntity->getStartAt(), $dateTimeZone, $today->add(new \DateInterval('P6M')), $dateTimeZone);
         $options = $scheduleEntity->getOptions();
         // generate instances
         if (count($instances)) {
             $count = 0;
             foreach ($instances as $startDateTime) {
                 if ($count < $maxInstances) {
                     // compute endDateTime based on duration
                     $endDateTime = clone $startDateTime;
                     $endDateTime->add(new \DateInterval($scheduleEntity->getDuration()));
                     $si = new ScheduleInstance();
                     $si->setScheduleEntity($scheduleEntity);
                     $si->setUser($scheduleEntity->getUser());
                     $si->setSchool($scheduleEntity->getSchool());
                     $si->setTitle($scheduleEntity->getTitle());
                     $si->setStart($startDateTime);
                     $si->setEnd($endDateTime);
                     $si->setIsAllDay(in_array('all_day', $scheduleEntity->getOptions()));
                     $lastEnd = $endDateTime;
                     $this->em->persist($si);
                     $count++;
                 }
             }
         } else {
             $instances = array(null);
         }
         $lastInstance = array_pop($instances);
     } else {
         // Handling a non-recurring event
         $si = new ScheduleInstance();
         $si->setScheduleEntity($scheduleEntity);
         $si->setUser($scheduleEntity->getUser());
         $si->setSchool($scheduleEntity->getSchool());
         $si->setTitle($scheduleEntity->getTitle());
         $si->setStart($scheduleEntity->getStartAt());
         $si->setEnd($scheduleEntity->getEndAt());
         $si->setIsAllDay(in_array('all_day', $scheduleEntity->getOptions()));
         $this->em->persist($si);
         // $lastEnd = $lastInstance = $scheduleEntity->getStartAt()->add(new \DateInterval($scheduleEntity->getDuration()));
     }
     // update last_processed
     // $scheduleEntity->setLastProcessedAt(new \DateTime());
     // update expireAt
     // $scheduleEntity->setExpiresAt($lastEnd);
     // update maxDateAt
     // $scheduleEntity->setMaxDateAt($lastInstance);
     // $this->em->persist($scheduleEntity);
     $this->em->flush();
 }