Example #1
0
        /// <summary>
        /// Edits the matrix item.
        /// </summary>
        /// <param name="matrixItemId">The matrix item identifier.</param>
        private void EditMatrixItem(int matrixItemId)
        {
            _hfMatrixItemId.Value = matrixItemId.ToString();

            // make a temp attributeMatrixItem to see what Attributes they have
            AttributeMatrixItem attributeMatrixItem = null;

            using (var rockContext = new RockContext())
            {
                if (matrixItemId > 0)
                {
                    attributeMatrixItem = new AttributeMatrixItemService(rockContext).Get(matrixItemId);
                }

                if (attributeMatrixItem == null)
                {
                    attributeMatrixItem = new AttributeMatrixItem();
                    attributeMatrixItem.AttributeMatrix = new AttributeMatrixService(rockContext).Get(this.AttributeMatrixGuid.Value);
                }

                if (this.AttributeMatrixTemplateId.HasValue && attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplateId != this.AttributeMatrixTemplateId)
                {
                    attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplateId = this.AttributeMatrixTemplateId.Value;
                    attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplate   = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplateId);
                }

                attributeMatrixItem.LoadAttributes();
            }

            _phMatrixItemAttributes.Controls.Clear();
            Rock.Attribute.Helper.AddEditControls(attributeMatrixItem, _phMatrixItemAttributes, true, GetValidationGroupForAttributeControls());

            _gMatrixItems.Visible      = false;
            _pnlEditMatrixItem.Visible = true;
        }
Example #2
0
        /// <summary>
        /// Handles the Click event of the btnSaveMatrixItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnSaveMatrixItem_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();
            var attributeMatrixItemService          = new AttributeMatrixItemService(rockContext);
            AttributeMatrixItem attributeMatrixItem = null;
            int attributeMatrixItemId = _hfMatrixItemId.Value.AsInteger();

            if (attributeMatrixItemId > 0)
            {
                attributeMatrixItem = attributeMatrixItemService.Get(attributeMatrixItemId);
            }

            if (attributeMatrixItem == null)
            {
                attributeMatrixItem = new AttributeMatrixItem();
                attributeMatrixItem.AttributeMatrix = new AttributeMatrixService(rockContext).Get(this.AttributeMatrixGuid.Value);
                attributeMatrixItemService.Add(attributeMatrixItem);
            }

            attributeMatrixItem.LoadAttributes(rockContext);
            Rock.Attribute.Helper.GetEditValues(_phMatrixItemAttributes, attributeMatrixItem);
            rockContext.SaveChanges();
            attributeMatrixItem.SaveAttributeValues(rockContext);

            _gMatrixItems.Visible      = true;
            _pnlEditMatrixItem.Visible = false;

            BindGrid(this.AttributeMatrixGuid);
        }
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService     attributeMatrixService     = new AttributeMatrixService(rockContext);
            AttributeMatrixItemService attributeMatrixItemService = new AttributeMatrixItemService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var attributeMatrixGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()).AsGuidOrNull();
            var itemGuid            = GetActionAttributeValue(action, "ItemGuid").ResolveMergeFields(GetMergeFields(action)).AsGuidOrNull();

            if (attributeMatrixGuid.HasValue && itemGuid.HasValue)
            {
                // Load the matrix
                AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.Value);

                AttributeMatrixItem item = matrix.AttributeMatrixItems.Where(i => i.Guid == itemGuid.Value).FirstOrDefault();

                if (item != null)
                {
                    matrix.AttributeMatrixItems.Remove(item);
                    attributeMatrixItemService.Delete(item);
                }
            }

            return(true);
        }
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var attributeMatrixGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()).AsGuidOrNull();
            var itemAttributes      = GetActionAttributeValue(action, "ItemAttributes").AsDictionaryOrNull();

            if (attributeMatrixGuid.HasValue && itemAttributes != null)
            {
                // Load the matrix
                AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.Value);

                // Create the new item
                AttributeMatrixItem item = new AttributeMatrixItem();
                item.AttributeMatrix = matrix;
                item.LoadAttributes();

                foreach (var attribute in item.Attributes)
                {
                    if (itemAttributes.ContainsKey(attribute.Key))
                    {
                        item.SetAttributeValue(attribute.Key, itemAttributes[attribute.Key].ResolveMergeFields(GetMergeFields(action)));
                    }
                }
                matrix.AttributeMatrixItems.Add(item);
                rockContext.SaveChanges();
                item.SaveAttributeValues(rockContext);
            }

            return(true);
        }
