コード例 #1
0
        protected void SaveDetails()
        {
            var      checkValue = false;
            CheckBox check      = ( CheckBox )fsAttributes.FindControl("SimpleCheckBox");

            if (check != null && check.Checked)
            {
                checkValue = true;
            }

            IHasAttributes entity = ContextEntity() as IHasAttributes;

            var rockContext          = new RockContext();
            var sourceAttributeKey   = GetAttributeValue("SourceEntityAttributeKey");
            var sourceAttributeValue = entity.GetAttributeValue(sourceAttributeKey);
            var attributeService     = new AttributeService(rockContext);
            var targetAttribute      = attributeService.GetByGuids(new List <Guid>()
            {
                sourceAttributeValue.AsGuid()
            }).ToList().FirstOrDefault();

            int personEntityTypeId = EntityTypeCache.Get(typeof(Person)).Id;

            var changes = new History.HistoryChangeList();

            var attribute = AttributeCache.Get(targetAttribute);

            if (CurrentPerson != null)
            {
                Control attributeControl = fsAttributes.FindControl(string.Format("attribute_field_{0}", attribute.Id));
                if (GetAttributeValue("CheckboxMode").AsBoolean(false) || attributeControl != null)
                {
                    string originalValue = CurrentPerson.GetAttributeValue(attribute.Key);
                    string newValue      = string.Empty;

                    if (GetAttributeValue("CheckboxMode").AsBoolean(false))
                    {
                        var valueMode = GetAttributeValue("CheckboxAttributeValueMode");
                        if (valueMode == "Date")
                        {
                            if (checkValue)
                            {
                                newValue = RockDateTime.Now.ToString();
                            }
                            else
                            {
                                newValue = string.Empty;
                            }
                        }
                        else if (valueMode == "Boolean")
                        {
                            if (checkValue)
                            {
                                newValue = "True";
                            }
                            else
                            {
                                newValue = "False";
                            }
                        }
                    }
                    else
                    {
                        newValue = attribute.FieldType.Field.GetEditValue(attributeControl, attribute.QualifierValues);
                    }

                    Rock.Attribute.Helper.SaveAttributeValue(CurrentPerson, attribute, newValue, rockContext);

                    // Check for changes to write to history
                    if ((originalValue ?? string.Empty).Trim() != (newValue ?? string.Empty).Trim())
                    {
                        string formattedOriginalValue = string.Empty;
                        if (!string.IsNullOrWhiteSpace(originalValue))
                        {
                            formattedOriginalValue = attribute.FieldType.Field.FormatValue(null, originalValue, attribute.QualifierValues, false);
                        }

                        string formattedNewValue = string.Empty;
                        if (!string.IsNullOrWhiteSpace(newValue))
                        {
                            formattedNewValue = attribute.FieldType.Field.FormatValue(null, newValue, attribute.QualifierValues, false);
                        }

                        History.EvaluateChange(changes, attribute.Name, formattedOriginalValue, formattedNewValue, attribute.FieldType.Field.IsSensitive());
                    }
                }
            }

            if (changes.Any())
            {
                HistoryService.SaveChanges(rockContext, typeof(Person), Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(),
                                           CurrentPerson.Id, changes);
            }

            string linkedPage = GetAttributeValue("RedirectPage");

            if (string.IsNullOrWhiteSpace(linkedPage))
            {
                ShowDetails();
            }
            else
            {
                var pageParams = new Dictionary <string, string>();
                pageParams.Add("av", "updated");
                var pageReference = new Rock.Web.PageReference(linkedPage, pageParams);
                Response.Redirect(pageReference.BuildUrl(), false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Shows the read-only attribute values.
        /// </summary>
        protected void ShowDetails()
        {
            var loadAttributeValue = GetAttributeValue("LoadAttributeValue").AsBoolean(false);

            ltTitle.Text = GetAttributeValue("PanelHeading");
            btnSave.Text = GetAttributeValue("SaveButtonText");

            IHasAttributes entity = ContextEntity() as IHasAttributes;

            if (entity != null && CurrentPerson != null)
            {
                var rockContext = new RockContext();

                if (entity.Attributes == null)
                {
                    entity.LoadAttributes();
                }

                if (CurrentPerson.Attributes == null)
                {
                    CurrentPerson.LoadAttributes();
                }

                var sourceAttributeKey   = GetAttributeValue("SourceEntityAttributeKey");
                var sourceAttributeValue = entity.GetAttributeValue(sourceAttributeKey);
                var attributeService     = new AttributeService(rockContext);
                var targetAttribute      = attributeService.GetByGuids(new List <Guid>()
                {
                    sourceAttributeValue.AsGuid()
                }).ToList().FirstOrDefault();

                fsAttributes.Controls.Clear();

                string validationGroup = string.Format("vgAttributeValues_{0}", this.BlockId);
                btnSave.ValidationGroup = validationGroup;

                var    attribute      = AttributeCache.Get(targetAttribute);
                string attributeValue = CurrentPerson.GetAttributeValue(attribute.Key);
                string formattedValue = string.Empty;

                if (GetAttributeValue("CheckboxMode").AsBoolean(false))
                {
                    var      checkboxText = GetAttributeValue("CheckboxText");
                    CheckBox checkbox     = new CheckBox();
                    checkbox.ID   = "SimpleCheckBox";
                    checkbox.Text = String.IsNullOrWhiteSpace(checkboxText) ? attribute.Name : checkboxText;

                    if (GetAttributeValue("InstantSave").AsBoolean(false))
                    {
                        checkbox.AutoPostBack = true;

                        checkbox.CheckedChanged += new EventHandler(this.Check_Change);
                        btnSave.Visible          = false;
                    }

                    if (loadAttributeValue && !string.IsNullOrWhiteSpace(attributeValue) && attributeValue.AsBoolean(true))
                    {
                        checkbox.Checked = true;
                    }

                    fsAttributes.Controls.Add(checkbox);
                }
                else
                {
                    attribute.AddControl(fsAttributes.Controls, attributeValue, validationGroup, loadAttributeValue, true);
                }

                pnlDetails.Visible = true;
            }
        }