Наследование: EncodableDataType, IAttendee
Пример #1
0
        public ActionResult GetIcsForCalendar(int id)
        {
            DDay.iCal.iCalendar iCal = new DDay.iCal.iCalendar();


            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;
                    //evt.GeographicLocation = new GeographicLocation(Convert.ToDouble(l.lat,CultureInfo.InvariantCulture), Convert.ToDouble(l.lon, CultureInfo.InvariantCulture));
                }

                var attendes = new List <IAttendee>();

                if (e.Organisator != null && e.Organisator != 0)
                {
                    var       ms          = Services.MemberService;
                    var       member      = ms.GetById(e.Organisator);
                    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;
                    //evt.GeographicLocation = new GeographicLocation(Convert.ToDouble(l.lat, CultureInfo.InvariantCulture), Convert.ToDouble(l.lon, CultureInfo.InvariantCulture));
                }

                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;
                }
                }
                switch (e.day)
                {
                case (int)DayOfWeekEnum.Mon:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Monday));
                    break;
                }

                case (int)DayOfWeekEnum.Tue:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Tuesday));
                    break;
                }

                case (int)DayOfWeekEnum.Wed:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Wednesday));
                    break;
                }

                case (int)DayOfWeekEnum.Thu:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Thursday));
                    break;
                }

                case (int)DayOfWeekEnum.Fri:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Friday));
                    break;
                }

                case (int)DayOfWeekEnum.Sat:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Saturday));
                    break;
                }

                case (int)DayOfWeekEnum.Sun:
                {
                    rp.ByDay.Add(new WeekDay(DayOfWeek.Sunday));
                    break;
                }
                }
                evt.RecurrenceRules.Add(rp);

                Schedule schedule = new Schedule(new ScheduleWidget.ScheduledEvents.Event()
                {
                    Title                  = e.title,
                    DaysOfWeekOptions      = (DayOfWeekEnum)e.day,
                    FrequencyTypeOptions   = (FrequencyTypeEnum)e.frequency,
                    MonthlyIntervalOptions = (MonthlyIntervalEnum)e.monthly_interval
                });

                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.Organisator != null && e.Organisator != 0)
                {
                    var       ms          = Services.MemberService;
                    var       member      = ms.GetById(e.Organisator);
                    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 DDay.iCal.Serialization.iCalendar.SerializerFactory();
            // Get a serializer for our object
            IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer;

            string output      = serializer.SerializeToString(iCal);
            var    contentType = "text/calendar";
            var    bytes       = Encoding.UTF8.GetBytes(output);

            return(File(bytes, contentType, cal.Calendarname + ".ics"));
        }
