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);
        }
Esempio n. 2
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="value">The value.</param>
        public override void SetEditValue(Control control, Dictionary <string, ConfigurationValue> configurationValues, string value)
        {
            AttributeMatrixEditor attributeMatrixEditor = control as AttributeMatrixEditor;

            if (attributeMatrixEditor != null)
            {
                var rockContext = new RockContext();
                AttributeMatrixTemplate attributeMatrixTemplate = null;
                if (attributeMatrixEditor.AttributeMatrixTemplateId.HasValue)
                {
                    attributeMatrixTemplate = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrixEditor.AttributeMatrixTemplateId.Value);
                }

                if (attributeMatrixTemplate != null)
                {
                    var             attributeMatrixService = new AttributeMatrixService(rockContext);
                    AttributeMatrix attributeMatrix        = null;
                    Guid?           attributeMatrixGuid    = value.AsGuidOrNull();
                    if (attributeMatrixGuid.HasValue)
                    {
                        attributeMatrix = attributeMatrixService.Get(attributeMatrixGuid.Value);
                    }

                    if (attributeMatrix == null)
                    {
                        // Create the AttributeMatrix now and save it even though they haven't hit save yet. We'll need the AttributeMatrix record to exist so that we can add AttributeMatrixItems to it
                        // If this ends up creating an orphan, we can clean up it up later
                        attributeMatrix = new AttributeMatrix {
                            Guid = Guid.NewGuid()
                        };
                        attributeMatrix.AttributeMatrixTemplateId = attributeMatrixEditor.AttributeMatrixTemplateId.Value;
                        attributeMatrix.AttributeMatrixItems      = new List <AttributeMatrixItem>();
                        attributeMatrixService.Add(attributeMatrix);
                        rockContext.SaveChanges();
                    }

                    // If the AttributeMatrixTemplateId jwas changed since the last time the attributeMatrix was saved, change it and wipe out the items
                    if (attributeMatrix.AttributeMatrixTemplateId != attributeMatrixEditor.AttributeMatrixTemplateId.Value)
                    {
                        attributeMatrix.AttributeMatrixTemplateId = attributeMatrixEditor.AttributeMatrixTemplateId.Value;

                        var attributeMatrixItemService = new AttributeMatrixItemService(rockContext);

                        // 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.
                        foreach (var attributeMatrixItem in attributeMatrix.AttributeMatrixItems.ToList())
                        {
                            attributeMatrixItemService.Delete(attributeMatrixItem);
                        }

                        attributeMatrix.AttributeMatrixItems.Clear();
                        rockContext.SaveChanges();
                    }

                    attributeMatrixEditor.AttributeMatrixGuid = attributeMatrix.Guid;
                }
            }
        }
Esempio n. 3
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);
            }
        }