public ItemCounterPartHandler(ICounterService counters, IAuthenticationService auth, IOrchardServices services) { OnRemoved<ItemCounterPart>((ctx, part) => counters.RemoveAllCounters(part.ContentItem.Id)); OnGetDisplayShape<ItemCounterPart>((ctx, part) => { var settings = part.Settings.GetModel<ItemCounterPartTypePartSettings>(); var type = part.Record == null ? settings.Type : part.Type; // Incrementing visit counters here var user = auth.GetAuthenticatedUser(); var site = services.WorkContext.CurrentSite; var countUser = 0; var countSite = 0; var countSession = 0; if (user != null){ countUser = ctx.DisplayType != "Detail" ? counters.GetCounter(part.ContentItem.Id, user.Id, CounterType.Visits, CounterStoreType.Database) : counters.Increment(part.ContentItem.Id, user.Id, CounterType.Visits, CounterStoreType.Database); } if (site != null){ countSite = ctx.DisplayType != "Detail" ? counters.GetCounter(part.ContentItem.Id, site.Id, CounterType.Visits, CounterStoreType.Database) : counters.Increment(part.ContentItem.Id, site.Id, CounterType.Visits, CounterStoreType.Database); } if (site != null && services.WorkContext.HttpContext != null){ countSession = ctx.DisplayType != "Detail" ? counters.GetCounter(part.ContentItem.Id, site.Id, CounterType.Visits, CounterStoreType.Session) : counters.Increment(part.ContentItem.Id, site.Id, CounterType.Visits, CounterStoreType.Session); } /* Setting appropriate counter */ part.Count = (type == VisitCounterType.PerSite) ? countSite : (type == VisitCounterType.PerUser && user != null) ? countUser : countSession; }); }
public CountersHandler(ICounterService counters, IAuthenticationService auth, IOrchardServices services) { OnRemoved<AutoroutePart>((ctx, part) => counters.RemoveAllCounters(part.ContentItem.Id)); OnGetDisplayShape<AutoroutePart>((ctx, part) => { if (ctx.DisplayType != "Detail") return; // Avoiding double incrementing counter for route parts and itemcounter parts if (part.Is<ItemCounterPart>()) return; // Incrementing visit counters here var user = auth.GetAuthenticatedUser(); var site = services.WorkContext.CurrentSite; if (user != null) counters.Increment(part.ContentItem.Id, user.Id, CounterType.Visits, CounterStoreType.Database); if (site != null) counters.Increment(part.ContentItem.Id, site.Id, CounterType.Visits, CounterStoreType.Database); if (site != null && services.WorkContext.HttpContext != null) counters.Increment(part.ContentItem.Id, site.Id, CounterType.Visits, CounterStoreType.Session); }); }