public void CollectChildContents(string html, Models.Content content)
        {
            var widgetModels = PageContentRenderHelper.ParseWidgetsFromHtml(html, true);

            if (widgetModels != null && widgetModels.Count > 0)
            {
                // Validate widget ids
                var widgetIds = widgetModels.Select(w => w.WidgetId).Distinct().ToArray();
                var widgets   = repository.AsQueryable <Models.Content>(c => widgetIds.Contains(c.Id)).Select(c => c.Id).ToList();
                widgetIds.Where(id => widgets.All(dbId => dbId != id)).ToList().ForEach(
                    id =>
                {
                    var message    = RootGlobalization.ChildContent_WidgetNotFound_ById;
                    var logMessage = string.Format("{0} Id: {1}", message, id);
                    throw new ValidationException(() => message, logMessage);
                });

                // Validate child content
                var group = widgetModels.GroupBy(w => w.AssignmentIdentifier).FirstOrDefault(g => g.Count() > 1);
                if (group != null)
                {
                    var message = string.Format(RootGlobalization.ChildContent_AssignmentAlreadyAdded, group.First().AssignmentIdentifier);
                    throw new ValidationException(() => message, message);
                }

                foreach (var model in widgetModels)
                {
                    // Add child content only if it's not added yet (for example, it may be added when restoring / cloning a content)
                    if (content.ChildContents.All(cc => cc.AssignmentIdentifier != model.AssignmentIdentifier))
                    {
                        // Create child content
                        var childContent = new ChildContent
                        {
                            Id     = Guid.NewGuid(),
                            Child  = repository.AsProxy <Models.Content>(model.WidgetId),
                            Parent = content,
                            AssignmentIdentifier = model.AssignmentIdentifier
                        };
                        content.ChildContents.Add(childContent);
                    }
                }
            }
        }
Exemple #2
0
        private void CheckIfContentHasDeletingWidgetsWithDynamicRegions(Widget widget, string targetHtml)
        {
            var sourceHtml    = ((IDynamicContentContainer)widget).Html;
            var sourceWidgets = PageContentRenderHelper.ParseWidgetsFromHtml(sourceHtml);
            var targetWidgets = PageContentRenderHelper.ParseWidgetsFromHtml(targetHtml);

            // Get ids of child widgets, which are being removed from the content
            var removingWidgetIds = sourceWidgets
                                    .Where(sw => targetWidgets.All(tw => tw.WidgetId != sw.WidgetId))
                                    .Select(sw => sw.WidgetId)
                                    .Distinct()
                                    .ToArray();

            if (removingWidgetIds.Any())
            {
                var childContents   = childContentService.RetrieveChildrenContentsRecursively(true, removingWidgetIds);
                var childContentIds = childContents.Select(cc => cc.Child.Id).Distinct().ToList();

                PageContent   pcAlias            = null;
                ContentRegion contentRegionAlias = null;

                var subQuery = QueryOver.Of(() => contentRegionAlias)
                               .Where(() => !contentRegionAlias.IsDeleted)
                               .And(Restrictions.In(Projections.Property(() => contentRegionAlias.Content.Id), childContentIds))
                               .And(() => contentRegionAlias.Region == pcAlias.Region)
                               .Select(cr => cr.Id);

                var areAnyContentUsages = repository.AsQueryOver(() => pcAlias)
                                          .WithSubquery.WhereExists(subQuery)
                                          .And(() => !pcAlias.IsDeleted)
                                          .RowCount() > 0;

                if (areAnyContentUsages)
                {
                    var message    = PagesGlobalization.SaveWidget_ContentHasChildrenWidgetWithDynamicContents_ConfirmationMessage;
                    var logMessage = string.Format("User is trying to remove child widget which has children with dynamic regions and assigned contents. Confirmation is required.");
                    throw new ConfirmationRequestException(() => message, logMessage);
                }
            }
        }