Ejemplo n.º 1
0
        public static Dictionary <int, string> GetYears(Guid siteID)
        {
            Dictionary <int, string> lst = new Dictionary <int, string>();
            List <int> years             = new List <int>();

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                MinMaxDate mm = (from c in db.vw_carrot_CalendarEventProfiles
                                 where c.SiteID == siteID
                                 group c by 1 into g
                                 select new MinMaxDate {
                    MinDate = g.Min(x => x.EventStartDate),
                    MaxDate = g.Max(x => x.EventEndDate)
                }).FirstOrDefault();

                if (mm != null)
                {
                    int startYear = mm.MinDate.Year;
                    int yearCount = mm.MaxDate.Year - startYear + 1;

                    years = Enumerable.Range(startYear, yearCount).ToList();
                }
            }

            lst = (from r in years
                   select new KeyValuePair <int, string>(r, String.Format("Events in {0}", r)))
                  .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            lst.Add(-3, "Future Only");
            lst.Add(-2, "Current Events");
            lst.Add(-1, "All Events");

            return(lst.OrderBy(x => x.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
        }
Ejemplo n.º 2
0
        public static List <carrot_CalendarEventCategory> GetCalendarCategories(Guid siteID)
        {
            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                if (!db.carrot_CalendarEventCategories.Where(x => x.SiteID == siteID).Any())
                {
                    carrot_CalendarEventCategory itm = new carrot_CalendarEventCategory();

                    itm = new carrot_CalendarEventCategory();
                    itm.CalendarEventCategoryID = Guid.NewGuid();
                    itm.SiteID = siteID;

                    itm.CategoryName    = "Default";
                    itm.CategoryFGColor = HEX_Black;
                    itm.CategoryBGColor = HEX_White;

                    db.carrot_CalendarEventCategories.InsertOnSubmit(itm);

                    db.SubmitChanges();
                }

                return((from c in db.carrot_CalendarEventCategories
                        orderby c.CategoryName
                        where c.SiteID == siteID
                        select c).ToList());
            }
        }
Ejemplo n.º 3
0
        protected void btnSaveButton_Click(object sender, EventArgs e)
        {
            bool bAdd = false;

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var itm = (from c in db.carrot_CalendarEventCategories
                           where c.CalendarEventCategoryID == ItemGuid
                           select c).FirstOrDefault();

                if (itm == null)
                {
                    bAdd     = true;
                    ItemGuid = Guid.NewGuid();
                    itm      = new carrot_CalendarEventCategory();
                    itm.CalendarEventCategoryID = ItemGuid;
                    itm.SiteID = SiteID;
                }

                itm.CategoryName    = txtCategoryName.Text;
                itm.CategoryFGColor = ddlFGColor.SelectedValue;
                itm.CategoryBGColor = ddlBGColor.SelectedValue;

                if (bAdd)
                {
                    db.carrot_CalendarEventCategories.InsertOnSubmit(itm);
                }

                db.SubmitChanges();
            }

            Response.Redirect(CreateLink(ModuleName, String.Format("id={0}", ItemGuid)));
        }
Ejemplo n.º 4
0
        protected void SetCalendar()
        {
            SiteData site = SiteData.CurrentSite;

            DateTime dtStart = Calendar1.CalendarDate.AddDays(1 - Calendar1.CalendarDate.Day).Date;
            DateTime dtEnd   = dtStart.AddMonths(1).Date;

            dtStart = site.ConvertSiteTimeToUTC(dtStart);
            dtEnd   = site.ConvertSiteTimeToUTC(dtEnd);

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                List <vw_carrot_CalendarEvent> lst = (from c in db.vw_carrot_CalendarEvents
                                                      where c.EventDate >= dtStart &&
                                                      c.EventDate < dtEnd &&
                                                      c.SiteID == SiteID
                                                      orderby c.EventDate, c.EventStartTime
                                                      select c).ToList();

                lst.ForEach(x => x.EventDate = site.ConvertUTCToSiteTime(x.EventDate));

                List <DateTime> dates = (from dd in lst select dd.EventDate.Date).Distinct().ToList();

                Calendar1.HilightDateList = dates;

                CalendarHelper.BindDataBoundControl(dgEvents, lst);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool bAdd = false;

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var currItem = (from c in db.carrot_CalendarEvents
                                where c.CalendarEventID == ItemGuid
                                select c).FirstOrDefault();

                if (currItem == null)
                {
                    bAdd     = true;
                    ItemGuid = Guid.NewGuid();
                    currItem = new carrot_CalendarEvent();
                    currItem.CalendarEventID = ItemGuid;
                }

                currItem.EventDetail = reContent.Text;
                currItem.IsCancelled = chkIsCancelled.Checked;

                currItem.EventStartTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventStartTime);
                currItem.EventEndTime   = CalendarHelper.GetTimeSpanFromTextbox(txtEventEndTime);

                if (bAdd)
                {
                    db.carrot_CalendarEvents.InsertOnSubmit(currItem);
                }

                db.SubmitChanges();
            }

            Response.Redirect(CreateLink(ModuleName, String.Format("id={0}", ItemGuid)));
        }