Example #5
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var attributeGuid       = GetActionAttributeValue(action, "AttributeMatrix").AsGuidOrNull();
            var attributeMatrixGuid = action.GetWorkflowAttributeValue(attributeGuid.HasValue ? attributeGuid.Value : Guid.Empty).AsGuidOrNull();
            var itemAttributes      = GetActionAttributeValue(action, "ItemAttributes").AsDictionaryOrNull();

            if (attributeGuid.HasValue && itemAttributes != null)
            {
                // Load the matrix
                AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.HasValue ? attributeMatrixGuid.Value : Guid.Empty);

                // If the matrix is null, create it first
                if (matrix == null)
                {
                    var attribute = AttributeCache.Get(GetActionAttributeValue(action, "AttributeMatrix").AsGuid());
                    matrix = new AttributeMatrix();
                    matrix.AttributeMatrixItems      = new List <AttributeMatrixItem>();
                    matrix.AttributeMatrixTemplateId = attribute.QualifierValues["attributematrixtemplate"]?.Value?.AsInteger() ?? 0;
                    attributeMatrixService.Add(matrix);

                    // Persist it and make sure it gets saved
                    rockContext.SaveChanges();
                    if (attribute.EntityTypeId == new Rock.Model.Workflow().TypeId)
                    {
                        action.Activity.Workflow.SetAttributeValue(attribute.Key, matrix.Guid.ToString());
                    }
                    else if (attribute.EntityTypeId == new WorkflowActivity().TypeId)
                    {
                        action.Activity.SetAttributeValue(attribute.Key, matrix.Guid.ToString());
                    }
                }

                // Create the new item
                AttributeMatrixItem item = new AttributeMatrixItem();
                item.AttributeMatrix = matrix;
                item.LoadAttributes();

                foreach (var attribute in item.Attributes)
                {
                    if (itemAttributes.ContainsKey(attribute.Key))
                    {
                        item.SetAttributeValue(attribute.Key, itemAttributes[attribute.Key].ResolveMergeFields(GetMergeFields(action)));
                    }
                }
                matrix.AttributeMatrixItems.Add(item);
                rockContext.SaveChanges();
                item.SaveAttributeValues(rockContext);
            }

            return(true);
        }
        private void BindGrid(IQueryable <GroupMember> members, List <Person> familyMembers, RockContext rockContext)
        {
            var gridData = new List <GridData>();
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            foreach (var person in familyMembers)
            {
                //Get all the camp group members for this person
                List <GroupMember> medicalMembers = members.Where(m => m.PersonId == person.Id).ToList();

                if (!medicalMembers.Any())
                {
                    continue;
                }

                GridData data = new GridData
                {
                    Id          = person.PrimaryAlias.Guid,
                    Name        = person.FullName,
                    Group       = string.Join("<br>", medicalMembers.Select(m => m.Group.Name)),
                    Medications = "No Medication Information"
                };
                person.LoadAttributes();
                var attribute       = person.GetAttributeValue(GetAttributeValue("MedicationMatrixKey"));
                var attributeMatrix = attributeMatrixService.Get(attribute.AsGuid());
                if (attributeMatrix != null)
                {
                    var lava     = attributeMatrix.AttributeMatrixTemplate.FormattedLava;
                    var template = attributeMatrix.AttributeMatrixTemplate;
                    template.LoadAttributes();
                    AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem();
                    tempAttributeMatrixItem.AttributeMatrix = attributeMatrix;
                    tempAttributeMatrixItem.LoadAttributes();
                    Dictionary <string, object> mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, null, new Rock.Lava.CommonMergeFieldsOptions {
                        GetLegacyGlobalMergeFields = false
                    });
                    mergeFields.Add("AttributeMatrix", attributeMatrix);
                    mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name));
                    mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order));
                    var medications = lava.ResolveMergeFields(mergeFields);
                    data.Medications = medications;
                }
                gridData.Add(data);
            }

            gGrid.DataSource = gridData;
            gGrid.DataBind();
        }
