Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Torben Warberg Rohde 35 posts 89 karma points
    Dec 09, 2009 @ 12:18
    Torben Warberg Rohde
    2

    Editable date for Blog4Umbraco 1.x (short how-to)

    This may be planned for the 2.x release (and others have probably already done it), but I thought I'd post it anyway.
    When we started using Umbraco it clearly became apparant that we where missing the ability to set another date for the posts than the current date (the documents CreateDateTime property).

    This is a short guide on how I added it in Umbraco 4.0.2:
    All the usual "at your own risk" disclaimers apply of course :-)

    1./ Download the source for v1.0 here (download link in the left sidebar):

    http://blog4umbraco.codeplex.com/SourceControl/changeset/view/8240

    2./ Open the project and copy the following DLLs to the bin folder (from your umbraco installation)

    • businesslogic.dll
    • cms.dll
    • interfaces.dll
    • umbraco.dll

    3./ Change BlogDateFolder to this:

    using System;
    using System.Collections.Generic;
    using System.Web;

    using umbraco;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.property;
    using umbraco.BusinessLogic;
    using umbraco.BusinessLogic.console;

    namespace Umlaut.Umb.Blog
    {
      /// <remarks>
      /// This is a modified version of http://blog4umbraco.codeplex.com/SourceControl/changeset/view/8240
      /// </remarks>
      public class BlogDateFolder : umbraco.BusinessLogic.ApplicationBase
      {
        /// <summary>
        /// Initializes a new instance of the <see cref="BlogDateFolder"/> class.
        /// </summary>
        public BlogDateFolder()
        {
         Document.New += new Document.NewEventHandler(Document_New);
         Document.BeforeSave += new Document.SaveEventHandler(Document_BeforeSave);
        }

        /// <summary>
        /// Updates created date and moves post to new datefolder
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="umbraco.cms.businesslogic.SaveEventArgs"/> instance containing the event data.</param>
        void Document_BeforeSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e)
        {
         SetDateFolderAndDate(sender);
        }

        /// <summary>
        /// Updates created date and moves post to new datefolder
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="umbraco.cms.businesslogic.NewEventArgs"/> instance containing the event data.</param>
        void Document_New(Document sender, umbraco.cms.businesslogic.NewEventArgs e)
        {
         SetDateFolderAndDate(sender);
        }

        void SetDateFolderAndDate(Document sender)
        {
          Document documentObject = sender;

          if (documentObject.ContentType.Alias == "BlogPost")
          {
           //Now uses "postDate" property instead of documentObject.CreateDateTime (if present, otherwise postDate is assigned to the documents CreateDateTime)
           DateTime postDate = documentObject.CreateDateTime; //default is create date
           Property postDateProperty = documentObject.getProperty("postDate");
           if (postDateProperty == null)
           {
            documentObject.getProperty("postDate").Value = documentObject.CreateDateTime;
           }
           else if (String.IsNullOrEmpty(Convert.ToString(postDateProperty.Value)))
           {
            documentObject.getProperty("postDate").Value = documentObject.CreateDateTime;
           }
           else
           {
            postDate = Convert.ToDateTime(postDateProperty.Value);
           }

           //if this BlogPost is not currently in a DateFolder create the hierarky and move it to the correct folder
            if (documentObject.Parent != null)
            {
             //Find root document (if this is a new document it will be the current documentObject.Parent, otherwise we have to hop out one folder at a time (we are editing - the post is in a DateFolder such as "2009 -> 12 -> 6")
              Document rootDocument = null;
              if (new Document(documentObject.Parent.Id).ContentType.Alias != "DateFolder")
              {
               rootDocument = documentObject;
              }
              else
              {
               Document parentDoc = new Document(documentObject.Parent.Id);
               while (parentDoc != null)
               {
                if (parentDoc.Parent != null)
                {
                 if (new Document(parentDoc.Parent.Id).ContentType.Alias != "DateFolder")
                 {
                  rootDocument = parentDoc;
                  break;
                 }
                }
                parentDoc = new Document(parentDoc.Parent.Id);
               }
              }
              
             //Create new DateFolder structure (or reuse existing)
              string[] strArray = (postDate.Year.ToString() + "-" + postDate.Month.ToString() + "-" + postDate.Day.ToString()).Split("-".ToCharArray());
               if (strArray.Length == 3)
               {
                //first level (year)
                 Document document = null;
                 //find existing
                 foreach (IconI ni in rootDocument.Parent.Children)
                 {
                   if (ni.Text == strArray[0])
                   {
                     document = new Document(ni.Id);
                     break;
                   }
                 }
                 //create new if it didn't exist
                 if (document == null)
                 {
                  document = Document.MakeNew(strArray[0], DocumentType.GetByAlias("DateFolder"), documentObject.User, rootDocument.Parent.Id);
                   document.Publish(documentObject.User);
                   library.UpdateDocumentCache(document.Id);
                 }
                 else
                 {

                   Log.Add(LogTypes.Debug, documentObject.User, documentObject.Id, "executed - year not null");
                 }
                 //second level (month)
                 Document document2 = null;
                 Document[] children = document.Children;
                 //find existing              
                 for (int i = 0; i < children.Length; i++)
                 {
                   IconI ni2 = children[i];
                   if (ni2.Text == strArray[1])
                   {
                     document2 = new Document(ni2.Id);
                     break;
                   }
                 }
                 //create new if it didn't exist              
                 if (document2 == null)
                 {
                   document2 = Document.MakeNew(strArray[1], DocumentType.GetByAlias("DateFolder"), documentObject.User, document.Id);
                   document2.Publish(documentObject.User);
                   library.UpdateDocumentCache(document2.Id);
                 }
                 //third level (day)
                 Document document3 = null;
                 Document[] documentArray2 = document2.Children;
                 //find existing              
                 for (int j = 0; j < documentArray2.Length; j++)
                 {
                   IconI ni3 = documentArray2[j];
                   if (ni3.Text == strArray[2])
                   {
                     document3 = new Document(ni3.Id);
                     break;
                   }
                 }
                 //create new if it didn't exist              
                 if (document3 == null)
                 {
                   document3 = Document.MakeNew(strArray[2], DocumentType.GetByAlias("DateFolder"), documentObject.User, document2.Id);
                   document3.Publish(documentObject.User);
                   library.UpdateDocumentCache(document3.Id);
                 }
                 documentObject.Move(document3.Id);

               }
            }
          }
        }
      }
    }

    4./ Build the project, and copy "bin\Umlaut.Umb.Blog.dll" to your umbraco installation bin, where it will overwrite the current version.

    5./ Go to the Umbraco backend and open BlogListPosts.xslt.
    Change

    <xsl:sort select="@createDate" order="descending" />

       to

    <xsl:sort select="data [@alias='postDate']" order="descending" />

       and

    <xsl:value-of select="umbraco.library:LongDate($post/@createDate)"/>

       to

    <xsl:value-of select="umbraco.library:LongDate($post/data [@alias = 'postDate'])"/>

    6./ Open your blog post template (typically masterpages\BlogPost.master) and change the date field to the postDate field

    <umbraco:Item field="postDate" formatAsDate="true" runat="server"></umbraco:Item>

    You're done.

  • Jukka-Pekka Keisala 90 posts 226 karma points
    Jan 16, 2010 @ 17:05
    Jukka-Pekka Keisala
    0

    Good stuff Torben. I discovered same issue on the updated blog4umbraco as well when I was importing old blog posts.

  • Paul Foley 77 posts 62 karma points
    Jan 22, 2010 @ 00:37
    Paul Foley
    0

    Awsome package update.

    One little problem we ran into was the date format being changed around so some posts were getting put into incorrect folders. (ie month and day were being swapped round)

    to get around this issue change line 72 of the class from:

    postDate = Convert.ToDateTime(postDateProperty.Value);

    to:

                        postDate = Convert.ToDateTime(postDateProperty.Value, System.Globalization.CultureInfo.InvariantCulture);

    Not the most elegant solution but it works :)

Please Sign in or register to post replies

Write your reply to:

Draft