Ejemplo n.º 6
0
 public static List <carrot_CalendarFrequency> GetCalendarFrequencies()
 {
     using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
         return((from c in db.carrot_CalendarFrequencies
                 orderby c.FrequencySortOrder
                 select c).ToList());
     }
 }
Ejemplo n.º 7
0
 public static carrot_CalendarEventProfile GetProfile(Guid calendarEventProfileID)
 {
     using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
         return((from c in db.carrot_CalendarEventProfiles
                 where c.CalendarEventProfileID == calendarEventProfileID
                 select c).FirstOrDefault());
     }
 }
Ejemplo n.º 8
0
 public static carrot_CalendarEventCategory GetCalendarCategory(Guid calendarEventCategoryID)
 {
     using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
         return((from c in db.carrot_CalendarEventCategories
                 where c.CalendarEventCategoryID == calendarEventCategoryID
                 select c).FirstOrDefault());
     }
 }
Ejemplo n.º 9
0
 public static List <vw_carrot_CalendarEvent> GetEventView(Guid calendarEventProfileID)
 {
     using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
         return((from c in db.vw_carrot_CalendarEvents
                 orderby c.EventDate
                 where c.CalendarEventProfileID == calendarEventProfileID
                 select c).ToList());
     }
 }
Ejemplo n.º 10
0
 public static List <vw_carrot_CalendarEventProfile> GetProfileView(Guid siteID)
 {
     using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
         return((from c in db.vw_carrot_CalendarEventProfiles
                 orderby c.EventStartDate
                 where c.SiteID == siteID
                 select c).ToList());
     }
 }
Ejemplo n.º 11
0
        public static carrot_CalendarEventProfile CopyEvent(Guid calendarEventProfileID)
        {
            var srcProfile = GetProfile(calendarEventProfileID);

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var item = new carrot_CalendarEventProfile();
                item.CalendarEventProfileID = Guid.NewGuid();
                item.SiteID      = srcProfile.SiteID;
                item.EventDetail = srcProfile.EventDetail;

                item.EventRepeatPattern      = srcProfile.EventRepeatPattern;
                item.CalendarFrequencyID     = srcProfile.CalendarFrequencyID;
                item.CalendarEventCategoryID = srcProfile.CalendarEventCategoryID;

                item.EventStartDate = srcProfile.EventStartDate;
                item.EventEndDate   = srcProfile.EventEndDate;
                item.EventStartTime = srcProfile.EventStartTime;
                item.EventEndTime   = srcProfile.EventEndTime;

                item.IsPublic          = srcProfile.IsPublic;
                item.IsAllDayEvent     = srcProfile.IsAllDayEvent;
                item.IsCancelled       = srcProfile.IsCancelled;
                item.IsCancelledPublic = srcProfile.IsCancelledPublic;

                db.carrot_CalendarEventProfiles.InsertOnSubmit(item);

                if (srcProfile != null)
                {
                    var lst = (from m in db.carrot_CalendarEvents
                               where m.CalendarEventProfileID == calendarEventProfileID
                               select m);

                    foreach (carrot_CalendarEvent date in lst)
                    {
                        carrot_CalendarEvent evt = new carrot_CalendarEvent {
                            CalendarEventID        = Guid.NewGuid(),
                            CalendarEventProfileID = item.CalendarEventProfileID,
                            EventDate   = date.EventDate,
                            EventDetail = date.EventDetail,
                            IsCancelled = date.IsCancelled
                        };

                        db.carrot_CalendarEvents.InsertOnSubmit(evt);
                    }

                    db.SubmitChanges();
                }

                return(item);
            }
        }
Ejemplo n.º 12
0
        public static List <vw_carrot_CalendarEventProfile> GetProfileView(Guid siteID, int eventYear)
        {
            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                if (eventYear == -1)
                {
                    return((from c in db.vw_carrot_CalendarEventProfiles
                            orderby c.EventStartDate
                            where c.SiteID == siteID
                            select c).ToList());
                }

                DateTime dateStart = DateTime.MinValue;
                DateTime dateEnd   = DateTime.MaxValue;

                if (eventYear == -2)
                {
                    dateStart = DateTime.Now.Date.AddDays(-90);
                    dateEnd   = DateTime.Now.Date.AddDays(180);
                }

                if (eventYear == -3)
                {
                    dateStart = DateTime.UtcNow.Date.AddDays(-1);
                    dateEnd   = DateTime.Now.Date.AddYears(200);
                }

                if (eventYear > 1000)
                {
                    dateStart = Convert.ToDateTime(String.Format("{0}-01-01", eventYear));
                    dateEnd   = Convert.ToDateTime(String.Format("{0}-01-01", eventYear + 1)).AddMilliseconds(-1);
                }

                if (eventYear == -2 || eventYear > 1000)
                {
                    return((from c in db.vw_carrot_CalendarEventProfiles
                            orderby c.EventStartDate
                            where c.SiteID == siteID &&
                            (c.EventEndDate >= dateStart && c.EventStartDate <= dateEnd)
                            select c).ToList());
                }

                return((from c in db.vw_carrot_CalendarEventProfiles
                        orderby c.EventStartDate
                        where c.SiteID == siteID &&
                        (c.EventStartDate >= dateStart)
                        select c).ToList());
            }
        }
Ejemplo n.º 13
0
        protected static void InsertEventsFromList(CalendarDataContext ctx, CalendarEvent item, List <DateTime> lstDates)
        {
            SiteData site = SiteData.CurrentSite;

            foreach (DateTime date in lstDates)
            {
                carrot_CalendarEvent evt = new carrot_CalendarEvent {
                    CalendarEventID        = Guid.NewGuid(),
                    CalendarEventProfileID = item.CalendarEventProfileID,
                    EventDate   = site.ConvertSiteTimeToUTC(date),
                    IsCancelled = false
                };

                ctx.carrot_CalendarEvents.InsertOnSubmit(evt);
            }
        }
        protected void SetCalendar()
        {
            SiteData site = SiteData.CurrentSite;

            DateTime dtStart = Calendar1.CalendarDate.AddDays(1 - Calendar1.CalendarDate.Day).Date;
            DateTime dtEnd   = dtStart.AddMonths(1).Date;

            dtStart = site.ConvertSiteTimeToUTC(dtStart);
            dtEnd   = site.ConvertSiteTimeToUTC(dtEnd);

            List <vw_carrot_CalendarEvent> lst = null;

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                lst = (from c in db.vw_carrot_CalendarEvents
                       where c.EventDate >= dtStart &&
                       c.EventDate < dtEnd &&
                       c.SiteID == SiteID &&
                       c.IsPublic == true &&
                       (!c.IsCancelledEvent || c.IsCancelledPublic) &&
                       (!c.IsCancelledSeries || c.IsCancelledPublic)
                       orderby c.EventDate ascending, c.EventStartTime ascending, c.IsCancelledEvent ascending
                       select c).ToList();
            }

            lst.ForEach(x => x.EventDate = site.ConvertUTCToSiteTime(x.EventDate));

            List <DateTime> dates = (from dd in lst select dd.EventDate.Date).Distinct().ToList();

            List <Guid> cats = (from dd in lst select dd.CalendarEventCategoryID).Distinct().ToList();

            Calendar1.HilightDateList = dates;

            CalendarHelper.BindRepeater(rpEvent, lst);

            if (lst.Count > 0)
            {
                phNone.Visible = false;
            }
            else
            {
                phNone.Visible = true;
            }

            SetDDLSelections();

            CalendarHelper.BindRepeater(rpCat, CalendarHelper.GetCalendarCategories(SiteID).Where(x => cats.Contains(x.CalendarEventCategoryID)));
        }
