function testEventOccurrence_weekly()
 {
     $this->user = 1;
     $event = $this->createEvent();
     $space = $this->createSpace();
     $occurrence = new EventOccurrence();
     $occurrence->rule = $this->getEventRule();
     $occurrence->event = $event;
     $occurrence->space = $space;
     $date1 = '1950-01-02';
     $date2 = '1950-11-01';
     $occurrence->frequency = 'weekly';
     $occurrence->startsOn = new DateTime($date1);
     $occurrence->until = new DateTime($date2);
     $occurrence->startsAt = new DateTime($date1 . ' 12:00:00');
     $occurrence->endsAt = new DateTime($date1 . ' 15:00:00');
     $recurrence = new EventOccurrenceRecurrence();
     $recurrence->eventOccurrence = $occurrence;
     $recurrence->day = 1;
     $this->assertEmpty($occurrence->validationErrors, print_r($occurrence->validationErrors, true));
     $occurrence->save(true);
     $recurrence->save(true);
     $date = '1950-01-2';
     $query = $this->createQuery($date, $date);
     $occurrences = $query->getArrayResult();
     $this->assertEquals(1, count($occurrences));
     $date = '1950-01-9';
     $query = $this->createQuery($date, $date);
     $occurrences = $query->getArrayResult();
     $this->assertEquals(1, count($occurrences));
     $date = '1950-01-16';
     $query = $this->createQuery($date, $date);
     $occurrences = $query->getArrayResult();
     $this->assertEquals(1, count($occurrences));
     $date = '1950-01-3';
     $query = $this->createQuery($date, $date);
     $occurrences = $query->getArrayResult();
     $this->assertEquals(0, count($occurrences));
     $occurrences = $this->app->repo('EventOccurrence')->findOneBy(array('frequency' => 'weekly'));
     $this->assertEquals(1, count($occurrences));
 }
 function setRule($value)
 {
     if ($value === '') {
         $this->_rule = '';
         return;
     }
     $value = (array) $value;
     $this->startsAt = $value['startsOn'] . ' ' . $value['startsAt'];
     //$this->endsAt = @$value['startsOn'] . ' ' . @$value['endsAt'];
     if (!empty($value['duration'])) {
         $value['duration'] = intval($value['duration']);
         $dateString = 'PT' . $value['duration'] . 'M';
         if ($this->startsAt instanceof \DateTime) {
             $startsAtCopy = new \DateTime($this->startsAt->format('Y-m-d H:i'));
             $this->endsAt = $startsAtCopy->add(new \DateInterval($dateString));
         }
     } else {
         $value['duration'] = 0;
         $this->endsAt = $this->startsAt;
         // don't attributing causes the duration to be 1 minute
     }
     if ($this->endsAt instanceof \DateTime) {
         $value['endsAt'] = $this->endsAt->format('H:i');
     }
     $this->startsOn = $value['startsOn'];
     $this->until = $value['until'] ? $value['until'] : null;
     $this->frequency = $value['frequency'];
     $this->_rule = json_encode($value);
     if ($this->validationErrors) {
         return;
     }
     foreach ($this->recurrences as $recurrence) {
         $recurrence->delete();
     }
     if ($value['frequency']) {
         $freq = $this->frequency;
         $days = isset($value['day']) ? $value['day'] : null;
         switch ($freq) {
             case 'weekly':
                 $this->flag_day_on = false;
                 if (is_null($days)) {
                     break;
                 }
                 foreach ($days as $key => $value) {
                     if ($value === 'off') {
                         break;
                     }
                     $this->flag_day_on = true;
                     $rec = new EventOccurrenceRecurrence();
                     $rec->eventOccurrence = $this;
                     $rec->day = (int) $key;
                     $rec->week = null;
                     $rec->month = null;
                     $rec->save();
                 }
                 break;
             case 'monthly':
                 if (isset($value['monthly']) && $value['monthly'] === 'week') {
                     $this->flag_day_on = false;
                     if (is_null($days)) {
                         break;
                     }
                     foreach ($days as $key => $value) {
                         if ($value === 'off') {
                             break;
                         }
                         $this->flag_day_on = true;
                         $rec = new EventOccurrenceRecurrence();
                         $rec->eventOccurrence = $this;
                         $rec->day = (int) $key;
                         $rec->week = 1;
                         # TODO: calc week
                         $rec->month = null;
                         $rec->save();
                     }
                 } else {
                     $rec = new EventOccurrenceRecurrence();
                     $rec->eventOccurrence = $this;
                     $rec->day = $this->startsOn === null ? 0 : $this->startsOn->format('j');
                     $rec->week = null;
                     $rec->month = null;
                 }
                 break;
         }
     }
 }