/*
         * The UploadFiles method coordinates saving a file to Azure BLOB storage with updating the database through a Model class.
         */
        public ActionResult UploadFiles(string EventID)
        {
            bool   isSaved     = true;
            string savedStatus = string.Empty;

            for (int idx = 0; idx < Request.Files.Count; idx++)
            {
                HttpPostedFileBase file       = (HttpPostedFileBase)Request.Files[idx];
                Stream             fileStream = file.InputStream;
                string             fileName   = getFileName(file.FileName);
                Models.EventData   eventData  = new Models.EventData();
                savedStatus = eventData.AddAttachmentItem(EventID, EventID + "/" + fileName);

                if (savedStatus.StartsWith("Point8020.Success"))
                {
                    savedStatus = SaveFileToAzure(fileStream, file.ContentType, EventID + "/" + fileName);
                    if (savedStatus != "Point8020.Success")
                    {
                        isSaved = false;
                    }
                }
                else
                {
                    isSaved = false;
                }
            }
            if (isSaved == true)
            {
                return(RedirectToAction("Events", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "File could not be saved: Error Code is: " + savedStatus, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        public ActionResult CreateEvent(string EventTitle, string EventVenue, string EventAddress1, string EventAddress2, string EventCity, string EventState, string EventPostalCode, DateTime EventStartDateTime, DateTime EventEndDateTime, string EventDescription)
        {
            Models.EventData eventData = new Models.EventData();
            string           saved     = eventData.AddEvent(EventTitle, EventVenue, EventAddress1, EventAddress2, EventCity, EventState, EventPostalCode, EventStartDateTime.ToUniversalTime(), EventEndDateTime.ToUniversalTime(), EventDescription);

            if (saved.StartsWith("Point8020.Error"))
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "Event could not be saved: Error Code is: " + saved, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    if (clientContext != null)
                    {
                        hostWeb = clientContext.Web;
                        clientContext.Load(hostWeb);
                        ListCollection hostLists = hostWeb.Lists;
                        clientContext.Load(hostLists);
                        clientContext.ExecuteQuery();
                        bool calendarFound = false;
                        foreach (List lst in hostLists)
                        {
                            if (lst.BaseTemplate == (int)ListTemplateType.Events)
                            {
                                if (lst.Title == "Contoso Events Calendar")
                                {
                                    calendarFound = true;
                                    break;
                                }
                            }
                        }
                        eVentCalendarExists = calendarFound;
                        if (calendarFound)
                        {
                            List calendar = hostWeb.Lists.GetByTitle("Contoso Events Calendar");
                            clientContext.Load(calendar);
                            clientContext.ExecuteQuery();
                            ListItemCreationInformation itemInfo = new ListItemCreationInformation();
                            ListItem newEvent = calendar.AddItem(itemInfo);
                            newEvent["Title"]     = EventTitle;
                            newEvent["Location"]  = EventCity;
                            newEvent["EventDate"] = EventStartDateTime.ToUniversalTime();
                            newEvent["EndDate"]   = EventEndDateTime.ToUniversalTime();
                            newEvent["Category"]  = saved;

                            newEvent.Update();
                            clientContext.ExecuteQuery();
                        }
                    }
                }

                return(RedirectToAction("Events", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        public ActionResult Events()
        {
            getSharePointData();
            ViewBag.Title         = "Contoso Events List";
            ViewBag.DatabaseCheck = checkDB();
            ViewBag.CalendarCheck = eVentCalendarExists;
            ViewBag.IsSiteOwner   = isSiteOwner;
            Models.EventData    eventData = new Models.EventData();
            List <Models.Event> events    = eventData.EventList(ViewBag.UserName, isSiteOwner);

            ViewBag.Events = events;
            return(View());
        }
        /*
         * The SaveRoles method uses the Model classes to interact with data in the database.
         */
        public ActionResult SaveRoles(string eventID, string items)
        {
            Models.EventData eventData = new Models.EventData();
            string           saved     = eventData.SaveRoles(eventID, items);

            if (saved.StartsWith("Point8020.Error"))
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "Security settings could not be saved: Error Code is: " + saved, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                return(RedirectToAction("Events", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        /*
         * The DeleteAttachmentItem method uses the Model classes to interact with data in the database.
         */
        public ActionResult DeleteAttachmentItem(string eventID, string itemID)
        {
            Models.EventData eventData = new Models.EventData();
            string           saved     = eventData.DeleteAttachmentItem(itemID);

            if (saved.StartsWith("Point8020.Error"))
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "Item could not be deleted: Error Code is: " + saved, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                return(RedirectToAction("Attachments", "Home", new { EventID = eventID, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        public ActionResult SaveCateringItem(string eventID, string itemID, string title, string description)
        {
            Models.EventData eventData = new Models.EventData();
            string           saved     = eventData.UpdateCateringItem(eventID, itemID, title, description);

            if (saved.StartsWith("Point8020.Error"))
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "Item could not be saved: Error Code is: " + saved, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                return(RedirectToAction("Catering", "Home", new { EventID = eventID, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        public ActionResult CreateAttendeeItem(string eventID, string name, string email)
        {
            Models.EventData eventData = new Models.EventData();
            string           saved     = eventData.AddAttendeeItem(eventID, name, email);

            if (saved.StartsWith("Point8020.Error"))
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "Item could not be saved: Error Code is: " + saved, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                return(RedirectToAction("Attendees", "Home", new { EventID = eventID, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        /*
         * The DeleteEvent method uses the Model classes to interact with data in the database.
         * It also uses the SharePoint client-side object model to delete the corresponding calendar item
         */
        public ActionResult DeleteEvent(string EventID)
        {
            Models.EventData eData  = new Models.EventData();
            bool             result = eData.DeleteEvent(EventID);
            var spContext           = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    hostWeb = clientContext.Web;
                    clientContext.Load(hostWeb);
                    ListCollection hostLists = hostWeb.Lists;
                    clientContext.Load(hostLists);
                    clientContext.ExecuteQuery();
                    bool calendarFound = false;
                    foreach (List lst in hostLists)
                    {
                        if (lst.BaseTemplate == (int)ListTemplateType.Events)
                        {
                            if (lst.Title == "Contoso Events Calendar")
                            {
                                calendarFound = true;
                                break;
                            }
                        }
                    }
                    eVentCalendarExists = calendarFound;
                    if (calendarFound)
                    {
                        List calendar = hostWeb.Lists.GetByTitle("Contoso Events Calendar");

                        clientContext.Load(calendar);
                        clientContext.ExecuteQuery();
                        CamlQuery camlQuery = new CamlQuery();
                        camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Category' /><Value Type='Text'>" + EventID + "</Value></Eq></Where></Query></View>";
                        ListItemCollection eventItems = calendar.GetItems(camlQuery);
                        clientContext.Load(eventItems);
                        clientContext.ExecuteQuery();
                        foreach (ListItem eventToDelete in eventItems)
                        {
                            eventToDelete.DeleteObject();
                        }
                        clientContext.ExecuteQuery();
                    }
                }
                return(RedirectToAction("events", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
        }
        public ActionResult Catering(string EventID)
        {
            getSharePointData();
            ViewBag.Title         = "Contoso Events Catering";
            ViewBag.DatabaseCheck = checkDB();
            ViewBag.CalendarCheck = eVentCalendarExists;
            ViewBag.IsSiteOwner   = isSiteOwner;
            ViewBag.IsCoordinator = isCoordinator(EventID);
            Models.EventData            eventData     = new Models.EventData();
            List <Models.EventCatering> cateringItems = eventData.Catering(EventID);

            ViewBag.EventID       = EventID;
            ViewBag.CateringItems = cateringItems;
            return(View());
        }
        public ActionResult Attachments(string EventID)
        {
            getSharePointData();
            ViewBag.Title         = "Contoso Events Attachments";
            ViewBag.DatabaseCheck = checkDB();
            ViewBag.CalendarCheck = eVentCalendarExists;
            ViewBag.IsSiteOwner   = isSiteOwner;
            ViewBag.IsCoordinator = isCoordinator(EventID);
            Models.EventData eventData = new Models.EventData();
            List <Models.EventAttachment> AttachmentItems = eventData.Attachments(EventID);

            ViewBag.EventID         = EventID;
            ViewBag.AttachmentItems = AttachmentItems;
            ViewBag.SPContextUrl    = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri;
            return(View());
        }
        public ActionResult Roles(string EventID)
        {
            getSharePointData();
            ViewBag.Title         = "Contoso Events Security";
            ViewBag.DatabaseCheck = checkDB();
            ViewBag.CalendarCheck = eVentCalendarExists;
            ViewBag.IsSiteOwner   = isSiteOwner;
            ViewBag.IsCoordinator = isCoordinator(EventID);
            Models.EventData        eventData    = new Models.EventData();
            List <Models.EventRole> coordinators = eventData.Coordinators(EventID);

            foreach (Models.EventRole coordinator in coordinators)
            {
                members.Remove(coordinator.MemberName);
            }
            ViewBag.SiteMembers  = members;
            ViewBag.EventID      = EventID;
            ViewBag.Coordinators = coordinators;
            return(View());
        }
        public ActionResult SaveEvent(string EventID, string EventTitle, string EventVenue, string EventAddress1, string EventAddress2, string EventCity, string EventState, string EventPostalCode, DateTime EventStartDateTime, DateTime EventEndDateTime, string EventDescription)
        {
            Models.EventData eventData = new Models.EventData();
            string           saved     = eventData.UpdateEvent(EventID, EventTitle, EventVenue, EventAddress1, EventAddress2, EventCity, EventState, EventPostalCode, EventStartDateTime.ToUniversalTime(), EventEndDateTime.ToUniversalTime(), EventDescription);

            if (saved.StartsWith("Point8020.Error"))
            {
                return(RedirectToAction("TrappedError", "Home", new { ErrorMessage = "Event could not be saved: Error Code is: " + saved, SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
            }
            else
            {
                var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    if (clientContext != null)
                    {
                        hostWeb = clientContext.Web;
                        clientContext.Load(hostWeb);
                        ListCollection hostLists = hostWeb.Lists;
                        clientContext.Load(hostLists);
                        clientContext.ExecuteQuery();
                        bool calendarFound = false;
                        foreach (List lst in hostLists)
                        {
                            if (lst.BaseTemplate == (int)ListTemplateType.Events)
                            {
                                if (lst.Title == "Contoso Events Calendar")
                                {
                                    calendarFound = true;
                                    break;
                                }
                            }
                        }
                        eVentCalendarExists = calendarFound;
                        if (calendarFound)
                        {
                            List calendar = hostWeb.Lists.GetByTitle("Contoso Events Calendar");

                            clientContext.Load(calendar);
                            clientContext.ExecuteQuery();
                            CamlQuery camlQuery = new CamlQuery();
                            camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Category' /><Value Type='Text'>" + EventID + "</Value></Eq></Where></Query></View>";
                            ListItemCollection eventItems = calendar.GetItems(camlQuery);
                            clientContext.Load(eventItems);
                            clientContext.ExecuteQuery();
                            ListItem editEvent = eventItems[0];


                            editEvent["Title"]     = EventTitle;
                            editEvent["Location"]  = EventCity;
                            editEvent["EventDate"] = EventStartDateTime.ToUniversalTime();
                            editEvent["EndDate"]   = EventEndDateTime.ToUniversalTime();
                            editEvent["Category"]  = saved;

                            editEvent.Update();
                            clientContext.ExecuteQuery();
                        }
                    }
                    return(RedirectToAction("Events", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }));
                }
            }
        }
 /*
  * The isCoordinator method verifies whether the current user is a coordinator for a specific event.
  */
 private bool isCoordinator(string EventID)
 {
     Models.EventData eventData = new Models.EventData();
     return(eventData.IsCoordinator(EventID, ViewBag.UserName));
 }