|
| 1 | +namespace Neolution.OrchardCoreModules.PageViewStats.Drivers |
| 2 | +{ |
| 3 | + using OrchardCore.ContentManagement.Display.ContentDisplay; |
| 4 | + using OrchardCore.DisplayManagement.ModelBinding; |
| 5 | + using OrchardCore.DisplayManagement.Views; |
| 6 | + using System; |
| 7 | + using System.Linq; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using OrchardCore.ContentManagement; |
| 10 | + using OrchardCore.Settings; |
| 11 | + using Neolution.OrchardCoreModules.PageViewStats.Services; |
| 12 | + using Microsoft.AspNetCore.Authorization; |
| 13 | + using Microsoft.AspNetCore.Http; |
| 14 | + using OrchardCore.Admin; |
| 15 | + |
| 16 | + public class PageViewStatsContentDisplayDriver : ContentDisplayDriver |
| 17 | + { |
| 18 | + private readonly IHttpContextAccessor httpContextAccessor; |
| 19 | + private readonly IAuthorizationService authorizationService; |
| 20 | + private readonly ISiteService siteService; |
| 21 | + private readonly IPageViewsRepository repository; |
| 22 | + |
| 23 | + public PageViewStatsContentDisplayDriver(IHttpContextAccessor httpContextAccessor, IAuthorizationService authorizationService, ISiteService siteService, IPageViewsRepository repository) |
| 24 | + { |
| 25 | + this.httpContextAccessor = httpContextAccessor; |
| 26 | + this.authorizationService = authorizationService; |
| 27 | + this.siteService = siteService; |
| 28 | + this.repository = repository; |
| 29 | + } |
| 30 | + |
| 31 | + public override async Task<IDisplayResult> DisplayAsync(ContentItem contentItem, IUpdateModel updater) |
| 32 | + { |
| 33 | + var context = this.httpContextAccessor.HttpContext; |
| 34 | + |
| 35 | + // Should only render on the back-end |
| 36 | + if (!AdminAttribute.IsApplied(context)) |
| 37 | + { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + // Should only render for users with proper permissions |
| 42 | + if (!await authorizationService.AuthorizeAsync(context?.User, PageViewStatsPermissions.ViewPageViewStats).ConfigureAwait(false)) |
| 43 | + { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + var settings = await this.siteService.GetSiteSettingsAsync().ConfigureAwait(false); |
| 48 | + var tz = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeZoneId); |
| 49 | + var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz); |
| 50 | + var today = DateOnly.FromDateTime(now); |
| 51 | + |
| 52 | + var pageViews = await this.repository.LoadPageViewsAsync(today.AddDays(-30), today).ConfigureAwait(false); |
| 53 | + var contentPageViews = pageViews.SelectMany(x => x.PageViews).Where(x => x.ContentItemId == contentItem.ContentItemId).ToList(); |
| 54 | + var totalViews = await this.repository.LoadAllPageViewsAsync().ConfigureAwait(false); |
| 55 | + var contentPageTotalViews = totalViews.SelectMany(x => x.PageViews).Where(x => x.ContentItemId == contentItem.ContentItemId).ToList(); |
| 56 | + |
| 57 | + return Initialize<PageViewStatsContentDisplayViewModel>("PageViewStatsPart_SummaryAdmin", m => |
| 58 | + { |
| 59 | + m.DailyViews = contentPageViews.Select(x => x.TotalViews).FirstOrDefault(); |
| 60 | + m.MonthlyViews = contentPageViews.Sum(x => x.TotalViews); |
| 61 | + m.TotalViews = contentPageTotalViews.Sum(x => x.TotalViews); |
| 62 | + }) |
| 63 | + .Location("SummaryAdmin", "Meta:90"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public class PageViewStatsContentDisplayViewModel |
| 68 | + { |
| 69 | + public int DailyViews { get; set; } |
| 70 | + public int MonthlyViews { get; set; } |
| 71 | + public int TotalViews { get; set; } |
| 72 | + } |
| 73 | +} |
0 commit comments