Example #7
0
        /// <summary>
        /// Handles the Click event of the deleteField control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        private void gMatrixItems_DeleteClick(object sender, RowEventArgs e)
        {
            int attributeMatrixItemId = e.RowKeyId;
            var rockContext           = new RockContext();
            AttributeMatrixItemService attributeMatrixItemService = new AttributeMatrixItemService(rockContext);
            AttributeMatrixItem        attributeMatrixItem        = attributeMatrixItemService.Get(attributeMatrixItemId);

            if (attributeMatrixItem != null)
            {
                var attributeMatrix = attributeMatrixItem.AttributeMatrix;
                attributeMatrixItemService.Delete(attributeMatrixItem);
                rockContext.SaveChanges();

                BindGrid(this.AttributeMatrixGuid);
            }
        }
Example #8
0
        /// <summary>
        /// Returns the field's current value(s)
        /// </summary>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="value">Information about the value</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="condensed">Flag indicating if the value should be condensed (i.e. for use in a grid column)</param>
        /// <returns></returns>
        public override string FormatValue(Control parentControl, string value, Dictionary <string, ConfigurationValue> configurationValues, bool condensed)
        {
            using (var rockContext = new RockContext())
            {
                var             attributeMatrixService = new AttributeMatrixService(rockContext);
                AttributeMatrix attributeMatrix        = null;
                Guid?           attributeMatrixGuid    = value.AsGuidOrNull();
                if (attributeMatrixGuid.HasValue)
                {
                    attributeMatrix = attributeMatrixService.GetNoTracking(attributeMatrixGuid.Value);
                }

                if (attributeMatrix != null)
                {
                    if (configurationValues.ContainsKey(ATTRIBUTE_MATRIX_TEMPLATE))
                    {
                        // set the AttributeMatrixTemplateId just in case it was changed since the last time the attributeMatrix was saved
                        int attributeMatrixTemplateId = configurationValues[ATTRIBUTE_MATRIX_TEMPLATE].Value.AsInteger();
                        if (attributeMatrix.AttributeMatrixTemplateId != attributeMatrixTemplateId)
                        {
                            attributeMatrix.AttributeMatrixTemplateId = attributeMatrixTemplateId;
                            attributeMatrix.AttributeMatrixTemplate   = new AttributeMatrixTemplateService(rockContext).GetNoTracking(attributeMatrix.AttributeMatrixTemplateId);

                            // If the AttributeMatrixTemplateId changed, all the values in the AttributeMatrixItems
                            // are referring to attributes from the old template, so wipe them out. All of them.
                            attributeMatrix.AttributeMatrixItems.Clear();
                        }
                    }

                    // make a temp attributeMatrixItem to see what Attributes they have
                    AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem();
                    tempAttributeMatrixItem.AttributeMatrix = attributeMatrix;
                    tempAttributeMatrixItem.LoadAttributes();

                    var lavaTemplate = attributeMatrix.AttributeMatrixTemplate.FormattedLava;
                    Dictionary <string, object> mergeFields = Lava.LavaHelper.GetCommonMergeFields(parentControl?.RockBlock()?.RockPage, null, new Lava.CommonMergeFieldsOptions {
                        GetLegacyGlobalMergeFields = false
                    });
                    mergeFields.Add("AttributeMatrix", attributeMatrix);
                    mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name));
                    mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order));
                    return(lavaTemplate.ResolveMergeFields(mergeFields));
                }
            }

            return(base.FormatValue(parentControl, value, configurationValues, condensed));
        }
        /// <summary>
        /// Edits the matrix item.
        /// </summary>
        /// <param name="matrixItemId">The matrix item identifier.</param>
        private void EditMatrixItem(int matrixItemId)
        {
            _hfMatrixItemId.Value = matrixItemId.ToString();

            // make a temp attributeMatrixItem to see what Attributes they have
            AttributeMatrixItem attributeMatrixItem = null;

            using (var rockContext = new RockContext())
            {
                if (matrixItemId > 0)
                {
                    attributeMatrixItem = new AttributeMatrixItemService(rockContext).Get(matrixItemId);
                }

                if (attributeMatrixItem == null)
                {
                    attributeMatrixItem = new AttributeMatrixItem();
                    attributeMatrixItem.AttributeMatrix = new AttributeMatrixService(rockContext).Get(this.AttributeMatrixGuid.Value);
                }

                if (this.AttributeMatrixTemplateId.HasValue && attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplateId != this.AttributeMatrixTemplateId)
                {
                    attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplateId = this.AttributeMatrixTemplateId.Value;
                    attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplate   = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrixItem.AttributeMatrix.AttributeMatrixTemplateId);
                }

                attributeMatrixItem.LoadAttributes();
            }

            _phMatrixItemAttributes.Controls.Clear();

            // set the validation group on the controls and save button
            string validationGroup = GetValidationGroupForAttributeControls();

            Rock.Attribute.Helper.AddEditControls(attributeMatrixItem, _phMatrixItemAttributes, true, validationGroup);

            // Make sure to set the validategroup on the save button to match, just in case it changed since CreateChildControls
            _btnSaveMatrixItem.ValidationGroup = validationGroup;

            _gMatrixItems.Visible      = false;
            _pnlEditMatrixItem.Visible = true;
        }