Пример #2
0
        /// <summary>
        /// Send mail ICalendar
        /// </summary>
        /// <param name="mailMeetingEntity">MailMeetingEntity</param>
        /// <param name="actionBeforeSendMeeting">Action<MailMessage></param>
        public string SendMeeting(MailMeetingEntity mailMeetingEntity, Action <MailMessage> actionBeforeSendMeeting)
        {
            SmtpClient m_SmtpClient = new SmtpClient();

            if (this.MailConfig != null)
            {
                m_SmtpClient.UseDefaultCredentials = MailConfig.UseDefaultCredentials;
                if (!MailConfig.UseDefaultCredentials) //tạo mới Smtp Credentials
                {
                    m_SmtpClient.Credentials = new NetworkCredential(MailConfig.Username, MailConfig.Password, MailConfig.Domain);
                }
                m_SmtpClient.Port      = MailConfig.Port;
                m_SmtpClient.Host      = MailConfig.Host;
                m_SmtpClient.EnableSsl = MailConfig.EnableSsl;
            }

            MailMessage m_MailMessage = new MailMessage()
            {
                From       = new MailAddress(mailMeetingEntity.From),
                Body       = mailMeetingEntity.Body,
                Subject    = mailMeetingEntity.Subject,
                IsBodyHtml = true,
            };

            //Parse MailMeetingEntity -> ICalendar Entity

            // Create a new iCalendar
            iCalendar m_iCalendar = new iCalendar()
            {
                Method    = MailServiceICal.ICalendarMethod_Request, //PUBLISH THÌ KO ADD VÀO TRONG CALENDAR
                Version   = MailServiceICal.ICalendar_Version,
                ProductID = MailServiceICal.ICalendar_ProductID,
            };

            // Create the event, and add it to the iCalendar
            Event m_Event = m_iCalendar.Create <Event>();

            // Set information about the event
            m_Event.UID         = mailMeetingEntity.UID;
            m_Event.DTStamp     = new iCalDateTime(mailMeetingEntity.Stamp);
            m_Event.Start       = new iCalDateTime(mailMeetingEntity.Start);
            m_Event.End         = new iCalDateTime(mailMeetingEntity.End);
            m_Event.Description = mailMeetingEntity.Description;
            m_Event.Location    = mailMeetingEntity.Location;
            m_Event.Summary     = mailMeetingEntity.Description;
            //m_event.Transparency = TransparencyType.Opaque;

            //CONFIG ALARM
            foreach (var m_AlarmEntity in mailMeetingEntity.Alarms)
            {
                AlarmAction m_AlarmAction = new AlarmAction();
                if (m_AlarmEntity.Trigger.Equals(MailServiceICal.Action_Audio))
                {
                    m_AlarmAction = AlarmAction.Audio;
                }
                else if (m_AlarmEntity.Trigger.Equals(MailServiceICal.Action_Display))
                {
                    m_AlarmAction = AlarmAction.Display;
                }
                else if (m_AlarmEntity.Trigger.Equals(MailServiceICal.Action_Email))
                {
                    m_AlarmAction = AlarmAction.Email;
                }
                else if (m_AlarmEntity.Trigger.Equals(MailServiceICal.Action_Procedure))
                {
                    m_AlarmAction = AlarmAction.Procedure;
                }
                m_Event.Alarms.Add(new Alarm
                {
                    Duration    = m_AlarmEntity.Duration,
                    Trigger     = new Trigger(m_AlarmEntity.Trigger),
                    Description = m_AlarmEntity.Description,
                    Repeat      = m_AlarmEntity.RepeatTime,
                    Action      = m_AlarmAction,
                });
            }

            //Add Attendees
            var m_Attendes = new List <IAttendee>();

            foreach (var m_AttendeesEntity in mailMeetingEntity.Attendees)
            {
                m_MailMessage.To.Add(new MailAddress(m_AttendeesEntity.Email));
                IAttendee m_Attendee = new DDay.iCal.Attendee(MailServiceICal.Attendee_MailTo + m_AttendeesEntity.Email);
                if (m_AttendeesEntity.IsOptional)
                {
                    m_Attendee.Role = MailServiceICal.Role_Optional;
                }
                else
                {
                    m_Attendee.Role = MailServiceICal.Role_Request;
                }
                m_Attendes.Add(m_Attendee);
            }

            if (m_Attendes != null && m_Attendes.Count > 0)
            {
                m_Event.Attendees = m_Attendes;
            }

            //Check before send meeting
            if (actionBeforeSendMeeting != null)
            {
                actionBeforeSendMeeting(m_MailMessage);
            }

            DDay.iCal.Serialization.iCalendar.iCalendarSerializer m_Serializer = new DDay.iCal.Serialization.iCalendar.iCalendarSerializer();
            //Convert iCal to string
            string m_iCalendarData = m_Serializer.SerializeToString(m_iCalendar);

            System.Net.Mime.ContentType m_Contype = new System.Net.Mime.ContentType(MailServiceICal.ICalendar_ContentType);
            m_Contype.Parameters.Add(MailServiceICal.ICalendar_Method, MailServiceICal.ICalendarMethod_Request);
            m_Contype.Parameters.Add(MailServiceICal.ICalendar_Name, MailServiceICal.ICalendar_FileName);
            AlternateView m_AlternateView = AlternateView.CreateAlternateViewFromString(m_iCalendarData, m_Contype);

            m_MailMessage.AlternateViews.Add(m_AlternateView);

            m_SmtpClient.Send(m_MailMessage);
            return(m_iCalendarData);
        }
        public ActionResult GetIcsForCalendar(int id)
        {
            DDay.iCal.iCalendar iCal = new DDay.iCal.iCalendar();
            

            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;
                    //evt.GeographicLocation = new GeographicLocation(Convert.ToDouble(l.lat,CultureInfo.InvariantCulture), Convert.ToDouble(l.lon, CultureInfo.InvariantCulture));
                }

                var attendes = new List<IAttendee>();

                if (e.Organisator != null && e.Organisator != 0)
                {
                    var ms = Services.MemberService;
                    var member = ms.GetById(e.Organisator);
                    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;
                    //evt.GeographicLocation = new GeographicLocation(Convert.ToDouble(l.lat, CultureInfo.InvariantCulture), Convert.ToDouble(l.lon, CultureInfo.InvariantCulture));
                }

                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;
                        }
                }
                switch (e.day)
                {
                    case (int)DayOfWeekEnum.Mon:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Monday));
                            break;
                        }
                    case (int)DayOfWeekEnum.Tue:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Tuesday));
                            break;
                        }
                    case (int)DayOfWeekEnum.Wed:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Wednesday));
                            break;
                        }
                    case (int)DayOfWeekEnum.Thu:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Thursday));
                            break;
                        }
                    case (int)DayOfWeekEnum.Fri:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Friday));
                            break;
                        }
                    case (int)DayOfWeekEnum.Sat:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Saturday));
                            break;
                        }
                    case (int)DayOfWeekEnum.Sun:
                        {
                            rp.ByDay.Add(new WeekDay(DayOfWeek.Sunday));
                            break;
                        }
                }
                evt.RecurrenceRules.Add(rp);

                Schedule schedule = new Schedule(new ScheduleWidget.ScheduledEvents.Event()
                {
                    Title = e.title,
                    DaysOfWeekOptions = (DayOfWeekEnum)e.day,
                    FrequencyTypeOptions = (FrequencyTypeEnum)e.frequency,
                    MonthlyIntervalOptions = (MonthlyIntervalEnum)e.monthly_interval
                });

                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.Organisator != null && e.Organisator != 0)
                {
                    var ms = Services.MemberService;
                    var member = ms.GetById(e.Organisator);
                    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 DDay.iCal.Serialization.iCalendar.SerializerFactory();
            // Get a serializer for our object
            IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer;

            string output = serializer.SerializeToString(iCal);
            var contentType = "text/calendar";
            var bytes = Encoding.UTF8.GetBytes(output);

            return File(bytes, contentType, cal.Calendarname + ".ics");
        }