Ejemplo n.º 1
0
        public async Task <ContentItem> LoadAsync(ContentItem contentItem)
        {
            if (!_contentManagerSession.RecallVersionId(contentItem.Id, out var loaded))
            {
                // store in session prior to loading to avoid some problems with simple circular dependencies
                _contentManagerSession.Store(contentItem);

                // create a context with a new instance to load
                var context = new LoadContentContext(contentItem);

                // invoke handlers to acquire state, or at least establish lazy loading callbacks
                await Handlers.InvokeAsync((handler, context) => handler.LoadingAsync(context), context, _logger);

                await ReversedHandlers.InvokeAsync((handler, context) => handler.LoadedAsync(context), context, _logger);

                loaded = context.ContentItem;
            }

            return(loaded);
        }
Ejemplo n.º 2
0
        public virtual ContentItem Get(int id, VersionOptions options)
        {
            ContentItem contentItem;

            ContentItemVersionRecord versionRecord = null;

            // obtain the root records based on version options
            if (options.VersionRecordId != 0)
            {
                // short-circuit if item held in session
                if (_contentManagerSession.RecallVersionRecordId(options.VersionRecordId, out contentItem))
                {
                    return(contentItem);
                }

                versionRecord = _contentItemStore.Get(id, options).VersionRecord;
            }
            else if (options.VersionNumber != 0)
            {
                // short-circuit if item held in session
                if (_contentManagerSession.RecallVersionNumber(id, options.VersionNumber, out contentItem))
                {
                    return(contentItem);
                }

                versionRecord = _contentItemStore.Get(id, options).VersionRecord;
            }
            else if (_contentManagerSession.RecallContentRecordId(id, out contentItem))
            {
                // try to reload a previously loaded published content item

                if (options.IsPublished)
                {
                    return(contentItem);
                }

                versionRecord = contentItem.VersionRecord;
            }

            // no record means content item is not in db
            if (versionRecord == null)
            {
                // check in memory
                var record = _contentItemStore.Get(id, options).VersionRecord;
                if (record == null)
                {
                    return(null);
                }

                versionRecord = record;
            }

            // return item if obtained earlier in session
            if (_contentManagerSession.RecallVersionRecordId(versionRecord.Id, out contentItem))
            {
                if (options.IsDraftRequired && versionRecord.Published)
                {
                    return(BuildNewVersion(contentItem));
                }
                return(contentItem);
            }

            // allocate instance and set record property
            contentItem = New(versionRecord.ContentItemRecord.ContentType.Name);
            contentItem.VersionRecord = versionRecord;

            // store in session prior to loading to avoid some problems with simple circular dependencies
            _contentManagerSession.Store(contentItem);

            // create a context with a new instance to load
            var context = new LoadContentContext(contentItem);

            // invoke handlers to acquire state, or at least establish lazy loading callbacks
            Handlers.Invoke(handler => handler.Loading(context));
            Handlers.Invoke(handler => handler.Loaded(context));

            // when draft is required and latest is published a new version is appended
            if (options.IsDraftRequired && versionRecord.Published)
            {
                contentItem = BuildNewVersion(context.ContentItem);
            }

            return(contentItem);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Render()
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ContentPreview))
            {
                return(Unauthorized());
            }

            var contentItemType = Request.Form["ContentItemType"];
            var contentItem     = await _contentManager.NewAsync(contentItemType);

            // Assign the ids from the currently edited item so that validation thinks
            // it's working on the same item. For instance if drivers are checking name unicity
            // they need to think this is the same existing item (AutoroutePart).

            var contentItemId        = Request.Form["PreviewContentItemId"];
            var contentItemVersionId = Request.Form["PreviewContentItemVersionId"];

            // Unique contentItem.Id that only Preview is using such that another
            // stored document can't have the same one in the IContentManagerSession index

            contentItem.Id                   = -1;
            contentItem.ContentItemId        = contentItemId;
            contentItem.ContentItemVersionId = contentItemVersionId;
            contentItem.CreatedUtc           = _clock.UtcNow;
            contentItem.ModifiedUtc          = _clock.UtcNow;
            contentItem.PublishedUtc         = _clock.UtcNow;
            contentItem.Published            = true;

            // TODO: we should probably get this value from the main editor as it might impact validators
            var model = await _contentItemDisplayManager.UpdateEditorAsync(contentItem, this, true);

            if (!ModelState.IsValid)
            {
                var errors = new List <string>();
                foreach (var modelState in ValidationHelpers.GetModelStateList(ViewData, false))
                {
                    for (var i = 0; i < modelState.Errors.Count; i++)
                    {
                        var modelError = modelState.Errors[i];
                        var errorText  = ValidationHelpers.GetModelErrorMessageOrDefault(modelError);
                        errors.Add(errorText);
                    }
                }

                return(StatusCode(500, new { errors = errors }));
            }

            var previewAspect = await _contentManager.PopulateAspectAsync(contentItem, new PreviewAspect());

            if (!String.IsNullOrEmpty(previewAspect.PreviewUrl))
            {
                // The PreviewPart is configured, we need to set the fake content item
                _contentManagerSession.Store(contentItem);

                if (!previewAspect.PreviewUrl.StartsWith('/'))
                {
                    previewAspect.PreviewUrl = "/" + previewAspect.PreviewUrl;
                }

                Request.HttpContext.Items["PreviewPath"] = previewAspect.PreviewUrl;

                return(Ok());
            }

            model = await _contentItemDisplayManager.BuildDisplayAsync(contentItem, this, "Detail");

            return(View(model));
        }