Beispiel #1
0
        /// <summary>
        /// Saves the attributes.
        /// </summary>
        /// <param name="channelId">The channel identifier.</param>
        /// <param name="entityTypeId">The entity type identifier.</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="rockContext">The rock context.</param>
        private void SaveAttributes(int channelId, int entityTypeId, List <Attribute> attributes, RockContext rockContext)
        {
            string qualifierColumn = "ContentChannelId";
            string qualifierValue  = channelId.ToString();

            AttributeService attributeService = new AttributeService(rockContext);

            // Get the existing attributes for this entity type and qualifier value
            var existingAttributes = attributeService.GetByEntityTypeQualifier(entityTypeId, qualifierColumn, qualifierValue, true);

            // Delete any of those attributes that were removed in the UI
            var selectedAttributeGuids = attributes.Select(a => a.Guid);

            foreach (var attr in existingAttributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
            {
                attributeService.Delete(attr);
            }

            rockContext.SaveChanges();

            int newOrder = 1000;

            // Update the Attributes that were assigned in the UI
            foreach (var attr in attributes.OrderBy(a => a.Order))
            {
                // Artificially exaggerate the order so that all channel specific attributes are displayed after the content-type specific attributes (unless categorized)
                attr.Order = newOrder++;
                Rock.Attribute.Helper.SaveAttributeEdits(attr, entityTypeId, qualifierColumn, qualifierValue, rockContext);
            }
        }
        /// <summary>
        /// Saves the attributes.
        /// </summary>
        /// <param name="contentTypeId">The content type identifier.</param>
        /// <param name="entityTypeId">The entity type identifier.</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="rockContext">The rock context.</param>
        private void SaveAttributes(int contentTypeId, int entityTypeId, List <Attribute> attributes, RockContext rockContext)
        {
            string qualifierColumn = "ContentChannelTypeId";
            string qualifierValue  = contentTypeId.ToString();

            AttributeService attributeService = new AttributeService(rockContext);

            // Get the existing attributes for this entity type and qualifier value
            var existingAttributes = attributeService.GetByEntityTypeQualifier(entityTypeId, qualifierColumn, qualifierValue, true);

            // Delete any of those attributes that were removed in the UI
            var selectedAttributeGuids = attributes.Select(a => a.Guid);

            foreach (var attr in existingAttributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
            {
                attributeService.Delete(attr);
            }

            rockContext.SaveChanges();

            // Update the Attributes that were assigned in the UI
            foreach (var attr in attributes)
            {
                Rock.Attribute.Helper.SaveAttributeEdits(attr, entityTypeId, qualifierColumn, qualifierValue, rockContext);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Saves the attribute value.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="fieldType">Type of the field.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="qualifiers">The qualifiers.</param>
        /// <returns></returns>
        private static bool SaveAttributeValue(Rock.Model.Workflow workflow, string key, string value,
                                               FieldTypeCache fieldType, RockContext rockContext, Dictionary <string, string> qualifiers = null)
        {
            bool createdNewAttribute = false;

            if (workflow.Attributes.ContainsKey(key))
            {
                workflow.SetAttributeValue(key, value);
            }
            else
            {
                // Read the attribute
                var attributeService = new AttributeService(rockContext);
                var attribute        = attributeService
                                       .GetByEntityTypeQualifier(workflow.TypeId, "WorkflowTypeId", workflow.WorkflowTypeId.ToString(), true)
                                       .Where(a => a.Key == key)
                                       .FirstOrDefault();

                // If workflow attribute doesn't exist, create it
                // ( should only happen first time a background check is processed for given workflow type)
                if (attribute == null)
                {
                    attribute = new Rock.Model.Attribute();
                    attribute.EntityTypeId = workflow.TypeId;
                    attribute.EntityTypeQualifierColumn = "WorkflowTypeId";
                    attribute.EntityTypeQualifierValue  = workflow.WorkflowTypeId.ToString();
                    attribute.Name        = key.SplitCase();
                    attribute.Key         = key;
                    attribute.FieldTypeId = fieldType.Id;
                    attributeService.Add(attribute);

                    if (qualifiers != null)
                    {
                        foreach (var keyVal in qualifiers)
                        {
                            var qualifier = new Rock.Model.AttributeQualifier();
                            qualifier.Key   = keyVal.Key;
                            qualifier.Value = keyVal.Value;
                            attribute.AttributeQualifiers.Add(qualifier);
                        }
                    }

                    createdNewAttribute = true;
                }

                // Set the value for this action's instance to the current time
                var attributeValue = new Rock.Model.AttributeValue();
                attributeValue.Attribute = attribute;
                attributeValue.EntityId  = workflow.Id;
                attributeValue.Value     = value;
                new AttributeValueService(rockContext).Add(attributeValue);
            }

            return(createdNewAttribute);
        }
Beispiel #4
0
        /// <summary>
        /// Gets the query for all attributes that will be presented in the list.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <returns>A queryable that will enumerate to all the attributes.</returns>
        private IQueryable <Rock.Model.Attribute> GetAttributeQuery(RockContext rockContext, Guid?entityTypeGuid)
        {
            IQueryable <Rock.Model.Attribute> query = null;
            AttributeService attributeService       = new AttributeService(rockContext);

            if (entityTypeGuid.HasValue)
            {
                if (entityTypeGuid.Value == default)
                {
                    // entity type not configured in block or in filter, so get Global Attributes
                    query = attributeService.GetByEntityTypeId(null, true);
                    query = query.Where(t => t.EntityTypeQualifierColumn == null || t.EntityTypeQualifierColumn == "");
                }
                else if (GetAttributeValue(AttributeKey.Entity).AsGuidOrNull().HasValue)
                {
                    // entity type is configured in block, so get by the entityType and qualifiers specified in the block settings
                    var entityTypeCache       = EntityTypeCache.Get(entityTypeGuid.Value);
                    var entityQualifierColumn = GetAttributeValue(AttributeKey.EntityQualifierColumn);
                    var entityQualifierValue  = GetAttributeValue(AttributeKey.EntityQualifierValue);
                    query = attributeService.GetByEntityTypeQualifier(entityTypeCache.Id, entityQualifierColumn, entityQualifierValue, true);
                }
                else
                {
                    // entity type is selected in the filter, so get all the attributes for that entityType. (There is no userfilter for qualifiers, so don't filter by those)
                    var entityTypeCache = EntityTypeCache.Get(entityTypeGuid.Value);
                    query = attributeService.GetByEntityTypeId(entityTypeCache.Id, true);
                }
            }

            // if filtering by block setting of categories
            if (!string.IsNullOrWhiteSpace(GetAttributeValue(AttributeKey.CategoryFilter)))
            {
                try
                {
                    var categoryGuids = GetAttributeValue(AttributeKey.CategoryFilter).Split(',').Select(Guid.Parse).ToList();

                    query = query.Where(a => a.Categories.Any(c => categoryGuids.Contains(c.Guid)));
                }
                catch { }
            }

            query = query.OrderBy(a => a.Order);

            return(query);
        }
Beispiel #5
0
        /// <summary>
        /// Saves the attributes.
        /// </summary>
        /// <param name="entityTypeId">The entity type identifier.</param>
        /// <param name="qualifierColumn">The qualifier column.</param>
        /// <param name="qualifierValue">The qualifier value.</param>
        /// <param name="viewStateAttributes">The view state attributes.</param>
        /// <param name="rockContext">The rock context.</param>
        private void SaveAttributes(int entityTypeId, string qualifierColumn, string qualifierValue, List <Attribute> viewStateAttributes, RockContext rockContext)
        {
            // Get the existing attributes for this entity type and qualifier value
            var attributeService = new AttributeService(rockContext);
            var attributes       = attributeService.GetByEntityTypeQualifier(entityTypeId, qualifierColumn, qualifierValue, true);

            // Delete any of those attributes that were removed in the UI
            var selectedAttributeGuids = viewStateAttributes.Select(a => a.Guid);

            foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
            {
                attributeService.Delete(attr);
                rockContext.SaveChanges();
            }

            // Update the Attributes that were assigned in the UI
            foreach (var attributeState in viewStateAttributes)
            {
                Helper.SaveAttributeEdits(attributeState, entityTypeId, qualifierColumn, qualifierValue, rockContext);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Shows the edit.
        /// </summary>
        /// <param name="attributeId">The attribute id.</param>
        protected void ShowEdit(int attributeId)
        {
            var rockContext      = new RockContext();
            var attributeService = new AttributeService(rockContext);
            var attributeModel   = attributeService.Get(attributeId);

            if (attributeModel == null)
            {
                mdAttribute.Title = "Add Attribute".FormatAsHtmlTitle();

                attributeModel             = new Rock.Model.Attribute();
                attributeModel.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.TEXT).Id;

                if (!_isEntityTypeConfigured)
                {
                    int entityTypeId = int.MinValue;
                    if (int.TryParse(rFilter.GetUserPreference("Entity Type"), out entityTypeId) && entityTypeId > 0)
                    {
                        attributeModel.EntityTypeId = entityTypeId;
                    }
                }
                else
                {
                    attributeModel.EntityTypeId = _entityTypeId;
                    attributeModel.EntityTypeQualifierColumn = _entityQualifierColumn;
                    attributeModel.EntityTypeQualifierValue  = _entityQualifierValue;
                }

                List <int> selectedCategoryIds = cpCategoriesFilter.SelectedValuesAsInt().ToList();
                new CategoryService(rockContext).Queryable().Where(c => selectedCategoryIds.Contains(c.Id)).ToList().ForEach(c =>
                                                                                                                             attributeModel.Categories.Add(c));
                edtAttribute.ActionTitle = Rock.Constants.ActionTitle.Add(Rock.Model.Attribute.FriendlyTypeName);
            }
            else
            {
                if (attributeModel.EntityType != null && attributeModel.EntityType.IsIndexingSupported == true && attributeModel.EntityType.IsIndexingEnabled)
                {
                    edtAttribute.IsIndexingEnabledVisible = true;
                }

                edtAttribute.ActionTitle = Rock.Constants.ActionTitle.Edit(Rock.Model.Attribute.FriendlyTypeName);
                mdAttribute.Title        = ("Edit " + attributeModel.Name).FormatAsHtmlTitle();

                edtAttribute.IsIndexingEnabled = attributeModel.IsIndexEnabled;
            }

            Type type = null;

            if (attributeModel.EntityTypeId.HasValue && attributeModel.EntityTypeId > 0)
            {
                type = EntityTypeCache.Get(attributeModel.EntityTypeId.Value).GetEntityType();
            }
            edtAttribute.ReservedKeyNames = attributeService.GetByEntityTypeQualifier(attributeModel.EntityTypeId, attributeModel.EntityTypeQualifierColumn, attributeModel.EntityTypeQualifierValue, true)
                                            .Where(a => a.Id != attributeId)
                                            .Select(a => a.Key)
                                            .Distinct()
                                            .ToList();
            edtAttribute.SetAttributeProperties(attributeModel, type);
            edtAttribute.AttributeEntityTypeId = attributeModel.EntityTypeId;

            if (_isEntityTypeConfigured)
            {
                pnlEntityTypeQualifier.Visible = false;
            }
            else
            {
                pnlEntityTypeQualifier.Visible = true;

                ddlAttrEntityType.SetValue(attributeModel.EntityTypeId.HasValue ? attributeModel.EntityTypeId.Value.ToString() : "0");
                tbAttrQualifierField.Text = attributeModel.EntityTypeQualifierColumn;
                tbAttrQualifierValue.Text = attributeModel.EntityTypeQualifierValue;
            }

            ShowDialog("Attribute", true);
        }
Beispiel #7
0
        /// <summary>
        /// Gets the data.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        private IQueryable <Rock.Model.Attribute> GetData(RockContext rockContext)
        {
            IQueryable <Rock.Model.Attribute> query = null;

            AttributeService attributeService = new AttributeService(rockContext);

            if (_entityTypeId.HasValue)
            {
                if (_entityTypeId == default(int))
                {
                    // entity type not configured in block or in filter, so get Global Attributes
                    query = attributeService.GetByEntityTypeId(null, true);
                    query = query.Where(t => t.EntityTypeQualifierColumn == null || t.EntityTypeQualifierColumn == "");
                }
                else if (_isEntityTypeConfigured)
                {
                    // entity type is configured in block, so get by the entityType and qualifiers specified in the block settings
                    query = attributeService.GetByEntityTypeQualifier(_entityTypeId, _entityQualifierColumn, _entityQualifierValue, true);
                }
                else
                {
                    // entity type is selected in the filter, so get all the attributes for that entityType. (There is no userfilter for qualifiers, so don't filter by those)
                    query = attributeService.GetByEntityTypeId(_entityTypeId, true);
                }
            }

            // if filtering by block setting of categories
            if (!string.IsNullOrWhiteSpace(GetAttributeValue("CategoryFilter")))
            {
                try
                {
                    var categoryGuids = GetAttributeValue("CategoryFilter").Split(',').Select(Guid.Parse).ToList();

                    query = query.Where(a => a.Categories.Any(c => categoryGuids.Contains(c.Guid)));
                }
                catch { }
            }

            if (ddlAnalyticsEnabled.Visible)
            {
                var filterValue = ddlAnalyticsEnabled.SelectedValue.AsBooleanOrNull();
                if (filterValue.HasValue)
                {
                    query = query.Where(a => (a.IsAnalytic || a.IsAnalyticHistory) == filterValue);
                }
            }

            if (ddlActiveFilter.Visible)
            {
                var filterValue = ddlActiveFilter.SelectedValue.AsBooleanOrNull();
                if (filterValue.HasValue)
                {
                    query = query.Where(a => a.IsActive == filterValue);
                }
            }

            var selectedCategoryIds = new List <int>();

            rFilter.GetUserPreference("Categories").SplitDelimitedValues().ToList().ForEach(s => selectedCategoryIds.Add(int.Parse(s)));
            if (selectedCategoryIds.Any())
            {
                query = query.Where(a => a.Categories.Any(c => selectedCategoryIds.Contains(c.Id)));
            }

            query = query.OrderBy(a => a.Order);

            return(query);
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BinaryFileType binaryFileType;

            var rockContext = new RockContext();
            BinaryFileTypeService     binaryFileTypeService     = new BinaryFileTypeService(rockContext);
            AttributeService          attributeService          = new AttributeService(rockContext);
            AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext);
            CategoryService           categoryService           = new CategoryService(rockContext);

            int binaryFileTypeId = int.Parse(hfBinaryFileTypeId.Value);

            if (binaryFileTypeId == 0)
            {
                binaryFileType = new BinaryFileType();
                binaryFileTypeService.Add(binaryFileType);
            }
            else
            {
                binaryFileType = binaryFileTypeService.Get(binaryFileTypeId);
            }

            binaryFileType.Name                       = tbName.Text;
            binaryFileType.Description                = tbDescription.Text;
            binaryFileType.IconCssClass               = tbIconCssClass.Text;
            binaryFileType.CacheToServerFileSystem    = cbCacheToServerFileSystem.Checked;
            binaryFileType.CacheControlHeaderSettings = cpCacheSettings.CurrentCacheablity.ToJson();
            binaryFileType.RequiresViewSecurity       = cbRequiresViewSecurity.Checked;
            binaryFileType.MaxWidth                   = nbMaxWidth.Text.AsInteger();
            binaryFileType.MaxHeight                  = nbMaxHeight.Text.AsInteger();
            binaryFileType.PreferredFormat            = ddlPreferredFormat.SelectedValueAsEnum <Format>();
            binaryFileType.PreferredResolution        = ddlPreferredResolution.SelectedValueAsEnum <Resolution>();
            binaryFileType.PreferredColorDepth        = ddlPreferredColorDepth.SelectedValueAsEnum <ColorDepth>();
            binaryFileType.PreferredRequired          = cbPreferredRequired.Checked;

            if (!string.IsNullOrWhiteSpace(cpStorageType.SelectedValue))
            {
                var entityTypeService = new EntityTypeService(rockContext);
                var storageEntityType = entityTypeService.Get(new Guid(cpStorageType.SelectedValue));

                if (storageEntityType != null)
                {
                    binaryFileType.StorageEntityTypeId = storageEntityType.Id;
                }
            }

            binaryFileType.LoadAttributes(rockContext);
            Rock.Attribute.Helper.GetEditValues(phAttributes, binaryFileType);

            if (!binaryFileType.IsValid)
            {
                // Controls will render the error messages
                return;
            }

            rockContext.WrapTransaction(() =>
            {
                rockContext.SaveChanges();

                // get it back to make sure we have a good Id for it for the Attributes
                binaryFileType = binaryFileTypeService.Get(binaryFileType.Guid);

                /* Take care of Binary File Attributes */
                var entityTypeId = EntityTypeCache.Get(typeof(BinaryFile)).Id;

                // delete BinaryFileAttributes that are no longer configured in the UI
                var attributes             = attributeService.GetByEntityTypeQualifier(entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), true);
                var selectedAttributeGuids = BinaryFileAttributesState.Select(a => a.Guid);
                foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
                {
                    attributeService.Delete(attr);
                }

                rockContext.SaveChanges();

                // add/update the BinaryFileAttributes that are assigned in the UI
                foreach (var attributeState in BinaryFileAttributesState)
                {
                    Rock.Attribute.Helper.SaveAttributeEdits(attributeState, entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), rockContext);
                }

                // SaveAttributeValues for the BinaryFileType
                binaryFileType.SaveAttributeValues(rockContext);
            });

            NavigateToParentPage();
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="attributeMatrixTemplateId">The attribute matrix template identifier.</param>
        public void ShowDetail(int attributeMatrixTemplateId)
        {
            pnlView.Visible = true;

            AttributeMatrixTemplate attributeMatrixTemplate = null;
            var rockContext = new RockContext();

            if (!attributeMatrixTemplateId.Equals(0))
            {
                attributeMatrixTemplate = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrixTemplateId);
                lActionTitle.Text       = ActionTitle.Edit(AttributeMatrixTemplate.FriendlyTypeName).FormatAsHtmlTitle();
                pdAuditDetails.SetEntity(attributeMatrixTemplate, ResolveRockUrl("~"));
            }

            if (attributeMatrixTemplate == null)
            {
                attributeMatrixTemplate = new AttributeMatrixTemplate {
                    Id = 0
                };
                attributeMatrixTemplate.FormattedLava = AttributeMatrixTemplate.FormattedLavaDefault;

                lActionTitle.Text = ActionTitle.Add(AttributeMatrixTemplate.FriendlyTypeName).FormatAsHtmlTitle();

                // hide the panel drawer that show created and last modified dates
                pdAuditDetails.Visible = false;
            }

            hfAttributeMatrixTemplateId.Value = attributeMatrixTemplate.Id.ToString();
            tbName.Text          = attributeMatrixTemplate.Name;
            cbIsActive.Checked   = attributeMatrixTemplate.IsActive;
            tbDescription.Text   = attributeMatrixTemplate.Description;
            tbMinimumRows.Text   = attributeMatrixTemplate.MinimumRows.ToString();
            tbMaximumRows.Text   = attributeMatrixTemplate.MaximumRows.ToString();
            ceFormattedLava.Text = attributeMatrixTemplate.FormattedLava;

            var attributeService = new AttributeService(rockContext);

            AttributesState = attributeService.GetByEntityTypeQualifier(new AttributeMatrixItem().TypeId, "AttributeMatrixTemplateId", attributeMatrixTemplate.Id.ToString(), true).AsQueryable()
                              .OrderBy(a => a.Order)
                              .ThenBy(a => a.Name)
                              .ToList();

            BindAttributesGrid();

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if (!IsUserAuthorized(Authorization.EDIT))
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(AttributeMatrixTemplate.FriendlyTypeName);
            }

            if (readOnly)
            {
                lActionTitle.Text = ActionTitle.View(AttributeMatrixTemplate.FriendlyTypeName);
                btnCancel.Text    = "Close";
            }

            tbName.ReadOnly          = readOnly;
            cbIsActive.Enabled       = !readOnly;
            tbDescription.ReadOnly   = readOnly;
            tbMinimumRows.ReadOnly   = readOnly;
            tbMaximumRows.ReadOnly   = readOnly;
            ceFormattedLava.ReadOnly = readOnly;
            btnSave.Visible          = !readOnly;
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            AttributeMatrixTemplate attributeMatrixTemplate;
            var rockContext = new RockContext();
            var attributeMatrixTemplateService = new AttributeMatrixTemplateService(rockContext);

            int attributeMatrixTemplateId = int.Parse(hfAttributeMatrixTemplateId.Value);

            if (attributeMatrixTemplateId == 0)
            {
                attributeMatrixTemplate = new AttributeMatrixTemplate();
                attributeMatrixTemplateService.Add(attributeMatrixTemplate);
            }
            else
            {
                attributeMatrixTemplate = attributeMatrixTemplateService.Get(attributeMatrixTemplateId);
            }

            attributeMatrixTemplate.Name          = tbName.Text;
            attributeMatrixTemplate.IsActive      = cbIsActive.Checked;
            attributeMatrixTemplate.Description   = tbDescription.Text;
            attributeMatrixTemplate.MinimumRows   = tbMinimumRows.Text.AsIntegerOrNull();
            attributeMatrixTemplate.MaximumRows   = tbMaximumRows.Text.AsIntegerOrNull();
            attributeMatrixTemplate.FormattedLava = ceFormattedLava.Text;

            // need WrapTransaction due to Attribute saves
            rockContext.WrapTransaction(() =>
            {
                rockContext.SaveChanges();

                /* Save Attributes */

                var entityTypeIdAttributeMatrix = EntityTypeCache.GetId <AttributeMatrixItem>();

                // Get the existing attributes for this entity type and qualifier value
                var attributeService = new AttributeService(rockContext);
                var regFieldService  = new RegistrationTemplateFormFieldService(rockContext);
                var attributes       = attributeService.GetByEntityTypeQualifier(entityTypeIdAttributeMatrix, "AttributeMatrixTemplateId", attributeMatrixTemplate.Id.ToString(), true);

                // Delete any of those attributes that were removed in the UI
                var selectedAttributeGuids = AttributesState.Select(a => a.Guid);
                foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
                {
                    foreach (var field in regFieldService.Queryable().Where(f => f.AttributeId.HasValue && f.AttributeId.Value == attr.Id).ToList())
                    {
                        regFieldService.Delete(field);
                    }

                    attributeService.Delete(attr);
                    rockContext.SaveChanges();
                }

                // Update the Attributes that were assigned in the UI
                foreach (var attributeState in AttributesState)
                {
                    Helper.SaveAttributeEdits(attributeState, entityTypeIdAttributeMatrix, "AttributeMatrixTemplateId", attributeMatrixTemplate.Id.ToString(), rockContext);
                }
            });

            NavigateToParentPage();
        }
Beispiel #11
0
        private static List <BreakoutGroupItem> GetBreakoutGroupItems(Person person)
        {
            List <BreakoutGroupItem> allBreakoutGroupItems = RockCache.Get(cacheKey) as List <BreakoutGroupItem>;

            if (allBreakoutGroupItems == null || !allBreakoutGroupItems.Any())
            {
                allBreakoutGroupItems = new List <BreakoutGroupItem>();
                RockContext rockContext = new RockContext();

                var attributeService      = new AttributeService(rockContext);
                var attributeValueService = new AttributeValueService(rockContext);

                var groupEntityTypeId        = EntityTypeCache.GetId(typeof(Rock.Model.Group));
                var breakoutGroupGroupTypeId = GroupTypeCache.GetId(Constants.GROUP_TYPE_BREAKOUT_GROUPS.AsGuid());

                if (groupEntityTypeId == null || breakoutGroupGroupTypeId == null)
                {
                    ExceptionLogService.LogException(new Exception("Could not load breakout groups due to missing breakoutgroup type"));
                    return(new List <BreakoutGroupItem>());
                }

                var letterAttribute = attributeService.GetByEntityTypeQualifier(groupEntityTypeId, "GroupTypeId", breakoutGroupGroupTypeId.Value.ToString(), false)
                                      .Where(a => a.Key == Constants.GROUP_ATTRIBUTE_KEY_LETTER)
                                      .FirstOrDefault();

                if (letterAttribute == null)
                {
                    ExceptionLogService.LogException(new Exception("Could not load breakout group letter attribute."));
                    return(new List <BreakoutGroupItem>());
                }

                var attributeQueryable = attributeValueService.Queryable().AsNoTracking()
                                         .Where(av => letterAttribute.Id == av.AttributeId);

                //Get all the data in one go
                var breakoutData = new GroupService(rockContext)
                                   .Queryable()
                                   .AsNoTracking()
                                   .Where(g => g.GroupTypeId == breakoutGroupGroupTypeId && g.IsActive && !g.IsArchived)
                                   .Join(attributeQueryable,
                                         g => g.Id,
                                         av => av.EntityId,
                                         (g, av) => new
                {
                    ScheduleId = g.ScheduleId ?? 0,
                    PersonIds  = g.Members.Where(gm => gm.IsArchived == false && gm.GroupMemberStatus == GroupMemberStatus.Active).Select(gm => gm.PersonId),
                    Letter     = av.Value
                })
                                   .ToList();

                foreach (var item in breakoutData)
                {
                    foreach (var personId in item.PersonIds)
                    {
                        allBreakoutGroupItems.Add(new BreakoutGroupItem
                        {
                            Letter     = item.Letter,
                            PersonId   = personId,
                            ScheduleId = item.ScheduleId
                        });
                    }
                }

                RockCache.AddOrUpdate(cacheKey, null, allBreakoutGroupItems, RockDateTime.Now.AddMinutes(10), Constants.CACHE_TAG);
            }

            return(allBreakoutGroupItems.Where(i => i.PersonId == person.Id).ToList());
        }