예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public UserReactionsTypes GetTypesTableWithStyles()
        {
            var reactionSettings = _orchardServices.WorkContext.CurrentSite.As <UserReactionsSettingsPart>();
            var userRT           = new UserReactionsTypes();
            var styleAcronime    = new Laser.Orchard.UserReactions.StyleAcroName();

            userRT.CssName = reactionSettings.StyleFileNameProvider;
            userRT.AllowMultipleChoices = reactionSettings.AllowMultipleChoices;

            userRT.UserReactionsType = GetTypesTable().Select(r => new UserReactionsTypeVM {
                Id         = r.Id,
                Priority   = r.Priority,
                TypeName   = r.TypeName,
                Activating = r.Activating,
                Delete     = false
            }).ToList();

            int newPriority = userRT.UserReactionsType.Count + 1;

            foreach (var type in Enum.GetNames(typeof(ReactionsNames)))
            {
                if (userRT.UserReactionsType.FirstOrDefault(x => x.TypeName == type) == null)
                {
                    userRT.UserReactionsType.Add(new UserReactionsTypeVM {
                        Id         = 0,
                        Activating = false,
                        Delete     = false,
                        Priority   = newPriority,
                        TypeName   = type
                    });
                    newPriority++;
                }
            }
            return(userRT);
        }
예제 #2
0
        public ActionResult Settings(UserReactionsTypes model)
        {
            if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Yout have to be an Administrator to edit Reaction types settings!")))
            {
                return(new HttpUnauthorizedResult());
            }

            if (!ModelState.IsValid)
            {
                _orchardServices.Notifier.Error(T("Settings update failed: {0}", T("check your input!")));
                return(View(model));
            }

            var reactionSettings = _orchardServices.WorkContext.CurrentSite.As <UserReactionsSettingsPart>();

            reactionSettings.StyleFileNameProvider = model.CssName;
            reactionSettings.AllowMultipleChoices  = model.AllowMultipleChoices;
            bool newTypesCreated = false;

            foreach (var item in model.UserReactionsType)
            {
                if (item.Id == 0)
                {
                    var    styleAcronime     = new Laser.Orchard.UserReactions.StyleAcroName();
                    string styleAcronimeName = styleAcronime.StyleAcronime + item.TypeName;
                    _repoTypes.Create(new Models.UserReactionsTypesRecord {
                        Priority   = item.Priority,
                        TypeName   = item.TypeName,
                        Activating = item.Activating
                    });
                    newTypesCreated = true;
                }
                else
                {
                    var record = _repoTypes.Get(item.Id);
                    record.Priority   = item.Priority;
                    record.TypeName   = item.TypeName;
                    record.Activating = item.Activating;
                    _repoTypes.Update(record);
                }
            }

            if (newTypesCreated)
            {
                // allinea i contenuti tramite un task schedulato
                _taskManager.CreateTask("Laser.Orchard.UserReactionsSettings.Task", DateTime.UtcNow.AddSeconds(5), null);
                _notifier.Add(NotifyType.Warning, T("A task has been scheduled to update reaction summaries for existing contents."));
            }
            _notifier.Add(NotifyType.Information, T("UserReaction settings updating"));
            return(RedirectToActionPermanent("Settings"));
        }
        public override void Validating(ValidatingContext context)
        {
            if (context.FormName == UserReactionsQueryFilterForm.FormName)
            {
                //UserReactions validation
                ///////////////////////////////////////////////////////////
                string reaction = context.ValueProvider.GetValue("Reaction").AttemptedValue.ToString().ToLower();
                var    userRT   = new UserReactionsTypes();
                userRT.UserReactionsType = _reactionsService.GetTypesTable().Select(r => new UserReactionsTypeVM {
                    Id       = r.Id,
                    TypeName = r.TypeName,
                }).ToList();

                int lenReaction   = reaction.Length;
                var reactionsList = userRT.UserReactionsType;

                if ((reaction.Substring(0, 1) != "{" && reaction.Substring(lenReaction - 1, 1) != "}") && (reactionsList.FirstOrDefault(x => x.TypeName == reaction) == null))
                {
                    context.ModelState.AddModelError("Reaction", T("The field {0} should contain a valid input.", T("Reactions").Text).Text);
                }

                if (!context.ModelState.IsValid)
                {
                    return;
                }
                ///////

                var isRange = new[] { "Between", "NotBetween" }.Contains(context.ValueProvider.GetValue("Operator").AttemptedValue);
                var min   = context.ValueProvider.GetValue("Min");
                var max   = context.ValueProvider.GetValue("Max");
                var value = context.ValueProvider.GetValue("Value");

                // validating mandatory values
                if (isRange)
                {
                    if (min == null || String.IsNullOrWhiteSpace(min.AttemptedValue))
                    {
                        context.ModelState.AddModelError("Min", T("The field {0} is required.", T("Min").Text).Text);
                    }

                    if (max == null || String.IsNullOrWhiteSpace(max.AttemptedValue))
                    {
                        context.ModelState.AddModelError("Max", T("The field {0} is required.", T("Max").Text).Text);
                    }
                }
                else
                {
                    if (min == null || String.IsNullOrWhiteSpace(value.AttemptedValue))
                    {
                        context.ModelState.AddModelError("Value", T("The field {0} is required.", T("Value").Text).Text);
                    }
                }

                if (!context.ModelState.IsValid)
                {
                    return;
                }

                decimal output;

                if (isRange)
                {
                    if (!Decimal.TryParse(min.AttemptedValue, out output) && !IsToken(min.AttemptedValue))
                    {
                        context.ModelState.AddModelError("Min", T("The field {0} should contain a valid number", T("Min").Text).Text);
                    }

                    if (!Decimal.TryParse(max.AttemptedValue, out output) && !IsToken(max.AttemptedValue))
                    {
                        context.ModelState.AddModelError("Max", T("The field {0} should contain a valid number", T("Max").Text).Text);
                    }
                }
                else
                {
                    if (!Decimal.TryParse(value.AttemptedValue, out output) && !IsToken(value.AttemptedValue))
                    {
                        context.ModelState.AddModelError("Value", T("The field {0} should contain a valid number", T("Value").Text).Text);
                    }
                }
            }
        }