Ejemplo n.º 1
0
        /// <summary>Creates a version of the item. This must be called before the item item is modified to save a version before modifications.</summary>
        /// <param name="item">The item to create a old version of.</param>
        /// <returns>The old version.</returns>
        public virtual ContentItem SaveVersion(ContentItem item)
        {
            CancellableItemEventArgs args = new CancellableItemEventArgs(item);

            if (ItemSavingVersion != null)
            {
                ItemSavingVersion.Invoke(this, args);
            }
            if (!args.Cancel)
            {
                item = args.AffectedItem;

                ContentItem oldVersion = item.Clone(false);
                if (item.State == ContentState.Published)
                {
                    stateChanger.ChangeTo(oldVersion, ContentState.Unpublished);
                }
                else
                {
                    stateChanger.ChangeTo(oldVersion, ContentState.Draft);
                }
                oldVersion.Expires   = Utility.CurrentTime().AddSeconds(-1);
                oldVersion.Updated   = Utility.CurrentTime().AddSeconds(-1);
                oldVersion.Parent    = null;
                oldVersion.VersionOf = item;
                if (item.Parent != null)
                {
                    oldVersion["ParentID"] = item.Parent.ID;
                }
                itemRepository.SaveOrUpdate(oldVersion);

                if (ItemSavedVersion != null)
                {
                    ItemSavedVersion.Invoke(this, new ItemEventArgs(oldVersion));
                }

                TrimVersionCountTo(item, maximumVersionsPerItem);

                return(oldVersion);
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>Creates a version of the item. This must be called before the item item is modified to save a version before modifications.</summary>
        /// <param name="item">The item to create a old version of.</param>
        /// <returns>The old version.</returns>
        public virtual ContentItem AddVersion(ContentItem item, bool asPreviousVersion = true)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            CancellableItemEventArgs args = new CancellableItemEventArgs(item);

            if (ItemSavingVersion != null)
            {
                ItemSavingVersion.Invoke(this, args);
            }

            if (args.Cancel)
            {
                return(null);
            }

            item = args.AffectedItem;

            if (!item.IsPage)
            {
                var page = Find.ClosestPage(item);
                if (page == null)
                {
                    throw new InvalidOperationException("Cannot create version of part which isn't on a page: " + item);
                }

                var pageVersion = AddVersion(page, asPreviousVersion: asPreviousVersion);
                var partVersion = pageVersion.FindPartVersion(item);
                return(partVersion);
            }

            ContentItem version = item.CloneForVersioningRecursive(stateChanger, asPreviousVersion);

            if (item.Parent != null)
            {
                version["ParentID"] = item.Parent.ID;
            }

            //ContentVersion savedVersion;
            if (asPreviousVersion)
            {
                /*savedVersion = */ Repository.Save(version, asPreviousVersion);
                item.VersionIndex = Repository.GetGreatestVersionIndex(item) + 1;
                itemRepository.SaveOrUpdate(item);
            }
            else
            {
                version.VersionIndex = Repository.GetGreatestVersionIndex(item) + 1;
                /*savedVersion = */ Repository.Save(version, asPreviousVersion);
            }

            if (ItemSavedVersion != null)
            {
                ItemSavedVersion.Invoke(this, new ItemEventArgs(version));
            }

            TrimVersionCountTo(item, maximumVersionsPerItem);

            return(version);           // Repository.DeserializeVersion(savedVersion);
        }