/** * * Sets multiple cc_show_instances table rows * @param unknown_type $showDay * @param unknown_type $populateUntil * @param unknown_type $repeatInterval * @param unknown_type $isRebroadcast */ private function createWeeklyRepeatInstances($showDay, $populateUntil, $repeatType, $repeatInterval, $daysAdded = null) { $show_id = $showDay->getDbShowId(); $first_show = $showDay->getDbFirstShow(); //non-UTC $last_show = $showDay->getDbLastShow(); //non-UTC $duration = $showDay->getDbDuration(); $day = $showDay->getDbDay(); $record = $showDay->getDbRecord(); $timezone = $showDay->getDbTimezone(); //DateTime local $start = $this->getNextRepeatingPopulateStartDateTime($showDay); if (is_null($repeatInterval) && $repeatType == REPEAT_MONTHLY_WEEKLY) { $repeatInterval = $this->getMonthlyWeeklyRepeatInterval($start, $timezone); } //DatePeriod in user's local time $datePeriod = $this->getDatePeriod($start, $timezone, $last_show, $repeatInterval, $populateUntil); if ($last_show) { $utcLastShowDateTime = new DateTime($last_show, new DateTimeZone($timezone)); $utcLastShowDateTime->setTimezone(new DateTimeZone("UTC")); } else { $utcLastShowDateTime = null; } $previousDate = clone $start; foreach ($datePeriod as $date) { list($utcStartDateTime, $utcEndDateTime) = $this->createUTCStartEndDateTime($date, $duration); /* * Make sure start date is less than populate until date AND * last show date is null OR start date is less than last show date */ if ($utcStartDateTime <= $populateUntil && (is_null($utcLastShowDateTime) || $utcStartDateTime < $utcLastShowDateTime)) { $lastCreatedShow = clone $utcStartDateTime; /* There may not always be an instance when editing a show * This will be the case when we are adding a new show day to * a repeating show */ if ($this->isUpdate) { if ($this->hasInstance($utcStartDateTime)) { $ccShowInstance = $this->getInstance($utcStartDateTime); $newInstance = false; $updateScheduleStatus = true; } else { $newInstance = true; $ccShowInstance = new CcShowInstances(); $updateScheduleStatus = false; } } else { $newInstance = true; $ccShowInstance = new CcShowInstances(); $updateScheduleStatus = false; } /* When editing the start/end time of a repeating show, we don't want to * change shows that are in the past so we check the end time. */ if ($newInstance || $ccShowInstance->getDbEnds() > gmdate("Y-m-d H:i:s")) { $ccShowInstance->setDbShowId($show_id); $ccShowInstance->setDbStarts($utcStartDateTime); $ccShowInstance->setDbEnds($utcEndDateTime); $ccShowInstance->setDbRecord($record); $ccShowInstance->save(); } if ($this->isRebroadcast) { $this->createRebroadcastInstances($showDay, $date, $ccShowInstance->getDbId()); } } $previousDate = clone $date; } /* We need to set the next populate date for repeat shows so when a user * moves forward in the calendar we know when to start generating new * show instances. * If $utcStartDateTime is not set then we know zero new shows were * created and we shouldn't update the next populate date. */ if (isset($lastCreatedShow)) { /* Set UTC to local time before setting the next repeat date. If we don't * the next repeat date might be scheduled for the following day * THIS MUST BE IN THE TIMEZONE THE SHOW WAS CREATED IN */ $lastCreatedShow->setTimezone(new DateTimeZone($timezone)); $nextDate = $lastCreatedShow->add($repeatInterval); $this->setNextRepeatingShowDate($nextDate->format("Y-m-d"), $day, $show_id); } }