Ejemplo n.º 15
0
        public static void RemoveEvent(Guid calendarEventProfileID)
        {
            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var profile = (from c in db.carrot_CalendarEventProfiles
                               where c.CalendarEventProfileID == calendarEventProfileID
                               select c).FirstOrDefault();

                if (profile != null)
                {
                    var lst = (from m in db.carrot_CalendarEvents
                               where m.CalendarEventProfileID == profile.CalendarEventProfileID
                               select m);

                    db.carrot_CalendarEvents.DeleteAllOnSubmit(lst);
                    db.carrot_CalendarEventProfiles.DeleteOnSubmit(profile);
                    db.SubmitChanges();
                }
            }
        }
Ejemplo n.º 16
0
        protected void SetCalendar()
        {
            SiteData site = SiteData.CurrentSite;

            DateTime dtStart = DateTime.Now.AddDays(DaysInPast).Date;
            DateTime dtEnd   = DateTime.Now.AddDays(DaysInFuture).Date;

            dtStart = site.ConvertSiteTimeToUTC(dtStart);
            dtEnd   = site.ConvertSiteTimeToUTC(dtEnd);

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var lst = (from c in db.vw_carrot_CalendarEvents
                           where c.EventDate >= dtStart &&
                           c.EventDate <= dtEnd &&
                           c.SiteID == SiteID &&
                           c.IsPublic == true &&
                           (!c.IsCancelledEvent || c.IsCancelledPublic) &&
                           (!c.IsCancelledSeries || c.IsCancelledPublic)
                           orderby c.EventDate, c.EventStartTime
                           select c).Take(TakeTop).ToList();

                lst.ForEach(x => x.EventDate = site.ConvertUTCToSiteTime(x.EventDate));

                CalendarHelper.BindRepeater(rpDates, lst);
            }

            if (!string.IsNullOrEmpty(this.CalendarURL))
            {
                lnkHyper.NavigateUrl = this.CalendarURL;
                phLink.Visible       = true;
            }
            else
            {
                phLink.Visible = false;
            }
        }
		public static void SaveFrequencies(CalendarDataContext ctx, CalendarEvent newItem, CalendarEvent oldItem) {
			SiteData site = SiteData.CurrentSite;
			DateTime todayDate = DateTime.UtcNow.Date;

			DateTime newStartDateTime = GetStartDateTimeFromItem(newItem);
			DateTime newEndDateTime = GetEndDateTimeFromItem(newItem);

			List<DateTime> eventDates = GetSequenceDates(todayDate, false, newItem);

			if (newItem.EventStartDate.Date <= todayDate.AddYears(-1)
				|| newItem.EventStartDate.Date >= todayDate.AddMonths(6)) {
				eventDates = eventDates.Union(GetSequenceDates(todayDate, true, newItem)).Distinct().ToList();
			}

			List<DateTime> eventUTCDates = (from d in eventDates select site.ConvertSiteTimeToUTC(d)).ToList();

			if (oldItem.SiteID == Guid.Empty && oldItem.CalendarFrequencyID == Guid.Empty) {
				InsertEventsFromList(ctx, newItem, eventDates);
			} else {
				DateTime oldStartDateTime = GetStartDateTimeFromItem(oldItem);

				int iDays = GetDateDiffDays(newStartDateTime, oldStartDateTime);

				TimeSpan newTS = new TimeSpan();
				if (newItem.EventStartTime.HasValue) {
					newTS = newItem.EventStartTime.Value;
				}
				TimeSpan oldTS = new TimeSpan();
				if (oldItem.EventStartTime.HasValue) {
					oldTS = oldItem.EventStartTime.Value;
				}
				int iMin = GetDateDiffMinutes(newTS, oldTS);

				DateTime dateMax = (from d in eventUTCDates
									orderby d.Date descending
									where d.Date >= DateTime.UtcNow.Date
									select d).FirstOrDefault();

				if (dateMax == DateTime.MinValue) {
					dateMax = DateTime.UtcNow.Date;
				}
				dateMax = site.ConvertSiteTimeToUTC(dateMax).Date;

				DateTime dStart = site.ConvertSiteTimeToUTC(newStartDateTime).Date;
				DateTime dEnd = site.ConvertSiteTimeToUTC(newEndDateTime).Date;

				int iFuture = (from d in eventUTCDates
							   where d.Date >= DateTime.UtcNow.Date
							   select d).Count();

				int iMaxRange = (from c in ctx.carrot_CalendarEvents
								 where c.CalendarEventProfileID == newItem.CalendarEventProfileID
										&& c.EventDate.Date >= dateMax.Date
								 select c.CalendarEventID).Count();

				if (oldItem.Frequency != newItem.Frequency || oldItem.RecursEvery != newItem.RecursEvery
					|| iMin != 0 || iDays != 0 || (iMaxRange != iFuture && iFuture > 0)
					|| oldItem.EventEndDate.Date != newItem.EventEndDate.Date
					|| oldItem.EventStartDate.Date != newItem.EventStartDate.Date) {
					var lstEvents = (from c in ctx.carrot_CalendarEvents
									 orderby c.EventDate
									 where c.CalendarEventProfileID == newItem.CalendarEventProfileID
									 select c).ToList();

					if (iDays != 0) {
						lstEvents.ForEach(x => x.EventDate = site.ConvertUTCToSiteTime(x.EventDate).Date);

						if (newItem.Frequency == FrequencyType.Daily || newItem.Frequency == FrequencyType.Once) {
							lstEvents.ForEach(x => x.EventDate = x.EventDate.Date.AddDays(iDays));
						}

						if (newItem.Frequency == FrequencyType.Weekly) {
							int iDayShift = iDays;
							if (newItem.EventRepeatPattern != null) {
								iDayShift = 7 * GetDateDiffWeeks(newItem.EventStartDate, oldItem.EventStartDate);
							}
							lstEvents.ForEach(x => x.EventDate = x.EventDate.Date.AddDays(iDayShift));
						}

						if (newItem.Frequency == FrequencyType.Monthly || newItem.Frequency == FrequencyType.Yearly) {
							int iMonths = GetDateDiffMonths(newItem.EventStartDate, oldItem.EventStartDate);

							lstEvents.ForEach(x => x.EventDate = x.EventDate.AddMonths(iMonths).Date);
						}

						if (newItem.Frequency == oldItem.Frequency && (newItem.Frequency == FrequencyType.Monthly || newItem.Frequency == FrequencyType.Yearly)) {
							lstEvents.ForEach(x => x.EventDate = CorrectMonthlyYearlyDate(x.EventDate, newItem.EventStartDate.Day).Date);
						}

						lstEvents.ForEach(x => x.EventDate = site.ConvertSiteTimeToUTC(x.EventDate));
					}

					var lstDel = (from l in lstEvents
								  orderby l.EventDate
								  where l.EventDate.Date < dStart || l.EventDate.Date > dEnd
									|| !eventUTCDates.Contains(l.EventDate)
								  select l).ToList();

					lstDel.RemoveAll(l => eventUTCDates.Contains(l.EventDate));

					ctx.carrot_CalendarEvents.DeleteAllOnSubmit(lstDel);

					var lstExist = (from l in lstEvents select site.ConvertUTCToSiteTime(l.EventDate).Date).Distinct().ToList();

					var lstAdd = (from d in eventDates
								  where !lstExist.Contains(d.Date)
								  select d).ToList();

					InsertEventsFromList(ctx, newItem, lstAdd);
				}
			}
		}
		protected static void InsertEventsFromList(CalendarDataContext ctx, CalendarEvent item, List<DateTime> lstDates) {
			SiteData site = SiteData.CurrentSite;

			foreach (DateTime date in lstDates) {
				carrot_CalendarEvent evt = new carrot_CalendarEvent {
					CalendarEventID = Guid.NewGuid(),
					CalendarEventProfileID = item.CalendarEventProfileID,
					EventDate = site.ConvertSiteTimeToUTC(date),
					IsCancelled = false
				};

				ctx.carrot_CalendarEvents.InsertOnSubmit(evt);
			}
		}
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool bAdd = false;

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var currItem = (from c in db.carrot_CalendarEventProfiles
                                where c.CalendarEventProfileID == ItemGuid
                                select c).FirstOrDefault();

                var origItem = new CalendarEvent(currItem);

                if (currItem == null)
                {
                    bAdd     = true;
                    ItemGuid = Guid.NewGuid();
                    currItem = new carrot_CalendarEventProfile();
                    currItem.CalendarEventProfileID = ItemGuid;
                    currItem.SiteID          = SiteID;
                    currItem.IsHoliday       = false;
                    currItem.IsAnnualHoliday = false;
                    currItem.RecursEvery     = 1;
                }

                currItem.CalendarFrequencyID     = new Guid(ddlRecurr.SelectedValue);
                currItem.CalendarEventCategoryID = new Guid(ddlCategory.SelectedValue);

                currItem.EventRepeatPattern = null;

                List <string> days = CalendarHelper.GetCheckedItemStringByValue(rpDays, true, "chkDay");

                if (CalendarFrequencyHelper.GetFrequencyTypeByID(currItem.CalendarFrequencyID) == CalendarFrequencyHelper.FrequencyType.Weekly &&
                    days.Count > 0)
                {
                    int dayMask = (from d in days select int.Parse(d)).Sum();

                    if (dayMask > 0)
                    {
                        currItem.EventRepeatPattern = dayMask;
                    }
                }

                currItem.EventTitle  = txtEventTitle.Text;
                currItem.EventDetail = reContent.Text;
                currItem.RecursEvery = int.Parse(txtRecursEvery.Text);

                currItem.IsPublic          = chkIsPublic.Checked;
                currItem.IsAllDayEvent     = chkIsAllDayEvent.Checked;
                currItem.IsCancelled       = chkIsCancelled.Checked;
                currItem.IsCancelledPublic = chkIsCancelledPublic.Checked;

                currItem.EventStartDate = Convert.ToDateTime(txtEventStartDate.Text);
                currItem.EventStartTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventStartTime);

                currItem.EventEndDate = Convert.ToDateTime(txtEventEndDate.Text);
                currItem.EventEndTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventEndTime);

                if (bAdd)
                {
                    db.carrot_CalendarEventProfiles.InsertOnSubmit(currItem);
                }

                CalendarFrequencyHelper.SaveFrequencies(db, new CalendarEvent(currItem), origItem);

                db.SubmitChanges();
            }

            Response.Redirect(CreateLink(ModuleName, String.Format("id={0}", ItemGuid)));
        }
