public void SaveToFile(string icsFilename) { var ctx = new SerializationContext(); var factory = new SerializerFactory(); var serializer = factory.Build(ical.GetType(), ctx) as IStringSerializer; var content = serializer.SerializeToString(ical); File.WriteAllText(icsFilename, content); }
private byte[] WrapResource(IUniqueComponent resource) { var iCal = new iCalendar(); iCal.UniqueComponents.Add(resource); ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; return Encoding.UTF8.GetBytes(serializer.SerializeToString(iCal)); }
public ActionResult GetIcsForEvent(int id, int type = 0) { var iCal = new iCalendar(); var evt = iCal.Create<Event>(); var cs = ApplicationContext.Current.Services.ContentService; var content = cs.GetById(id); var seminarStart = Convert.ToDateTime(content.GetValue("start").ToString()); var start = Convert.ToDateTime(content.GetValue("start").ToString()); var end = Convert.ToDateTime(content.GetValue("end").ToString()); evt.Start = new iCalDateTime(start.ToUniversalTime()); evt.End = new iCalDateTime(end.ToUniversalTime()); var eventName = evt.Name= content.Name.ToCleanString(CleanStringType.FileName); var f =VirtualPathUtility.ToAbsolute(Umbraco.Media(content.GetValue("seminarPoster").ToString()).umbracoFile); Attachment poster = new Attachment(f); evt.Attachments.Add(poster); var gmapLocation = content.GetValue<GMapsLocation>("location"); var lat = gmapLocation.Lat; var lng = gmapLocation.Lng; evt.GeographicLocation = new GeographicLocation(Convert.ToDouble(lat), Convert.ToDouble(lng)); ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); // Get a serializer for our object var serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; var output = serializer.SerializeToString(iCal); var contentType = "text/calendar"; var bytes = Encoding.UTF8.GetBytes(output); return File(bytes, contentType, eventName + ".ics"); //if (type == 0) //{ // //Fetch Event // var ectrl = new EventApiController(); // var e = ectrl.GetById(id); // if (e.End != null) // { // var end = (DateTime)e.End; // } // evt.IsAllDay = e.AllDay; // //If it has a location fetch it // if (e.locationId != 0) // { // l = lctrl.GetById(e.locationId); // evt.Location = l.LocationName + "," + l.Street + "," + l.ZipCode + " " + l.City + "," + l.Country; // } // var attendes = new List<IAttendee>(); // if (e.Organiser != null && e.Organiser != 0) // { // var ms = Services.MemberService; // var member = ms.GetById(e.Organiser); // string attendee = "MAILTO:" + member.Email; // IAttendee reqattendee = new DDay.iCal.Attendee(attendee) // { // CommonName = member.Name, // Role = "REQ-PARTICIPANT" // }; // attendes.Add(reqattendee); // } // if (attendes != null && attendes.Count > 0) // { // evt.Attendees = attendes; // } //} //else if (type == 1) //{ // var ectrl = new REventApiController(); // var e = ectrl.GetById(id); // evt.Summary = e.Title; // evt.IsAllDay = e.AllDay; // //If it has a location fetch it // if (e.locationId != 0) // { // l = lctrl.GetById(e.locationId); // evt.Location = l.LocationName + "," + l.Street + "," + l.ZipCode + " " + l.City + "," + l.Country; // } // RecurrencePattern rp = null; // rp = new RecurrencePattern(); // var frequency = (FrequencyTypeEnum)e.Frequency; // switch (frequency) // { // case FrequencyTypeEnum.Daily: // { // rp = new RecurrencePattern(FrequencyType.Daily); // break; // } // case FrequencyTypeEnum.Monthly: // { // rp = new RecurrencePattern(FrequencyType.Monthly); // break; // } // case FrequencyTypeEnum.Weekly: // { // rp = new RecurrencePattern(FrequencyType.Weekly); // break; // } // case FrequencyTypeEnum.Yearly: // { // rp = new RecurrencePattern(FrequencyType.Yearly); // break; // } // default: // { // rp = new RecurrencePattern(FrequencyType.Monthly); // break; // } // } // rp.ByDay.AddRange(e.Days.Select(x => new WeekDay(x.ToString()))); // evt.RecurrenceRules.Add(rp); // var tmp_event = new ScheduleWidget.ScheduledEvents.Event() // { // Title = e.Title, // FrequencyTypeOptions = (FrequencyTypeEnum)e.Frequency // }; // foreach (var day in e.Days) // { // tmp_event.DaysOfWeekOptions = tmp_event.DaysOfWeekOptions | (DayOfWeekEnum)day; // } // foreach (var i in e.MonthlyIntervals) // { // tmp_event.MonthlyIntervalOptions = tmp_event.MonthlyIntervalOptions | (MonthlyIntervalEnum)i; // } // Schedule schedule = new Schedule(tmp_event); // var occurence = Convert.ToDateTime(schedule.NextOccurrence(DateTime.Now)); // evt.Start = new iCalDateTime(new DateTime(occurence.Year, occurence.Month, occurence.Day, e.Start.Hour, e.Start.Minute, 0)); // var attendes = new List<IAttendee>(); // if (e.Organiser != null && e.Organiser != 0) // { // var ms = Services.MemberService; // var member = ms.GetById(e.Organiser); // string attendee = "MAILTO:" + member.Email; // IAttendee reqattendee = new DDay.iCal.Attendee(attendee) // { // CommonName = member.Name, // Role = "REQ-PARTICIPANT" // }; // attendes.Add(reqattendee); // } // if (attendes != null && attendes.Count > 0) // { // evt.Attendees = attendes; // } //} // Create a serialization context and serializer factory. // These will be used to build the serializer for our object. }
public ActionResult GetIcsdddForCalendar(int id) { var iCal = new iCalendar(); //c.Url = "/" + content.GetValue("umbracoUrlAlias").ToString(); //var lctrl = new LocationApiController(); //var calctrl = new CalendarApiController(); //var cal = calctrl.GetById(id); //EventLocation l = null; //iCal.Properties.Add(new CalendarProperty("X-WR-CALNAME", cal.Calendarname)); ////Get normal events for calendar //var nectrl = new EventApiController(); //foreach (var e in nectrl.GetAll().Where(x => x.calendarId == id)) //{ // // Create the event, and add it to the iCalendar // DDay.iCal.Event evt = iCal.Create<DDay.iCal.Event>(); // var start = (DateTime)e.Start; // evt.Start = new iCalDateTime(start.ToUniversalTime()); // if (e.End != null) // { // var end = (DateTime)e.End; // evt.End = new iCalDateTime(end.ToUniversalTime()); // } // evt.Description = this.GetDescription(e, CultureInfo.CurrentCulture.ToString()); // evt.Summary = e.Title; // evt.IsAllDay = e.AllDay; // evt.Categories.AddRange(e.Categories.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList()); // //If it has a location fetch it // if (e.locationId != 0) // { // l = lctrl.GetById(e.locationId); // evt.Location = l.LocationName + "," + l.Street + "," + l.ZipCode + " " + l.City + "," + l.Country; // } // var attendes = new List<IAttendee>(); // if (e.Organiser != null && e.Organiser != 0) // { // var ms = Services.MemberService; // var member = ms.GetById(e.Organiser); // string attendee = "MAILTO:" + member.Email; // IAttendee reqattendee = new DDay.iCal.Attendee(attendee) // { // CommonName = member.Name, // Role = "REQ-PARTICIPANT" // }; // attendes.Add(reqattendee); // } // if (attendes != null && attendes.Count > 0) // { // evt.Attendees = attendes; // } //} ////Get recurring events //var rectrl = new REventApiController(); //foreach (var e in rectrl.GetAll().Where(x => x.calendarId == id)) //{ // // Create the event, and add it to the iCalendar // DDay.iCal.Event evt = iCal.Create<DDay.iCal.Event>(); // evt.Description = this.GetDescription(e, CultureInfo.CurrentCulture.ToString()); // evt.Summary = e.Title; // evt.IsAllDay = e.AllDay; // evt.Categories.AddRange(e.Categories.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList()); // //If it has a location fetch it // if (e.locationId != 0) // { // l = lctrl.GetById(e.locationId); // evt.Location = l.LocationName + "," + l.Street + "," + l.ZipCode + " " + l.City + "," + l.Country; // } // RecurrencePattern rp = null; // var frequency = (FrequencyTypeEnum)e.Frequency; // switch (frequency) // { // case FrequencyTypeEnum.Daily: // { // rp = new RecurrencePattern(FrequencyType.Daily); // break; // } // case FrequencyTypeEnum.Monthly: // { // rp = new RecurrencePattern(FrequencyType.Monthly); // break; // } // case FrequencyTypeEnum.Weekly: // { // rp = new RecurrencePattern(FrequencyType.Weekly); // break; // } // case FrequencyTypeEnum.Yearly: // { // rp = new RecurrencePattern(FrequencyType.Yearly); // break; // } // default: // { // rp = new RecurrencePattern(FrequencyType.Monthly); // break; // } // } // rp.ByDay.AddRange(e.Days.Select(x => new WeekDay(x.ToString()))); // evt.RecurrenceRules.Add(rp); // var tmp_event = new ScheduleWidget.ScheduledEvents.Event() // { // Title = e.Title, // FrequencyTypeOptions = (FrequencyTypeEnum)e.Frequency // }; // foreach (var day in e.Days) // { // tmp_event.DaysOfWeekOptions = tmp_event.DaysOfWeekOptions | (DayOfWeekEnum)day; // } // foreach (var i in e.MonthlyIntervals) // { // tmp_event.MonthlyIntervalOptions = tmp_event.MonthlyIntervalOptions | (MonthlyIntervalEnum)i; // } // Schedule schedule = new Schedule(tmp_event); // var occurence = Convert.ToDateTime(schedule.NextOccurrence(DateTime.Now)); // evt.Start = new iCalDateTime(new DateTime(occurence.Year, occurence.Month, occurence.Day, e.Start.Hour, e.Start.Minute, 0)); // var attendes = new List<IAttendee>(); // if (e.Organiser != null && e.Organiser != 0) // { // var ms = Services.MemberService; // var member = ms.GetById(e.Organiser); // string attendee = "MAILTO:" + member.Email; // IAttendee reqattendee = new DDay.iCal.Attendee(attendee) // { // CommonName = member.Name, // Role = "REQ-PARTICIPANT" // }; // attendes.Add(reqattendee); // } // if (attendes != null && attendes.Count > 0) // { // evt.Attendees = attendes; // } //} // Create a serialization context and serializer factory. // These will be used to build the serializer for our object. ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); // Get a serializer for our object var serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; var output = serializer.SerializeToString(iCal); var contentType = "text/calendar"; var bytes = Encoding.UTF8.GetBytes(output); return File(bytes, contentType, "name" + ".ics"); }
/// <summary> /// Function is called to create the course ics file /// Function is called everytime the course calendar needs to be updated /// </summary> /// <param name="id"></param> public void CreateCourseCalendar(int id) { //Get the Course var course = (from d in db.Courses where d.ID == id select new { d.StartDate, d.EndDate, d.Prefix, d.Number, d.Semester, d.Year, d.CourseMeetings, d.CourseBreaks, d.ID }).FirstOrDefault(); //get course events var events = (from e in db.Events where e.Poster.AbstractCourseID == ActiveCourseUser.AbstractCourseID && e.StartDate >= course.StartDate && e.StartDate <= course.EndDate && e.Approved select new { e.StartDate, e.EndDate, e.Title, e.Description }).ToList(); //Create the calendar object iCalendar courseCalendar = new iCalendar(); //initalize the Calendar object courseCalendar.AddTimeZone(DateTimeExtensions.GetTimeZone(((Course)ActiveCourseUser.AbstractCourse).TimeZoneOffset)); courseCalendar.Method = "PUBLISH"; courseCalendar.Name = "VCALENDAR"; courseCalendar.Version = "2.0"; courseCalendar.ProductID = "-//Washington State University//PLUS.OSBLE.org//EN"; courseCalendar.Scale = "GREGORIAN"; courseCalendar.AddProperty("X-WR-CALNAME", course.Prefix + "-" + course.Number + "-" + course.Semester + "-" + course.Year); //get course breaks if (ActiveCourseUser.AbstractCourse is Course && ((ActiveCourseUser.AbstractCourse as Course).ShowMeetings == true)) { foreach (CourseMeeting cm in course.CourseMeetings) { StringBuilder rpPattern = new StringBuilder("FREQ=WEEKLY;UNTIL="); rpPattern.Append(new iCalDateTime(course.EndDate.UTCToCourse(ActiveCourseUser.AbstractCourseID)).ToString(@"yyyyMMdd\THHmmss\Z")); rpPattern.Append(";WKST=SU;BYDAY="); if (cm.Sunday) { rpPattern.Append("SU,"); } if (cm.Monday) { rpPattern.Append("MO,"); } if (cm.Tuesday) { rpPattern.Append("TU,"); } if (cm.Wednesday) { rpPattern.Append("WE,"); } if (cm.Thursday) { rpPattern.Append("TH,"); } if (cm.Friday) { rpPattern.Append("FR,"); } if (cm.Saturday) { rpPattern.Append("SA"); } //trim trailing comma if it is there if (rpPattern[rpPattern.Length - 1] == ',') { rpPattern.Remove(rpPattern.Length - 1, 1); } RecurringComponent recurringComponent = new RecurringComponent(); RecurrencePattern pattern = new RecurrencePattern(rpPattern.ToString()); DDay.iCal.Event evt = courseCalendar.Create <DDay.iCal.Event>(); //may cause issues DateTime evtStart = DateTime.SpecifyKind(course.StartDate, DateTimeKind.Utc); evtStart = evtStart.Add(cm.StartTime.TimeOfDay); DateTime evtEnd = DateTime.SpecifyKind(course.StartDate, DateTimeKind.Utc); evtEnd = evtEnd.Add(cm.EndTime.TimeOfDay); //we need to test if right NOW is in DST. //case: course starts during DST, and it's now after DST, result: after DST event is now wrong. DateTime currentTime = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, evtStart.Hour, evtStart.Minute, evtStart.Second, DateTimeKind.Utc); bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(currentTime.UTCToCourse(ActiveCourseUser.AbstractCourseID)); if (isDaylight) { evt.Start = new iCalDateTime(evtStart.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.End = new iCalDateTime(evtEnd.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.LastModified = new iCalDateTime(DateTime.Now); evt.Summary = cm.Name; evt.Location = cm.Location; evt.RecurrenceRules.Add(pattern); } else //no longer DST { bool matchingStartDay = false; /* * if there happens to be a "meeting times" in the database without a day of the week associated with * the meeting, we need to skip the while and switch case, otherwise it's an infinite loop! */ bool meetingTimeWithoutDaysOfWeek = cm.Sunday || cm.Monday || cm.Tuesday || cm.Wednesday || cm.Thursday || cm.Friday || cm.Saturday ? false : true; int count = 0; //infinite loop protection x2! //WHILE matchingStartDay is false (default false) AND the meeting time has at least one day of the week //AND if we've hit a count of 7, that means we've already checked each day of the week and will never find one! //exit while if we fail either of these conditions! while ((!matchingStartDay && !meetingTimeWithoutDaysOfWeek) && count < 7) { switch (currentTime.DayOfWeek) //find which day of the week for this meeting { case DayOfWeek.Monday: if (cm.Monday) { matchingStartDay = true; } break; case DayOfWeek.Tuesday: if (cm.Tuesday) { matchingStartDay = true; } break; case DayOfWeek.Wednesday: if (cm.Wednesday) { matchingStartDay = true; } break; case DayOfWeek.Thursday: if (cm.Thursday) { matchingStartDay = true; } break; case DayOfWeek.Friday: if (cm.Friday) { matchingStartDay = true; } break; case DayOfWeek.Saturday: if (cm.Saturday) { matchingStartDay = true; } break; case DayOfWeek.Sunday: if (cm.Sunday) { matchingStartDay = true; } break; default: matchingStartDay = false; break; } if (!matchingStartDay) { currentTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.AddDays(1).Day, evtStart.Hour, evtStart.Minute, evtStart.Second, DateTimeKind.Utc); } count++; //infinite loop protection x2! if we've reached 7 we've cycled all weekdays, so get us out of here! } //proceed only if there was a day of the week found for this course meeting time if (!meetingTimeWithoutDaysOfWeek && matchingStartDay) { evt.Start = new iCalDateTime(currentTime.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.End = new iCalDateTime(evtEnd.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.LastModified = new iCalDateTime(DateTime.Now); evt.Summary = cm.Name; evt.Location = cm.Location; evt.RecurrenceRules.Add(pattern); } } } //create the course breaks foreach (CourseBreak cb in course.CourseBreaks) { DDay.iCal.Event evt = courseCalendar.Create <DDay.iCal.Event>(); DateTime evtStart = cb.StartDate.Date; DateTime evtEnd = cb.EndDate.Date.AddDays(1); evt.Summary = cb.Name; evt.Start = new iCalDateTime(evtStart); evt.End = new iCalDateTime(evtEnd); evt.LastModified = new iCalDateTime(DateTime.Now); } }//end if //add all the events to the calendar foreach (var e in events) { DDay.iCal.Event evt = courseCalendar.Create <DDay.iCal.Event>(); evt.Start = new iCalDateTime(e.StartDate.UTCToCourse(ActiveCourseUser.AbstractCourseID)); if (e.EndDate == null) { evt.End = new iCalDateTime(e.StartDate.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.End.AddMinutes(1); } else { evt.End = new iCalDateTime(((DateTime)e.EndDate).UTCToCourse(ActiveCourseUser.AbstractCourseID)); } evt.Summary = e.Title; if (e.Description != null) { evt.Description = e.Description; } } // Create a serialization context and serializer factory. // These will be used to build the serializer for our object. ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new DDay.iCal.Serialization.iCalendar.SerializerFactory(); // Get a serializer for our object IStringSerializer serializer = factory.Build(courseCalendar.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(courseCalendar); var bytes = Encoding.UTF8.GetBytes(output); SaveCourseCalendar(bytes, course.ID); }
public ActionResult TeamFeed(int seasonId, int teamId, string desc) { var seasonName = _context.Seasons.Where(x => x.SeasonId == seasonId).Single().SeasonName; var teamName = _context.Teams.Where(x => x.TeamId == teamId).Single().TeamNameLong; List<GameTeam> gameTeams = _context.GameTeams .IncludeAll() .Where(x => x.SeasonId == seasonId && x.TeamId == teamId) .ToList(); iCalendar ical = new iCalendar(); ical.Properties.Set("X-WR-CALNAME", "LO30Schedule-" + teamName.Replace(" ", "") + "-" + seasonName.Replace(" ", "")); foreach (var gameTeam in gameTeams) { Event icalEvent = ical.Create<Event>(); var summary = gameTeam.OpponentTeam.TeamNameShort + " vs " + gameTeam.Team.TeamNameShort; if (gameTeam.HomeTeam) { summary = gameTeam.Team.TeamNameShort + " vs " + gameTeam.OpponentTeam.TeamNameShort; } icalEvent.Summary = summary; icalEvent.Description = summary + " " + gameTeam.Game.Location; var year = gameTeam.Game.GameDateTime.Year; var mon = gameTeam.Game.GameDateTime.Month; var day = gameTeam.Game.GameDateTime.Day; var hr = gameTeam.Game.GameDateTime.Hour; var min = gameTeam.Game.GameDateTime.Minute; var sec = gameTeam.Game.GameDateTime.Second; icalEvent.Start = new iCalDateTime(gameTeam.Game.GameDateTime); icalEvent.Duration = TimeSpan.FromHours(1.25); //icalEvent.Location = "Eddie Edgar " + gameTeam.Game.Location; // TODO set the Rink location icalEvent.Location = "Eddie Edgar"; } ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); IStringSerializer serializer = factory.Build(ical.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(ical); var contentType = "text/calendar"; return Content(output, contentType); }
public IActionResult TeamFeed(int seasonId, int teamId, string desc) { var seasonName = _context.Seasons.Where(x => x.SeasonId == seasonId).Single().SeasonName; var teamName = _context.Teams.Where(x => x.TeamId == teamId).Single().TeamNameLong; List<GameTeam> gameTeams = _context.GameTeams .Include(x => x.Season) .Include(x => x.Team) .Include(x => x.OpponentTeam) .Include(x => x.Game).ThenInclude(y => y.GameOutcomes) .Include(x => x.Game).ThenInclude(y => y.GameScores) .Where(x => x.SeasonId == seasonId && x.TeamId == teamId) .ToList(); // HACK, for some reason, the team was not getting "included" gameTeams = gameTeams.Join(_context.Teams, a => a.TeamId, b => b.TeamId, (a, b) => new { a, b }) .Select(m => new GameTeam { Game = m.a.Game, GameId = m.a.GameId, HomeTeam = m.a.HomeTeam, OpponentTeam = m.a.OpponentTeam, OpponentTeamId = m.a.OpponentTeamId, Season = m.a.Season, SeasonId = m.a.SeasonId, Team = m.b, TeamId = m.a.TeamId }) .ToList(); iCalendar ical = new iCalendar(); ical.Properties.Set("X-WR-CALNAME", "LO30Schedule-" + teamName.Replace(" ", "") + "-" + seasonName.Replace(" ","")); foreach (var gameTeam in gameTeams) { Event icalEvent = ical.Create<Event>(); var summary = gameTeam.OpponentTeam.TeamNameShort + " vs " + gameTeam.Team.TeamNameShort; if (gameTeam.HomeTeam) { summary = gameTeam.Team.TeamNameShort + " vs " + gameTeam.OpponentTeam.TeamNameShort; } icalEvent.Summary = summary; icalEvent.Description = summary + " " + gameTeam.Game.Location; var year = gameTeam.Game.GameDateTime.Year; var mon = gameTeam.Game.GameDateTime.Month; var day = gameTeam.Game.GameDateTime.Day; var hr = gameTeam.Game.GameDateTime.Hour; var min = gameTeam.Game.GameDateTime.Minute; var sec = gameTeam.Game.GameDateTime.Second; icalEvent.Start = new iCalDateTime(gameTeam.Game.GameDateTime); icalEvent.Duration = TimeSpan.FromHours(1.25); icalEvent.Location = "Eddie Edgar " + gameTeam.Game.Location; } ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); IStringSerializer serializer = factory.Build(ical.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(ical); var contentType = "text/calendar"; return Content(output, contentType); }