コード例 #1
0
        /// <summary>
        /// Extract a specific territory from the configured hierarchy
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private TerritoryPart GetTerritory(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }
            var id = 0;

            if (int.TryParse(value, out id))
            {
                return(_addressConfigurationService.SingleTerritory(id));
            }
            return(_addressConfigurationService.SingleTerritory(value));
        }
コード例 #2
0
        public static void ReinflateViewModelAddresses(
            CheckoutViewModel vm, IContentManager contentManager, IAddressConfigurationService addressConfigurationService)
        {
            // addresses
            if ((vm.ShippingAddressVM == null || vm.BillingAddressVM == null) &&
                !string.IsNullOrWhiteSpace(vm.SerializedAddresses))
            {
                vm.DecodeAddresses();
            }
            Func <string, int, string> inflateName = (str, id) => {
                if (string.IsNullOrWhiteSpace(str))
                {
                    var territory = addressConfigurationService
                                    .SingleTerritory(id);
                    if (territory != null)
                    {
                        return(contentManager
                               .GetItemMetadata(territory).DisplayText);
                    }
                }
                return(str);
            };

            if (vm.ShippingAddressVM != null)
            {
                if (vm.ShippingAddress == null)
                {
                    vm.ShippingAddress = AddressFromVM(vm.ShippingAddressVM);
                }
                // reinflate the names of country, province and city
                vm.ShippingAddressVM.Country = inflateName(
                    vm.ShippingAddressVM.Country, vm.ShippingAddressVM.CountryId);
                vm.ShippingAddressVM.Province = inflateName(
                    vm.ShippingAddressVM.Province, vm.ShippingAddressVM.ProvinceId);
                vm.ShippingAddressVM.City = inflateName(
                    vm.ShippingAddressVM.City, vm.ShippingAddressVM.CityId);
            }
            if (vm.BillingAddressVM != null)
            {
                if (vm.BillingAddress == null)
                {
                    vm.BillingAddress = AddressFromVM(vm.BillingAddressVM);
                }
                // reinflate the names of country, province and city
                vm.BillingAddressVM.Country = inflateName(
                    vm.BillingAddressVM.Country, vm.BillingAddressVM.CountryId);
                vm.BillingAddressVM.Province = inflateName(
                    vm.BillingAddressVM.Province, vm.BillingAddressVM.ProvinceId);
                vm.BillingAddressVM.City = inflateName(
                    vm.BillingAddressVM.City, vm.BillingAddressVM.CityId);
            }
        }
コード例 #3
0
        public JsonResult GetChildren(int territoryId = 0)
        {
            var parent = _addressConfigurationService
                         .SingleTerritory(territoryId);

            if (parent == null)
            {
                // this is an error
                return(null);
            }
            else
            {
                return(Json(new {
                    Success = true,
                    Territories = parent.Children
                                  .Select(ci => {
                        var tp = ci.As <TerritoryPart>();
                        var id = tp.Record.TerritoryInternalRecord.Id;
                        var adminTypePart = tp.As <TerritoryAdministrativeTypePart>();
                        var adminType = TerritoryAdministrativeType.None;
                        if (adminTypePart != null)
                        {
                            adminType = adminTypePart.AdministrativeType;
                        }
                        var isCountry = adminType == TerritoryAdministrativeType.Country;
                        var isProvince = adminType == TerritoryAdministrativeType.Province;
                        var isCity = adminType == TerritoryAdministrativeType.City;
                        var isNone = adminType == TerritoryAdministrativeType.None;
                        var recordsChildrenCount = _territoryPartRecordService.GetTerritoriesChildCount(tp);
                        return new {
                            Id = id,
                            DisplayText = _contentManager
                                          .GetItemMetadata(ci).DisplayText
                                          + " " + T("(Administrative type: {0})", administrativeTypeNames[adminType]),
                            IsCountry = isCountry,
                            IsProvince = isProvince,
                            IsCity = isCity,
                            IsNone = isNone,
                            HasChildren = recordsChildrenCount > 0 ? true : false,
                            ChildrenCount = recordsChildrenCount
                        };
                    })
                }));
            }
        }
コード例 #4
0
        /// <summary>
        /// validation of the vm coming from a create/edit action
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        /// <remarks>
        /// It would be cleaner to do this in its own validation classes,
        /// but we need a bunch of IDependencies, so putting this code
        /// here is less of an hassle.
        /// </remarks>
        public bool Validate(AddressEditViewModel vm)
        {
            var validCountries = _addressConfigurationService
                                 .GetAllCountries(vm.AddressType);
            var countryTP = _addressConfigurationService
                            .GetCountry(vm.CountryId);

            if (!SubValidation(validCountries, countryTP))
            {
                return(false);
            }

            var provinceTP = GetTerritory(vm.Province)
                             ?? _addressConfigurationService.SingleTerritory(vm.ProvinceId);

            if (provinceTP == null)
            {
                // maybe we did not find a territory because it's not configured,
                // but we had a free text input for the province
                var provinceName = vm.Province.Trim();
                if (provinceName.Length < 2)
                {
                    // at least two characters
                    return(false);
                }
            }
            else
            {
                // check in the configuration parts if they are a valid province or not
                var adminTypePart = provinceTP.As <TerritoryAdministrativeTypePart>();
                if (adminTypePart != null)
                {
                    if (adminTypePart.AdministrativeType == TerritoryAdministrativeType.Province)
                    {
                        var territoryAddressTypePart = provinceTP.As <TerritoryAddressTypePart>();
                        if (territoryAddressTypePart != null)
                        {
                            if (vm.AddressType == AddressRecordType.ShippingAddress)
                            {
                                if (!territoryAddressTypePart.Shipping)
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                if (!territoryAddressTypePart.Billing)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            var cityTP = GetTerritory(vm.City)
                         ?? _addressConfigurationService.SingleTerritory(vm.CityId);

            // check in the configuration parts if they are a valid city or not
            if (cityTP != null)
            {
                var adminTypePart = cityTP.As <TerritoryAdministrativeTypePart>();
                if (adminTypePart != null)
                {
                    if (adminTypePart.AdministrativeType == TerritoryAdministrativeType.City)
                    {
                        var territoryAddressTypePart = cityTP.As <TerritoryAddressTypePart>();
                        if (territoryAddressTypePart != null)
                        {
                            if (vm.AddressType == AddressRecordType.ShippingAddress)
                            {
                                if (!territoryAddressTypePart.Shipping)
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                if (!territoryAddressTypePart.Billing)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            // https://en.wikipedia.org/wiki/List_of_postal_codes
            // we had to make the change because we first checked
            // it was just a number
            // during use we noticed that the Netherlands have PS in the cap
            // therefore changed the control over the character
            if (string.IsNullOrWhiteSpace(vm.PostalCode))
            {
                return(false);
            }
            else
            {
                foreach (char c in vm.PostalCode)
                {
                    if (!char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c))
                    {
                        vm.Errors.Add(T("Postal or ZIP code may contain only characters or digits.").Text);
                        return(false);
                    }
                }
            }
            return(true);
        }