mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-02 17:59:14 +02:00
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package ical
|
|
|
|
import (
|
|
"github.com/jordic/goics"
|
|
"htwkalender/model"
|
|
"htwkalender/service/functions"
|
|
"time"
|
|
)
|
|
|
|
// 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 {
|
|
layout := "2006-01-02 15:04:05 -0700 MST"
|
|
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")
|
|
timeEnd, _ := time.Parse(layout, event.End)
|
|
timeStart, _ := time.Parse(layout, event.Start)
|
|
k, v := goics.FormatDateTime("DTEND;TZID=Europe/Berlin", timeEnd)
|
|
s.AddProperty(k, v)
|
|
k, v = goics.FormatDateTime("DTSTART;TZID=Europe/Berlin", timeStart)
|
|
s.AddProperty(k, v)
|
|
s.AddProperty("SUMMARY", replaceNameIfUserDefined(event.Name, icalModel.Mapping))
|
|
s.AddProperty("DESCRIPTION", generateDescription(event))
|
|
s.AddProperty("LOCATION", event.Rooms)
|
|
c.AddComponent(s)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func replaceNameIfUserDefined(name string, mapping []model.FeedCollection) string {
|
|
for _, mapEntry := range mapping {
|
|
if mapEntry.Name == name && !functions.OnlyWhitespace(mapEntry.UserDefinedName) {
|
|
return mapEntry.UserDefinedName
|
|
}
|
|
}
|
|
return name
|
|
}
|
|
|
|
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 + "\n"
|
|
}
|
|
|
|
return description
|
|
}
|