|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Threading; |
|
using Telerik.Sitefinity.Data; |
|
using Telerik.Sitefinity.Modules.News; |
|
using Telerik.Sitefinity.News.Model; |
|
using Telerik.Sitefinity.Security; |
|
using Telerik.Sitefinity.Services; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class Global : System.Web.HttpApplication |
|
{ |
|
protected void Application_Start(object sender, EventArgs e) |
|
{ |
|
NewsManager.Executing += new EventHandler<ExecutingEventArgs>(this.NewsProvider_Executing); |
|
NewsManager.Executed += new EventHandler<ExecutedEventArgs>(this.NewsProvider_Executed); |
|
} |
|
|
|
private void NewsProvider_Executing(object sender, ExecutingEventArgs args) |
|
{ |
|
if (!(args.CommandName == "CommitTransaction" || args.CommandName == "FlushTransaction")) |
|
return; |
|
|
|
var provider = sender as NewsDataProvider; |
|
|
|
var dirtyItems = provider.GetDirtyItems(); |
|
if (dirtyItems.Count == 0) |
|
return; |
|
|
|
var newsItemsTracking = new List<Tuple<Guid, string, SecurityConstants.TransactionActionType>>(); |
|
foreach (var item in dirtyItems) |
|
{ |
|
var newsItem = item as NewsItem; |
|
if (newsItem != null && newsItem.Status != Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master) |
|
{ |
|
var itemStatus = provider.GetDirtyItemStatus(item); |
|
|
|
// Collect data about the change and persist it in ExecutionStateData |
|
newsItemsTracking.Add(new Tuple<Guid, string, SecurityConstants.TransactionActionType>(newsItem.Id, newsItem.Title, itemStatus)); |
|
|
|
//Perform some modifications to the item properties, for example change the URL |
|
using (new Telerik.Sitefinity.Localization.CultureRegion(SystemManager.CurrentContext.Culture)) |
|
{ |
|
if (SystemManager.CurrentContext.Culture.TwoLetterISOLanguageName.ToLower() == "es" && !string.IsNullOrEmpty(newsItem.UrlName)) |
|
{ |
|
// change the original UrlName, assigned by the provider here |
|
//the modified UrlName will be persisted |
|
newsItem.UrlName = newsItem.UrlName.ToString().Replace("es-", "en-"); |
|
} |
|
} |
|
} |
|
|
|
// In this step the changes are not committed yet. |
|
// Therefore, you need to persist them in the current execution context for consuming them in the Executed enevt handler. |
|
if (newsItemsTracking.Any()) |
|
provider.SetExecutionStateData("tracked_changes", newsItemsTracking); |
|
} |
|
} |
|
|
|
private void NewsProvider_Executed(object sender, ExecutedEventArgs args) |
|
{ |
|
if (args.CommandName != "CommitTransaction") |
|
return; |
|
|
|
var provider = sender as NewsDataProvider; |
|
|
|
var newsItemsTracking = provider.GetExecutionStateData("tracked_changes") as List<Tuple<Guid, string, SecurityConstants.TransactionActionType>>; |
|
if (newsItemsTracking != null) |
|
{ |
|
// The changes are now committed. |
|
// Add your logic here and finally remove the tracking information from the execution context |
|
provider.SetExecutionStateData("tracked_changes", null); |
|
} |
|
} |
|
} |
|
} |