protected override void Published(PublishContentContext context) // L'update delle risposte può essere fatto solo nel published, altrimenti si alza un'eccezione "save the transient instance before flushing..." { base.Published(context); var userRegistrationPolicy = context.ContentItem.As <UserRegistrationPolicyPart>(); if (userRegistrationPolicy == null) { return; } if (_utilsServices.FeatureIsEnabled("Laser.Orchard.Policy")) { var policies = _usersExtensionsServices.BuildEditorForRegistrationPolicies(); if (_controllerAccessor.Context != null) { var answers = _controllerAccessor.Context.Controller.TempData["VolatileAnswers"] != null ? _controllerAccessor.Context.Controller.TempData["VolatileAnswers"].ToString() : ""; //userRegistrationPolicy.VolatileAnswers; if (!String.IsNullOrWhiteSpace(answers)) { var updateModel = policies.Select(x => new PolicyForUserViewModel { PolicyTextId = x.PolicyId, Accepted = answers.Split(',').Contains(x.PolicyId.ToString()), AnswerDate = DateTime.MinValue, // verrà automaticamente valorizzata in fase di salvataggio OldAccepted = false // non valorizza UserId in caso di nuove policy perché viene valorizzato dal metodo che le salva }).ToList(); _policyServices.PolicyForUserMassiveUpdate(updateModel, (IUser)context.ContentItem.As <UserPart>()); _controllerAccessor.Context.Controller.TempData["VolatileAnswers"] = null; } } } }
//GET protected override DriverResult Editor(UserRegistrationPolicyPart part, dynamic shapeHelper) { if (currentControllerAction != CONTROLLER_ACTION) { return(null); } if (_utilsServices.FeatureIsEnabled("Laser.Orchard.Policy")) { var shapeName = "Parts_UserRegistrationPolicy_Edit"; var templateName = "Parts/UserRegistrationPolicy_Edit"; var policies = _usersExtensionsServices.BuildEditorForRegistrationPolicies(); return(ContentShape(shapeName, () => shapeHelper.EditorTemplate(TemplateName: templateName, Model: policies, Prefix: Prefix))); } else { return(null); } }
//POST protected override DriverResult Editor(UserRegistrationPolicyPart part, IUpdateModel updater, dynamic shapeHelper) { var policyPart = part.As <PolicyPart>(); var settings = _orchardServices.WorkContext.CurrentSite.As <UserRegistrationSettingsPart>(); IList <UserPolicyAnswerWithContent> policies; if (part.As <PolicyPart>() != null && part.As <UserPart>() == null) { // The content has the PolicyPart and is not a User. // Having UserRegistrationPolicyPart means that we want to display policy accaptance before Saving/Publishing the content. policies = _usersExtensionsServices.BuildEditorForPolicies(policyPart); } else if (currentControllerAction != CONTROLLER_ACTION) { return(null); } else if (_utilsServices.FeatureIsEnabled("Laser.Orchard.Policy") && settings.IncludePendingPolicy == Policy.IncludePendingPolicyOptions.Yes) { policies = _usersExtensionsServices.BuildEditorForRegistrationPolicies(); } else { return(null); } if (updater != null && updater.TryUpdateModel(policies, Prefix, null, null)) { if (policies.Count(x => ( (x.PolicyAnswer == false) && x.UserHaveToAccept)) > 0) { updater.AddModelError("NotAcceptedPolicies", T("User has to accept policies!")); } //Insert answers in a ViewBag so they can be used from other modules var answers = policies.Select(x => new PolicyAnswer { PolicyTextId = x.PolicyId, Accepted = x.PolicyAnswer }).ToList(); _controllerAccessor.Context.Controller.ViewBag.PolicyAnswers = answers; _controllerAccessor.Context.Controller.TempData["VolatileAnswers"] = String.Join(",", policies.Where(x => x.PolicyAnswer).Select(x => x.PolicyId.ToString())); } return(ContentShape(_shapeName, () => shapeHelper.EditorTemplate(TemplateName: _templateName, Model: policies, Prefix: Prefix))); }