Last active
November 21, 2018 08:37
-
-
Save LucGosso/ab51ba36acc1f4645a2fdedc133922f7 to your computer and use it in GitHub Desktop.
ILM: Episerver Report to monitor pages not updated for a while [https://devblog.gosso.se/?p=961]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model Gosso.Mvc.Models.ViewModels.PageUpdateReportViewModel | |
@using EPiServer.DataAbstraction | |
@using EPiServer.Framework.Web.Resources | |
@using EPiServer.Shell.Web.Mvc.Html | |
@{ | |
Layout = null; | |
//put this file in Views/PageNotUpdatedReport/ | |
} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>@ViewBag.Title</title> | |
<meta http-equiv="X-UA-Compatible" content="IE=Edge" /> | |
<!-- Shell --> | |
@Html.Raw(ClientResources.RenderResources("ShellCore")) | |
<!-- LightTheme --> | |
@Html.Raw(ClientResources.RenderResources("ShellCoreLightTheme")) | |
<link href="/EPiServer/CMS/App_Themes/Default/Styles/system.css" type="text/css" rel="stylesheet"> | |
<link href="/EPiServer/CMS/App_Themes/Default/Styles/ToolButton.css" type="text/css" rel="stylesheet"> | |
</head> | |
<body> | |
@Html.Raw(Html.ShellInitializationScript()) | |
<div class="epi-contentContainer epi-padding"> | |
<div class="epi-contentArea"> | |
<div class="EP-systemImage" style="background-image: url('/App_Themes/Default/Images/ReportCenter/PublishedPages.gif');"> | |
<h1 class="EP-prefix"> | |
Existing Pages | |
</h1> | |
<p class="EP-systemInfo"> | |
This report displays pages that has not been updated since some date | |
</p> | |
</div> | |
<div id="FullRegion_ValidationSummary" class="EP-validationSummary" style="color: Black; display: none;"> | |
</div> | |
</div> | |
@using (Html.BeginForm("ListPages", "PageNotUpdatedReport", FormMethod.Post)) | |
{ | |
<script src="/Util/javascript/episerverscriptmanager.js" type="text/javascript"></script> | |
<script src="/EPiServer/CMS/javascript/system.js" type="text/javascript"></script> | |
<script src="/EPiServer/CMS/javascript/dialog.js" type="text/javascript"></script> | |
<script src="/EPiServer/CMS/javascript/system.aspx" type="text/javascript"></script> | |
<input type="hidden" id="doExport" name="doExport" value="False"> | |
<div class="epi-formArea"> | |
<fieldset> | |
<legend> | |
Report Criteria | |
</legend> | |
<div class="epi-size10"> | |
<label for="pageTypes">Select Date</label> | |
<input type="date" value="@Model.SelectedDate" name="date"/> | |
</div> | |
</fieldset> | |
<div class="epitoolbuttonrow"> | |
<span class="epi-cmsButton"><input class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Report" type="submit" name="showReport" id="showReport" value="Show Report" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)" /></span> | |
@*<span class="epi-cmsButton"><input class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Report" type="submit" name="exportReport" id="exportReport" value="Export Report" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)" /></span>*@ | |
</div> | |
</div> | |
} | |
@if (Model.Pages != null && Model.Pages.Count > 0) | |
{ | |
<div class="epi-floatLeft epi-marginVertical-small">Number of Hits: @Model.Pages.Count</div> | |
<div class="epi-contentArea epi-clear"> | |
<div> | |
<table class="epi-default epi-default-legacy" cellspacing="0" id="FullRegion_MainRegion_ReportView" style="border-style: None; width: 100%; border-collapse: collapse;"> | |
<tr> | |
<th scope="col" style="width: 30px">Page Id</th> | |
<th scope="col">Type</th> | |
<th scope="col">Page Url</th> | |
<th scope="col"style="width: 30px">Changed Date</th> | |
</tr> | |
@foreach (var page in Model.Pages) | |
{ | |
<tr> | |
<td>@page.ContentLink.ID</td> | |
<td>@page.PageTypeName</td> | |
<td><a href="@Model.EditUrlResolver.GetEditViewUrl(page.ContentLink)" title="@page.PageName" target="EPiServerMainUI">@Url.ContentUrl(page.ContentLink)</a></td> | |
<td>@(page.Changed.ToString("yyyy-MM-dd"))</td> | |
</tr> | |
} | |
</table> | |
</div> | |
</div> | |
} | |
</div> | |
<script type="text/javascript"> | |
document.getElementById("exportReport").onclick = function () { | |
document.getElementById("doExport").value = "True"; | |
}; | |
document.getElementById("showReport").onclick = function () { | |
document.getElementById("doExport").value = "False"; | |
}; | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Security; | |
using EPiServer.Web.Routing; | |
using Gosso.Mvc.Business.Initialization; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace Gosso.Mvc.Controllers.Reports | |
{ | |
[EPiServer.PlugIn.GuiPlugIn( | |
Area = EPiServer.PlugIn.PlugInArea.ReportMenu, | |
Url = "~/existingpagesreport", | |
Category = "Custom Reports", | |
DisplayName = "Pages not updated")] | |
[Authorize(Roles = "Administrators, WebAdmins")] | |
public class PageNotUpdatedReportController : Controller | |
{ | |
protected readonly PageUpdateService PageUpdateService; | |
protected readonly IContentRepository contentRepository; | |
protected readonly EditUrlResolver editUrlResolver; | |
private UrlResolver urlResolver; | |
public PageNotUpdatedReportController(PageUpdateService pageUpdateService, | |
EditUrlResolver editUrlResolver, | |
UrlResolver urlResolver, | |
IContentRepository contentRepository) | |
{ | |
PageUpdateService = pageUpdateService; | |
this.editUrlResolver = editUrlResolver; | |
this.urlResolver = urlResolver; | |
this.contentRepository = contentRepository; | |
} | |
public ActionResult Index() | |
{ | |
var model = new PageUpdateReportViewModel { | |
SelectedDate = DateTime.Now.AddMonths(-13).ToString("yyyy-MM-dd") | |
}; | |
return View(model); | |
} | |
[HttpPost] | |
public ActionResult ListPages(FormCollection form) | |
{ | |
var pages = new List<PageData>(); | |
DateTime dt; | |
if (DateTime.TryParse(form["date"], out dt)) | |
{ | |
pages = PageUpdateService.GetPagesNotUpdated(dt, ContentReference.StartPage); | |
} | |
var model = new PageUpdateReportViewModel | |
{ | |
Pages = pages.OrderBy(x => x.Created).ToList(), | |
SelectedDate = form["date"], | |
EditUrlResolver = editUrlResolver | |
}; | |
return View("Index", model); | |
} | |
public void Export(PageDataCollection pagesToExport, HttpResponse response) | |
{ | |
//using (var package = new ExcelPackage()) | |
//{ | |
// ExcelWorksheet ws = package.Workbook.Worksheets.Add("pages"); | |
// ws.Cells[1, 1].Value = "PageId"; | |
// ws.Cells[1, 2].Value = "PageName"; | |
// ws.Cells[1, 3].Value = "PageUrl"; | |
// ws.Cells[1, 4].Value = "Published Date"; | |
// ws.Row(1).Style.Font.Bold = true; | |
// ws.Row(1).Style.Locked = true; | |
// int row = 2; | |
// foreach (var page in pagesToExport) | |
// { | |
// ws.Cells[row, 1].Value = page.ContentLink.ID; | |
// ws.Cells[row, 2].Value = page.PageName; | |
// ws.Cells[row, 3].Value = Url.ContentUrl(page.ContentLink); | |
// ws.Cells[row, 4].Value = page.StartPublish.HasValue ? | |
//page.StartPublish.Value.ToString("yyyy-MM-dd HH:mm") : "Not published"; | |
// ++row; | |
// } | |
// response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; | |
// response.AddHeader("content-disposition", string.Format("attachment; filename=pages{0}.xlsx", | |
//DateTime.Now.ToString("yyyyMMdd"))); | |
// response.BinaryWrite(package.GetAsByteArray()); | |
// response.Flush(); | |
// response.End(); | |
//} | |
} | |
} | |
public class PageUpdateReportViewModel | |
{ | |
public List<PageData> Pages { get; set; } | |
public string SelectedDate { get; set; } | |
public EditUrlResolver EditUrlResolver { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Security; | |
using EPiServer.ServiceLocation; | |
using Gosso.Mvc.Models.Pages; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Gosso.Mvc.Business.Initialization | |
{ | |
[ServiceConfiguration(ServiceType = typeof(PageUpdateService),Lifecycle = ServiceInstanceScope.Singleton)] | |
public class PageUpdateService | |
{ | |
private readonly IContentRepository _contentRepository; | |
public PageUpdateService(IContentRepository contentRepository) | |
{ | |
_contentRepository = contentRepository; | |
} | |
/// <summary> | |
/// Service to get all pages that is not updated from a past date | |
/// </summary> | |
/// <param name="date"></param> | |
/// <param name="startPage"></param> | |
/// <returns></returns> | |
public List<PageData> GetPagesNotUpdated(DateTime date, ContentReference startPage) | |
{ | |
var pageList = new List<PageData>(); | |
foreach (var pr in _contentRepository.GetDescendents(startPage)) | |
{ | |
BasePageData pd = null; | |
try | |
{ | |
pd = _contentRepository.Get<BasePageData>(pr); | |
} | |
catch | |
{ | |
pd = null; | |
} | |
if (checkAccess("Everyone", pd)) | |
{ | |
if (pd != null) | |
{ | |
foreach (var language in pd.ExistingLanguages) | |
{ | |
if (_contentRepository.Get<PageData>(pd.ContentGuid, language) is BasePageData pdLang | |
&& pdLang.CheckPublishedStatus(PagePublishedStatus.Published) && pdLang.Changed < date) | |
{ | |
if (!pageList.Contains(pdLang)) | |
{ | |
if (pdLang.LinkType != PageShortcutType.External && CheckPageType(pdLang)) | |
{ | |
pageList.Add(pdLang); | |
} | |
} | |
} | |
} // foreach lang | |
} | |
} | |
} | |
return pageList; | |
} | |
private bool CheckPageType(BasePageData page) | |
{ | |
//if you rather dont want to evaluate som pagetypes, put them here | |
if (page is NewsArticlePage | |
|| page is NewsArticleListPage | |
) | |
return false; | |
return true; | |
} | |
private bool checkAccess(string role, PageData pd) | |
{ | |
if (pd == null) | |
return false; | |
try | |
{ | |
var list = pd.ACL.Entries.FirstOrDefault(x => x.Name == "Everyone"); | |
if (list != null && list.Access == AccessLevel.Read) | |
{ | |
return true; | |
} | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment