Ejemplo n.º 1
0
        private void AddSubmitButton(dynamic form)
        {
            var viewContext = new ViewContext { HttpContext = HttpContext, Controller = this };
            var siteSalt = Services.WorkContext.CurrentSite.SiteSalt;
            var token = new HtmlHelper(viewContext, new ViewDataContainer()).AntiForgeryToken(siteSalt);

            // add a submit button to the form
            form
                ._Actions(Shape.Fieldset(
                    _RequestAntiForgeryToken: Shape.Markup(
                        Value: token.ToString()),
                    _Save: Shape.Submit(
                        Name: "op",
                        Value: T("Save"))
                    )
                );
        }
        public ActionResult Edit(int id, string category, string type, int actionId = -1) {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
                return new HttpUnauthorizedResult();

            var action = _rulesManager.DescribeActions().SelectMany(x => x.Descriptors).Where(x => x.Category == category && x.Type == type).FirstOrDefault();

            if (action == null) {
                return HttpNotFound();
            }

            // if there is no form to edit, save the action and go back to the rule
            if (action.Form == null) {
                if (actionId == -1) {
                    var rule = _rulesServices.GetRule(id);
                    rule.Actions.Add(new ActionRecord { Category = category, Type = type, Position = rule.Actions.Count + 1 });
                }

                return RedirectToAction("Edit", "Admin", new { id });
            }

            // build the form, and let external components alter it
            var form = _formManager.Build(action.Form);

            // generate an anti forgery token
            var viewContext = new ViewContext { HttpContext = HttpContext, Controller = this };
            var siteSalt = Services.WorkContext.CurrentSite.SiteSalt;
            var token = new HtmlHelper(viewContext, new ViewDataContainer()).AntiForgeryToken(siteSalt);

            // add a submit button to the form
            form
                ._Actions(Shape.Fieldset(
                    _RequestAntiForgeryToken: Shape.Markup(
                        Value: token.ToString()),
                    _Save: Shape.Submit(
                        Name: "op",
                        Value: T("Save"))
                    )
                );

            // bind form with existing values).
            if (actionId != -1) {
                var rule = _rulesServices.GetRule(id);
                var actionRecord = rule.Actions.Where(a => a.Id == actionId).FirstOrDefault();
                if (actionRecord != null) {
                    var parameters = FormParametersHelper.FromString(actionRecord.Parameters);
                    _formManager.Bind(form, new DictionaryValueProvider<string>(parameters, CultureInfo.InvariantCulture));
                }
            }

            var viewModel = new EditActionViewModel { Id = id, Action = action, Form = form };
            return View(viewModel);
        }