private Return SaveHistory(IMediaDetail history) { selectedItem = BaseMapper.GetObjectFromContext((MediaDetail)selectedItem); history.HistoryForMediaDetailID = selectedItem.ID; if (selectedItem.History.Count == 0) { history.HistoryVersionNumber = 1; } else { history.HistoryVersionNumber = selectedItem.History.OrderByDescending(i => i.HistoryVersionNumber).Take(1).FirstOrDefault().HistoryVersionNumber + 1; } if (selectedItem.History.Count >= MediaDetailsMapper.MaxHistory) { int count = selectedItem.History.Count - MediaDetailsMapper.MaxHistory; IEnumerable <MediaDetail> items = selectedItem.History.OrderBy(i => i.HistoryVersionNumber).Take(count); foreach (MediaDetail item in items) { if (!item.IsDraft) { MediaDetailsMapper.ClearObjectRelations(item); MediaDetailsMapper.DeleteObjectFromContext(item); } } } /*if (history.MainContent.Trim() == "") * history.MainContent = "TBD";*/ return(MediaDetailsMapper.Insert(history)); }
public IMediaDetail HandleDuplicate(IMediaDetail detail, Media parentMedia, bool duplicateChildren = false, string newName = "", bool autoPublish = true) { var duplicatedItem = MediaDetailsMapper.CreateObject(detail.MediaTypeID, null, parentMedia, false); duplicatedItem.CopyFrom(detail, new List <string> { "MediaID", "Media" }); if (newName != "") { duplicatedItem.Title = duplicatedItem.LinkTitle = newName; } duplicatedItem.CachedVirtualPath = duplicatedItem.CalculatedVirtualPath(); /*if (duplicatedItem.ID == 0) * { * var mediaType = MediaTypesMapper.GetByID(detail.MediaTypeID); * var createdItems = mediaType.MediaDetails.Where(i => !i.IsHistory && i.ParentMediaID == detail.ParentMediaID && i.LanguageID == detail.LanguageID).Select(i => i); * * var newIndex = createdItems.Count() + 1; * * duplicatedItem.LinkTitle = detail.Language.DisplayName + " - " + mediaType.Name + " " + newIndex; * }*/ duplicatedItem.Media.OrderIndex = parentMedia.ChildMedias.Count(i => i.ID != 0); duplicatedItem.PublishDate = duplicatedItem.DateCreated = duplicatedItem.DateLastModified = DateTime.Now; //var fields = detail.Fields.ToList(); foreach (var item in detail.Fields) { var mediaDetailField = new MediaDetailField(); mediaDetailField.CopyFrom(item); if (newName != "" && mediaDetailField.FieldCode == "SectionTitle") { mediaDetailField.FieldValue = newName; } mediaDetailField.DateCreated = mediaDetailField.DateLastModified = DateTime.Now; foreach (var association in item.FieldAssociations) { var fieldAssociation = new FieldAssociation(); fieldAssociation.CopyFrom(association); if (item.AdminControl.Contains("MultiFile")) { var associatedMediaDetail = MediaDetailsMapper.GetByID(fieldAssociation.AssociatedMediaDetailID); fieldAssociation.AssociatedMediaDetailID = 0; fieldAssociation.MediaDetail = (MediaDetail)HandleDuplicate(associatedMediaDetail, associatedMediaDetail.Media.ParentMedia, true); } mediaDetailField.FieldAssociations.Add(fieldAssociation); } duplicatedItem.Fields.Add(mediaDetailField); } if (!autoPublish) { duplicatedItem.PublishDate = null; } var returnObj = MediaDetailsMapper.Insert(duplicatedItem); if (returnObj.IsError) { throw returnObj.Error.Exception; } else { if (duplicateChildren) { var childMediaDetails = detail.ChildMediaDetails; foreach (var childDetail in childMediaDetails) { HandleDuplicate(childDetail, duplicatedItem.Media); } /*foreach (var child in detail.Media.ChildMedias) * { * var mediaDetailsToCopy = child.MediaDetails.Where(i => !i.IsDraft && !i.IsHistory); * * foreach (var childDetail in mediaDetailsToCopy) * { * HandleDuplicate(childDetail, duplicatedItem.Media); * } * }*/ } } return(duplicatedItem); }
//private void ChangeLinksForAllMediaDetails(string oldAbsoluteUrl, string newAbsoluteUrl) //{ // if (string.IsNullOrEmpty(oldAbsoluteUrl) || oldAbsoluteUrl == newAbsoluteUrl) // return; // var oldTemplateVarUrl = ParserHelper.ParseData(oldAbsoluteUrl, this.TemplateVars, true); // var newTemplateVarUrl = ParserHelper.ParseData(newAbsoluteUrl, this.TemplateVars, true); // var foundItems = MediaDetailsMapper.GetDataModel().MediaDetails.Where(i => i.MainContent.Contains(oldTemplateVarUrl) || i.ShortDescription.Contains(oldTemplateVarUrl)); // if (foundItems.Any()) // { // foreach (var item in foundItems) // { // item.ShortDescription = item.ShortDescription.Replace(oldTemplateVarUrl, newTemplateVarUrl); // item.MainContent = item.MainContent.Replace(oldTemplateVarUrl, newTemplateVarUrl); // } // var numberOfItemsEffected = MediaDetailsMapper.GetDataModel().SaveChanges(); // } //} protected void Save_OnClick(object sender, EventArgs e) { if (CanAccessItem != null && CanAccessItem.IsError) { DisplayErrorMessage("Error saving item", CanAccessItem.Error); return; } if (!CurrentUser.HasPermission(PermissionsEnum.Publish)) { DisplayErrorMessage("Error saving item", ErrorHelper.CreateError(new Exception("You do not have the appropriate permissions to save items"))); return; } string commandArgument = ((LinkButton)sender).CommandArgument; bool isDraft = false; if (commandArgument == "CreateDraft") { isDraft = true; } IMediaDetail history = CreateHistory(isDraft); Return returnObj = BaseMapper.GenerateReturn(); if ((history != null) && (history.IsDraft)) { history.CopyFrom(selectedItem); UpdateObjectFromFields(history); history.IsDraft = isDraft; returnObj = SaveHistory(history); if (returnObj.IsError) { DisplayErrorMessage("Error Saving Item", returnObj.Error); } else { DisplaySuccessMessage("Successfully Saved Item as Draft"); UpdateFieldsFromObject(); } RedirectToAdminUrl(selectedItem, history.HistoryVersionNumber); return; } var tmpSelectedItem = BaseMapper.GetObjectFromContext((MediaDetail)selectedItem); if (tmpSelectedItem != null) { selectedItem = tmpSelectedItem; } if (parentMediaItem != null) { parentMediaItem = BaseMapper.GetObjectFromContext(parentMediaItem); } else { parentMediaItem = selectedItem?.Media?.ParentMedia; } if (selectedItem == null) { selectedItem = MediaDetailsMapper.CreateObject(mediaTypeId, selectedMediaItem, parentMediaItem); } var oldLinkTitle = selectedItem.LinkTitle; var oldVirtualPath = selectedItem.VirtualPath; var canRender = selectedItem.CanRender; var oldAbsoluteUrl = selectedItem.AbsoluteUrl; var oldPostPublishDate = selectedItem.PublishDate; UpdateObjectFromFields(selectedItem); var validate = selectedItem.Validate(); if (validate.IsError) { DisplayErrorMessage("Error saving item", validate.Error); return; } if ((oldPostPublishDate != selectedItem.PublishDate) && !CurrentUser.HasPermission(PermissionsEnum.Publish)) { DisplayErrorMessage("Error saving item", ErrorHelper.CreateError(new Exception("You do not have the appropriate permissions to publish items"))); return; } if (selectedItem.ID == 0) { selectedItem.CreatedByUserID = CurrentUser.ID; if ((parentMediaItem != null) && (selectedItem.Media.ID == 0)) { selectedItem.Media.OrderIndex = parentMediaItem.ChildMedias.Count(i => i.ID != 0); } returnObj = MediaDetailsMapper.Insert(selectedItem); if (!returnObj.IsError) { ContextHelper.Clear(ContextType.Cache); FileCacheHelper.ClearAllCache(); } else { DisplayErrorMessage("Error", returnObj.Error); } } else { if (!isDraft) { if (history != null) { returnObj = SaveHistory(history); } //selectedItem.Media.ReorderChildren(); if (!returnObj.IsError) { returnObj = MediaDetailsMapper.Update(selectedItem); } } } if (returnObj.IsError) { DisplayErrorMessage("Error Saving Item", returnObj.Error); } else { if (history != null) { selectedItem.History.Add((MediaDetail)history); Return historyReturnObj = MediaDetailsMapper.Update(selectedItem); } selectedItem.RemoveFromCache(); FileCacheHelper.DeleteGenerateNavCache(); if (oldVirtualPath != selectedItem.VirtualPath || canRender != selectedItem.CanRender || oldLinkTitle != selectedItem.LinkTitle) { ContextHelper.Clear(ContextType.Cache); //FileCacheHelper.ClearCacheDir("generatenav"); //FileCacheHelper.ClearAllCache(); selectedItem.ClearAutoCalculatedVirtualPathCache(); if (commandArgument == "SaveAndPublish") { PublishNow_OnClick(sender, e); //RedirectToMediaDetail(mediaTypeEnum, selectedItem.MediaID, selectedItem.Media.ParentMediaID); } else { RedirectToAdminUrl(selectedItem.MediaTypeID, selectedItem.MediaID, selectedItem.Media.ParentMediaID); } //ChangeLinksForAllMediaDetails(oldAbsoluteUrl, selectedItem.AbsoluteUrl); } DisplaySuccessMessage("Successfully Saved Item"); ExecuteRawJS("ReloadPreviewPanel()"); if (!selectedItem.IsHistory) { SelectedMediaDetail = selectedItem; SelectedMedia = MediasMapper.GetByID(selectedItem.MediaID); } if (((Request["selectedMediaId"] == null) || (Request["selectedMediaId"].ToString() == "0")) && (commandArgument != "SaveAndPublish")) { RedirectToAdminUrl(selectedItem.MediaTypeID, selectedItem.MediaID, selectedItem.Media.ParentMediaID); } else { if (commandArgument == "SaveAndPublish") { PublishNow_OnClick(sender, e); //RedirectToMediaDetail(mediaTypeEnum, selectedItem.MediaID, selectedItem.Media.ParentMediaID); } UpdateFieldsFromObject(); /*else * Response.Redirect(Request.Url.AbsoluteUri);*/ //UpdateFieldsFromObject(); //RedirectToMediaDetail(mediaTypeEnum, selectedItem.MediaID, selectedItem.Media.ParentMediaID); } } }