/// <summary>
        /// Gets a URL for displaying a map on the website of the given address
        /// </summary>
        /// <param name="address">The address to show a map of</param>
        /// <param name="displayName">The display name of the page to link from</param>
        /// <param name="returnUrl">The URL to return to, if not the current page</param>
        /// <returns>URI of a page which will display the map, or null if the map is not available</returns>
        /// <remarks>returnUrl property is useful for CMS, where the URL of the current page is translated on-the-fly</remarks>
        private static string GetWebsiteMapUrl(BS7666Address address, string displayName, Uri returnUrl)
        {
            if (address == null || address.Postcode == null || address.Postcode.Length == 0)
            {
                return(null);
            }
            if (String.IsNullOrEmpty(address.AdministrativeArea) || String.Compare(address.AdministrativeArea.ToUpperInvariant(), "EAST SUSSEX", true, CultureInfo.CurrentCulture) != 0)
            {
                return(null);
            }
            if (address.Town != null && address.Town.Length > 0 && address.Town.ToUpperInvariant().IndexOf("BRIGHTON", StringComparison.Ordinal) > -1)
            {
                return(null);
            }

            // if no dots, assume it's an internal dev box and re-use the current host
            // otherwise use the live site
            HttpContext ctx  = HttpContext.Current;
            string      host = (ctx != null && ctx.Request.Url.Host.IndexOf(".", StringComparison.Ordinal) == -1) ? ctx.Request.Url.Host : "www.eastsussex.gov.uk";

            // Build up URL
            StringBuilder url = new StringBuilder(Uri.UriSchemeHttp).Append("://").Append(host).Append("/contactus/map.aspx");

            url.Append("?postcode=").Append(Uri.EscapeDataString(address.Postcode));
            if (address.GeoCoordinate != null)
            {
                url.Append("&e=").Append(Uri.EscapeDataString(address.GeoCoordinate.Easting.ToString(CultureInfo.CurrentCulture)));
                url.Append("&n=").Append(Uri.EscapeDataString(address.GeoCoordinate.Northing.ToString(CultureInfo.CurrentCulture)));
            }

            if (address.Paon != null && address.Paon.Length > 0)
            {
                url.Append("&paon=").Append(Uri.EscapeDataString(address.Paon));
            }

            url.Append("&mapOf=").Append(Uri.EscapeDataString(HttpUtility.HtmlDecode(address.GetSimpleAddress().ToString())));

            if (returnUrl != null)
            {
                url.Append("&backToUrl=").Append(Uri.EscapeDataString(returnUrl.ToString()));
            }

            if (displayName != null && displayName.Length > 0)
            {
                url.Append("&backTo=").Append(Uri.EscapeDataString(displayName));
            }

            return(url.ToString());
        }
コード例 #2
0
        public override IEnumerable <object> ProcessSubmittedValue(Field field, IEnumerable <object> postedValues, HttpContextBase context)
        {
            // We don't want the posted value, which is only used to pass validation.
            // We want the values from all the fields that make up the selected address.
            if (context.Request.HttpMethod.ToUpperInvariant() == "POST")
            {
                var address = new BS7666Address()
                {
                    Uprn               = context.Request.Form[field.Id + ".Uprn"],
                    Usrn               = context.Request.Form[field.Id + ".Usrn"],
                    Paon               = context.Request.Form[field.Id + ".Paon"],
                    StreetName         = context.Request.Form[field.Id + ".StreetName"],
                    Locality           = context.Request.Form[field.Id + ".Locality"],
                    Town               = context.Request.Form[field.Id + ".Town"],
                    AdministrativeArea = context.Request.Form[field.Id + ".AdministrativeArea"],
                    Postcode           = context.Request.Form[field.Id + ".Postcode"]
                };

                double parsed;
                if (!String.IsNullOrEmpty(context.Request.Form[field.Id + ".GeoCoordinate.Latitude"]))
                {
                    if (double.TryParse(context.Request.Form[field.Id + ".GeoCoordinate.Latitude"], out parsed))
                    {
                        address.GeoCoordinate.Latitude = parsed;
                    }
                }
                if (!String.IsNullOrEmpty(context.Request.Form[field.Id + ".GeoCoordinate.Longitude"]))
                {
                    if (double.TryParse(context.Request.Form[field.Id + ".GeoCoordinate.Longitude"], out parsed))
                    {
                        address.GeoCoordinate.Longitude = parsed;
                    }
                }

                // Store two values for the field: a human-readable version and a JSON object including metadata
                return(new object[] { address.GetSimpleAddress().ToString(), JsonConvert.SerializeObject(address) });
            }

            // There is no value when the control is initially loaded on a GET request
            return(new object[0]);
        }