Example #10
0
        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // HiddenField to store which AttributeMatrix record we are editing
            _hfAttributeMatrixGuid = new HiddenField {
                ID = "_hfAttributeMatrixGuid"
            };
            this.Controls.Add(_hfAttributeMatrixGuid);

            _nbWarning = new NotificationBox {
                ID = "_nbWarning", NotificationBoxType = NotificationBoxType.Warning, Dismissable = true
            };
            this.Controls.Add(_nbWarning);

            // Grid with view of MatrixItems
            _gMatrixItems = new Grid {
                ID = "_gMatrixItems", DisplayType = GridDisplayType.Light
            };
            this.Controls.Add(_gMatrixItems);
            _gMatrixItems.DataKeyNames      = new string[] { "Id" };
            _gMatrixItems.Actions.AddClick += gMatrixItems_AddClick;
            _gMatrixItems.Actions.ShowAdd   = true;
            _gMatrixItems.IsDeleteEnabled   = true;
            _gMatrixItems.GridReorder      += gMatrixItems_GridReorder;
            _gMatrixItems.GridRebind       += _gMatrixItems_GridRebind;

            _gMatrixItems.Columns.Add(new ReorderField());

            AttributeMatrixItem tempAttributeMatrixItem = null;

            if (this.AttributeMatrixTemplateId.HasValue)
            {
                tempAttributeMatrixItem = new AttributeMatrixItem();
                tempAttributeMatrixItem.AttributeMatrix = new AttributeMatrix {
                    AttributeMatrixTemplateId = this.AttributeMatrixTemplateId.Value
                };
                tempAttributeMatrixItem.LoadAttributes();

                foreach (var attribute in tempAttributeMatrixItem.Attributes.Select(a => a.Value))
                {
                    _gMatrixItems.Columns.Add(new AttributeField {
                        DataField = attribute.Key, HeaderText = attribute.Name
                    });
                }
            }

            DeleteField deleteField = new DeleteField();

            deleteField.Click += gMatrixItems_DeleteClick;
            _gMatrixItems.Columns.Add(deleteField);

            _gMatrixItems.RowSelected += gMatrixItems_RowSelected;

            // Edit Item
            _pnlEditMatrixItem = new Panel {
                ID = "_pnlEditMatrixItem", Visible = false, CssClass = "well js-validation-group"
            };
            _hfMatrixItemId = new HiddenField {
                ID = "_hfMatrixItemId"
            };
            _pnlEditMatrixItem.Controls.Add(_hfMatrixItemId);

            _phMatrixItemAttributes = new DynamicPlaceholder {
                ID = "_phMatrixItemAttributes"
            };
            _pnlEditMatrixItem.Controls.Add(_phMatrixItemAttributes);

            string validationGroup = GetValidationGroupForAttributeControls();

            if (tempAttributeMatrixItem != null)
            {
                Rock.Attribute.Helper.AddEditControls(tempAttributeMatrixItem, _phMatrixItemAttributes, false, validationGroup);
            }

            _pnlActions = new Panel {
                ID = "_pnlActions", CssClass = "actions"
            };
            _pnlEditMatrixItem.Controls.Add(_pnlActions);

            _btnSaveMatrixItem = new LinkButton {
                ID = "_btnSaveMatrixItem", CssClass = "btn btn-primary btn-sm", Text = "Save", ValidationGroup = validationGroup, CausesValidation = true
            };
            _btnSaveMatrixItem.Click += btnSaveMatrixItem_Click;
            _pnlActions.Controls.Add(_btnSaveMatrixItem);

            _btnCancelMatrixItem = new LinkButton {
                ID = "_btnCancelMatrixItem", CssClass = "btn btn-link", Text = "Cancel", CausesValidation = false
            };
            _btnCancelMatrixItem.Click += btnCancelMatrixItem_Click;
            _pnlActions.Controls.Add(_btnCancelMatrixItem);

            this.Controls.Add(_pnlEditMatrixItem);
        }
        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // HiddenField to store which AttributeMatrix record we are editing
            _hfAttributeMatrixGuid = new HiddenField {
                ID = "_hfAttributeMatrixGuid"
            };
            this.Controls.Add(_hfAttributeMatrixGuid);

            _nbWarning = new NotificationBox {
                ID = "_nbWarning", NotificationBoxType = NotificationBoxType.Warning, Dismissable = true
            };
            this.Controls.Add(_nbWarning);

            // Grid with view of MatrixItems
            _gMatrixItems = new Grid {
                ID = "_gMatrixItems", DisplayType = GridDisplayType.Light
            };
            this.Controls.Add(_gMatrixItems);
            _gMatrixItems.DataKeyNames      = new string[] { "Id" };
            _gMatrixItems.Actions.AddClick += gMatrixItems_AddClick;
            _gMatrixItems.Actions.ShowAdd   = true;
            _gMatrixItems.IsDeleteEnabled   = true;
            _gMatrixItems.GridReorder      += gMatrixItems_GridReorder;
            _gMatrixItems.GridRebind       += _gMatrixItems_GridRebind;

            _hfRowCount = new HiddenFieldWithValidationProperty {
                ID = "_hfRowCount"
            };
            this.Controls.Add(_hfRowCount);

            _requiredRowCountRangeValidator    = new HiddenFieldRangeValidator();
            _requiredRowCountRangeValidator.ID = _hfRowCount.ID + "_rfv";
            _requiredRowCountRangeValidator.ControlToValidate = _hfRowCount.ID;
            _requiredRowCountRangeValidator.Display           = ValidatorDisplay.Dynamic;
            _requiredRowCountRangeValidator.Type     = ValidationDataType.Integer;
            _requiredRowCountRangeValidator.CssClass = "validation-error help-inline";
            _requiredRowCountRangeValidator.Enabled  = false;

            this.Controls.Add(_requiredRowCountRangeValidator);

            _gMatrixItems.Columns.Add(new ReorderField());

            AttributeMatrixItem tempAttributeMatrixItem = null;

            if (this.AttributeMatrixTemplateId.HasValue)
            {
                tempAttributeMatrixItem = new AttributeMatrixItem();
                tempAttributeMatrixItem.AttributeMatrix = new AttributeMatrix {
                    AttributeMatrixTemplateId = this.AttributeMatrixTemplateId.Value
                };
                tempAttributeMatrixItem.LoadAttributes();

                foreach (var attribute in tempAttributeMatrixItem.Attributes.Select(a => a.Value))
                {
                    _gMatrixItems.Columns.Add(new AttributeField {
                        DataField = attribute.Key, HeaderText = attribute.Name
                    });
                }

                AttributeMatrixTemplateService attributeMatrixTemplateService = new AttributeMatrixTemplateService(new RockContext());
                var attributeMatrixTemplateRanges = attributeMatrixTemplateService.GetSelect(this.AttributeMatrixTemplateId.Value, s => new { s.MinimumRows, s.MaximumRows });

                // If a value is required, make sure we have a minumum row count of at least 1.
                var minRowCount = attributeMatrixTemplateRanges.MinimumRows.GetValueOrDefault(0);
                if (this.Required &&
                    minRowCount < 1)
                {
                    minRowCount = 1;
                }
                _requiredRowCountRangeValidator.MinimumValue = minRowCount.ToString();

                _requiredRowCountRangeValidator.Enabled = minRowCount > 0;
                if (minRowCount == 1)
                {
                    _requiredRowCountRangeValidator.ErrorMessage = "At least 1 row is required.";
                }
                else
                {
                    _requiredRowCountRangeValidator.ErrorMessage = $"At least {minRowCount} rows are required";
                }
            }

            DeleteField deleteField = new DeleteField();

            deleteField.Click += gMatrixItems_DeleteClick;
            _gMatrixItems.Columns.Add(deleteField);

            _gMatrixItems.RowSelected += gMatrixItems_RowSelected;

            // Edit Item
            _pnlEditMatrixItem = new Panel {
                ID = "_pnlEditMatrixItem", Visible = false, CssClass = "well js-validation-group validation-group"
            };
            _hfMatrixItemId = new HiddenField {
                ID = "_hfMatrixItemId"
            };
            _pnlEditMatrixItem.Controls.Add(_hfMatrixItemId);

            _phMatrixItemAttributes = new DynamicPlaceholder {
                ID = "_phMatrixItemAttributes"
            };
            _pnlEditMatrixItem.Controls.Add(_phMatrixItemAttributes);

            string validationGroup = GetValidationGroupForAttributeControls();

            if (tempAttributeMatrixItem != null)
            {
                Rock.Attribute.Helper.AddEditControls(tempAttributeMatrixItem, _phMatrixItemAttributes, false, validationGroup);
            }

            _pnlActions = new Panel {
                ID = "_pnlActions", CssClass = "actions"
            };
            _pnlEditMatrixItem.Controls.Add(_pnlActions);

            _btnSaveMatrixItem = new LinkButton {
                ID = "_btnSaveMatrixItem", CssClass = "btn btn-primary btn-sm", Text = "Save", ValidationGroup = validationGroup, CausesValidation = true
            };
            _btnSaveMatrixItem.Click += btnSaveMatrixItem_Click;
            _pnlActions.Controls.Add(_btnSaveMatrixItem);

            _btnCancelMatrixItem = new LinkButton {
                ID = "_btnCancelMatrixItem", CssClass = "btn btn-link", Text = "Cancel", CausesValidation = false
            };
            _btnCancelMatrixItem.Click += btnCancelMatrixItem_Click;
            _pnlActions.Controls.Add(_btnCancelMatrixItem);

            this.Controls.Add(_pnlEditMatrixItem);
        }
