Exemple #1
0
        public IPagedList <RuleSetEntity> GetAllRuleSets(
            bool forEdit,
            bool withRules,
            RuleScope?scope       = null,
            int pageIndex         = 0,
            int pageSize          = int.MaxValue,
            bool includeSubGroups = false,
            bool includeHidden    = false)
        {
            var query = forEdit
                ? _rsRuleSets.Table
                : _rsRuleSets.TableUntracked;

            if (withRules)
            {
                query = query.Include(x => x.Rules);
            }

            if (!includeHidden)
            {
                query = query.Where(x => x.IsActive);
            }

            if (scope != null)
            {
                query = query.Where(x => x.Scope == scope.Value);
            }

            if (!includeSubGroups)
            {
                query = query.Where(x => !x.IsSubGroup);
            }

            return(new PagedList <RuleSetEntity>(query, pageIndex, pageSize));
        }
        // Ajax.
        public ActionResult AllRuleSets(string selectedIds, RuleScope?scope)
        {
            var ruleSets    = _ruleStorage.GetAllRuleSets(false, false, scope, includeHidden: true);
            var selectedArr = selectedIds.ToIntArray();

            ruleSets.Add(new RuleSetEntity {
                Id = -1, Name = T("Admin.Rules.AddRule").Text + "…"
            });

            var data = ruleSets
                       .Select(x => new
            {
                id       = x.Id.ToString(),
                text     = x.Name,
                selected = selectedArr.Contains(x.Id),
                urlTitle = x.Id == -1 ? string.Empty : T("Admin.Rules.OpenRule").Text,
                url      = x.Id == -1
                        ? Url.Action("Create", "Rule", new { area = "admin" })
                        : Url.Action("Edit", "Rule", new { id = x.Id, area = "admin" })
            })
                       .ToList();

            return(new JsonResult {
                Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemple #3
0
        /// <summary>
        /// Gets a list of all available rule sets.
        /// </summary>
        /// <param name="selectedIds">Ids of selected entities.</param>
        /// <param name="RuleScope">Specifies the <see cref="RuleScope"/>.</param>
        /// <returns>List of all rule sets as JSON.</returns>
        public async Task <IActionResult> AllRuleSets(string selectedIds, RuleScope?scope)
        {
            var ruleSets = await _db
                           .RuleSets
                           .AsNoTracking()
                           .ApplyStandardFilter(scope, includeHidden: true)
                           .ToListAsync();

            var selectedArr = selectedIds.ToIntArray();

            ruleSets.Add(new RuleSetEntity {
                Id = -1, Name = T("Admin.Rules.AddRule").Value + "…"
            });

            // TODO: (mh) (core) Implement Create & Edit.
            var data = ruleSets
                       .Select(x => new ChoiceListItem
            {
                Id       = x.Id.ToString(),
                Text     = x.Name,
                Selected = selectedArr.Contains(x.Id),
                UrlTitle = x.Id == -1 ? string.Empty : T("Admin.Rules.OpenRule").Value,
                Url      = x.Id == -1
                        ? Url.Action("Create", "Rule", new { scope, area = "Admin" })
                        : Url.Action("Edit", "Rule", new { id = x.Id, area = "Admin" })
            })
                       .ToList();

            return(new JsonResult(data));
        }
        // Ajax.
        public ActionResult AllRuleSets(string label, string selectedIds, RuleScope?scope)
        {
            var ruleSets    = _ruleStorage.GetAllRuleSets(false, false, scope, includeHidden: true);
            var selectedArr = selectedIds.ToIntArray();

            if (label.HasValue())
            {
                ruleSets.Insert(0, new RuleSetEntity {
                    Name = label, Id = 0
                });
            }

            var data = ruleSets
                       .Select(x => new
            {
                id       = x.Id.ToString(),
                text     = x.Name,
                selected = selectedArr.Contains(x.Id)
            })
                       .ToList();

            return(new JsonResult {
                Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemple #5
0
        /// <summary>
        /// Applies ruleset standard filter and sorts by <see cref="RuleSetEntity.IsActive"/> DESC, then by <see cref="RuleSetEntity.Scope"/>.
        /// </summary>
        public static IOrderedQueryable <RuleSetEntity> ApplyStandardFilter(this IQueryable <RuleSetEntity> query,
                                                                            RuleScope?scope       = null,
                                                                            bool includeSubGroups = false,
                                                                            bool includeHidden    = false)
        {
            Guard.NotNull(query, nameof(query));

            if (!includeHidden)
            {
                query = query.Where(x => x.IsActive);
            }

            if (scope != null)
            {
                query = query.Where(x => x.Scope == scope.Value);
            }

            if (!includeSubGroups)
            {
                query = query.Where(x => !x.IsSubGroup);
            }

            return(query
                   .OrderByDescending(x => x.IsActive)
                   .ThenBy(x => x.Scope));
        }
Exemple #6
0
        public ActionResult Create(RuleScope?scope)
        {
            var model = new RuleSetModel();

            PrepareModel(model, null, scope);
            PrepareTemplateViewBag(0);

            return(View(model));
        }
Exemple #7
0
        /// <summary>
        /// Creates a new rule from deserialized rule representation.
        /// </summary>
        /// <param name="deserializedRule">
        /// Deserialized rule.
        /// </param>
        /// <returns>
        /// The rule from <paramref name="deserializedRule"/>.
        /// </returns>
        internal Rule.RegexRule CreateFrom(DTO.RegexRule deserializedRule)
        {
            RuleScope?scope = null;

            switch (deserializedRule.scope)
            {
            case DTO.RuleScope.Entity:
                scope = RuleScope.Entity;
                break;

            case DTO.RuleScope.Attribute:
                scope = RuleScope.Attribute;
                break;

            case DTO.RuleScope.Lookup:
                scope = RuleScope.Lookup;
                break;

            default:
                throw new NotImplementedException(
                          $"No mapping for DTO rule scope {deserializedRule.scope}."
                          );
            }

            var exclusions = new string[] { };

            if (deserializedRule.Exclude != null)
            {
                exclusions = deserializedRule.Exclude;
            }

            var rule = new RegexRule(deserializedRule.pattern,
                                     scope.Value,
                                     exclusions);

            return(rule);
        }
Exemple #8
0
        private void PrepareModel(RuleSetModel model, RuleSetEntity entity, RuleScope?scope = null)
        {
            var scopes = (entity?.Scope ?? scope ?? RuleScope.Cart).ToSelectList();

            model.Scopes = scopes
                           .Select(x =>
            {
                var item = new ExtendedSelectListItem
                {
                    Value    = x.Value,
                    Text     = x.Text,
                    Selected = x.Selected
                };

                var ruleScope = (RuleScope)x.Value.ToInt();
                item.CustomProperties["Description"] = ruleScope.GetLocalizedEnum(Services.Localization, Services.WorkContext, true);

                return(item);
            })
                           .ToList();

            if ((entity?.Id ?? 0) != 0)
            {
                var provider = _ruleProvider(entity.Scope);

                model.ScopeName       = entity.Scope.GetLocalizedEnum(Services.Localization, Services.WorkContext);
                model.ExpressionGroup = _ruleFactory.CreateExpressionGroup(entity, provider, true);

                model.AssignedToDiscounts = entity.Discounts
                                            .Select(x => new RuleSetModel.AssignedToEntityModel {
                    Id = x.Id, Name = x.Name.NullEmpty() ?? x.Id.ToString()
                })
                                            .ToList();

                model.AssignedToShippingMethods = entity.ShippingMethods
                                                  .Select(x => new RuleSetModel.AssignedToEntityModel {
                    Id = x.Id, Name = x.GetLocalized(y => y.Name)
                })
                                                  .ToList();

                var paymentMethods = entity.PaymentMethods;
                if (paymentMethods.Any())
                {
                    var paymentProviders = _paymentService.Value.LoadAllPaymentMethods().ToDictionarySafe(x => x.Metadata.SystemName);

                    model.AssignedToPaymentMethods = paymentMethods
                                                     .Select(x =>
                    {
                        string friendlyName = null;
                        if (paymentProviders.TryGetValue(x.PaymentMethodSystemName, out var paymentProvider))
                        {
                            friendlyName = _pluginMediator.Value.GetLocalizedFriendlyName(paymentProvider.Metadata);
                        }

                        return(new RuleSetModel.AssignedToEntityModel
                        {
                            Id = x.Id,
                            Name = friendlyName.NullEmpty() ?? x.PaymentMethodSystemName,
                            SystemName = x.PaymentMethodSystemName
                        });
                    })
                                                     .ToList();
                }

                model.AssignedToCustomerRoles = entity.CustomerRoles
                                                .Select(x => new RuleSetModel.AssignedToEntityModel {
                    Id = x.Id, Name = x.Name
                })
                                                .ToList();

                model.AssignedToCategories = entity.Categories
                                             .Select(x => new RuleSetModel.AssignedToEntityModel {
                    Id = x.Id, Name = x.GetLocalized(y => y.Name)
                })
                                             .ToList();
            }
        }