Ejemplo n.º 20
0
        public static void SaveFrequencies(CalendarDataContext ctx, CalendarEvent newItem, CalendarEvent oldItem)
        {
            SiteData site      = SiteData.CurrentSite;
            DateTime todayDate = DateTime.UtcNow.Date;

            DateTime newStartDateTime = GetStartDateTimeFromItem(newItem);
            DateTime newEndDateTime   = GetEndDateTimeFromItem(newItem);

            List <DateTime> eventDates = GetSequenceDates(todayDate, false, newItem);

            if (newItem.EventStartDate.Date <= todayDate.AddYears(-1) ||
                newItem.EventStartDate.Date >= todayDate.AddMonths(6))
            {
                eventDates = eventDates.Union(GetSequenceDates(todayDate, true, newItem)).Distinct().ToList();
            }

            List <DateTime> eventUTCDates = (from d in eventDates select site.ConvertSiteTimeToUTC(d)).ToList();

            if (oldItem.SiteID == Guid.Empty && oldItem.CalendarFrequencyID == Guid.Empty)
            {
                InsertEventsFromList(ctx, newItem, eventDates);
            }
            else
            {
                DateTime oldStartDateTime = GetStartDateTimeFromItem(oldItem);

                int iDays = GetDateDiffDays(newStartDateTime, oldStartDateTime);

                TimeSpan newTS = new TimeSpan();
                if (newItem.EventStartTime.HasValue)
                {
                    newTS = newItem.EventStartTime.Value;
                }
                TimeSpan oldTS = new TimeSpan();
                if (oldItem.EventStartTime.HasValue)
                {
                    oldTS = oldItem.EventStartTime.Value;
                }
                int iMin = GetDateDiffMinutes(newTS, oldTS);

                DateTime dateMax = (from d in eventUTCDates
                                    orderby d.Date descending
                                    where d.Date >= DateTime.UtcNow.Date
                                    select d).FirstOrDefault();

                if (dateMax == DateTime.MinValue)
                {
                    dateMax = DateTime.UtcNow.Date;
                }
                dateMax = site.ConvertSiteTimeToUTC(dateMax).Date;

                DateTime dStart = site.ConvertSiteTimeToUTC(newStartDateTime).Date;
                DateTime dEnd   = site.ConvertSiteTimeToUTC(newEndDateTime).Date;

                int iFuture = (from d in eventUTCDates
                               where d.Date >= DateTime.UtcNow.Date
                               select d).Count();

                int iMaxRange = (from c in ctx.carrot_CalendarEvents
                                 where c.CalendarEventProfileID == newItem.CalendarEventProfileID &&
                                 c.EventDate.Date >= dateMax.Date
                                 select c.CalendarEventID).Count();

                if (oldItem.Frequency != newItem.Frequency || oldItem.RecursEvery != newItem.RecursEvery ||
                    iMin != 0 || iDays != 0 || (iMaxRange != iFuture && iFuture > 0) ||
                    oldItem.EventEndDate.Date != newItem.EventEndDate.Date ||
                    oldItem.EventStartDate.Date != newItem.EventStartDate.Date)
                {
                    var lstEvents = (from c in ctx.carrot_CalendarEvents
                                     orderby c.EventDate
                                     where c.CalendarEventProfileID == newItem.CalendarEventProfileID
                                     select c).ToList();

                    if (iDays != 0)
                    {
                        lstEvents.ForEach(x => x.EventDate = site.ConvertUTCToSiteTime(x.EventDate).Date);

                        if (newItem.Frequency == FrequencyType.Daily || newItem.Frequency == FrequencyType.Once)
                        {
                            lstEvents.ForEach(x => x.EventDate = x.EventDate.Date.AddDays(iDays));
                        }

                        if (newItem.Frequency == FrequencyType.Weekly)
                        {
                            int iDayShift = iDays;
                            if (newItem.EventRepeatPattern != null)
                            {
                                iDayShift = 7 * GetDateDiffWeeks(newItem.EventStartDate, oldItem.EventStartDate);
                            }
                            lstEvents.ForEach(x => x.EventDate = x.EventDate.Date.AddDays(iDayShift));
                        }

                        if (newItem.Frequency == FrequencyType.Monthly || newItem.Frequency == FrequencyType.Yearly)
                        {
                            int iMonths = GetDateDiffMonths(newItem.EventStartDate, oldItem.EventStartDate);

                            lstEvents.ForEach(x => x.EventDate = x.EventDate.AddMonths(iMonths).Date);
                        }

                        if (newItem.Frequency == oldItem.Frequency && (newItem.Frequency == FrequencyType.Monthly || newItem.Frequency == FrequencyType.Yearly))
                        {
                            lstEvents.ForEach(x => x.EventDate = CorrectMonthlyYearlyDate(x.EventDate, newItem.EventStartDate.Day).Date);
                        }

                        lstEvents.ForEach(x => x.EventDate = site.ConvertSiteTimeToUTC(x.EventDate));
                    }

                    var lstDel = (from l in lstEvents
                                  orderby l.EventDate
                                  where l.EventDate.Date <dStart || l.EventDate.Date> dEnd ||
                                  !eventUTCDates.Contains(l.EventDate)
                                  select l).ToList();

                    lstDel.RemoveAll(l => eventUTCDates.Contains(l.EventDate));

                    ctx.carrot_CalendarEvents.DeleteAllOnSubmit(lstDel);

                    var lstExist = (from l in lstEvents select site.ConvertUTCToSiteTime(l.EventDate).Date).Distinct().ToList();

                    var lstAdd = (from d in eventDates
                                  where !lstExist.Contains(d.Date)
                                  select d).ToList();

                    InsertEventsFromList(ctx, newItem, lstAdd);
                }
            }
        }