Example #12
0
        protected override void OnLoad(EventArgs e)
        {
            nbAlert.Visible = false;

            if (CurrentPerson == null)
            {
                nbAlert.Visible = true;
                nbAlert.Text    = "Please log in to continue.";
                return;
            }
            if (!Page.IsPostBack)
            {
                RockContext rockContext   = new RockContext();
                var         familyMembers = CurrentPerson.GetFamilies().SelectMany(f => f.Members).Select(m => m.Person).ToList();

                AddCaretakees(familyMembers, rockContext);


                var groupTypeStrings = GetAttributeValue("GroupTypes").SplitDelimitedValues();
                var groupTypeIds     = new List <int>();
                foreach (var groupType in groupTypeStrings)
                {
                    var groupTypeCache = GroupTypeCache.Get(groupType.AsGuid());
                    if (groupTypeCache != null)
                    {
                        groupTypeIds.Add(groupTypeCache.Id);
                    }
                }

                var groups = new GroupService(rockContext).Queryable()
                             .Where(g => g.IsActive && !g.IsArchived && groupTypeIds.Contains(g.GroupTypeId));
                if (!groups.Any())
                {
                    nbAlert.Visible = true;
                    nbAlert.Text    = "Please configure this block.";
                    return;
                }

                var members = groups.SelectMany(g => g.Members);


                var gridData = new List <GridData>();
                AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);
                foreach (var person in familyMembers)
                {
                    //Get all the camp group members for this person
                    List <GroupMember> medicalMembers = members.Where(m => m.PersonId == person.Id).ToList();

                    if (!medicalMembers.Any())
                    {
                        continue;
                    }

                    GridData data = new GridData
                    {
                        Id          = person.PrimaryAlias.Guid,
                        Name        = person.FullName,
                        Group       = string.Join("<br>", medicalMembers.Select(m => m.Group.Name)),
                        Medications = "No Medication Information"
                    };
                    person.LoadAttributes();
                    var attribute       = person.GetAttributeValue(GetAttributeValue("MedicationMatrixKey"));
                    var attributeMatrix = attributeMatrixService.Get(attribute.AsGuid());
                    if (attributeMatrix != null)
                    {
                        var lava     = attributeMatrix.AttributeMatrixTemplate.FormattedLava;
                        var template = attributeMatrix.AttributeMatrixTemplate;
                        template.LoadAttributes();
                        AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem();
                        tempAttributeMatrixItem.AttributeMatrix = attributeMatrix;
                        tempAttributeMatrixItem.LoadAttributes();
                        Dictionary <string, object> mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, null, new Rock.Lava.CommonMergeFieldsOptions {
                            GetLegacyGlobalMergeFields = false
                        });
                        mergeFields.Add("AttributeMatrix", attributeMatrix);
                        mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name));
                        mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order));
                        var medications = lava.ResolveMergeFields(mergeFields);
                        data.Medications = medications;
                    }
                    gridData.Add(data);
                }

                gGrid.DataSource = gridData;
                gGrid.DataBind();
            }
        }
