Below is the full code for the over ridden file.
As I aid in my initial post the GetPageTemplateInfoInternal is usually being called correctly each time a page loads. Then for some unexplained reason it stops being called for a few pages.
using CMS.PortalEngine;
using System;
using System.Linq;
using System.Web;
using System.Data;
using CMS.GlobalHelper;
using CMS.SiteProvider;
using CMS.SettingsProvider;
using True.Kentico.Helpers;
using System.Collections.Generic;
using CMS.CMSHelper;
using System.Text.RegularExpressions;
using True.Kentico.Helpers.Content;
///
public class CustomPageTemplateInfoProvider : PageTemplateInfoProvider
{
private const string HOME_ALIASPATH = "/home";
/// <summary>
/// Override to add delivery instructions to order so they can be accessed through the CMS Desk
/// </summary>
/// <param name="cartObj"></param>
/// <returns></returns>
protected override void SetPageTemplateInfoInternal(PageTemplateInfo template)
{
bool flag;
if (template != null)
{
int categoryID = 0;
bool pageTemplateId = template.PageTemplateId > 0;
if (template.ItemChanged("PageTemplateLayout"))
{
flag = true;
}
else
{
flag = template.ItemChanged("PageTemplateCSS");
}
bool flag1 = flag;
if (flag1 || string.IsNullOrEmpty(template.PageTemplateVersionGUID))
{
Guid guid = Guid.NewGuid();
template.PageTemplateVersionGUID = guid.ToString();
}
if (template.IsReusable)
{
template.PageTemplateSiteID = 0;
}
if (!template.IsReusable)
{
PageTemplateCategoryInfo pageTemplateCategoryInfo = PageTemplateCategoryInfoProvider.GetPageTemplateCategoryInfo(template.CategoryID);
if (pageTemplateCategoryInfo == null)
{
pageTemplateCategoryInfo = PageTemplateCategoryInfoProvider.GetAdHocCategory();
if (pageTemplateCategoryInfo != null)
{
template.CategoryID = pageTemplateCategoryInfo.CategoryId;
}
else
{
throw new Exception("[PageTemplateInfoProvider.SetPageTemplateInfoInternal] Ad-hoc category is no available.");
}
}
}
template.WebParts = template.TemplateInstance.GetZonesXML();
if (pageTemplateId)
{
PageTemplateInfo infoByCodeName = this.GetInfoByCodeName(template.CodeName, template.PageTemplateSiteID, false);
if (infoByCodeName != null)
{
if (infoByCodeName.PageTemplateId == template.PageTemplateId)
{
categoryID = infoByCodeName.CategoryID;
}
else
{
throw new Exception(string.Concat("Page template '", template.CodeName, "' already exists."));
}
}
}
this.SetInfo(template);
UpdateNodeTemplateWebParts(CMSContext.CurrentDocument.NodeID, template.PageTemplateId, template.WebParts);
if (pageTemplateId && template.Generalized.TouchCacheDependencies)
{
int num = template.PageTemplateId;
CacheHelper.TouchKey(string.Concat("template|", num.ToString()));
}
PageTemplateCategoryInfoProvider.UpdateCategoryTemplateChildCount(categoryID, template.CategoryID);
if (HttpContext.Current != null)
{
string str = "CMSVirtualWebParts";
CacheHelper.Remove(str);
}
if (PageTemplateInfoProvider.CreateSearchTasks && SearchIndexInfoProvider.SearchEnabled)
{
LogSearchTasks(template);
}
}
}
protected override PageTemplateInfo GetPageTemplateInfoInternal(int templateId)
{
PageTemplateInfo templateInfo = base.GetPageTemplateInfoInternal(templateId);
int nodeId = 0;
Guid currentDocumentGuid;
string pageUrl = HttpContext.Current.Request.RawUrl.Equals("/") ? HOME_ALIASPATH : HttpContext.Current.Request.RawUrl,
documentGuidString = Regex.Match(pageUrl,
@"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b",
RegexOptions.IgnoreCase).Value;
if (Guid.TryParse(documentGuidString, out currentDocumentGuid))
{
// Design mode
var currentNode = NodeHelper.GetSingleNodeFromGuid(currentDocumentGuid);
nodeId = currentNode != null ? currentNode.NodeID : nodeId;
}
else
{
// Standard mode
var currentNode = NodeHelper.GetSingleNode(pageUrl);
nodeId = currentNode != null ? currentNode.NodeID : nodeId;
}
if (nodeId == 0)
{
return templateInfo;
}
string nodeXml = GetNodeTemplateWebParts(nodeId, templateId);
if (nodeXml != string.Empty)
{
templateInfo.WebParts = nodeXml;
}
return templateInfo;
}
private static void LogSearchTasks(PageTemplateInfo template)
{
string str = string.Format("DocumentPageTemplateID = {0} OR NodeTemplateID = {0} OR NodeWireframeTemplateID = {0}", template.PageTemplateId);
ModuleCommands.TreeEngineLogSearchTasks(str);
}
private static void UpdateNodeTemplateWebParts(int nodeId, int pageTemplateId, string webPartXml)
{
string where = String.Format("PageTemplateID = {0} and NodeID = {1}", pageTemplateId, nodeId);
DataRow row = CustomTable.GetCustomTableItemData("True.NodeWebParts", where);
var rowValues = new Dictionary<string, object>();
rowValues.Add("NodeID", nodeId);
rowValues.Add("PageTemplateID", pageTemplateId);
rowValues.Add("NodeWebParts", webPartXml);
if (row == null)
{
// Add data to custom table
CustomTable.CreateCustomTableItem("True.NodeWebParts", rowValues);
}
else
{
// Update custom table
CustomTable.UpdateCustomTableItem("True.NodeWebParts", (int)row["ItemID"], rowValues);
}
}
private static string GetNodeTemplateWebParts(int nodeId, int pageTemplateId)
{
string where = String.Format("PageTemplateID = {0} and NodeID = {1}", pageTemplateId, nodeId);
DataRow row = CustomTable.GetCustomTableItemData("True.NodeWebParts", where);
if (row == null)
{
return string.Empty;
}
return row["NodeWebParts"].ToString();
}
}