Пример #1
0
        /// <summary>
        /// Loads the prerequisite list.
        /// </summary>
        private void SyncPrerequisiteList()
        {
            var config = GetAchievementConfiguration();
            var achievementTypeCache = GetAchievementTypeCache();
            var isNew = IsAddMode();

            List <AchievementTypeCache> eligiblePrerequisites;

            if (isNew)
            {
                eligiblePrerequisites = config == null ?
                                        new List <AchievementTypeCache>() :
                                        AchievementTypeService.GetEligiblePrerequisiteAchievementTypeCachesForNewAchievement(config.AchieverEntityTypeCache.Id);
            }
            else
            {
                eligiblePrerequisites = AchievementTypeService.GetEligiblePrerequisiteAchievementTypeCaches(achievementTypeCache);
            }

            cblPrerequsities.DataSource = eligiblePrerequisites;
            cblPrerequsities.DataBind();
            cblPrerequsities.Visible = eligiblePrerequisites.Any();

            if (!isNew)
            {
                cblPrerequsities.SetValues(achievementTypeCache.Prerequisites.Select(statp => statp.PrerequisiteAchievementTypeId));
            }
        }
Пример #2
0
        /// <summary>
        /// Save the current record.
        /// </summary>
        /// <returns></returns>
        private void SaveRecord()
        {
            var rockContext            = GetRockContext();
            var achievementType        = GetAchievementType();
            var achievementTypeService = GetAchievementTypeService();
            var isNew = achievementType == null;

            if (isNew)
            {
                achievementType = new AchievementType();
                achievementTypeService.Add(achievementType);
                achievementType.ComponentEntityTypeId = cpAchievementComponent.SelectedEntityTypeId ?? 0;

                var configuration = GetAchievementConfiguration();

                if (configuration != null)
                {
                    achievementType.SourceEntityTypeId   = configuration.SourceEntityTypeCache.Id;
                    achievementType.AchieverEntityTypeId = configuration.AchieverEntityTypeCache.Id;
                }
            }

            achievementType.Name                             = tbName.Text;
            achievementType.Description                      = tbDescription.Text;
            achievementType.IsActive                         = cbActive.Checked;
            achievementType.IsPublic                         = cbIsPublic.Checked;
            achievementType.AchievementIconCssClass          = tbIconCssClass.Text;
            achievementType.MaxAccomplishmentsAllowed        = cbAllowOverachievement.Checked ? 1 : nbMaxAccomplishments.IntegerValue;
            achievementType.AllowOverAchievement             = cbAllowOverachievement.Checked;
            achievementType.AchievementStartWorkflowTypeId   = wtpStartWorkflowType.SelectedValueAsInt();
            achievementType.AchievementSuccessWorkflowTypeId = wtpSuccessWorkflowType.SelectedValueAsInt();
            achievementType.AchievementFailureWorkflowTypeId = wtpFailureWorkflowType.SelectedValueAsInt();
            achievementType.BadgeLavaTemplate                = ceBadgeLava.Text;
            achievementType.ResultsLavaTemplate              = ceResultsLava.Text;

            var binaryFileService = new BinaryFileService(rockContext);

            if (achievementType.ImageBinaryFileId != imgupImageBinaryFile.BinaryFileId)
            {
                var oldImageTemplatePreview = binaryFileService.Get(achievementType.ImageBinaryFileId ?? 0);
                if (oldImageTemplatePreview != null)
                {
                    // the old image won't be needed anymore, so make it IsTemporary and have it get cleaned up later
                    oldImageTemplatePreview.IsTemporary = true;
                }
            }

            achievementType.ImageBinaryFileId = imgupImageBinaryFile.BinaryFileId;

            // Ensure that the Image is not set as IsTemporary=True
            if (achievementType.ImageBinaryFileId.HasValue)
            {
                var imageTemplatePreview = binaryFileService.Get(achievementType.ImageBinaryFileId.Value);
                if (imageTemplatePreview != null && imageTemplatePreview.IsTemporary)
                {
                    imageTemplatePreview.IsTemporary = false;
                }
            }

            achievementType.CustomSummaryLavaTemplate = ceCustomSummaryLavaTemplate.Text;

            achievementType.CategoryId = cpCategory.SelectedValueAsInt();

            // Both step type and status are required together or neither can be set
            var stepTypeId   = spstStepType.StepTypeId;
            var stepStatusId = sspStepStatus.SelectedValueAsInt();

            if (cbAddStep.Checked && stepTypeId.HasValue && stepStatusId.HasValue)
            {
                achievementType.AchievementStepTypeId   = stepTypeId;
                achievementType.AchievementStepStatusId = stepStatusId;
            }
            else
            {
                achievementType.AchievementStepTypeId   = null;
                achievementType.AchievementStepStatusId = null;
            }

            // Upsert Prerequisites
            var prerequisiteService = new AchievementTypePrerequisiteService(rockContext);
            var selectedPrerequisiteAchievementTypeIds = cblPrerequsities.SelectedValuesAsInt;

            // Remove existing prerequisites that are not selected
            var removePrerequisiteAchievementTypes = achievementType.Prerequisites
                                                     .Where(statp => !selectedPrerequisiteAchievementTypeIds.Contains(statp.PrerequisiteAchievementTypeId)).ToList();

            foreach (var prerequisite in removePrerequisiteAchievementTypes)
            {
                achievementType.Prerequisites.Remove(prerequisite);
                prerequisiteService.Delete(prerequisite);
            }

            // Add selected achievement types prerequisites that are not existing
            var addPrerequisiteAchievementTypeIds = selectedPrerequisiteAchievementTypeIds
                                                    .Where(statId => !achievementType.Prerequisites.Any(statp => statp.PrerequisiteAchievementTypeId == statId));

            foreach (var prerequisiteAchievementTypeId in addPrerequisiteAchievementTypeIds)
            {
                achievementType.Prerequisites.Add(new AchievementTypePrerequisite
                {
                    AchievementTypeId             = achievementType.Id,
                    PrerequisiteAchievementTypeId = prerequisiteAchievementTypeId
                });
            }

            // Validate Prerequisites.
            // This is necessary because other Achievement Types may have been modified after this record edit was started.
            if (!isNew)
            {
                var achievementTypeCache     = GetAchievementTypeCache();
                var eligibleAchievementTypes = AchievementTypeService.GetEligiblePrerequisiteAchievementTypeCaches(achievementTypeCache);

                foreach (var prerequisite in achievementType.Prerequisites)
                {
                    if (!eligibleAchievementTypes.Any(stat => stat.Id == prerequisite.PrerequisiteAchievementTypeId))
                    {
                        cvCustomValidator.IsValid      = false;
                        cvCustomValidator.ErrorMessage = string.Format(
                            "This achievement type cannot have prerequisite \"{0}\" because it would create a circular dependency.",
                            prerequisite.PrerequisiteAchievementType.Name);
                        return;
                    }
                }
            }

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

            try
            {
                rockContext.SaveChanges();

                if (!achievementType.IsAuthorized(Authorization.VIEW, CurrentPerson))
                {
                    achievementType.AllowPerson(Authorization.VIEW, CurrentPerson, rockContext);
                }

                if (!achievementType.IsAuthorized(Authorization.EDIT, CurrentPerson))
                {
                    achievementType.AllowPerson(Authorization.EDIT, CurrentPerson, rockContext);
                }

                if (!achievementType.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson))
                {
                    achievementType.AllowPerson(Authorization.ADMINISTRATE, CurrentPerson, rockContext);
                }
            }
            catch (Exception ex)
            {
                ShowBlockException(nbEditModeMessage, ex);
                return;
            }

            achievementType.LoadAttributes(rockContext);
            avcComponentAttributes.GetEditValues(achievementType);
            achievementType.SaveAttributeValues(rockContext);

            // Now that the component attributes are saved, generate the config JSON from the component
            var updatedCacheItem = AchievementTypeCache.Get(achievementType.Id);
            var component        = updatedCacheItem.AchievementComponent;
            var configDictionary = component.GenerateConfigFromAttributeValues(updatedCacheItem);

            achievementType.ComponentConfigJson = configDictionary.ToJson();
            rockContext.SaveChanges();

            // If the save was successful, reload the page using the new record Id.
            NavigateToPage(RockPage.Guid, new Dictionary <string, string> {
                { PageParameterKey.AchievementTypeId, achievementType.Id.ToString() }
            });
        }