Exemple #1
0
        public async Task <IActionResult> AddBranch(BranchesListViewModel model)
        {
            if (model != null)
            {
                try
                {
                    model.Branch.Geolocation = null;
                    var branch = await _siteService.AddBranchAsync(model.Branch);

                    ShowAlertSuccess($"Added Branch '{branch.Name}'");

                    if (await _siteLookupService.IsSiteSettingSetAsync(GetCurrentSiteId(),
                                                                       SiteSettingKey.Events.GoogleMapsAPIKey))
                    {
                        var result = await _spatialService
                                     .GetGeocodedAddressAsync(branch.Address);

                        if (result.Status == ServiceResultStatus.Success)
                        {
                            branch.Geolocation = result.Data;
                            await _siteService.UpdateBranchAsync(branch);
                        }
                        else if (result.Status == ServiceResultStatus.Warning)
                        {
                            ShowAlertWarning("Unable to set branch geolocation: ", result.Message);
                        }
                        else
                        {
                            ShowAlertDanger("Unable to set branch geolocation: ", result.Message);
                        }
                    }
                }
                catch (GraException gex)
                {
                    ShowAlertDanger("Unable to add Branch: ", gex);
                }
            }
            return(RedirectToAction("Branches", new { search = model?.Search }));
        }
        public async Task <IActionResult> Index(int page                  = 1,
                                                string sort               = null,
                                                string search             = null,
                                                string near               = null,
                                                int?system                = null,
                                                int?branch                = null,
                                                int?location              = null,
                                                int?program               = null,
                                                string StartDate          = null,
                                                string EndDate            = null,
                                                bool Favorites            = false,
                                                string Visited            = null,
                                                EventType eventType       = EventType.Event,
                                                HttpStatusCode httpStatus = HttpStatusCode.OK)
        {
            var site = await GetCurrentSiteAsync();

            if (!string.IsNullOrEmpty(site.ExternalEventListUrl))
            {
                return(new RedirectResult(site.ExternalEventListUrl));
            }

            ModelState.Clear();
            var filter = new EventFilter(page)
            {
                Search    = search,
                EventType = (int)eventType
            };

            var nearSearchEnabled = await _siteLookupService
                                    .IsSiteSettingSetAsync(site.Id, SiteSettingKey.Events.GoogleMapsAPIKey);

            if (!string.IsNullOrWhiteSpace(sort) && Enum.IsDefined(typeof(SortEventsBy), sort))
            {
                filter.SortBy = (SortEventsBy)Enum.Parse(typeof(SortEventsBy), sort);
            }
            else
            {
                if (nearSearchEnabled && !string.IsNullOrWhiteSpace(near))
                {
                    filter.SortBy = SortEventsBy.Distance;
                }
            }
            if (AuthUser.Identity.IsAuthenticated)
            {
                filter.Favorites = Favorites;
                if (string.IsNullOrWhiteSpace(Visited) ||
                    string.Equals(Visited, VisitedNo, StringComparison.OrdinalIgnoreCase))
                {
                    filter.IsAttended = false;
                }
                else if (string.Equals(Visited, VisitedYes, StringComparison.OrdinalIgnoreCase))
                {
                    filter.IsAttended = true;
                }
            }
            if (nearSearchEnabled)
            {
                if (!string.IsNullOrWhiteSpace(near))
                {
                    var geocodeResult = await _spatialService.GetGeocodedAddressAsync(near);

                    if (geocodeResult.Status == ServiceResultStatus.Success)
                    {
                        filter.SpatialDistanceHeaderId = await _spatialService
                                                         .GetSpatialDistanceIdForGeolocationAsync(geocodeResult.Data);
                    }
                    else
                    {
                        ShowAlertWarning("Not able to find that location.");
                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            else
            {
                // ignore location if branch has value
                if (branch.HasValue)
                {
                    filter.BranchIds = new List <int>()
                    {
                        branch.Value
                    };
                }
                else if (system.HasValue)
                {
                    filter.SystemIds = new List <int>()
                    {
                        system.Value
                    };
                }
                else if (location.HasValue)
                {
                    filter.LocationIds = new List <int?>()
                    {
                        location.Value
                    };
                }
            }

            if (program.HasValue)
            {
                filter.ProgramIds = new List <int?>()
                {
                    program.Value
                };
            }

            if (!string.Equals(StartDate, "False", StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(StartDate))
                {
                    if (DateTime.TryParse(StartDate, out var startDate))
                    {
                        filter.StartDate = startDate.Date;
                    }
                }
                else
                {
                    filter.StartDate = _dateTimeProvider.Now.Date;
                }
            }

            if (!string.IsNullOrWhiteSpace(EndDate) && DateTime.TryParse(EndDate, out var endDate))
            {
                filter.EndDate = endDate.Date;
            }
            var eventList = await _eventService.GetPaginatedListAsync(filter);

            var paginateModel = new PaginateViewModel
            {
                ItemCount    = eventList.Count,
                CurrentPage  = page,
                ItemsPerPage = filter.Take.Value
            };

            if (paginateModel.PastMaxPage)
            {
                return(RedirectToRoute(
                           new
                {
                    page = paginateModel.LastPage ?? 1
                }));
            }

            var viewModel = new EventsListViewModel
            {
                IsAuthenticated = AuthUser.Identity.IsAuthenticated,
                Events          = eventList.Data.ToList(),
                PaginateModel   = paginateModel,
                Sort            = filter.SortBy.ToString(),
                Search          = search,
                StartDate       = filter.StartDate,
                EndDate         = filter.EndDate,
                Favorites       = Favorites,
                IsLoggedIn      = AuthUser.Identity.IsAuthenticated,
                ProgramId       = program,
                ProgramList     = new SelectList(await _siteService.GetProgramList(), "Id", "Name"),
                EventType       = eventType,
                ShowNearSearch  = nearSearchEnabled,
            };

            if (nearSearchEnabled)
            {
                viewModel.Near = near?.Trim();

                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    var user = await _userService.GetDetails(GetActiveUserId());

                    if (!string.IsNullOrWhiteSpace(user.PostalCode))
                    {
                        viewModel.UserZipCode = user.PostalCode;
                    }
                }
            }
            else
            {
                viewModel.SystemList = new SelectList(
                    await _siteService.GetSystemList(), "Id", "Name");
                viewModel.LocationList = new SelectList(
                    await _eventService.GetLocations(), "Id", "Name");

                if (branch.HasValue)
                {
                    var selectedBranch = await _siteService.GetBranchByIdAsync(branch.Value);

                    viewModel.SystemId   = selectedBranch.SystemId;
                    viewModel.BranchList = new SelectList(
                        await _siteService.GetBranches(selectedBranch.SystemId),
                        "Id", "Name", branch.Value);
                }
                else if (system.HasValue)
                {
                    viewModel.SystemId   = system;
                    viewModel.BranchList = new SelectList(
                        await _siteService.GetBranches(system.Value), "Id", "Name");
                }
                else
                {
                    viewModel.BranchList = new SelectList(await _siteService.GetAllBranches(),
                                                          "Id", "Name");
                }

                if (location.HasValue && !branch.HasValue)
                {
                    viewModel.LocationId  = location.Value;
                    viewModel.UseLocation = true;
                }
            }

            var(descriptionTextSet, communityExperienceDescription) = await _siteLookupService
                                                                      .GetSiteSettingStringAsync(site.Id,
                                                                                                 SiteSettingKey.Events.CommunityExperienceDescription);

            if (descriptionTextSet)
            {
                viewModel.CommunityExperienceDescription = communityExperienceDescription;
            }

            if (eventType == EventType.StreamingEvent)
            {
                viewModel.Viewed = Visited;
            }
            else
            {
                viewModel.Visited = Visited;
            }

            if (httpStatus != HttpStatusCode.OK)
            {
                Response.StatusCode = (int)httpStatus;
            }

            return(View(nameof(Index), viewModel));
        }