/**
  * @inheritdoc
  */
 public function disableContentContainer(ContentContainerActiveRecord $container)
 {
     parent::disableContentContainer($container);
     foreach (CalendarEntry::find()->contentContainer($container)->all() as $entry) {
         $entry->delete();
     }
 }
 /**
  * Returns a readable calendar entry by given id
  *
  * @param int $id
  * @return CalendarEntry
  */
 protected function getCalendarEntry($id)
 {
     return CalendarEntry::find()->contentContainer($this->contentContainer)->readable()->where(['calendar_entry.id' => $id])->one();
 }
 private function iCalUpdateEvents()
 {
     require_once __DIR__ . "/../libs/iCalcreator.php";
     //load file into lines array
     $ical = @file($this->url, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
     if (!$ical) {
         Yii::error("Can't download calendar from {$this->url}.");
         return false;
     }
     if ($this->last_update) {
         $last_update = strtotime($this->last_update);
     } else {
         $last_update = 0;
     }
     //browse lines array to update only last modified events and retrieve all event uids
     $last_element_num_line = -1;
     $step = 0;
     $uids = [];
     foreach ($ical as $num_line => $line) {
         if ($num_line == 0 && $line != "BEGIN:VCALENDAR") {
             Yii::error("Invalid calendar format from {$this->url}.");
             return false;
         }
         if (substr_compare($line, "UID:", 0, 4) == 0) {
             $uids[] = substr($line, 4);
         } elseif ($step < 2 && substr_compare($line, "LAST-MODIFIED:", 0, 14) == 0) {
             $event_last_modified = strtotime(substr($line, 14));
             if ($last_update >= $event_last_modified) {
                 $step = $step == 0 ? 3 : 2;
             } else {
                 $step = 1;
             }
         } elseif ($step < 3 && substr_compare($line, "END:", 0, 4) == 0) {
             if ($line !== "END:VCALENDAR") {
                 $last_element_num_line = $num_line;
             }
             if ($step > 1) {
                 $step = 3;
             }
         }
     }
     //only update entries if there is any recent change
     if ($last_element_num_line !== -1) {
         $tmp_file = tempnam(sys_get_temp_dir(), 'ical');
         $tmp_ical = implode("\n", array_slice($ical, 0, $last_element_num_line + 1)) . "\nEND:VCALENDAR\n";
         file_put_contents($tmp_file, $tmp_ical);
         $this->iCalParseTmpFile($tmp_file);
     }
     //delete events not included in ical file
     $to_delete = CalendarEntry::find()->where(['and', ["external_source_id" => $this->id], ['not in', 'external_uid', $uids]])->all();
     foreach ($to_delete as $event) {
         $event->delete();
     }
     return true;
 }
 public static function getUpcomingEntries(ContentContainerActiveRecord $contentContainer = null, $daysInFuture = 7, $limit = 5)
 {
     $entries = array();
     $start = new DateTime();
     $query = CalendarEntry::find();
     $query->orderBy('start_datetime ASC');
     if ($daysInFuture > 0) {
         $startEnd = new DateTime();
         $startEnd->add(new DateInterval("P" . $daysInFuture . "D"));
         $query->andWhere(['>=', 'start_datetime', $start->format('Y-m-d H:i:s')]);
         $query->andWhere(['<=', 'end_datetime', $startEnd->format('Y-m-d H:i:s')]);
     } else {
         $query->andWhere(['>=', 'start_datetime', $start->format('Y-m-d H:i:s')]);
     }
     if ($contentContainer == null) {
         // When no contentcontainer is specified - limit to events where current user participate
         $query->leftJoin('calendar_entry_participant', 'calendar_entry.id=calendar_entry_participant.calendar_entry_id AND calendar_entry_participant.user_id=:userId', [':userId' => Yii::$app->user->id]);
         $query->andWhere(['IS NOT', 'calendar_entry_participant.id', new \yii\db\Expression('NULL')]);
     }
     if ($limit != 0) {
         $query->limit($limit);
     }
     if ($contentContainer !== null) {
         $query->contentContainer($contentContainer);
     }
     foreach ($query->all() as $entry) {
         if ($entry->content->canRead()) {
             $entries[] = $entry;
         }
     }
     return $entries;
 }