Example #1
0
        private IEnumerable <RelatedEntityViewModel> GetRelationships(string contentType)
        {
            var records = _relationshipService.GetRelationships(contentType)
                          .Where(x => (x.PrimaryEntity.Name == contentType) ||
                                 ((RelationshipType)x.Type) == RelationshipType.ManyToMany).ToList();

            var pluralService = PluralizationService.CreateService(new CultureInfo("en-US"));

            foreach (var record in records)
            {
                var relationshipType = (RelationshipType)record.Type;

                if (relationshipType == RelationshipType.OneToMany)
                {
                    var oneToMany = _relationshipService.GetOneToMany(record.Id);
                    if (oneToMany.ShowRelatedList)
                    {
                        yield return(new RelatedEntityViewModel {
                            RelationId = _relationshipService.GetReferenceField(record.RelatedEntity.Name, record.Name),
                            RelationType = "OneToMany",
                            Label = oneToMany.RelatedListLabel,
                            RelatedEntityName = pluralService.Pluralize(record.RelatedEntity.Name),
                            ProjectionId = oneToMany.RelatedListProjection.Id
                        });
                    }
                }
                else
                {
                    var manyToMany        = _relationshipService.GetManyToMany(record.Id);
                    var relatedEntityName = record.PrimaryEntity.Name == contentType ? record.RelatedEntity.Name : record.PrimaryEntity.Name;
                    var projectionId      = record.PrimaryEntity.Name == contentType ? manyToMany.RelatedListProjection.Id : manyToMany.PrimaryListProjection.Id;
                    var label             = record.PrimaryEntity.Name == contentType ? manyToMany.RelatedListLabel : manyToMany.PrimaryListLabel;
                    var showList          = record.PrimaryEntity.Name == contentType ? manyToMany.ShowRelatedList : manyToMany.ShowPrimaryList;
                    if (showList)
                    {
                        yield return(new RelatedEntityViewModel {
                            RelationId = record.Name,
                            RelationType = "ManyToMany",
                            Label = label,
                            RelatedEntityName = pluralService.Pluralize(relatedEntityName),
                            ProjectionId = projectionId
                        });
                    }
                }
            }
        }
Example #2
0
        public ActionResult EditManyToMany(string entityName, int relationId)
        {
            if (!Services.Authorizer.Authorize(Permissions.EditContent, T("Not allowed to edit a content.")))
            {
                return(new HttpUnauthorizedResult());
            }

            var manyToMany = _relationshipService.GetManyToMany(relationId);

            if (manyToMany == null || manyToMany.Id == 0)
            {
                return(ResponseError("Relationship not found"));
            }
            var primaryFields = _contentDefinitionManager
                                .GetPartDefinition(manyToMany.Relationship.PrimaryEntity.Name).Fields
                                .Select(x => new SelectListItem {
                Text = x.DisplayName, Value = x.Name
            });
            var relatedFields = _contentDefinitionManager
                                .GetPartDefinition(manyToMany.Relationship.RelatedEntity.Name).Fields
                                .Select(x => new SelectListItem {
                Text = x.DisplayName, Value = x.Name
            });

            return(View("CreateManyToMany", new ManyToManyRelationshipModel {
                IsCreate = false,
                Name = manyToMany.Relationship.Name,
                PrimaryEntity = manyToMany.Relationship.PrimaryEntity.Name,
                RelatedEntity = manyToMany.Relationship.RelatedEntity.Name,
                PrimaryListLabel = manyToMany.PrimaryListLabel,
                RelatedListLabel = manyToMany.RelatedListLabel,
                ShowPrimaryList = manyToMany.ShowPrimaryList,
                ShowRelatedList = manyToMany.ShowRelatedList,
                PrimaryFields = primaryFields,
                RelatedFields = relatedFields,
                PrimaryColumnList = manyToMany.PrimaryListProjection.LayoutRecord.Properties.Select(x => x.GetFiledName()).ToArray(),
                RelatedColumnList = manyToMany.RelatedListProjection.LayoutRecord.Properties.Select(x => x.GetFiledName()).ToArray()
            }));
        }