Exemplo n.º 1
0
        public ActionResult Edit(int?locationId, int?organizationId, NewLocationModel locationModel)
        {
            var location = GetLocation(locationId, organizationId);

            if (ModelState.IsValid)
            {
                // Google limits number of lookups per day. No reason to waste them.
                bool needGeocode = location.IsNew ||
                                   locationModel.AddressLine1 != location.AddressLine1 ||
                                   (locationModel.AddressLine2 ?? "") != location.AddressLine2 ||
                                   locationModel.City != location.City ||
                                   locationModel.State != location.State ||
                                   locationModel.Country != location.Country;

                // Admin can edit geocode if the lookup fails.
                if (!needGeocode && RoleUtils.IsUserServiceAdmin())
                {
                    // If they aren't set by admin, then lookup, ... or retry.
                    if ((!locationModel.Latitude.HasValue || locationModel.Latitude == 0) &&
                        (!locationModel.Longitude.HasValue || locationModel.Longitude == 0))
                    {
                        needGeocode = true;
                    }
                    else
                    {
                        // Admin set them manually. So use the edited values, ... or
                        // the same unedited values as before.
                        location.Latitude  = locationModel.Latitude;
                        location.Longitude = locationModel.Longitude;
                    }
                }

                // this is already set at this point by GetLocation() location.OrganizationId = locationModel.OrganizationId;
                location.Name         = locationModel.Name;
                location.AddressLine1 = locationModel.AddressLine1;
                location.AddressLine2 = locationModel.AddressLine2;
                location.City         = locationModel.City;
                location.State        = locationModel.State;
                location.Country      = locationModel.Country;
                location.PostalCode   = locationModel.PostalCode;
                location.PhoneNumber  = locationModel.PhoneNumber;

                if (needGeocode)
                {
                    if (GoogleMaps.GetGeocode(location) == null && RoleUtils.IsUserServiceAdmin())
                    {
                        location.Latitude  = locationModel.Latitude;
                        location.Longitude = locationModel.Longitude;
                    }
                }

                if (RoleUtils.IsUserServiceAdmin())
                {
                    location.IsActive = locationModel.IsActive;
                }

                if (location.IsNew)
                {
                    location.UniqueIdentifier = LocationUtils.CreateUid();
                }

                location.Save();
                return(new EmptyResult());
            }
            else
            {
                Response.StatusCode             = 417;
                Response.TrySkipIisCustomErrors = true;
            }

            return(PartialView(locationModel));
        }