/// <summary> /// Save current page to audit /// </summary> /// <param name="model"></param> /// <returns></returns> public ResponseModel SavePageLog(PageLogManageModel model) { var page = _pageRepository.GetById(model.PageId); if (page != null) { /* * Map page log model to log entity * Get last updated version of page log * Create Change Log * If there are nothing change then do not do anything * Otherwise insert log */ Mapper.CreateMap<PageLogManageModel, PageLog>(); var log = Mapper.Map<PageLogManageModel, PageLog>(model); var pageLog = GetAll().Where(a => a.PageId == page.Id).OrderByDescending(a => a.Id).FirstOrDefault(); log.ChangeLog = pageLog != null ? ChangeLog(pageLog, model) : string.Format("** Create Page **"); if (string.IsNullOrEmpty(log.ChangeLog)) { return new ResponseModel { Success = true }; } log.SessionId = HttpContext.Current.Session.SessionID; return Insert(log); } return new ResponseModel { Success = false, Message = _localizedResourceServices.T("AdminModule:::Pages:::Messages:::ObjectNotFounded:::Page is not founded.") }; }
/// <summary> /// Update data and create change log /// </summary> /// <param name="pageLog"></param> /// <param name="pageLogModel"></param> /// <returns></returns> private string ChangeLog(PageLog pageLog, PageLogManageModel pageLogModel) { var changeLog = new StringBuilder(); const string format = "- Update field: {0}\n"; if (!ConvertUtilities.Compare(pageLog.Title, pageLogModel.Title)) { changeLog.AppendFormat(format, "Title"); pageLog.Title = pageLogModel.Title; } if (!ConvertUtilities.Compare(pageLog.FriendlyUrl, pageLogModel.FriendlyUrl)) { changeLog.AppendFormat(format, "FriendlyUrl"); pageLog.FriendlyUrl = pageLogModel.FriendlyUrl; } if (!ConvertUtilities.Compare(pageLog.Content, pageLogModel.Content)) { changeLog.AppendFormat(format, "Content"); pageLog.Content = pageLogModel.Content; } if (!ConvertUtilities.Compare(pageLog.ContentWorking, pageLogModel.ContentWorking)) { changeLog.AppendFormat(format, "ContentWorking"); pageLog.ContentWorking = pageLogModel.ContentWorking; } if (!ConvertUtilities.Compare(pageLog.Caption, pageLogModel.Caption)) { changeLog.AppendFormat(format, "Caption"); pageLog.Caption = pageLogModel.Caption; } if (!ConvertUtilities.Compare(pageLog.CaptionWorking, pageLogModel.CaptionWorking)) { changeLog.AppendFormat(format, "CaptionWorking"); pageLog.CaptionWorking = pageLogModel.CaptionWorking; } if (!ConvertUtilities.Compare(pageLog.Status, pageLogModel.Status)) { changeLog.AppendFormat(format, "Status"); pageLog.Status = pageLogModel.Status; } if (!ConvertUtilities.Compare(pageLog.Keywords, pageLogModel.Keywords)) { changeLog.AppendFormat(format, "Keywords"); pageLog.Keywords = pageLogModel.Keywords; } if (!ConvertUtilities.Compare(pageLog.FileTemplateId, pageLogModel.FileTemplateId)) { changeLog.AppendFormat(format, "FileTemplateId"); pageLog.FileTemplateId = pageLogModel.FileTemplateId; } if (!ConvertUtilities.Compare(pageLog.PageTemplateId, pageLogModel.PageTemplateId)) { changeLog.AppendFormat(format, "PageTemplateId"); pageLog.PageTemplateId = pageLogModel.PageTemplateId; } if (!ConvertUtilities.Compare(pageLog.ParentId, pageLogModel.ParentId)) { changeLog.AppendFormat(format, "ParentId"); pageLog.ParentId = pageLogModel.ParentId; } if (!ConvertUtilities.Compare(pageLog.IncludeInSiteNavigation, pageLogModel.IncludeInSiteNavigation)) { changeLog.AppendFormat(format, "IncludeInSiteNavigation"); pageLog.IncludeInSiteNavigation = pageLogModel.IncludeInSiteNavigation; } if (!ConvertUtilities.Compare(pageLog.StartPublishingDate, pageLogModel.StartPublishingDate)) { changeLog.AppendFormat(format, "StartPublishingDate"); pageLog.StartPublishingDate = pageLogModel.StartPublishingDate; } if (!ConvertUtilities.Compare(pageLog.EndPublishingDate, pageLogModel.EndPublishingDate)) { changeLog.AppendFormat(format, "EndPublishingDate"); pageLog.EndPublishingDate = pageLogModel.EndPublishingDate; } if (!string.IsNullOrEmpty(changeLog.ToString())) { changeLog.Insert(0, string.Format("** Update Page **\n")); } return changeLog.ToString(); }
/// <summary> /// Save page manage model /// </summary> /// <param name="model"></param> /// <returns></returns> public ResponseModel SavePageManageModel(PageManageModel model) { Page relativePage; ResponseModel response; var page = GetById(model.Id); #region Edit Page if (page != null) { var pageLog = new PageLogManageModel(page); page.Title = model.Title; page.PageTemplateId = model.PageTemplateId; page.FileTemplateId = model.FileTemplateId; page.Status = model.Status; //Set content & caption base on status if (model.Status == (int)PageEnums.PageStatusEnums.Draft) { page.ContentWorking = model.Content; page.CaptionWorking = model.Caption; } else { page.Content = model.Content; page.Caption = model.Caption; } var currentTags = page.PageTags.Select(t => t.TagId).ToList(); foreach (var id in currentTags.Where(id => model.Tags == null || !model.Tags.Contains(id))) { _pageTagRepository.Delete(page.Id, id); } if (model.Tags != null && model.Tags.Any()) { foreach (var tagId in model.Tags) { if (currentTags.All(n => n != tagId)) { var pageTag = new PageTag { PageId = page.Id, TagId = tagId }; _pageTagRepository.Insert(pageTag); } } } page.StartPublishingDate = model.StartPublishingDate; page.EndPublishingDate = model.EndPublishingDate; //Parse friendly url page.FriendlyUrl = string.IsNullOrWhiteSpace(model.FriendlyUrl) ? model.Title.ToUrlString() : model.FriendlyUrl.ToUrlString(); //Get page record order relativePage = GetById(model.RelativePageId); if (relativePage != null) { /* * If position is not changed, donot need to update order of relative pages * If position is changed, check if position is before or after and update the record other of all relative pages */ var relativePages = Fetch(p => p.Id != page.Id && relativePage.ParentId.HasValue ? p.ParentId == relativePage.ParentId : p.ParentId == null) .OrderBy(p => p.RecordOrder); if (model.Position == (int)PageEnums.PositionEnums.Before) { if (page.RecordOrder > relativePage.RecordOrder || relativePages.Any(p => p.RecordOrder > page.RecordOrder && p.RecordOrder < relativePage.RecordOrder)) { page.RecordOrder = relativePage.RecordOrder; var query = string.Format( "Update Pages set RecordOrder = RecordOrder + 1 Where {0} And RecordOrder >= {1}", relativePage.ParentId.HasValue ? string.Format(" ParentId = {0}", relativePage.ParentId) : "ParentId Is NULL", relativePage.RecordOrder); _pageRepository.ExcuteSql(query); } } else { if (page.RecordOrder < relativePage.RecordOrder || relativePages.Any(p => p.RecordOrder < page.RecordOrder && p.RecordOrder > relativePage.RecordOrder)) { page.RecordOrder = relativePage.RecordOrder + 1; var query = string.Format( "Update Pages set RecordOrder = RecordOrder + 1 Where {0} And RecordOrder > {1}", relativePage.ParentId.HasValue ? string.Format(" ParentId = {0}", relativePage.ParentId) : "ParentId Is NULL", relativePage.RecordOrder); _pageRepository.ExcuteSql(query); } } } page.ParentId = model.ParentId; response = HierarchyUpdate(page); if (response.Success) { _clientMenuServices.SavePageToClientMenu(page.Id); _pageLogServices.SavePageLog(pageLog); } return response.SetMessage(response.Success ? _localizedResourceServices.T("AdminModule:::Pages:::Messages:::UpdateSuccessfully:::Update page successfully.") : _localizedResourceServices.T("AdminModule:::Pages:::Messages:::UpdateFailure:::Update page failed. Please try again later.")); } #endregion page = new Page { Title = model.Title, Status = model.Status, Content = model.Content, Caption = model.Caption, ParentId = model.ParentId, RecordOrder = 0, PageTemplateId = model.PageTemplateId, FileTemplateId = model.FileTemplateId, FriendlyUrl = string.IsNullOrWhiteSpace(model.FriendlyUrl) ? model.Title.ToUrlString() : model.FriendlyUrl.ToUrlString() }; //Set content & caption base on status if (model.Status == (int)PageEnums.PageStatusEnums.Draft) { page.ContentWorking = model.Content; page.CaptionWorking = model.Caption; } //Get page record order relativePage = GetById(model.RelativePageId); if (relativePage != null) { if (model.Position == (int)PageEnums.PositionEnums.Before) { page.RecordOrder = relativePage.RecordOrder; var query = string.Format( "Update Pages set RecordOrder = RecordOrder + 1 Where {0} And RecordOrder >= {1}", relativePage.ParentId.HasValue ? string.Format(" ParentId = {0}", relativePage.ParentId) : "ParentId Is NULL", relativePage.RecordOrder); _pageRepository.ExcuteSql(query); } else { page.RecordOrder = relativePage.RecordOrder + 1; var query = string.Format( "Update Pages set RecordOrder = RecordOrder + 1 Where {0} And RecordOrder > {1}", relativePage.ParentId.HasValue ? string.Format(" ParentId = {0}", relativePage.ParentId) : "ParentId Is NULL", relativePage.RecordOrder); _pageRepository.ExcuteSql(query); } } response = HierarchyInsert(page); if (response.Success) { _clientMenuServices.SavePageToClientMenu(response.Data.ToInt()); } return response.SetMessage(response.Success ? _localizedResourceServices.T("AdminModule:::Pages:::Messages:::CreateSuccessfully:::Create page successfully.") : _localizedResourceServices.T("AdminModule:::Pages:::Messages:::CreateFailure:::Create page failed. Please try again later.")); }