package ical import ( "htwkalender/model" "htwkalender/service/functions" "htwkalender/service/names" "time" "github.com/jordic/goics" ) // IcalModel local type for EmitICal function type IcalModel struct { Events model.Events Mapping []model.FeedCollection } // EmitICal implements the interface for goics func (icalModel IcalModel) EmitICal() goics.Componenter { europeTime, _ := time.LoadLocation("Europe/Berlin") c := goics.NewComponent() c.SetType("VCALENDAR") c.AddProperty("VERSION", "2.0") c.AddProperty("CALSCAL", "GREGORIAN") c.AddProperty("TZID", "Europe/Berlin") c.AddProperty("X-WR-CALNAME", "HTWK Kalender") c.AddProperty("X-WR-TIMEZONE", "Europe/Berlin") c.AddProperty("X-LIC-LOCATION", "Europe/Berlin") for _, event := range icalModel.Events { s := goics.NewComponent() s.SetType("VEVENT") k, v := goics.FormatDateTime("DTEND;TZID=Europe/Berlin", event.End.Time().Local().In(europeTime)) s.AddProperty(k, v) k, v = goics.FormatDateTime("DTSTART;TZID=Europe/Berlin", event.Start.Time().Local().In(europeTime)) s.AddProperty(k, v) addPropertyIfNotEmpty(s, "SUMMARY", replaceNameIfUserDefined(&event, icalModel.Mapping)) addPropertyIfNotEmpty(s, "DESCRIPTION", generateDescription(event)) addPropertyIfNotEmpty(s, "LOCATION", event.Rooms) c.AddComponent(s) } return c } // replaceNameIfUserDefined replaces the name of the event with the user defined name if it is not empty // all contained template strings will be replaced with the corresponding values from the event func replaceNameIfUserDefined(event *model.Event, mapping []model.FeedCollection) string { for _, mapEntry := range mapping { if mapEntry.Name == event.Name && !functions.OnlyWhitespace(mapEntry.UserDefinedName) { return names.ReplaceTemplateSubStrings(mapEntry.UserDefinedName, *event) } } return event.Name } // AddPropertyIfNotEmpty adds a property to the component if the value is not empty // or contains only whitespaces func addPropertyIfNotEmpty(component *goics.Component, key string, value string) { if !functions.OnlyWhitespace(value) { component.AddProperty(key, value) } } func generateDescription(event model.Event) string { var description string if !functions.OnlyWhitespace(event.Notes) { description += "Notizen: " + event.Notes + "\n" } if !functions.OnlyWhitespace(event.Prof) { description += "Prof: " + event.Prof + "\n" } if !functions.OnlyWhitespace(event.Course) { description += "Gruppe: " + event.Course + "\n" } if !functions.OnlyWhitespace(event.EventType) { description += "Typ: " + event.EventType + event.Compulsory + "\n" } return description }