Ejemplo n.º 1
0
        /// <summary>
        /// Deserializes attributes XML into a product variant query
        /// </summary>
        /// <param name="query">Product variant query</param>
        /// <param name="productId">Product identifier</param>
        /// <param name="bundleItemId">Bundle item identifier</param>
        /// <param name="attributesXml">XML formatted attributes</param>
        public virtual void DeserializeQuery(ProductVariantQuery query, int productId, string attributesXml, int bundleItemId = 0)
        {
            Guard.NotNull(query, nameof(query));
            Guard.NotZero(productId, nameof(productId));

            if (attributesXml.IsEmpty() || productId == 0)
            {
                return;
            }

            var attributeMap = _productAttributeParser.DeserializeProductVariantAttributes(attributesXml);
            var attributes   = _productAttributeService.GetProductVariantAttributesByIds(attributeMap.Keys);

            foreach (var attribute in attributes)
            {
                foreach (var originalValue in attributeMap[attribute.Id])
                {
                    var      value = originalValue;
                    DateTime?date  = null;

                    if (attribute.AttributeControlType == AttributeControlType.Datepicker)
                    {
                        date = originalValue.ToDateTime(new string[] { "D" }, CultureInfo.CurrentCulture, DateTimeStyles.None, null);
                        if (date == null)
                        {
                            continue;
                        }

                        value = string.Join("-", date.Value.Year, date.Value.Month, date.Value.Day);
                    }

                    var queryItem = new ProductVariantQueryItem(value);
                    queryItem.ProductId          = productId;
                    queryItem.BundleItemId       = bundleItemId;
                    queryItem.AttributeId        = attribute.ProductAttributeId;
                    queryItem.VariantAttributeId = attribute.Id;
                    queryItem.Alias  = _catalogSearchQueryAliasMapper.Value.GetVariantAliasById(attribute.ProductAttributeId, _languageId);
                    queryItem.Date   = date;
                    queryItem.IsFile = attribute.AttributeControlType == AttributeControlType.FileUpload;
                    queryItem.IsText = attribute.AttributeControlType == AttributeControlType.TextBox || attribute.AttributeControlType == AttributeControlType.MultilineTextbox;

                    if (attribute.ShouldHaveValues())
                    {
                        queryItem.ValueAlias = _catalogSearchQueryAliasMapper.Value.GetVariantOptionAliasById(value.ToInt(), _languageId);
                    }

                    query.AddVariant(queryItem);
                }
            }
        }
        public static string CreateSelectedAttributesXml(
            this ProductVariantQuery query,
            int productId,
            int bundleItemId,
            IEnumerable <ProductVariantAttribute> variantAttributes,
            IProductAttributeParser productAttributeParser,
            ILocalizationService localization,
            IDownloadService downloadService,
            CatalogSettings catalogSettings,
            HttpRequestBase request,
            List <string> warnings)
        {
            var result = string.Empty;

            foreach (var pva in variantAttributes)
            {
                var selectedItems = query.Variants.Where(x =>
                                                         x.ProductId == productId &&
                                                         x.BundleItemId == bundleItemId &&
                                                         x.AttributeId == pva.ProductAttributeId &&
                                                         x.VariantAttributeId == pva.Id);

                switch (pva.AttributeControlType)
                {
                case AttributeControlType.DropdownList:
                case AttributeControlType.RadioList:
                case AttributeControlType.Boxes:
                {
                    var firstItemValue = selectedItems.FirstOrDefault()?.Value;
                    if (firstItemValue.HasValue())
                    {
                        var selectedAttributeId = firstItemValue.SplitSafe(",").SafeGet(0).ToInt();
                        if (selectedAttributeId > 0)
                        {
                            result = productAttributeParser.AddProductAttribute(result, pva, selectedAttributeId.ToString());
                        }
                    }
                }
                break;

                case AttributeControlType.Checkboxes:
                    foreach (var item in selectedItems)
                    {
                        var selectedAttributeId = item.Value.SplitSafe(",").SafeGet(0).ToInt();
                        if (selectedAttributeId > 0)
                        {
                            result = productAttributeParser.AddProductAttribute(result, pva, selectedAttributeId.ToString());
                        }
                    }
                    break;

                case AttributeControlType.TextBox:
                case AttributeControlType.MultilineTextbox:
                {
                    var selectedValue = string.Join(",", selectedItems.Select(x => x.Value));
                    if (selectedValue.HasValue())
                    {
                        result = productAttributeParser.AddProductAttribute(result, pva, selectedValue);
                    }
                }
                break;

                case AttributeControlType.Datepicker:
                {
                    var firstItemDate = selectedItems.FirstOrDefault()?.Date;
                    if (firstItemDate.HasValue)
                    {
                        result = productAttributeParser.AddProductAttribute(result, pva, firstItemDate.Value.ToString("D"));
                    }
                }
                break;

                case AttributeControlType.FileUpload:
                    if (request == null)
                    {
                        var firstItemValue = selectedItems.FirstOrDefault()?.Value;
                        Guid.TryParse(firstItemValue, out var downloadGuid);

                        var download = downloadService.GetDownloadByGuid(downloadGuid);
                        if (download != null)
                        {
                            if (download.IsTransient)
                            {
                                download.IsTransient = false;
                                downloadService.UpdateDownload(download);
                            }

                            result = productAttributeParser.AddProductAttribute(result, pva, download.DownloadGuid.ToString());
                        }
                    }
                    else
                    {
                        var postedFile = request.Files[ProductVariantQueryItem.CreateKey(productId, bundleItemId, pva.ProductAttributeId, pva.Id)];
                        if (postedFile != null && postedFile.FileName.HasValue())
                        {
                            if (postedFile.ContentLength > catalogSettings.FileUploadMaximumSizeBytes)
                            {
                                warnings.Add(localization.GetResource("ShoppingCart.MaximumUploadedFileSize").FormatInvariant((int)(catalogSettings.FileUploadMaximumSizeBytes / 1024)));
                            }
                            else
                            {
                                var download = new Download
                                {
                                    DownloadGuid   = Guid.NewGuid(),
                                    UseDownloadUrl = false,
                                    DownloadUrl    = "",
                                    UpdatedOnUtc   = DateTime.UtcNow,
                                    EntityId       = productId,
                                    EntityName     = "ProductAttribute"
                                };

                                downloadService.InsertDownload(download, postedFile.InputStream, postedFile.FileName);

                                result = productAttributeParser.AddProductAttribute(result, pva, download.DownloadGuid.ToString());
                            }
                        }
                    }
                    break;
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
 public string GetControlId(int productId, int bundleItemId)
 {
     return(ProductVariantQueryItem.CreateKey(productId, bundleItemId, ProductAttributeId, Id));
 }
Ejemplo n.º 4
0
 public override string BuildControlId()
 {
     return(ProductVariantQueryItem.CreateKey(ProductId, BundleItemId, ProductAttributeId, Id));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds selected product variant attributes to a product variant query.
        /// </summary>
        /// <param name="query">Target product variant query.</param>
        /// <param name="source">Selected attributes.</param>
        /// <param name="productId">Product identifier.</param>
        /// <param name="bundleItemId">Bundle item identifier.</param>
        /// <param name="attributes">Product variant attributes.</param>
        public virtual async Task AddAttributesToQueryAsync(
            ProductVariantQuery query,
            ProductVariantAttributeSelection source,
            int productId,
            int bundleItemId = 0,
            ICollection <ProductVariantAttribute> attributes = null)
        {
            Guard.NotNull(query, nameof(query));

            if (productId == 0 || !(source?.AttributesMap?.Any() ?? false))
            {
                return;
            }

            if (attributes == null)
            {
                var ids = source.AttributesMap.Select(x => x.Key);
                attributes = await _db.ProductVariantAttributes.GetManyAsync(ids);
            }

            var languageId = _workContext.WorkingLanguage.Id;

            foreach (var attribute in attributes)
            {
                var item = source.AttributesMap.FirstOrDefault(x => x.Key == attribute.Id);
                if (item.Key != 0)
                {
                    foreach (var originalValue in item.Value)
                    {
                        var      value = originalValue.ToString();
                        DateTime?date  = null;

                        if (attribute.AttributeControlType == AttributeControlType.Datepicker)
                        {
                            date = value.ToDateTime(new[] { "D" }, CultureInfo.CurrentCulture, DateTimeStyles.None, null);
                            if (date == null)
                            {
                                continue;
                            }

                            value = string.Join("-", date.Value.Year, date.Value.Month, date.Value.Day);
                        }

                        var queryItem = new ProductVariantQueryItem(value)
                        {
                            ProductId          = productId,
                            BundleItemId       = bundleItemId,
                            AttributeId        = attribute.ProductAttributeId,
                            VariantAttributeId = attribute.Id,
                            Alias  = _catalogSearchQueryAliasMapper.Value.GetVariantAliasById(attribute.ProductAttributeId, languageId),
                            Date   = date,
                            IsFile = attribute.AttributeControlType == AttributeControlType.FileUpload,
                            IsText = attribute.AttributeControlType == AttributeControlType.TextBox || attribute.AttributeControlType == AttributeControlType.MultilineTextbox
                        };

                        if (attribute.IsListTypeAttribute())
                        {
                            queryItem.ValueAlias = _catalogSearchQueryAliasMapper.Value.GetVariantOptionAliasById(value.ToInt(), languageId);
                        }

                        query.AddVariant(queryItem);
                    }
                }
            }
        }