public virtual IActionResult Create(VendorAttributeModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
            {
                return(AccessDeniedView());
            }

            if (ModelState.IsValid)
            {
                var vendorAttribute = model.ToEntity <VendorAttribute>();
                _vendorAttributeService.InsertVendorAttribute(vendorAttribute);

                //activity log
                _customerActivityService.InsertActivity("AddNewVendorAttribute",
                                                        string.Format(_localizationService.GetResource("ActivityLog.AddNewVendorAttribute"), vendorAttribute.Id), vendorAttribute);

                //locales
                UpdateAttributeLocales(vendorAttribute, model);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Vendors.VendorAttributes.Added"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = vendorAttribute.Id }));
            }

            //prepare model
            model = _vendorAttributeModelFactory.PrepareVendorAttributeModel(model, null, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        /// <summary>
        /// Prepare vendor attribute model
        /// </summary>
        /// <param name="model">Vendor attribute model</param>
        /// <param name="vendorAttribute">Vendor attribute</param>
        /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param>
        /// <returns>Vendor attribute model</returns>
        public virtual VendorAttributeModel PrepareVendorAttributeModel(VendorAttributeModel model,
                                                                        VendorAttribute vendorAttribute, bool excludeProperties = false)
        {
            Action <VendorAttributeLocalizedModel, int> localizedModelConfiguration = null;

            if (vendorAttribute != null)
            {
                //fill in model values from the entity
                model = model ?? vendorAttribute.ToModel <VendorAttributeModel>();

                //prepare nested search model
                PrepareVendorAttributeValueSearchModel(model.VendorAttributeValueSearchModel, vendorAttribute);

                //define localized model configuration action
                localizedModelConfiguration = (locale, languageId) =>
                {
                    locale.Name = _localizationService.GetLocalized(vendorAttribute, entity => entity.Name, languageId, false, false);
                };
            }

            //prepare localized models
            if (!excludeProperties)
            {
                model.Locales = _localizedModelFactory.PrepareLocalizedModels(localizedModelConfiguration);
            }

            return(model);
        }
Пример #3
0
        //create
        public virtual IActionResult Create()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
            {
                return(AccessDeniedView());
            }

            var model = new VendorAttributeModel();

            //locales
            AddLocales(_languageService, model.Locales);
            return(View(model));
        }
Пример #4
0
        public virtual IActionResult Edit(VendorAttributeModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
            {
                return(AccessDeniedView());
            }

            //try to get a vendor attribute with the specified id
            var vendorAttribute = _vendorAttributeService.GetVendorAttributeById(model.Id);

            if (vendorAttribute == null)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                vendorAttribute = model.ToEntity(vendorAttribute);
                _vendorAttributeService.UpdateVendorAttribute(vendorAttribute);

                //activity log
                _customerActivityService.InsertActivity("EditVendorAttribute",
                                                        string.Format(_localizationService.GetResource("ActivityLog.EditVendorAttribute"), vendorAttribute.Id), vendorAttribute);

                //locales
                UpdateAttributeLocales(vendorAttribute, model);

                SuccessNotification(_localizationService.GetResource("Admin.Vendors.VendorAttributes.Updated"));
                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                //selected tab
                SaveSelectedTabName();

                return(RedirectToAction("Edit", new { id = vendorAttribute.Id }));
            }

            //prepare model
            model = _vendorAttributeModelFactory.PrepareVendorAttributeModel(model, vendorAttribute, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        /// <summary>
        /// Prepare vendor attribute models
        /// </summary>
        /// <param name="vendorAttributesXml">Vendor attributes in XML format</param>
        /// <returns>List of the vendor attribute model</returns>
        protected virtual IList <VendorAttributeModel> PrepareVendorAttributes(string vendorAttributesXml)
        {
            var result = new List <VendorAttributeModel>();

            var vendorAttributes = _vendorAttributeService.GetAllVendorAttributes();

            foreach (var attribute in vendorAttributes)
            {
                var attributeModel = new VendorAttributeModel
                {
                    Id                   = attribute.Id,
                    Name                 = _localizationService.GetLocalized(attribute, x => x.Name),
                    IsRequired           = attribute.IsRequired,
                    AttributeControlType = attribute.AttributeControlType,
                };

                if (attribute.ShouldHaveValues())
                {
                    //values
                    var attributeValues = _vendorAttributeService.GetVendorAttributeValues(attribute.Id);
                    foreach (var attributeValue in attributeValues)
                    {
                        var valueModel = new VendorAttributeValueModel
                        {
                            Id            = attributeValue.Id,
                            Name          = _localizationService.GetLocalized(attributeValue, x => x.Name),
                            IsPreSelected = attributeValue.IsPreSelected
                        };
                        attributeModel.Values.Add(valueModel);
                    }
                }

                switch (attribute.AttributeControlType)
                {
                case AttributeControlType.DropdownList:
                case AttributeControlType.RadioList:
                case AttributeControlType.Checkboxes:
                {
                    if (!string.IsNullOrEmpty(vendorAttributesXml))
                    {
                        //clear default selection
                        foreach (var item in attributeModel.Values)
                        {
                            item.IsPreSelected = false;
                        }

                        //select new values
                        var selectedValues = _vendorAttributeParser.ParseVendorAttributeValues(vendorAttributesXml);
                        foreach (var attributeValue in selectedValues)
                        {
                            foreach (var item in attributeModel.Values)
                            {
                                if (attributeValue.Id == item.Id)
                                {
                                    item.IsPreSelected = true;
                                }
                            }
                        }
                    }
                }
                break;

                case AttributeControlType.ReadonlyCheckboxes:
                {
                    //do nothing
                    //values are already pre-set
                }
                break;

                case AttributeControlType.TextBox:
                case AttributeControlType.MultilineTextbox:
                {
                    if (!string.IsNullOrEmpty(vendorAttributesXml))
                    {
                        var enteredText = _vendorAttributeParser.ParseValues(vendorAttributesXml, attribute.Id);
                        if (enteredText.Any())
                        {
                            attributeModel.DefaultValue = enteredText[0];
                        }
                    }
                }
                break;

                case AttributeControlType.ColorSquares:
                case AttributeControlType.ImageSquares:
                case AttributeControlType.Datepicker:
                case AttributeControlType.FileUpload:
                default:
                    //not supported attribute control types
                    break;
                }

                result.Add(attributeModel);
            }

            return(result);
        }
Пример #6
0
 protected virtual void UpdateAttributeLocales(VendorAttribute vendorAttribute, VendorAttributeModel model)
 {
     foreach (var localized in model.Locales)
     {
         _localizedEntityService.SaveLocalizedValue(vendorAttribute,
                                                    x => x.Name,
                                                    localized.Name,
                                                    localized.LanguageId);
     }
 }
 /// <returns>A task that represents the asynchronous operation</returns>
 protected virtual async Task UpdateAttributeLocalesAsync(VendorAttribute vendorAttribute, VendorAttributeModel model)
 {
     foreach (var localized in model.Locales)
     {
         await _localizedEntityService.SaveLocalizedValueAsync(vendorAttribute,
                                                               x => x.Name,
                                                               localized.Name,
                                                               localized.LanguageId);
     }
 }
Пример #8
0
 public static VendorAttribute ToEntity(this VendorAttributeModel model, VendorAttribute destination)
 {
     return(model.MapTo(destination));
 }
Пример #9
0
 public static VendorAttribute ToEntity(this VendorAttributeModel model)
 {
     return(model.MapTo <VendorAttributeModel, VendorAttribute>());
 }