Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Created December 1, 2021 17:48
Show Gist options
  • Save jonathanread/782554723051229a4be55c2518d91cfa to your computer and use it in GitHub Desktop.
Save jonathanread/782554723051229a4be55c2518d91cfa to your computer and use it in GitHub Desktop.
News default view template with view count incrementor implemented on Line #7 of both cshtml files.
@model Telerik.Sitefinity.Frontend.Mvc.Models.ContentDetailsViewModel
@using Telerik.Sitefinity;
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;
@using Telerik.Sitefinity.Web.DataResolving;
@{SitefinityWebApp.Extensions.ViewsCountHelper.IncrementNewsViewCount(Model.Item.Fields.Id);}
<div class="@Model.CssClass" @Html.InlineEditingAttributes(Model.ProviderName, Model.ContentType.FullName, (Guid)Model.Item.Fields.Id)>
<h3>
<span @Html.InlineEditingFieldAttributes("Title", "ShortText")>@Model.Item.Fields.Title</span>
</h3>
<div>
@Model.Item.GetDateTime("PublicationDate", "MMM d, yyyy, HH:mm tt")
@Html.Resource("By")
@DataResolver.Resolve(@Model.Item.DataItem, "Author", null)
@Html.CommentsCount(string.Empty, @Model.Item.DataItem)
</div>
<div @Html.InlineEditingFieldAttributes("Summary", "LongText")>@Html.HtmlSanitize((string)Model.Item.Fields.Summary)</div>
<div class="sf-Long-text" @Html.InlineEditingFieldAttributes("Content", "LongText")>@Html.HtmlSanitize((string)Model.Item.Fields.Content)</div>
@Html.CommentsList(@Model.Item.DataItem)
</div>
@model Telerik.Sitefinity.Frontend.Mvc.Models.ContentDetailsViewModel
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;
@using Telerik.Sitefinity.Web.DataResolving;
@{SitefinityWebApp.Extensions.ViewsCountHelper.IncrementNewsViewCount(Model.Item.Fields.Id);}
<div class="@Model.CssClass" @Html.InlineEditingAttributes(Model.ProviderName, Model.ContentType.FullName, (Guid)Model.Item.Fields.Id)>
<h3>
<span @Html.InlineEditingFieldAttributes("Title", "ShortText")>@Model.Item.Fields.Title</span>
</h3>
<div>
@Model.Item.GetDateTime("PublicationDate", "MMM d, yyyy, HH:mm tt")
@Html.Resource("By")
@DataResolver.Resolve(@Model.Item.DataItem, "Author", null)
@Html.CommentsCount(string.Empty, @Model.Item.DataItem)
</div>
<div @Html.InlineEditingFieldAttributes("Summary", "LongText")>@Html.HtmlSanitize((string)Model.Item.Fields.Summary)</div>
<div class="sf-Long-text" @Html.InlineEditingFieldAttributes("Content", "LongText")>@Html.HtmlSanitize((string)Model.Item.Fields.Content)</div>
@Html.CommentsList(@Model.Item.DataItem)
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.Blogs;
using Telerik.Sitefinity.Modules.GenericContent;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.Services;
namespace SitefinityWebApp.Extensions
{
public static class ViewsCountHelper
{
public static void IncrementNewsViewCount(Guid id)
{
if (!WasSessionTracked(id))
{
if (ShouldItemTrackView())
{
NewsManager man = NewsManager.GetManager();
var item = man.GetNewsItems().FirstOrDefault(i => i.Id == id);
if (item != null)
{
man.Provider.SuppressSecurityChecks = true;
HttpContext.Current.Session[id.ToString()] = true;
item.ViewsCount++;
man.SaveChanges();
man.Provider.SuppressSecurityChecks = false;
}
}
}
}
public static void IncrementBlogsViewCount(Guid id)
{
if (!WasSessionTracked(id))
{
if (ShouldItemTrackView())
{
BlogsManager man = BlogsManager.GetManager();
var item = man.GetBlogPosts().FirstOrDefault(i => i.Id == id);
if (item != null)
{
man.Provider.SuppressSecurityChecks = true;
HttpContext.Current.Session[id.ToString()] = true;
item.ViewsCount++;
man.SaveChanges();
man.Provider.SuppressSecurityChecks = false;
}
}
}
}
/// <summary>
/// Check the Current Session if this has already been tracked
/// </summary>
/// <param name="id">Id of the content item</param>
/// <returns>True when the id is not in the current session</returns>
private static bool WasSessionTracked(Guid id)
{
return HttpContext.Current.Session[id.ToString()] != null && (bool)HttpContext.Current.Session[id.ToString()] == true;
}
/// <summary>
/// Check if the item should be tracked
/// </summary>
/// <returns>True when not design more, inline editing or preview</returns>
private static bool ShouldItemTrackView()
{
return !SystemManager.IsDesignMode && !SystemManager.IsInlineEditingMode && !SystemManager.IsPreviewMode;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment