Пример #1
0
        private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
        {
            
            var documentToMediaRelationType = relationService.GetRelationTypeByAlias(documentToMediaRelationTypeAlias);
            var documentToDocumentRelationType = relationService.GetRelationTypeByAlias(documentToDocumentRelationTypeAlias);


            var entitiesSaved = e.SavedEntities;

            foreach (var entity in entitiesSaved)
            {
                //Deletes all relation for parentID(entityID) before going on to recreate them later in the function
                foreach (var relation in relationService.GetByParentId(entity.Id))
                {
                    relationService.Delete(relation);
                }
                foreach (var prop in entity.Properties)
                {
                    try
                    {
                        if (prop.PropertyType.Name.Equals("contentBlocks") || prop.PropertyType.Name.Equals("FeatureBlocks"))
                        {
                            var propValue = prop?.GetValue();
                            if (propValue != null)
                            {
                                //processArchetypeBlocks(prop, entity);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        if (/*getMediaContentPropertyAliases().Contains(prop.PropertyType.Name)*/true)
                        {
                            var propValue = prop.GetValue();

                            if (propValue == null) continue;

                            processRelations(prop, entity);
                        }
                    }
                    catch (Exception msg)
                    {
                        Current.Logger.Error(GetType(), "\n\nException while creating relation. Content id: " + entity.Id+". Entity  type: "+ entity.ContentType.Alias+". Property: "+prop?.Alias+". Property type: "+prop?.PropertyType.Alias,msg);
                        continue;
                    }
                }
            }
        }
Пример #2
0
        public void SaveRelations(ContentRelationsDto contentRelations)
        {
            if (contentRelations.Sets == null || !contentRelations.Sets.Any())
            {
                return;
            }

            var relations    = relationService.GetByParentId(contentRelations.ParentId).ToList();
            var parentEntity = entityService.Get(contentRelations.ParentId, contentRelations.ParentType);

            foreach (var set in contentRelations.Sets)
            {
                var typeId       = set.RelationTypeId;
                var type         = relationService.GetRelationTypeById(set.RelationTypeId);
                var setRelations = relations.Where(r => r.RelationTypeId == typeId);
                foreach (var removeRelation in setRelations)
                {
                    relationService.Delete(removeRelation);
                }

                foreach (var relation in set.Relations)
                {
                    if (relation.State == RelationStateEnum.Deleted || relation.Readonly)
                    {
                        continue;
                    }

                    var childEntity = entityService.Get(relation.ChildId, UmbracoObjectTypesExtensions.GetUmbracoObjectType(type.ChildObjectType));
                    relationService.Relate(parentEntity, childEntity, type);
                }
            }
        }
Пример #3
0
        private static void ContentService_Saved(IContentService contentService, ContentSavedEventArgs e)
        {
            foreach (IContent content in e.SavedEntities)
            {
                List <string> editors = new List <string>
                {
                    Umbraco.Core.Constants.PropertyEditors.Aliases.Grid,
                    BentoItemDataEditor.EditorAlias,
                    BentoStackDataEditor.EditorAlias
                };

                foreach (Property contentProperty in content.Properties.Where(x => editors.Contains(x.PropertyType.PropertyEditorAlias)))
                {
                    IDataType editor = DataTypeService.GetDataType(contentProperty.PropertyType.DataTypeId);

                    IRelationType bentoBlocksRelationType = RelationService.GetRelationTypeByAlias(RelationTypes.BentoItemsAlias);

                    if (bentoBlocksRelationType == null)
                    {
                        RelationType relationType = new RelationType(
                            RelationTypes.BentoItemsAlias,
                            RelationTypes.BentoItemsName,
                            true,
                            UmbracoObjectTypes.Document.GetGuid(),
                            UmbracoObjectTypes.Document.GetGuid());

                        RelationService.Save(relationType);

                        bentoBlocksRelationType = RelationService.GetRelationTypeByAlias(RelationTypes.BentoItemsAlias);
                    }

                    //todo: this does work but it's a bit brute force...
                    //i guess we could store the current relationships and then store the ones we're creating and compare them and then
                    //delete the ones from the old list that arent in the new list? but that's a lot of db hits...
                    IEnumerable <IRelation> rels = RelationService.GetByParentId(content.Id);
                    foreach (IRelation currentRelation in rels.Where(x => x.RelationType.Id == bentoBlocksRelationType.Id))
                    {
                        RelationService.Delete(currentRelation);
                    }

                    if (contentProperty.PropertyType.PropertyEditorAlias == BentoItemDataEditor.EditorAlias)
                    {
                        foreach (Property.PropertyValue value in contentProperty.Values)
                        {
                            if (value.PublishedValue == null)
                            {
                                break;
                            }

                            var area = JsonConvert.DeserializeObject <Area>(value.PublishedValue.ToString());

                            if (area.Id <= 0)
                            {
                                continue;
                            }

                            var bentoContent = contentService.GetById(area.Id);

                            if (bentoContent == null)
                            {
                                continue;
                            }

                            BentoItemConfiguration config = (BentoItemConfiguration)editor.Configuration;

                            ProcessRelationship(contentService, bentoContent, content, bentoBlocksRelationType, config.ItemDoctypeCompositionAlias);
                        }
                    }
                    else
                    {
                        foreach (Property.PropertyValue value in contentProperty.Values)
                        {
                            if (value.PublishedValue == null)
                            {
                                break;
                            }

                            string valueString = value.PublishedValue?.ToString();

                            if (string.IsNullOrWhiteSpace(valueString))
                            {
                                continue;
                            }

                            IEnumerable <StackItem> items = JsonConvert.DeserializeObject <IEnumerable <StackItem> >(valueString, new StackItemConverter());

                            var itemList = items.Where(x => x.Areas != null && x.Areas.Any())
                                           .SelectMany(stackItem => stackItem.Areas.Where(x => x.Id > 0), (stackItem, x) => contentService.GetById(x.Id))
                                           .Where(bentoContent => bentoContent != null)
                                           .Distinct();

                            BentoStackConfiguration config = (BentoStackConfiguration)editor.Configuration;

                            foreach (IContent item in itemList)
                            {
                                ProcessRelationship(contentService, item, content, bentoBlocksRelationType, config.ItemDoctypeCompositionAlias);
                            }
                        }
                    }
                }
            }
        }