Skip to content

Instantly share code, notes, and snippets.

@dampee
Created October 3, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dampee/bfc292b5c64e9ffeaa98c7378299b8d2 to your computer and use it in GitHub Desktop.
Save dampee/bfc292b5c64e9ffeaa98c7378299b8d2 to your computer and use it in GitHub Desktop.
datefolder setup for news
<?xml version="1.0"?>
<DateFolder DateFolderAlias="dateFolder">
<DocumentType Alias="newsItem" DatePropertyAlias="postDate" />
<!--<DocumentType Alias="PressReleaseItem" DatePropertyAlias="date" />
<DocumentType Alias="EventItem" DatePropertyAlias="startDatetime" />-->
</DateFolder>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace TheaterWebsite.Ext.Events
{
/// <summary>
/// move news items in date folders
/// </summary>
/// <remarks>
/// based on http://umbracoext.codeplex.com/SourceControl/changeset/view/21521#266432
/// </remarks>
public class DateFolderEvents : ApplicationEventHandler
{
private string _dateFolderAlias = "DateFolder";
private string _datePropertyName = "CreateDateTime";
private readonly Dictionary<string, string> _documentTypesToMove = new Dictionary<string, string>();
public DateFolderEvents()
{
this.Init();
}
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
LogHelper.Info<DateFolderEvents>("Register DateFolder events");
ContentService.Published += ContentService_Published;
}
void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
{
foreach (var publishedentity in e.PublishedEntities)
{
MoveDocument(publishedentity);
}
}
private void MoveDocument(IContent publishedentity)
{
LogHelper.Debug<DateFolderEvents>("Checking if we need to move in dateFolders");
var currentDocType = publishedentity.ContentType.Alias;
if (!this._documentTypesToMove.ContainsKey(currentDocType))
{
// don't catch documentType
return;
}
var parentContent = ApplicationContext.Current.Services.ContentService.GetById(publishedentity.ParentId);
var parentDocType = parentContent.ContentType.Alias;
if (parentDocType == _dateFolderAlias)
{
// don't move object which are already in a datafolder
return;
}
_datePropertyName = this._documentTypesToMove[currentDocType];
if (string.IsNullOrWhiteSpace(_datePropertyName))
{
LogHelper.Debug<DateFolderEvents>("No property alias defined for: " + currentDocType);
return;
}
DateTime itemDate;
if (!publishedentity.HasProperty(_datePropertyName) || string.IsNullOrWhiteSpace(publishedentity.GetValue<string>(_datePropertyName)))
{
itemDate = publishedentity.UpdateDate;
}
else
{
itemDate = DateTime.Parse(publishedentity.GetValue<string>(_datePropertyName));
}
IContent parentNode = publishedentity.Parent();
parentNode = FindOrCreateDateFolder(parentNode, itemDate.Year);
parentNode = FindOrCreateDateFolder(parentNode, itemDate.Month);
// parentNode = FindOrCreateDateFolder(parentNode, itemDate.Day);
ApplicationContext.Current.Services.ContentService.Move(publishedentity, parentNode.Id);
}
private IContent FindOrCreateDateFolder(IContent parentNode, int datePart)
{
var children = parentNode.Children();
// Search for matching node
var matchingNode = children.FirstOrDefault(c => c.Name == datePart.ToString("D2"));
if (matchingNode != null)
{
return matchingNode;
}
// If no node is found, create a new node
var newDateFolderNode = ApplicationContext.Current.Services.ContentService.CreateContentWithIdentity(datePart.ToString("D2"), parentNode, this._dateFolderAlias);
ApplicationContext.Current.Services.ContentService.PublishWithStatus(newDateFolderNode);
return newDateFolderNode;
}
private void Init()
{
LogHelper.Info<DateFolderEvents>("Init Called");
XElement xd = XElement.Load(HttpContext.Current.Server.MapPath("/config/Custom.DateFolder.config"));
if (xd.Attribute("DateFolderAlias") != null)
{
_dateFolderAlias = xd.Attribute("DateFolderAlias").Value.Trim();
LogHelper.Info<DateFolderEvents>("DateFolder docType alias:" + _dateFolderAlias);
}
foreach (var docType in xd.Elements("DocumentType"))
{
var docTypeAlias = docType.Attribute("Alias").Value;
var propertyAlias = docType.Attribute("DatePropertyAlias").Value;
_documentTypesToMove.Add(docTypeAlias, propertyAlias);
LogHelper.Info<DateFolderEvents>("Discovered doctype: " + docTypeAlias + " - property: " + propertyAlias);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment