//edit
        public async Task <IActionResult> Edit(string id)
        {
            var contactAttribute = await _contactAttributeService.GetContactAttributeById(id);

            if (contactAttribute == null)
            {
                //No contact attribute found with the specified id
                return(RedirectToAction("List"));
            }

            var model = contactAttribute.ToModel();

            //locales
            await AddLocales(_languageService, model.Locales, (locale, languageId) =>
            {
                locale.Name       = contactAttribute.GetLocalized(x => x.Name, languageId, false, false);
                locale.TextPrompt = contactAttribute.GetLocalized(x => x.TextPrompt, languageId, false, false);
            });

            //ACL
            await model.PrepareACLModel(contactAttribute, false, _customerService);

            //Stores
            await model.PrepareStoresMappingModel(contactAttribute, _storeService, false);

            //condition
            await _contactAttributeViewModelService.PrepareConditionAttributes(model, contactAttribute);

            return(View(model));
        }
        /// <summary>
        /// Gets selected contact attributes
        /// </summary>
        /// <param name="customAttributes">Attributes</param>
        /// <returns>Selected contact attributes</returns>
        public virtual async Task <IList <ContactAttribute> > ParseContactAttributes(IList <CustomAttribute> customAttributes)
        {
            var result = new List <ContactAttribute>();

            if (customAttributes == null || !customAttributes.Any())
            {
                return(result);
            }

            foreach (var customAttribute in customAttributes.GroupBy(x => x.Key))
            {
                var attribute = await _contactAttributeService.GetContactAttributeById(customAttribute.Key);

                if (attribute != null)
                {
                    result.Add(attribute);
                }
            }
            return(result);
        }
        /// <summary>
        /// Gets selected contact attributes
        /// </summary>
        /// <param name="attributesXml">Attributes in XML format</param>
        /// <returns>Selected contact attributes</returns>
        public virtual IList <ContactAttribute> ParseContactAttributes(string attributesXml)
        {
            var result = new List <ContactAttribute>();

            if (String.IsNullOrEmpty(attributesXml))
            {
                return(result);
            }

            var ids = ParseContactAttributeIds(attributesXml);

            foreach (string id in ids)
            {
                var attribute = _contactAttributeService.GetContactAttributeById(id);
                if (attribute != null)
                {
                    result.Add(attribute);
                }
            }
            return(result);
        }
Exemple #4
0
        public virtual async Task <IActionResult> UploadFileContactAttribute(string attributeId, [FromServices] IDownloadService downloadService,
                                                                             [FromServices] IContactAttributeService contactAttributeService)
        {
            var attribute = await contactAttributeService.GetContactAttributeById(attributeId);

            if (attribute == null || attribute.AttributeControlType != AttributeControlType.FileUpload)
            {
                return(Json(new
                {
                    success = false,
                    downloadGuid = Guid.Empty,
                }));
            }
            var form = await HttpContext.Request.ReadFormAsync();

            var httpPostedFile = form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty,
                }));
            }

            var fileBinary = httpPostedFile.GetDownloadBits();

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (String.IsNullOrEmpty(fileName) && form.ContainsKey(qqFileNameParameter))
            {
                fileName = form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = Path.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            if (attribute.ValidationFileMaximumSize.HasValue)
            {
                //compare in bytes
                var maxFileSizeBytes = attribute.ValidationFileMaximumSize.Value * 1024;
                if (fileBinary.Length > maxFileSizeBytes)
                {
                    //when returning JSON the mime-type must be set to text/plain
                    //otherwise some browsers will pop-up a "Save As" dialog.
                    return(Json(new
                    {
                        success = false,
                        message = string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), attribute.ValidationFileMaximumSize.Value),
                        downloadGuid = Guid.Empty,
                    }));
                }
            }

            var download = new Download {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = "",
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = Path.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            await downloadService.InsertDownload(download);

            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                message = _localizationService.GetResource("ShoppingCart.FileUploaded"),
                downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
                downloadGuid = download.DownloadGuid,
            }));
        }
Exemple #5
0
        protected virtual async Task SaveConditionAttributes(ContactAttribute contactAttribute, ContactAttributeModel model)
        {
            string attributesXml = null;

            if (model.ConditionModel.EnableCondition)
            {
                var attribute = await _contactAttributeService.GetContactAttributeById(model.ConditionModel.SelectedAttributeId);

                if (attribute != null)
                {
                    switch (attribute.AttributeControlType)
                    {
                    case AttributeControlType.DropdownList:
                    case AttributeControlType.RadioList:
                    case AttributeControlType.ColorSquares:
                    case AttributeControlType.ImageSquares:
                    {
                        var selectedAttribute = model.ConditionModel.ConditionAttributes
                                                .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId);
                        var selectedValue = selectedAttribute != null ? selectedAttribute.SelectedValueId : null;
                        if (!String.IsNullOrEmpty(selectedValue))
                        {
                            attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, attribute, selectedValue);
                        }
                        else
                        {
                            attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, attribute, string.Empty);
                        }
                    }
                    break;

                    case AttributeControlType.Checkboxes:
                    {
                        var selectedAttribute = model.ConditionModel.ConditionAttributes
                                                .FirstOrDefault(x => x.Id == model.ConditionModel.SelectedAttributeId);
                        var selectedValues = selectedAttribute != null?selectedAttribute.Values.Where(x => x.Selected).Select(x => x.Value) : null;

                        if (selectedValues.Any())
                        {
                            foreach (var value in selectedValues)
                            {
                                attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, attribute, value);
                            }
                        }
                        else
                        {
                            attributesXml = _contactAttributeParser.AddContactAttribute(attributesXml, attribute, string.Empty);
                        }
                    }
                    break;

                    case AttributeControlType.ReadonlyCheckboxes:
                    case AttributeControlType.TextBox:
                    case AttributeControlType.MultilineTextbox:
                    case AttributeControlType.Datepicker:
                    case AttributeControlType.FileUpload:
                    default:
                        //these attribute types are not supported as conditions
                        break;
                    }
                }
            }
            contactAttribute.ConditionAttributeXml = attributesXml;
        }