Example #13
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var sourceMatrixAttributeGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "SourceAttributeMatrix").AsGuid()).AsGuidOrNull();
            var targetMatrixAttributeGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "TargetAttributeMatrix").AsGuid()).AsGuidOrNull();

            if (sourceMatrixAttributeGuid.HasValue)
            {
                // Load the source matrix
                AttributeMatrix sourceMatrix = attributeMatrixService.Get(sourceMatrixAttributeGuid.Value);
                AttributeMatrix targetMatrix = null;

                if (targetMatrixAttributeGuid.HasValue)
                {
                    // Just delete all the existing items and add new items from the source attribute
                    targetMatrix = attributeMatrixService.Get(targetMatrixAttributeGuid.Value);
                    targetMatrix.AttributeMatrixItems.Clear();
                }
                else
                {
                    targetMatrix = new AttributeMatrix();
                    targetMatrix.AttributeMatrixItems      = new List <AttributeMatrixItem>();
                    targetMatrix.AttributeMatrixTemplateId = sourceMatrix.AttributeMatrixTemplateId;
                    attributeMatrixService.Add(targetMatrix);
                }

                // Now copy all the items from the source to the target
                foreach (var sourceItem in sourceMatrix.AttributeMatrixItems)
                {
                    var targetItem = new AttributeMatrixItem();
                    sourceItem.LoadAttributes();
                    targetItem.AttributeMatrix = targetMatrix;
                    targetItem.LoadAttributes();
                    foreach (var attribute in sourceItem.AttributeValues)
                    {
                        targetItem.SetAttributeValue(attribute.Key, attribute.Value.Value);
                    }
                    targetMatrix.AttributeMatrixItems.Add(targetItem);
                    rockContext.SaveChanges(true);
                    targetItem.SaveAttributeValues(rockContext);
                }


                // Now store the target attribute
                var targetAttribute = AttributeCache.Get(GetActionAttributeValue(action, "TargetAttributeMatrix").AsGuid(), rockContext);
                if (targetAttribute.EntityTypeId == new Rock.Model.Workflow().TypeId)
                {
                    action.Activity.Workflow.SetAttributeValue(targetAttribute.Key, targetMatrix.Guid.ToString());
                }
                else if (targetAttribute.EntityTypeId == new WorkflowActivity().TypeId)
                {
                    action.Activity.SetAttributeValue(targetAttribute.Key, targetMatrix.Guid.ToString());
                }
            }

            return(true);
        }